@motionscript/audio 0.0.0-stage → 0.1.0-alpha.10

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +49 -0
  4. package/README.md +91 -3
  5. package/dist/browser/chunks/chunk-DD3KHRUL.js +2 -0
  6. package/dist/browser/chunks/chunk-DD3KHRUL.js.map +7 -0
  7. package/dist/browser/index.js +2 -0
  8. package/dist/browser/index.js.map +7 -0
  9. package/dist/browser/kit.js +2 -0
  10. package/dist/browser/kit.js.map +7 -0
  11. package/dist/browser/manifest.json +13 -0
  12. package/dist/index.d.ts +3 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +3 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/kit.d.ts +13 -0
  17. package/dist/kit.d.ts.map +1 -0
  18. package/dist/kit.js +11 -0
  19. package/dist/kit.js.map +1 -0
  20. package/dist/nodes.d.ts +16 -0
  21. package/dist/nodes.d.ts.map +1 -0
  22. package/dist/nodes.js +17 -0
  23. package/dist/nodes.js.map +1 -0
  24. package/dist/waveform/envelope.d.ts +120 -0
  25. package/dist/waveform/envelope.d.ts.map +1 -0
  26. package/dist/waveform/envelope.js +126 -0
  27. package/dist/waveform/envelope.js.map +1 -0
  28. package/dist/waveform/full-waveform.d.ts +118 -0
  29. package/dist/waveform/full-waveform.d.ts.map +1 -0
  30. package/dist/waveform/full-waveform.js +146 -0
  31. package/dist/waveform/full-waveform.js.map +1 -0
  32. package/dist/waveform/index.d.ts +23 -0
  33. package/dist/waveform/index.d.ts.map +1 -0
  34. package/dist/waveform/index.js +19 -0
  35. package/dist/waveform/index.js.map +1 -0
  36. package/dist/waveform/live-waveform.d.ts +129 -0
  37. package/dist/waveform/live-waveform.d.ts.map +1 -0
  38. package/dist/waveform/live-waveform.js +149 -0
  39. package/dist/waveform/live-waveform.js.map +1 -0
  40. package/dist/waveform/shared.d.ts +98 -0
  41. package/dist/waveform/shared.d.ts.map +1 -0
  42. package/dist/waveform/shared.js +149 -0
  43. package/dist/waveform/shared.js.map +1 -0
  44. package/package.json +72 -3
  45. package/registry.json +28 -0
  46. package/src/index.ts +2 -0
  47. package/src/kit.ts +22 -0
  48. package/src/nodes.ts +17 -0
  49. package/src/waveform/envelope.ts +176 -0
  50. package/src/waveform/full-waveform.ts +188 -0
  51. package/src/waveform/index.ts +30 -0
  52. package/src/waveform/live-waveform.ts +206 -0
  53. package/src/waveform/shared.ts +197 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/waveform/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACvE,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC5D,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,UAAU,EACV,SAAS,EACT,UAAU,GACX,MAAM,UAAU,CAAA;AACjB,YAAY,EAAE,MAAM,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The waveform family: one subject, two questions.
