@toneflix/studio 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ import { type Peaks } from './peaks.ts';
2
+ import type { Clip } from './types.ts';
3
+ export interface WaveformDraw {
4
+ clip: Clip;
5
+ peaks?: Peaks;
6
+ pxPerSecond: number;
7
+ /** Where the canvas starts inside the clip, in CSS pixels */
8
+ from: number;
9
+ color: string;
10
+ pixelRatio: number;
11
+ }
12
+ /** Clip loudness at a time into the clip: its gain shaped by the fades. */
13
+ export declare const envelopeAt: (clip: Clip, time: number) => number;
14
+ /** Draw a slice of a clip's waveform, one bar per device pixel, with fade lines on top. */
15
+ export declare const drawWaveform: (canvas: HTMLCanvasElement, draw: WaveformDraw) => void;
@@ -0,0 +1,61 @@
1
+ import { peakAt } from './peaks.js';
2
+ /** Clip loudness at a time into the clip: its gain shaped by the fades. */
3
+ export const envelopeAt = (clip, time) => {
4
+ let gain = clip.gain;
5
+ if (clip.fadeIn > 0 && time < clip.fadeIn)
6
+ gain *= Math.max(0, time / clip.fadeIn);
7
+ if (clip.fadeOut > 0 && time > clip.duration - clip.fadeOut)
8
+ gain *= Math.max(0, (clip.duration - time) / clip.fadeOut);
9
+ return gain;
10
+ };
11
+ /** Draw a slice of a clip's waveform, one bar per device pixel, with fade lines on top. */
12
+ export const drawWaveform = (canvas, draw) => {
13
+ const ctx = canvas.getContext('2d');
14
+ if (!ctx)
15
+ return;
16
+ const { clip, peaks, pxPerSecond, from, color, pixelRatio } = draw;
17
+ const width = canvas.width;
18
+ const rows = canvas.height;
19
+ const middle = rows / 2;
20
+ const perPixel = 1 / (pxPerSecond * pixelRatio);
21
+ ctx.clearRect(0, 0, width, rows);
22
+ ctx.fillStyle = color;
23
+ if (!peaks) {
24
+ ctx.globalAlpha = 0.5;
25
+ ctx.fillRect(0, Math.floor(middle), width, Math.max(1, pixelRatio));
26
+ ctx.globalAlpha = 1;
27
+ return;
28
+ }
29
+ for (let x = 0; x < width; x++) {
30
+ const time = from / pxPerSecond + x * perPixel;
31
+ if (time > clip.duration)
32
+ break;
33
+ const source = clip.offset + time;
34
+ const [min, max] = peakAt(peaks, source, source + perPixel);
35
+ const gain = Math.min(2, envelopeAt(clip, time));
36
+ const top = middle - Math.min(1, max * gain) * middle;
37
+ const bottom = middle - Math.max(-1, min * gain) * middle;
38
+ ctx.fillRect(x, top, 1, Math.max(1, bottom - top));
39
+ }
40
+ drawFadeLines(ctx, draw, rows);
41
+ };
42
+ const drawFadeLines = (ctx, draw, rows) => {
43
+ const { clip, pxPerSecond, from, pixelRatio } = draw;
44
+ const px = (time) => (time * pxPerSecond - from) * pixelRatio;
45
+ ctx.strokeStyle = draw.color;
46
+ ctx.lineWidth = pixelRatio;
47
+ ctx.globalAlpha = 0.9;
48
+ if (clip.fadeIn > 0) {
49
+ ctx.beginPath();
50
+ ctx.moveTo(px(0), rows);
51
+ ctx.lineTo(px(clip.fadeIn), 0);
52
+ ctx.stroke();
53
+ }
54
+ if (clip.fadeOut > 0) {
55
+ ctx.beginPath();
56
+ ctx.moveTo(px(clip.duration - clip.fadeOut), 0);
57
+ ctx.lineTo(px(clip.duration), rows);
58
+ ctx.stroke();
59
+ }
60
+ ctx.globalAlpha = 1;
61
+ };
@@ -0,0 +1,20 @@
1
+ import { type PeaksStore } from './peaks-cache.ts';
2
+ import { PeaksWorker } from './peaks-worker.ts';
3
+ import type { Peaks } from './peaks.ts';
4
+ type AudioLike = Parameters<PeaksWorker['compute']>[0];
5
+ /**
6
+ * Peaks for audio, read from the device cache when they were worked out
7
+ * before, otherwise computed in the worker and cached.
8
+ */
9
+ export declare class Waveforms {
10
+ private pending;
11
+ readonly worker: PeaksWorker;
12
+ readonly cache: PeaksStore;
13
+ constructor(worker?: PeaksWorker, cache?: PeaksStore);
14
+ /** Peaks for a cache key; `audio` is only called when they aren't cached. */
15
+ get(key: string, audio: () => AudioLike | Promise<AudioLike>): Promise<Peaks>;
16
+ /** Peaks already cached for a key, without computing any. */
17
+ cached(key: string): Promise<Peaks | null>;
18
+ destroy(): void;
19
+ }
20
+ export {};
@@ -0,0 +1,38 @@
1
+ import { PeaksCache } from './peaks-cache.js';
2
+ import { PeaksWorker } from './peaks-worker.js';
3
+ /**
4
+ * Peaks for audio, read from the device cache when they were worked out
5
+ * before, otherwise computed in the worker and cached.
6
+ */
7
+ export class Waveforms {
8
+ pending = new Map();
9
+ worker;
10
+ cache;
11
+ constructor(worker = new PeaksWorker(), cache = new PeaksCache()) {
12
+ this.worker = worker;
13
+ this.cache = cache;
14
+ }
15
+ /** Peaks for a cache key; `audio` is only called when they aren't cached. */
16
+ get(key, audio) {
17
+ const running = this.pending.get(key);
18
+ if (running)
19
+ return running;
20
+ const job = (async () => {
21
+ const cached = await this.cache.get(key);
22
+ if (cached)
23
+ return cached;
24
+ const peaks = await this.worker.compute(await audio());
25
+ await this.cache.put(key, peaks);
26
+ return peaks;
27
+ })().finally(() => this.pending.delete(key));
28
+ this.pending.set(key, job);
29
+ return job;
30
+ }
31
+ /** Peaks already cached for a key, without computing any. */
32
+ cached(key) {
33
+ return this.cache.get(key);
34
+ }
35
+ destroy() {
36
+ this.worker.terminate();
37
+ }
38
+ }
package/package.json CHANGED
@@ -1,6 +1,67 @@
1
1
  {
2
2
  "name": "@toneflix/studio",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "The studio engine for the web: multitrack audio editing that feels like a desktop DAW, in any browser app.",
5
+ "license": "MIT",
6
+ "author": "Toneflix <code@toneflix.com.ng>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/toneflix/studio.git"
10
+ },
11
+ "keywords": [
12
+ "daw",
13
+ "audio",
14
+ "waveform",
15
+ "timeline",
16
+ "web-audio",
17
+ "music"
18
+ ],
19
+ "type": "module",
20
+ "sideEffects": false,
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "README.md",
32
+ "LICENSE"
33
+ ],
34
+ "scripts": {
35
+ "build": "tsc -p tsconfig.build.json",
36
+ "typecheck": "tsc -p tsconfig.json",
37
+ "lint": "eslint .",
38
+ "test": "node --test \"test/**/*.test.ts\"",
39
+ "demo": "vite demo",
40
+ "docs:dev": "vitepress dev docs",
41
+ "docs:build": "vitepress build docs",
42
+ "docs:preview": "vitepress preview docs"
43
+ },
44
+ "homepage": "https://github.com/toneflix/studio#readme",
45
+ "bugs": {
46
+ "url": "https://github.com/toneflix/studio/issues"
47
+ },
48
+ "devDependencies": {
49
+ "@eslint/js": "^10.0.1",
50
+ "@types/node": "^26.6.6",
51
+ "@typescript/native": "npm:typescript@^7.0.2",
52
+ "eslint": "^10.14.0",
53
+ "globals": "^17.14.0",
54
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
55
+ "typescript-eslint": "^8.72.1",
56
+ "vite": "^8.3.5",
57
+ "vitepress": "^1.6.4"
58
+ },
59
+ "engines": {
60
+ "node": ">=22.18"
61
+ },
62
+ "packageManager": "pnpm@12.16.1",
63
+ "publishConfig": {
64
+ "access": "public",
65
+ "provenance": true
66
+ }
67
+ }