3
+ *
4
+ * - {@link FullWaveform} draws the **file** — the whole recording's silhouette,
5
+ * with a played portion that advances. "How long is this, and where are the
6
+ * loud parts."
7
+ * - {@link LiveWaveform} draws the **moment** — bars moving with playback, the
8
+ * visualiser a podcast clip puts beside the artwork. "Is anything happening
9
+ * right now, and whose voice is it."
10
+ *
11
+ * Both read the same baked {@link AudioEnvelope}, which is why they cannot
12
+ * disagree about a recording, and why neither listens to anything — see that
13
+ * module for why a scene that has to export cannot use a live analyser.
14
+ */
15
+ export { FullWaveform } from "./full-waveform.js";
16
+ export { LiveWaveform } from "./live-waveform.js";
17
+ export { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from "./envelope.js";
18
+ export { VOICE_PALETTE, WAVEFORM_ALIGNMENTS, WAVEFORM_STYLES, groupByPaint, layOutBars, paintBars, voiceColor, } from "./shared.js";
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/waveform/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEvE,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,UAAU,EACV,SAAS,EACT,UAAU,GACX,MAAM,UAAU,CAAA","sourcesContent":["/**\n * The waveform family: one subject, two questions.\n *\n * - {@link FullWaveform} draws the **file** — the whole recording's silhouette,\n * with a played portion that advances. \"How long is this, and where are the\n * loud parts.\"\n * - {@link LiveWaveform} draws the **moment** — bars moving with playback, the\n * visualiser a podcast clip puts beside the artwork. \"Is anything happening\n * right now, and whose voice is it.\"\n *\n * Both read the same baked {@link AudioEnvelope}, which is why they cannot\n * disagree about a recording, and why neither listens to anything — see that\n * module for why a scene that has to export cannot use a live analyser.\n */\nexport { FullWaveform } from \"./full-waveform\"\nexport type { FullWaveformProps } from \"./full-waveform\"\nexport { LiveWaveform } from \"./live-waveform\"\nexport type { LiveWaveformProps } from \"./live-waveform\"\nexport { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from \"./envelope\"\nexport type { AudioEnvelope, EnvelopeBar } from \"./envelope\"\nexport {\n VOICE_PALETTE,\n WAVEFORM_ALIGNMENTS,\n WAVEFORM_STYLES,\n groupByPaint,\n layOutBars,\n paintBars,\n voiceColor,\n} from \"./shared\"\nexport type { BarBox, WaveformAlignment, WaveformStyle } from \"./shared\"\n"]}
@@ -0,0 +1,129 @@
1
+ import { type Fill, type FillResolved, type NodeOf, type Node2D as _Node2D, type Node2DProps as _Node2DProps } from "@motionscript/sdk";
2
+ import { type AudioEnvelope } from "./envelope.js";
3
+ /**
4
+ * A **live audio visualiser**: bars that move with the recording as it plays.
5
+ *
6
+ * The thing every podcast-to-video tool puts beside the artwork. It is not a
7
+ * picture of the file — that is {@link FullWaveform} — it is a picture of *this
8
+ * moment*, and what it is for is making a static frame of a talking head feel
9
+ * like it is playing.
10
+ *
11
+ * ## "Live" without listening
12
+ *
13
+ * Nothing here reads an audio device. The bars are sampled out of a baked
14
+ * envelope at the node's own elapsed time, which is what makes the visualiser
15
+ * survive the thing it exists for: an **export**, where there is no playback to
16
+ * analyse and frames may be rendered out of order or in parallel. A frame is a
17
+ * pure function of its time, so scrubbing back and forth over the visualiser
18
+ * redraws exactly what it drew the first time — which an analyser node, by
19
+ * construction, cannot promise. See {@link AudioEnvelope}.
20
+ *
21
+ * ## The speaker filter
22
+ *
23
+ * `speaker` narrows the node to one voice of a diarized recording, and it
24
+ * is the reason this node knows about transcripts at all. Two visualisers beside
25
+ * two headshots, each filtered to its own host, is a podcast edit that would
26
+ * otherwise be done by hand for every turn — and because the filter *silences*
27
+ * rather than skips (see {@link forSpeaker}), the quiet host visibly stops
28
+ * talking instead of vanishing. Nothing about that requires the words: only who
29
+ * is talking when, which is what the diarizer produces.
30
+ *
31
+ * A filter naming a voice the recording doesn't have draws a resting row rather
32
+ * than everything — a picture of "this person never speaks here", which is
33
+ * true, where falling back to the unfiltered mix would be a silent lie about
34
+ * whose voice you were watching.
35
+ */
36
+ export declare const LiveWaveform: import("@motionscript/sdk").Component<"liveWaveform", import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
37
+ /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
38
+ readonly envelope: import("@motionscript/sdk").Type<AudioEnvelope, AudioEnvelope, false>;
39
+ /** How many bars the visualiser is made of. */
40
+ readonly bars: 48;
41
+ /** Space between bars, as a fraction of the pitch. */
42
+ readonly gap: 0.35;
43
+ readonly radius: 4;
44
+ /** How many seconds of audio are on screen at once. */
45
+ readonly window: 3;
46
+ /** Seconds into the file the node's first frame shows. */
47
+ readonly offset: 0;
48
+ /** Advance with the scene clock. Off freezes the picture at `offset`. */
49
+ readonly playing: true;
50
+ /** Multiplies every magnitude before it is clamped. */
51
+ readonly gain: 1.4;
52
+ /** How much of the box a silent bar still occupies, `0`–`1`. */
53
+ readonly floor: 0.04;
54
+ readonly align: import("@motionscript/sdk").Type<"center" | "bottom", "center" | "bottom", false>;
55
+ readonly style: import("@motionscript/sdk").Type<"bars" | "line" | "blocks", "bars" | "line" | "blocks", false>;
56
+ /**
57
+ * Show only this voice's activity, by index into the recording's own
58
+ * numbering. `-1` — the default — is every voice.
59
+ */
60
+ readonly speaker: -1;
61
+ /** Colour each bar by whoever is speaking in it. */
62
+ readonly byVoice: false;
63
+ /** A colour per voice index. Short or empty falls back to the palette. */
64
+ readonly voiceColors: import("@motionscript/sdk").Type<string[], string[], false>;
65
+ readonly fill: import("@motionscript/sdk").Type<Fill, FillResolved[], false>;
66
+ }>, {}, null, _Node2D<_Node2DProps>, import("@motionscript/sdk").CallProps<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
67
+ /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
68
+ readonly envelope: import("@motionscript/sdk").Type<AudioEnvelope, AudioEnvelope, false>;
69
+ /** How many bars the visualiser is made of. */
70
+ readonly bars: 48;
71
+ /** Space between bars, as a fraction of the pitch. */
72
+ readonly gap: 0.35;
73
+ readonly radius: 4;
74
+ /** How many seconds of audio are on screen at once. */
75
+ readonly window: 3;
76
+ /** Seconds into the file the node's first frame shows. */
77
+ readonly offset: 0;
78
+ /** Advance with the scene clock. Off freezes the picture at `offset`. */
79
+ readonly playing: true;
80
+ /** Multiplies every magnitude before it is clamped. */
81
+ readonly gain: 1.4;
82
+ /** How much of the box a silent bar still occupies, `0`–`1`. */
83
+ readonly floor: 0.04;
84
+ readonly align: import("@motionscript/sdk").Type<"center" | "bottom", "center" | "bottom", false>;
85
+ readonly style: import("@motionscript/sdk").Type<"bars" | "line" | "blocks", "bars" | "line" | "blocks", false>;
86
+ /**
87
+ * Show only this voice's activity, by index into the recording's own
88
+ * numbering. `-1` — the default — is every voice.
89
+ */
90
+ readonly speaker: -1;
91
+ /** Colour each bar by whoever is speaking in it. */
92
+ readonly byVoice: false;
93
+ /** A colour per voice index. Short or empty falls back to the palette. */
94
+ readonly voiceColors: import("@motionscript/sdk").Type<string[], string[], false>;
95
+ readonly fill: import("@motionscript/sdk").Type<Fill, FillResolved[], false>;
96
+ }>, null, import("@motionscript/sdk").BuiltNode<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
97
+ /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
98
+ readonly envelope: import("@motionscript/sdk").Type<AudioEnvelope, AudioEnvelope, false>;
99
+ /** How many bars the visualiser is made of. */
100
+ readonly bars: 48;
101
+ /** Space between bars, as a fraction of the pitch. */
102
+ readonly gap: 0.35;
103
+ readonly radius: 4;
104
+ /** How many seconds of audio are on screen at once. */
105
+ readonly window: 3;
106
+ /** Seconds into the file the node's first frame shows. */
107
+ readonly offset: 0;
108
+ /** Advance with the scene clock. Off freezes the picture at `offset`. */
109
+ readonly playing: true;
110
+ /** Multiplies every magnitude before it is clamped. */
111
+ readonly gain: 1.4;
112
+ /** How much of the box a silent bar still occupies, `0`–`1`. */
113
+ readonly floor: 0.04;
114
+ readonly align: import("@motionscript/sdk").Type<"center" | "bottom", "center" | "bottom", false>;
115
+ readonly style: import("@motionscript/sdk").Type<"bars" | "line" | "blocks", "bars" | "line" | "blocks", false>;
116
+ /**
117
+ * Show only this voice's activity, by index into the recording's own
118
+ * numbering. `-1` — the default — is every voice.
119
+ */
120
+ readonly speaker: -1;
121
+ /** Colour each bar by whoever is speaking in it. */
122
+ readonly byVoice: false;
123
+ /** A colour per voice index. Short or empty falls back to the palette. */
124
+ readonly voiceColors: import("@motionscript/sdk").Type<string[], string[], false>;
125
+ readonly fill: import("@motionscript/sdk").Type<Fill, FillResolved[], false>;
126
+ }>, {}, _Node2D<_Node2DProps>>>>;
127
+ export type LiveWaveform = NodeOf<typeof LiveWaveform>;
128
+ export type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>;
129
+ //# sourceMappingURL=live-waveform.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-waveform.d.ts","sourceRoot":"","sources":["../../src/waveform/live-waveform.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,IAAI,EACT,KAAK,YAAY,EACjB,KAAK,MAAM,EAGX,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,IAAI,YAAY,EACjC,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAIL,KAAK,aAAa,EAEnB,MAAM,YAAY,CAAA;AAYnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,eAAO,MAAM,YAAY;IAGrB,0EAA0E;;IAE1E,+CAA+C;;IAE/C,sDAAsD;;;IAGtD,uDAAuD;;IAEvD,0DAA0D;;IAE1D,yEAAyE;;IAEzE,uDAAuD;;IAEvD,gEAAgE;;;;IAIhE;;;OAGG;;IAEH,oDAAoD;;IAEpD,0EAA0E;;;;IA1B1E,0EAA0E;;IAE1E,+CAA+C;;IAE/C,sDAAsD;;;IAGtD,uDAAuD;;IAEvD,0DAA0D;;IAE1D,yEAAyE;;IAEzE,uDAAuD;;IAEvD,gEAAgE;;;;IAIhE;;;OAGG;;IAEH,oDAAoD;;IAEpD,0EAA0E;;;;IA1B1E,0EAA0E;;IAE1E,+CAA+C;;IAE/C,sDAAsD;;;IAGtD,uDAAuD;;IAEvD,0DAA0D;;IAE1D,yEAAyE;;IAEzE,uDAAuD;;IAEvD,gEAAgE;;;;IAIhE;;;OAGG;;IAEH,oDAAoD;;IAEpD,0EAA0E;;;gCAU5E,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,YAAY,CAAC,CAAA;AACtD,MAAM,MAAM,iBAAiB,GAAG,WAAW,CAAC,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA"}
@@ -0,0 +1,149 @@
1
+ import { Types, component2d, fillOps, } from "@motionscript/sdk";
2
+ import { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope, } from "./envelope.js";
3
+ import { clamp01, groupByPaint, layOutBars, livePaintKey, paintBars, voiceColor, } from "./shared.js";
4
+ /**
5
+ * A **live audio visualiser**: bars that move with the recording as it plays.
6
+ *
7
+ * The thing every podcast-to-video tool puts beside the artwork. It is not a
8
+ * picture of the file — that is {@link FullWaveform} — it is a picture of *this
9
+ * moment*, and what it is for is making a static frame of a talking head feel
10
+ * like it is playing.
11
+ *
12
+ * ## "Live" without listening
13
+ *
14
+ * Nothing here reads an audio device. The bars are sampled out of a baked
15
+ * envelope at the node's own elapsed time, which is what makes the visualiser
16
+ * survive the thing it exists for: an **export**, where there is no playback to
17
+ * analyse and frames may be rendered out of order or in parallel. A frame is a
18
+ * pure function of its time, so scrubbing back and forth over the visualiser
19
+ * redraws exactly what it drew the first time — which an analyser node, by
20
+ * construction, cannot promise. See {@link AudioEnvelope}.
21
+ *
22
+ * ## The speaker filter
23
+ *
24
+ * `speaker` narrows the node to one voice of a diarized recording, and it
25
+ * is the reason this node knows about transcripts at all. Two visualisers beside
26
+ * two headshots, each filtered to its own host, is a podcast edit that would
27
+ * otherwise be done by hand for every turn — and because the filter *silences*
28
+ * rather than skips (see {@link forSpeaker}), the quiet host visibly stops
29
+ * talking instead of vanishing. Nothing about that requires the words: only who
30
+ * is talking when, which is what the diarizer produces.
31
+ *
32
+ * A filter naming a voice the recording doesn't have draws a resting row rather
33
+ * than everything — a picture of "this person never speaks here", which is
34
+ * true, where falling back to the unfiltered mix would be a silent lie about
35
+ * whose voice you were watching.
36
+ */
37
+ export const LiveWaveform = component2d({
38
+ key: "liveWaveform",
39
+ state: {
40
+ /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
41
+ envelope: Types.custom(EMPTY_ENVELOPE),
42
+ /** How many bars the visualiser is made of. */
43
+ bars: 48,
44
+ /** Space between bars, as a fraction of the pitch. */
45
+ gap: 0.35,
46
+ radius: 4,
47
+ /** How many seconds of audio are on screen at once. */
48
+ window: 3,
49
+ /** Seconds into the file the node's first frame shows. */
50
+ offset: 0,
51
+ /** Advance with the scene clock. Off freezes the picture at `offset`. */
52
+ playing: true,
53
+ /** Multiplies every magnitude before it is clamped. */
54
+ gain: 1.4,
55
+ /** How much of the box a silent bar still occupies, `0`–`1`. */
56
+ floor: 0.04,
57
+ align: Types.custom("center"),
58
+ style: Types.custom("bars"),
59
+ /**
60
+ * Show only this voice's activity, by index into the recording's own
61
+ * numbering. `-1` — the default — is every voice.
62
+ */
63
+ speaker: -1,
64
+ /** Colour each bar by whoever is speaking in it. */
65
+ byVoice: false,
66
+ /** A colour per voice index. Short or empty falls back to the palette. */
67
+ voiceColors: Types.custom([]),
68
+ fill: Types.custom("#6366f1", { resolve: fillOps.resolve, lerp: fillOps.lerp }),
69
+ },
70
+ // Nothing declares a component's paints but itself.
71
+ load: (loader, state) => loader.fill(state.fill),
72
+ render: (ctx, state) => {
73
+ drawBars(ctx, state);
74
+ ctx.children();
75
+ },
76
+ });
77
+ /**
78
+ * The second of the recording the visualiser is showing.
79
+ *
80
+ * Off the node's own clock rather than the scene's, which is what "from the
81
+ * moment this node appeared" means — the same clock the engine's own
82
+ * `Video` times its picture from, and the same one that makes a node dropped
83
+ * halfway through a clip start its recording at its entrance rather than
84
+ * partway in.
85
+ */
86
+ function at(ctx, s) {
87
+ return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0);
88
+ }
89
+ function drawBars(ctx, s) {
90
+ const { width, height } = ctx.size;
91
+ if (width <= 0 || height <= 0)
92
+ return;
93
+ const count = Math.max(1, Math.round(s.bars));
94
+ const span = Math.max(0.05, s.window);
95
+ const now = at(ctx, s);
96
+ const voice = s.speaker < 0 ? null : Math.round(s.speaker);
97
+ // The window is centred on the playhead, so the bars run *toward* the
98
+ // middle and away again — the arrangement every mixer's meter uses, and the
99
+ // one that reads as sound arriving rather than as a strip scrolling past.
100
+ const sampled = forSpeaker(sampleEnvelope(s.envelope, now - span / 2, now + span / 2, count), voice);
101
+ const shaped = s.style === "line" ? taper(sampled) : sampled;
102
+ const boxes = layOutBars(shaped, {
103
+ width,
104
+ height,
105
+ gap: s.style === "blocks" ? 0.12 : s.gap,
106
+ align: s.align,
107
+ gain: s.gain,
108
+ floor: s.floor,
109
+ });
110
+ if (boxes.length === 0)
111
+ return;
112
+ const groups = groupByPaint(boxes, (i) => livePaintKey(shaped[i].speaker, s.byVoice, voice));
113
+ for (const [key, run] of groups) {
114
+ const graphics = paintBars(run, paintFor(s, key), s.radius);
115
+ if (graphics)
116
+ ctx.draw(graphics);
117
+ }
118
+ }
119
+ /** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */
120
+ function paintFor(s, key) {
121
+ if (key === "all")
122
+ return s.fill;
123
+ const voice = Number(key.slice("voice:".length));
124
+ if (!Number.isFinite(voice) || voice < 0)
125
+ return s.fill;
126
+ return s.voiceColors[voice] ?? voiceColor(voice);
127
+ }
128
+ /**
129
+ * Rolls the ends of a window off toward silence.
130
+ *
131
+ * What separates the `line` style from `bars`: a hard-edged window makes the
132
+ * loud bar entering at the right edge appear from nothing, which reads as a
133
+ * glitch rather than as sound arriving. Fading the outer fifth turns the same
134
+ * data into a shape that grows out of the baseline — the profile a spectrum
135
+ * display has, and the reason it looks like one object rather than a row of
136
+ * separate meters.
137
+ */
138
+ function taper(bars) {
139
+ const count = bars.length;
140
+ if (count < 3)
141
+ return [...bars];
142
+ const edge = Math.max(1, Math.floor(count / 5));
143
+ return bars.map((bar, i) => {
144
+ const fromEnd = Math.min(i, count - 1 - i);
145
+ const weight = clamp01(fromEnd / edge);
146
+ return { magnitude: bar.magnitude * weight, speaker: bar.speaker };
147
+ });
148
+ }
149
+ //# sourceMappingURL=live-waveform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-waveform.js","sourceRoot":"","sources":["../../src/waveform/live-waveform.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,WAAW,EACX,OAAO,GAQR,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EACL,cAAc,EACd,UAAU,EACV,cAAc,GAGf,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,OAAO,EACP,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,SAAS,EACT,UAAU,GAGX,MAAM,UAAU,CAAA;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,WAAW,CAAC;IACtC,GAAG,EAAE,cAAc;IACnB,KAAK,EAAE;QACL,0EAA0E;QAC1E,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAgB,cAAc,CAAC;QACrD,+CAA+C;QAC/C,IAAI,EAAE,EAAE;QACR,sDAAsD;QACtD,GAAG,EAAE,IAAI;QACT,MAAM,EAAE,CAAC;QACT,uDAAuD;QACvD,MAAM,EAAE,CAAC;QACT,0DAA0D;QAC1D,MAAM,EAAE,CAAC;QACT,yEAAyE;QACzE,OAAO,EAAE,IAAI;QACb,uDAAuD;QACvD,IAAI,EAAE,GAAG;QACT,gEAAgE;QAChE,KAAK,EAAE,IAAI;QACX,KAAK,EAAE,KAAK,CAAC,MAAM,CAAoB,QAAQ,CAAC;QAChD,KAAK,EAAE,KAAK,CAAC,MAAM,CAAgB,MAAM,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CAAC,OAAO,EAAE,CAAA;IACpE,CAAC,CAAC,CAAA;AACJ,CAAC","sourcesContent":["import {\n Types,\n component2d,\n fillOps,\n type Fill,\n type FillResolved,\n type NodeOf,\n type RenderContext,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n} from \"@motionscript/sdk\"\n\nimport {\n EMPTY_ENVELOPE,\n forSpeaker,\n sampleEnvelope,\n type AudioEnvelope,\n type EnvelopeBar,\n} from \"./envelope\"\nimport {\n clamp01,\n groupByPaint,\n layOutBars,\n livePaintKey,\n paintBars,\n voiceColor,\n type WaveformAlignment,\n type WaveformStyle,\n} from \"./shared\"\n\n/**\n * A **live audio visualiser**: bars that move with the recording as it plays.\n *\n * The thing every podcast-to-video tool puts beside the artwork. It is not a\n * picture of the file — that is {@link FullWaveform} — it is a picture of *this\n * moment*, and what it is for is making a static frame of a talking head feel\n * like it is playing.\n *\n * ## \"Live\" without listening\n *\n * Nothing here reads an audio device. The bars are sampled out of a baked\n * envelope at the node's own elapsed time, which is what makes the visualiser\n * survive the thing it exists for: an **export**, where there is no playback to\n * analyse and frames may be rendered out of order or in parallel. A frame is a\n * pure function of its time, so scrubbing back and forth over the visualiser\n * redraws exactly what it drew the first time — which an analyser node, by\n * construction, cannot promise. See {@link AudioEnvelope}.\n *\n * ## The speaker filter\n *\n * `speaker` narrows the node to one voice of a diarized recording, and it\n * is the reason this node knows about transcripts at all. Two visualisers beside\n * two headshots, each filtered to its own host, is a podcast edit that would\n * otherwise be done by hand for every turn — and because the filter *silences*\n * rather than skips (see {@link forSpeaker}), the quiet host visibly stops\n * talking instead of vanishing. Nothing about that requires the words: only who\n * is talking when, which is what the diarizer produces.\n *\n * A filter naming a voice the recording doesn't have draws a resting row rather\n * than everything — a picture of \"this person never speaks here\", which is\n * true, where falling back to the unfiltered mix would be a silent lie about\n * whose voice you were watching.\n */\nexport const LiveWaveform = component2d({\n key: \"liveWaveform\",\n state: {\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\n /** How many bars the visualiser is made of. */\n bars: 48,\n /** Space between bars, as a fraction of the pitch. */\n gap: 0.35,\n radius: 4,\n /** How many seconds of audio are on screen at once. */\n window: 3,\n /** Seconds into the file the node's first frame shows. */\n offset: 0,\n /** Advance with the scene clock. Off freezes the picture at `offset`. */\n playing: true,\n /** Multiplies every magnitude before it is clamped. */\n gain: 1.4,\n /** How much of the box a silent bar still occupies, `0`–`1`. */\n floor: 0.04,\n align: Types.custom<WaveformAlignment>(\"center\"),\n style: Types.custom<WaveformStyle>(\"bars\"),\n /**\n * Show only this voice's activity, by index into the recording's own\n * numbering. `-1` — the default — is every voice.\n */\n speaker: -1,\n /** Colour each bar by whoever is speaking in it. */\n byVoice: false,\n /** A colour per voice index. Short or empty falls back to the palette. */\n voiceColors: Types.custom<string[]>([]),\n fill: Types.custom<Fill, FillResolved[]>(\"#6366f1\", { resolve: fillOps.resolve, lerp: fillOps.lerp }),\n },\n // Nothing declares a component's paints but itself.\n load: (loader, state) => loader.fill(state.fill),\n render: (ctx, state) => {\n drawBars(ctx, state)\n ctx.children()\n },\n})\n\nexport type LiveWaveform = NodeOf<typeof LiveWaveform>\nexport type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>\n\ninterface LiveState {\n readonly envelope: AudioEnvelope\n readonly bars: number\n readonly gap: number\n readonly radius: number\n readonly window: number\n readonly offset: number\n readonly playing: boolean\n readonly gain: number\n readonly floor: number\n readonly align: WaveformAlignment\n readonly style: WaveformStyle\n readonly speaker: number\n readonly byVoice: boolean\n readonly voiceColors: string[]\n readonly fill: FillResolved[]\n}\n\n/**\n * The second of the recording the visualiser is showing.\n *\n * Off the node's own clock rather than the scene's, which is what \"from the\n * moment this node appeared\" means — the same clock the engine's own\n * `Video` times its picture from, and the same one that makes a node dropped\n * halfway through a clip start its recording at its entrance rather than\n * partway in.\n */\nfunction at(ctx: RenderContext, s: LiveState): number {\n return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0)\n}\n\nfunction drawBars(ctx: RenderContext, s: LiveState): void {\n const { width, height } = ctx.size\n if (width <= 0 || height <= 0) return\n\n const count = Math.max(1, Math.round(s.bars))\n const span = Math.max(0.05, s.window)\n const now = at(ctx, s)\n const voice = s.speaker < 0 ? null : Math.round(s.speaker)\n\n // The window is centred on the playhead, so the bars run *toward* the\n // middle and away again — the arrangement every mixer's meter uses, and the\n // one that reads as sound arriving rather than as a strip scrolling past.\n const sampled = forSpeaker(\n sampleEnvelope(s.envelope, now - span / 2, now + span / 2, count),\n voice\n )\n\n const shaped = s.style === \"line\" ? taper(sampled) : sampled\n const boxes = layOutBars(shaped, {\n width,\n height,\n gap: s.style === \"blocks\" ? 0.12 : s.gap,\n align: s.align,\n gain: s.gain,\n floor: s.floor,\n })\n if (boxes.length === 0) return\n\n const groups = groupByPaint(boxes, (i) =>\n livePaintKey(shaped[i].speaker, s.byVoice, voice)\n )\n\n for (const [key, run] of groups) {\n const graphics = paintBars(run, paintFor(s, key), s.radius)\n if (graphics) ctx.draw(graphics)\n }\n}\n\n/** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */\nfunction paintFor(s: LiveState, key: string): Fill {\n if (key === \"all\") return s.fill\n const voice = Number(key.slice(\"voice:\".length))\n if (!Number.isFinite(voice) || voice < 0) return s.fill\n return s.voiceColors[voice] ?? voiceColor(voice)\n}\n\n/**\n * Rolls the ends of a window off toward silence.\n *\n * What separates the `line` style from `bars`: a hard-edged window makes the\n * loud bar entering at the right edge appear from nothing, which reads as a\n * glitch rather than as sound arriving. Fading the outer fifth turns the same\n * data into a shape that grows out of the baseline — the profile a spectrum\n * display has, and the reason it looks like one object rather than a row of\n * separate meters.\n */\nfunction taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {\n const count = bars.length\n if (count < 3) return [...bars]\n const edge = Math.max(1, Math.floor(count / 5))\n\n return bars.map((bar, i) => {\n const fromEnd = Math.min(i, count - 1 - i)\n const weight = clamp01(fromEnd / edge)\n return { magnitude: bar.magnitude * weight, speaker: bar.speaker }\n })\n}\n"]}
@@ -0,0 +1,98 @@
1
+ import { Graphics2D, type Fill } from "@motionscript/sdk";
2
+ import type { EnvelopeBar } from "./envelope.js";
3
+ /**
4
+ * Geometry and paint shared by the two waveform nodes.
5
+ *
6
+ * They draw the same object — a row of bars whose heights are loudness — and
7
+ * differ only in what decides the heights: the whole file for one, a window
8
+ * around the playhead for the other. Everything about *how a row of bars is
9
+ * laid out and painted* is therefore common, and lives here so the two cannot
10
+ * drift into looking like different components.
11
+ */
12
+ /** How a bar is grown out of its baseline. */
13
+ export declare const WAVEFORM_ALIGNMENTS: readonly ["center", "bottom"];
14
+ export type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number];
15
+ /** What the bars are shaped like. */
16
+ export declare const WAVEFORM_STYLES: readonly ["bars", "line", "blocks"];
17
+ export type WaveformStyle = (typeof WAVEFORM_STYLES)[number];
18
+ /**
19
+ * The colours voices are drawn in when nobody has assigned any.
20
+ *
21
+ * Eight hues that can be told apart at a glance, handed out by voice index. They
22
+ * are literals rather than looked up, because a scene has to build in a
23
+ * headless render with no database in scope; a host that names its voices
24
+ * supplies its own colours instead.
25
+ */
26
+ export declare const VOICE_PALETTE: readonly ["#6366f1", "#14b8a6", "#f59e0b", "#ec4899", "#22c55e", "#3b82f6", "#f97316", "#a855f7"];
27
+ /** The colour a voice takes, wrapping past the end of the palette. */
28
+ export declare function voiceColor(index: number): string;
29
+ /**
30
+ * Where one bar sits and how tall it came out: its top-left corner and size, in
31
+ * the node's own pixels with **y pointing down** from the middle of the box.
32
+ * {@link paintBars} turns that into the centred, y-up rect the renderer draws.
33
+ */
34
+ export interface BarBox {
35
+ x: number;
36
+ y: number;
37
+ width: number;
38
+ height: number;
39
+ }
40
+ /**
41
+ * Lays a row of bars out across a box.
42
+ *
43
+ * The gap is stated as a *fraction of the pitch* rather than in pixels, and
44
+ * that is the one decision here worth stating out loud: the bar count is a
45
+ * control, so a gap in pixels would silently become a solid block at 200 bars
46
+ * and a picket fence at 20. As a fraction, the row keeps the same rhythm at
47
+ * every density — which is what lets the count be a slider anyone can drag.
48
+ *
49
+ * A bar is never thinner than a hairline and never shorter than one either: a
50
+ * silent passage has to read as "audio, and nothing here" rather than as a hole
51
+ * in the drawing.
52
+ */
53
+ export declare function layOutBars(bars: readonly EnvelopeBar[], options: {
54
+ width: number;
55
+ height: number;
56
+ gap: number;
57
+ align: WaveformAlignment;
58
+ /** Multiplies every magnitude before it is clamped back into 0…1. */
59
+ gain: number;
60
+ /** Fraction of the box a silent bar still occupies. */
61
+ floor: number;
62
+ }): BarBox[];
63
+ /**
64
+ * Draws a run of bars in one paint.
65
+ *
66
+ * Batched by colour by the caller, and this is why: every `Graphics2D` handed to
67
+ * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a
68
+ * thousand surfaces a frame. Chaining the rects of one colour into a single
69
+ * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill
70
+ * maps across the whole run rather than restarting inside every bar, which is
71
+ * the behaviour anyone reaching for a gradient here actually wants.
72
+ */
73
+ export declare function paintBars(boxes: readonly BarBox[], fill: Fill, radius: number): Graphics2D | null;
74
+ /**
75
+ * Groups bars by the paint they take, preserving order within each group.
76
+ *
77
+ * The seam that makes {@link paintBars} worth batching: a diarized waveform has
78
+ * as many colours as it has voices, not as many as it has bars, so a
79
+ * conversation of two people is two draws however long it runs.
80
+ */
81
+ export declare function groupByPaint<K>(boxes: readonly BarBox[], keyOf: (index: number) => K): Map<K, BarBox[]>;
82
+ export declare function clamp01(value: number): number;
83
+ /**
84
+ * The {@link groupByPaint} key one bar of a live visualiser takes.
85
+ *
86
+ * Filtered to one voice, every bar is that voice regardless of `byVoice` or
87
+ * whose turn the bucket originally belonged to. That second part is the one
88
+ * worth spelling out: {@link forSpeaker} silences a bar by zeroing its
89
+ * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the
90
+ * middle of a *different* voice's turn is still stamped with that voice's
91
+ * index — grouping by `byVoice`'s usual per-bar key would paint those silent
92
+ * stretches in whoever used to be talking there, in flickers too short to
93
+ * read as anything but noise. Forcing every bar under the filtered voice's own
94
+ * key is the true picture instead: the node is drawing exactly one person, on
95
+ * or off, never anyone else's colour.
96
+ */
97
+ export declare function livePaintKey(barSpeaker: number, byVoice: boolean, filteredVoice: number | null): string;
98
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/waveform/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,IAAI,EAAqB,MAAM,mBAAmB,CAAA;AAE5E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;;;;;;;GAQG;AAEH,8CAA8C;AAC9C,eAAO,MAAM,mBAAmB,+BAAgC,CAAA;AAChE,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAA;AAEpE,qCAAqC;AACrC,eAAO,MAAM,eAAe,qCAAsC,CAAA;AAClE,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAA;AAE5D;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,mGAShB,CAAA;AAEV,sEAAsE;AACtE,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKhD;AAED;;;;GAIG;AACH,MAAM,WAAW,MAAM;IACrB,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,OAAO,EAAE;IACP,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,iBAAiB,CAAA;IACxB,qEAAqE;IACrE,IAAI,EAAE,MAAM,CAAA;IACZ,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAA;CACd,GACA,MAAM,EAAE,CA2BV;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,MAAM,GACb,UAAU,GAAG,IAAI,CAmBnB;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC5B,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,GAC1B,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CASlB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAC1B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,OAAO,EAChB,aAAa,EAAE,MAAM,GAAG,IAAI,GAC3B,MAAM,CAGR"}
@@ -0,0 +1,149 @@
1
+ import { Graphics2D } from "@motionscript/sdk";
2
+ /**
3
+ * Geometry and paint shared by the two waveform nodes.
4
+ *
5
+ * They draw the same object — a row of bars whose heights are loudness — and
6
+ * differ only in what decides the heights: the whole file for one, a window
7
+ * around the playhead for the other. Everything about *how a row of bars is
8
+ * laid out and painted* is therefore common, and lives here so the two cannot
9
+ * drift into looking like different components.
10
+ */
11
+ /** How a bar is grown out of its baseline. */
12
+ export const WAVEFORM_ALIGNMENTS = ["center", "bottom"];
13
+ /** What the bars are shaped like. */
14
+ export const WAVEFORM_STYLES = ["bars", "line", "blocks"];
15
+ /**
16
+ * The colours voices are drawn in when nobody has assigned any.
17
+ *
18
+ * Eight hues that can be told apart at a glance, handed out by voice index. They
19
+ * are literals rather than looked up, because a scene has to build in a
20
+ * headless render with no database in scope; a host that names its voices
21
+ * supplies its own colours instead.
22
+ */
23
+ export const VOICE_PALETTE = [
24
+ "#6366f1",
25
+ "#14b8a6",
26
+ "#f59e0b",
27
+ "#ec4899",
28
+ "#22c55e",
29
+ "#3b82f6",
30
+ "#f97316",
31
+ "#a855f7",
32
+ ];
33
+ /** The colour a voice takes, wrapping past the end of the palette. */
34
+ export function voiceColor(index) {
35
+ return VOICE_PALETTE[((index % VOICE_PALETTE.length) + VOICE_PALETTE.length) %
36
+ VOICE_PALETTE.length];
37
+ }
38
+ /**
39
+ * Lays a row of bars out across a box.
40
+ *
41
+ * The gap is stated as a *fraction of the pitch* rather than in pixels, and
42
+ * that is the one decision here worth stating out loud: the bar count is a
43
+ * control, so a gap in pixels would silently become a solid block at 200 bars
44
+ * and a picket fence at 20. As a fraction, the row keeps the same rhythm at
45
+ * every density — which is what lets the count be a slider anyone can drag.
46
+ *
47
+ * A bar is never thinner than a hairline and never shorter than one either: a
48
+ * silent passage has to read as "audio, and nothing here" rather than as a hole
49
+ * in the drawing.
50
+ */
51
+ export function layOutBars(bars, options) {
52
+ const { width, height, gap, align, gain, floor } = options;
53
+ const count = bars.length;
54
+ if (count === 0 || width <= 0 || height <= 0)
55
+ return [];
56
+ const pitch = width / count;
57
+ const barWidth = Math.max(1, pitch * (1 - clamp01(gap)));
58
+ // The tallest a bar may be. Centre-aligned bars grow both ways from the
59
+ // middle, so the *half* is what a magnitude of 1 fills.
60
+ const full = align === "center" ? height / 2 : height;
61
+ const out = new Array(count);
62
+ for (let i = 0; i < count; i++) {
63
+ const magnitude = clamp01(bars[i].magnitude * gain);
64
+ const reach = Math.max(full * clamp01(floor), full * magnitude);
65
+ const x = -width / 2 + pitch * i + (pitch - barWidth) / 2;
66
+ // A hairline minimum, taken before the position so the bar stays inside the
67
+ // box: a silent bar 1px tall that kept the position of a zero-height one
68
+ // would hang half a pixel (centred) or a whole pixel (bottom) outside it.
69
+ const h = Math.max(1, align === "center" ? reach * 2 : reach);
70
+ out[i] =
71
+ align === "center"
72
+ ? { x, y: -h / 2, width: barWidth, height: h }
73
+ : { x, y: height / 2 - h, width: barWidth, height: h };
74
+ }
75
+ return out;
76
+ }
77
+ /**
78
+ * Draws a run of bars in one paint.
79
+ *
80
+ * Batched by colour by the caller, and this is why: every `Graphics2D` handed to
81
+ * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a
82
+ * thousand surfaces a frame. Chaining the rects of one colour into a single
83
+ * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill
84
+ * maps across the whole run rather than restarting inside every bar, which is
85
+ * the behaviour anyone reaching for a gradient here actually wants.
86
+ */
87
+ export function paintBars(boxes, fill, radius) {
88
+ if (boxes.length === 0)
89
+ return null;
90
+ if (fill.length === 0)
91
+ return null;
92
+ const graphics = new Graphics2D();
93
+ for (const box of boxes) {
94
+ graphics.rect({
95
+ x: box.x + box.width / 2,
96
+ // The box is stated y-down and the rect is drawn y-up. A centred bar is
97
+ // symmetric about the middle, so only a bottom-aligned one shows the flip.
98
+ y: -(box.y + box.height / 2),
99
+ width: box.width,
100
+ height: box.height,
101
+ // Never rounder than the bar is wide, or a thin bar becomes a lozenge
102
+ // whose ends taper away to nothing.
103
+ cornerRadius: Math.min(radius, box.width / 2, box.height / 2),
104
+ });
105
+ }
106
+ return graphics.fill(fill);
107
+ }
108
+ /**
109
+ * Groups bars by the paint they take, preserving order within each group.
110
+ *
111
+ * The seam that makes {@link paintBars} worth batching: a diarized waveform has
112
+ * as many colours as it has voices, not as many as it has bars, so a
113
+ * conversation of two people is two draws however long it runs.
114
+ */
115
+ export function groupByPaint(boxes, keyOf) {
116
+ const groups = new Map();
117
+ for (let i = 0; i < boxes.length; i++) {
118
+ const key = keyOf(i);
119
+ const run = groups.get(key);
120
+ if (run)
121
+ run.push(boxes[i]);
122
+ else
123
+ groups.set(key, [boxes[i]]);
124
+ }
125
+ return groups;
126
+ }
127
+ export function clamp01(value) {
128
+ return value < 0 ? 0 : value > 1 ? 1 : value;
129
+ }
130
+ /**
131
+ * The {@link groupByPaint} key one bar of a live visualiser takes.
132
+ *
133
+ * Filtered to one voice, every bar is that voice regardless of `byVoice` or
134
+ * whose turn the bucket originally belonged to. That second part is the one
135
+ * worth spelling out: {@link forSpeaker} silences a bar by zeroing its
136
+ * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the
137
+ * middle of a *different* voice's turn is still stamped with that voice's
138
+ * index — grouping by `byVoice`'s usual per-bar key would paint those silent
139
+ * stretches in whoever used to be talking there, in flickers too short to
140
+ * read as anything but noise. Forcing every bar under the filtered voice's own
141
+ * key is the true picture instead: the node is drawing exactly one person, on
142
+ * or off, never anyone else's colour.
143
+ */
144
+ export function livePaintKey(barSpeaker, byVoice, filteredVoice) {
145
+ if (filteredVoice !== null)
146
+ return `voice:${filteredVoice}`;
147
+ return byVoice ? `voice:${barSpeaker}` : "all";
148
+ }
149
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/waveform/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAgC,MAAM,mBAAmB,CAAA;AAI5E;;;;;;;;GAQG;AAEH,8CAA8C;AAC9C,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAU,CAAA;AAGhE,qCAAqC;AACrC,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAU,CAAA;AAGlE;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;CACD,CAAA;AAEV,sEAAsE;AACtE,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,aAAa,CAClB,CAAC,CAAC,KAAK,GAAG,aAAa,CAAC,MAAM,CAAC,GAAG,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Resolved } from \"@motionscript/sdk\"\n\nimport type { EnvelopeBar } from \"./envelope\"\n\n/**\n * Geometry and paint shared by the two waveform nodes.\n *\n * They draw the same object — a row of bars whose heights are loudness — and\n * differ only in what decides the heights: the whole file for one, a window\n * around the playhead for the other. Everything about *how a row of bars is\n * laid out and painted* is therefore common, and lives here so the two cannot\n * drift into looking like different components.\n */\n\n/** How a bar is grown out of its baseline. */\nexport const WAVEFORM_ALIGNMENTS = [\"center\", \"bottom\"] as const\nexport type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number]\n\n/** What the bars are shaped like. */\nexport const WAVEFORM_STYLES = [\"bars\", \"line\", \"blocks\"] as const\nexport type WaveformStyle = (typeof WAVEFORM_STYLES)[number]\n\n/**\n * The colours voices are drawn in when nobody has assigned any.\n *\n * Eight hues that can be told apart at a glance, handed out by voice index. They\n * are literals rather than looked up, because a scene has to build in a\n * headless render with no database in scope; a host that names its voices\n * supplies its own colours instead.\n */\nexport const VOICE_PALETTE = [\n \"#6366f1\",\n \"#14b8a6\",\n \"#f59e0b\",\n \"#ec4899\",\n \"#22c55e\",\n \"#3b82f6\",\n \"#f97316\",\n \"#a855f7\",\n] as const\n\n/** The colour a voice takes, wrapping past the end of the palette. */\nexport function voiceColor(index: number): string {\n return VOICE_PALETTE[\n ((index % VOICE_PALETTE.length) + VOICE_PALETTE.length) %\n VOICE_PALETTE.length\n ]\n}\n\n/**\n * Where one bar sits and how tall it came out: its top-left corner and size, in\n * the node's own pixels with **y pointing down** from the middle of the box.\n * {@link paintBars} turns that into the centred, y-up rect the renderer draws.\n */\nexport interface BarBox {\n x: number\n y: number\n width: number\n height: number\n}\n\n/**\n * Lays a row of bars out across a box.\n *\n * The gap is stated as a *fraction of the pitch* rather than in pixels, and\n * that is the one decision here worth stating out loud: the bar count is a\n * control, so a gap in pixels would silently become a solid block at 200 bars\n * and a picket fence at 20. As a fraction, the row keeps the same rhythm at\n * every density — which is what lets the count be a slider anyone can drag.\n *\n * A bar is never thinner than a hairline and never shorter than one either: a\n * silent passage has to read as \"audio, and nothing here\" rather than as a hole\n * in the drawing.\n */\nexport function layOutBars(\n bars: readonly EnvelopeBar[],\n options: {\n width: number\n height: number\n gap: number\n align: WaveformAlignment\n /** Multiplies every magnitude before it is clamped back into 0…1. */\n gain: number\n /** Fraction of the box a silent bar still occupies. */\n floor: number\n }\n): BarBox[] {\n const { width, height, gap, align, gain, floor } = options\n const count = bars.length\n if (count === 0 || width <= 0 || height <= 0) return []\n\n const pitch = width / count\n const barWidth = Math.max(1, pitch * (1 - clamp01(gap)))\n // The tallest a bar may be. Centre-aligned bars grow both ways from the\n // middle, so the *half* is what a magnitude of 1 fills.\n const full = align === \"center\" ? height / 2 : height\n\n const out: BarBox[] = new Array(count)\n for (let i = 0; i < count; i++) {\n const magnitude = clamp01(bars[i].magnitude * gain)\n const reach = Math.max(full * clamp01(floor), full * magnitude)\n const x = -width / 2 + pitch * i + (pitch - barWidth) / 2\n\n // A hairline minimum, taken before the position so the bar stays inside the\n // box: a silent bar 1px tall that kept the position of a zero-height one\n // would hang half a pixel (centred) or a whole pixel (bottom) outside it.\n const h = Math.max(1, align === \"center\" ? reach * 2 : reach)\n out[i] =\n align === \"center\"\n ? { x, y: -h / 2, width: barWidth, height: h }\n : { x, y: height / 2 - h, width: barWidth, height: h }\n }\n return out\n}\n\n/**\n * Draws a run of bars in one paint.\n *\n * Batched by colour by the caller, and this is why: every `Graphics2D` handed to\n * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a\n * thousand surfaces a frame. Chaining the rects of one colour into a single\n * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill\n * maps across the whole run rather than restarting inside every bar, which is\n * the behaviour anyone reaching for a gradient here actually wants.\n */\nexport function paintBars(\n boxes: readonly BarBox[],\n fill: Fill,\n radius: number\n): Graphics2D | null {\n if (boxes.length === 0) return null\n if ((fill as FillResolved[]).length === 0) return null\n\n const graphics = new Graphics2D()\n for (const box of boxes) {\n graphics.rect({\n x: box.x + box.width / 2,\n // The box is stated y-down and the rect is drawn y-up. A centred bar is\n // symmetric about the middle, so only a bottom-aligned one shows the flip.\n y: -(box.y + box.height / 2),\n width: box.width,\n height: box.height,\n // Never rounder than the bar is wide, or a thin bar becomes a lozenge\n // whose ends taper away to nothing.\n cornerRadius: Math.min(radius, box.width / 2, box.height / 2),\n })\n }\n return graphics.fill(fill)\n}\n\n/**\n * Groups bars by the paint they take, preserving order within each group.\n *\n * The seam that makes {@link paintBars} worth batching: a diarized waveform has\n * as many colours as it has voices, not as many as it has bars, so a\n * conversation of two people is two draws however long it runs.\n */\nexport function groupByPaint<K>(\n boxes: readonly BarBox[],\n keyOf: (index: number) => K\n): Map<K, BarBox[]> {\n const groups = new Map<K, BarBox[]>()\n for (let i = 0; i < boxes.length; i++) {\n const key = keyOf(i)\n const run = groups.get(key)\n if (run) run.push(boxes[i])\n else groups.set(key, [boxes[i]])\n }\n return groups\n}\n\nexport function clamp01(value: number): number {\n return value < 0 ? 0 : value > 1 ? 1 : value\n}\n\n/**\n * The {@link groupByPaint} key one bar of a live visualiser takes.\n *\n * Filtered to one voice, every bar is that voice regardless of `byVoice` or\n * whose turn the bucket originally belonged to. That second part is the one\n * worth spelling out: {@link forSpeaker} silences a bar by zeroing its\n * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the\n * middle of a *different* voice's turn is still stamped with that voice's\n * index — grouping by `byVoice`'s usual per-bar key would paint those silent\n * stretches in whoever used to be talking there, in flickers too short to\n * read as anything but noise. Forcing every bar under the filtered voice's own\n * key is the true picture instead: the node is drawing exactly one person, on\n * or off, never anyone else's colour.\n */\nexport function livePaintKey(\n barSpeaker: number,\n byVoice: boolean,\n filteredVoice: number | null\n): string {\n if (filteredVoice !== null) return `voice:${filteredVoice}`\n return byVoice ? `voice:${barSpeaker}` : \"all\"\n}\n"]}