@motionscript/audio 0.1.0-alpha.9 → 0.1.1-alpha.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +1 -1
  3. package/dist/browser/chunks/chunk-WUH3G22N.js +2 -0
  4. package/dist/browser/chunks/chunk-WUH3G22N.js.map +7 -0
  5. package/dist/browser/index.js +1 -1
  6. package/dist/browser/index.js.map +3 -3
  7. package/dist/browser/kit.js +1 -1
  8. package/dist/browser/manifest.json +1 -1
  9. package/dist/kit.d.ts +1 -9
  10. package/dist/kit.d.ts.map +1 -1
  11. package/dist/kit.js +2 -9
  12. package/dist/kit.js.map +1 -1
  13. package/dist/nodes.d.ts +0 -12
  14. package/dist/nodes.d.ts.map +1 -1
  15. package/dist/nodes.js +0 -12
  16. package/dist/nodes.js.map +1 -1
  17. package/dist/waveform/envelope.d.ts +27 -98
  18. package/dist/waveform/envelope.d.ts.map +1 -1
  19. package/dist/waveform/envelope.js +17 -77
  20. package/dist/waveform/envelope.js.map +1 -1
  21. package/dist/waveform/full-waveform.d.ts +32 -61
  22. package/dist/waveform/full-waveform.d.ts.map +1 -1
  23. package/dist/waveform/full-waveform.js +25 -66
  24. package/dist/waveform/full-waveform.js.map +1 -1
  25. package/dist/waveform/index.d.ts +0 -16
  26. package/dist/waveform/index.d.ts.map +1 -1
  27. package/dist/waveform/index.js +0 -14
  28. package/dist/waveform/index.js.map +1 -1
  29. package/dist/waveform/live-waveform.d.ts +33 -70
  30. package/dist/waveform/live-waveform.d.ts.map +1 -1
  31. package/dist/waveform/live-waveform.js +24 -74
  32. package/dist/waveform/live-waveform.js.map +1 -1
  33. package/dist/waveform/shared.d.ts +21 -62
  34. package/dist/waveform/shared.d.ts.map +1 -1
  35. package/dist/waveform/shared.js +33 -71
  36. package/dist/waveform/shared.js.map +1 -1
  37. package/package.json +4 -4
  38. package/src/kit.ts +1 -9
  39. package/src/nodes.ts +0 -12
  40. package/src/waveform/envelope.ts +28 -100
  41. package/src/waveform/full-waveform.ts +25 -67
  42. package/src/waveform/index.ts +0 -16
  43. package/src/waveform/live-waveform.ts +24 -74
  44. package/src/waveform/shared.ts +33 -78
  45. package/dist/browser/chunks/chunk-DD3KHRUL.js +0 -2
  46. package/dist/browser/chunks/chunk-DD3KHRUL.js.map +0 -7
@@ -1,25 +1,8 @@
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
- */
1
+ import { Graphics2D, clamp } from "@motionscript/sdk";
11
2
  /** How a bar is grown out of its baseline. */
12
3
  export const WAVEFORM_ALIGNMENTS = ["center", "bottom"];
13
- /** What the bars are shaped like. */
14
4
  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
- */
5
+ /** Eight distinct hues handed out by voice index. Literals, because a scene builds headless with no database in scope; a host that names its voices supplies its own. */
23
6
  export const VOICE_PALETTE = [
24
7
  "#6366f1",
25
8
  "#14b8a6",
@@ -36,17 +19,10 @@ export function voiceColor(index) {
36
19
  VOICE_PALETTE.length];
37
20
  }
38
21
  /**
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.
22
+ * Lays a row of bars across a box. The gap is a fraction of the pitch, not px:
23
+ * the bar count is a control, and a px gap would become a solid block at 200
24
+ * bars and a picket fence at 20. A bar is never thinner or shorter than a
25
+ * hairline, so a silent passage reads as audio with nothing in it, not a hole.
50
26
  */
51
27
  export function layOutBars(bars, options) {
52
28
  const { width, height, gap, align, gain, floor } = options;
@@ -54,18 +30,15 @@ export function layOutBars(bars, options) {
54
30
  if (count === 0 || width <= 0 || height <= 0)
55
31
  return [];
56
32
  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.
33
+ const barWidth = Math.max(1, pitch * (1 - clamp(gap, 0, 1)));
34
+ // Centre-aligned bars grow both ways, so half the height is what a magnitude of 1 fills.
60
35
  const full = align === "center" ? height / 2 : height;
61
36
  const out = new Array(count);
62
37
  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);
38
+ const magnitude = clamp(bars[i].magnitude * gain, 0, 1);
39
+ const reach = Math.max(full * clamp(floor, 0, 1), full * magnitude);
65
40
  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.
41
+ // The hairline minimum is taken before the position, or a 1px bar placed as if zero-height would hang outside the box.
69
42
  const h = Math.max(1, align === "center" ? reach * 2 : reach);
70
43
  out[i] =
71
44
  align === "center"
@@ -75,14 +48,10 @@ export function layOutBars(bars, options) {
75
48
  return out;
76
49
  }
77
50
  /**
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.
51
+ * Draws a run of bars in one paint. Callers batch by colour: every `Graphics2D`
52
+ * handed to `ctx.draw` is a surface the renderer composites, so a bar apiece
53
+ * would be a thousand surfaces a frame. One `Graphics2D` per run also maps a
54
+ * gradient across the whole run instead of restarting inside every bar.
86
55
  */
87
56
  export function paintBars(boxes, fill, radius) {
88
57
  if (boxes.length === 0)
@@ -93,25 +62,17 @@ export function paintBars(boxes, fill, radius) {
93
62
  for (const box of boxes) {
94
63
  graphics.rect({
95
64
  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.
65
+ // The box is y-down and the rect y-up; only a bottom-aligned bar shows the flip.
98
66
  y: -(box.y + box.height / 2),
99
67
  width: box.width,
100
68
  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.
69
+ // A thin bar rounder than it is wide becomes a lozenge.
103
70
  cornerRadius: Math.min(radius, box.width / 2, box.height / 2),
104
71
  });
105
72
  }
106
73
  return graphics.fill(fill);
107
74
  }
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
- */
75
+ /** Groups bars by the paint they take, in order within each group: a diarized waveform has as many colours as voices, so two people is two draws however long it runs. */
115
76
  export function groupByPaint(boxes, keyOf) {
116
77
  const groups = new Map();
117
78
  for (let i = 0; i < boxes.length; i++) {
@@ -124,22 +85,23 @@ export function groupByPaint(boxes, keyOf) {
124
85
  }
125
86
  return groups;
126
87
  }
127
- export function clamp01(value) {
128
- return value < 0 ? 0 : value > 1 ? 1 : value;
88
+ /**
89
+ * The colour of a `voice:N` group key, or `fallback` for a bad index. Always a
90
+ * colour string, never one of the node's fills: a voice is identified by hue, so
91
+ * a gradient or image would defeat the point, and it keeps these paints from
92
+ * declaring assets.
93
+ */
94
+ export function voiceFill(key, voiceColors, fallback) {
95
+ const voice = Number(key.slice("voice:".length));
96
+ if (!Number.isFinite(voice) || voice < 0)
97
+ return fallback;
98
+ return voiceColors[voice] ?? voiceColor(voice);
129
99
  }
130
100
  /**
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.
101
+ * The {@link groupByPaint} key of one live bar. Filtered to a voice, every bar
102
+ * takes that voice's key: {@link forSpeaker} zeroes a bar's magnitude but leaves
103
+ * its `speaker`, so the usual per-bar key would paint the silenced stretches in
104
+ * whoever used to talk there, in flickers too short to read as anything but noise.
143
105
  */
144
106
  export function livePaintKey(barSpeaker, byVoice, filteredVoice) {
145
107
  if (filteredVoice !== null)
@@ -1 +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,e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AAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;AAC9C,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,YAAY,CAC1B,UAAkB,EAClB,OAAgB,EAChB,aAA4B;IAE5B,IAAI,aAAa,KAAK,IAAI;QAAE,OAAO,SAAS,aAAa,EAAE,CAAA;IAC3D,OAAO,OAAO,CAAC,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,CAAC,CAAC,KAAK,CAAA;AAChD,CAAC","sourcesContent":["import { Graphics2D, type Fill, type FillResolved } 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"]}
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/waveform/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAgC,MAAM,mBAAmB,CAAA;AAInF,8CAA8C;AAC9C,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAU,CAAA;AAGhE,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAU,CAAA;AAGlE,yKAAyK;AACzK,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,aAAa,CAAC,MAAM,CAAC;QACrD,aAAa,CAAC,MAAM,CACvB,CAAA;AACH,CAAC;AAUD;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CACxB,IAA4B,EAC5B,OAQC;IAED,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,OAAO,CAAA;IAC1D,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAA;IACzB,IAAI,KAAK,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC;QAAE,OAAO,EAAE,CAAA;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;QACtC,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;QACpB,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC3B,IAAI,GAAG;YAAE,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;;YACtB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAClC,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW,EAAE,WAA8B,EAAE,QAAc;IACnF,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAA;IAChD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC;QAAE,OAAO,QAAQ,CAAA;IACzD,OAAO,WAAW,CAAC,KAAK,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAA;AAChD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAC1B,UAAkB,EAClB,OAAgB,EAChB,aAA4B;IAE5B,IAAI,aAAa,KAAK,IAAI;QAAE,OAAO,SAAS,aAAa,EAAE,CAAA;IAC3D,OAAO,OAAO,CAAC,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,CAAC,CAAC,KAAK,CAAA;AAChD,CAAC","sourcesContent":["import { Graphics2D, clamp, type Fill, type FillResolved } from \"@motionscript/sdk\"\n\nimport type { EnvelopeBar } from \"./envelope\"\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\nexport const WAVEFORM_STYLES = [\"bars\", \"line\", \"blocks\"] as const\nexport type WaveformStyle = (typeof WAVEFORM_STYLES)[number]\n\n/** Eight distinct hues handed out by voice index. Literals, because a scene builds headless with no database in scope; a host that names its voices supplies its own. */\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/** A bar's top-left corner and size in the node's pixels, with y pointing down from the middle of the box; {@link paintBars} turns it into the centred y-up rect. */\nexport interface BarBox {\n x: number\n y: number\n width: number\n height: number\n}\n\n/**\n * Lays a row of bars across a box. The gap is a fraction of the pitch, not px:\n * the bar count is a control, and a px gap would become a solid block at 200\n * bars and a picket fence at 20. A bar is never thinner or shorter than a\n * hairline, so a silent passage reads as audio with nothing in it, not a hole.\n */\nexport function layOutBars(\n bars: readonly EnvelopeBar[],\n options: {\n width: number\n height: number\n gap: number\n align: WaveformAlignment\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 - clamp(gap, 0, 1)))\n // Centre-aligned bars grow both ways, so half the height 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 = clamp(bars[i].magnitude * gain, 0, 1)\n const reach = Math.max(full * clamp(floor, 0, 1), full * magnitude)\n const x = -width / 2 + pitch * i + (pitch - barWidth) / 2\n\n // The hairline minimum is taken before the position, or a 1px bar placed as if zero-height would hang outside the box.\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. Callers batch by colour: every `Graphics2D`\n * handed to `ctx.draw` is a surface the renderer composites, so a bar apiece\n * would be a thousand surfaces a frame. One `Graphics2D` per run also maps a\n * gradient across the whole run instead of restarting inside every bar.\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 y-down and the rect y-up; only a bottom-aligned bar shows the flip.\n y: -(box.y + box.height / 2),\n width: box.width,\n height: box.height,\n // A thin bar rounder than it is wide becomes a lozenge.\n cornerRadius: Math.min(radius, box.width / 2, box.height / 2),\n })\n }\n return graphics.fill(fill)\n}\n\n/** Groups bars by the paint they take, in order within each group: a diarized waveform has as many colours as voices, so two people is two draws however long it runs. */\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\n/**\n * The colour of a `voice:N` group key, or `fallback` for a bad index. Always a\n * colour string, never one of the node's fills: a voice is identified by hue, so\n * a gradient or image would defeat the point, and it keeps these paints from\n * declaring assets.\n */\nexport function voiceFill(key: string, voiceColors: readonly string[], fallback: Fill): Fill {\n const voice = Number(key.slice(\"voice:\".length))\n if (!Number.isFinite(voice) || voice < 0) return fallback\n return voiceColors[voice] ?? voiceColor(voice)\n}\n\n/**\n * The {@link groupByPaint} key of one live bar. Filtered to a voice, every bar\n * takes that voice's key: {@link forSpeaker} zeroes a bar's magnitude but leaves\n * its `speaker`, so the usual per-bar key would paint the silenced stretches in\n * whoever used to talk there, in flickers too short to read as anything but noise.\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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@motionscript/audio",
3
- "version": "0.1.0-alpha.9",
3
+ "version": "0.1.1-alpha.0",
4
4
  "type": "module",
5
5
  "description": "Waveform components for Motion Script: a full recording drawn as bars, and a live meter that moves with playback. Both draw a loudness envelope the host supplies.",
6
6
  "repository": {
@@ -50,7 +50,7 @@
50
50
  "license": "Apache-2.0",
51
51
  "author": "Motionscript",
52
52
  "peerDependencies": {
53
- "@motionscript/sdk": "^0.1.0-alpha.9"
53
+ "@motionscript/sdk": "^0.1.1-alpha.0"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@types/node": "^25.6.2",
@@ -61,8 +61,8 @@
61
61
  "typescript": "^6.0.3",
62
62
  "typescript-eslint": "^8.48.0",
63
63
  "vitest": "^4.1.5",
64
- "@motionscript/core": "0.1.0-alpha.9",
65
- "@motionscript/sdk": "0.1.0-alpha.9"
64
+ "@motionscript/core": "0.1.1-alpha.0",
65
+ "@motionscript/sdk": "0.1.1-alpha.0"
66
66
  },
67
67
  "scripts": {
68
68
  "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json --resolve-full-paths && node ../../../scripts/build-browser.mjs",
package/src/kit.ts CHANGED
@@ -1,18 +1,10 @@
1
- /**
2
- * What the two waveform nodes share, importable without copying it.
3
- *
4
- * A registry item copies `envelope.ts` and `shared.ts` beside its node: they
5
- * hold no class and no registered state, so a fork owning its own copy splits
6
- * no identity. This is the same surface for a host that samples envelopes or
7
- * lays bars out itself, and for a fork that keeps only its node file.
8
- */
1
+ // A registry item copies `envelope.ts` and `shared.ts` beside its node (no class or registered state, so a copy splits no identity); this is the same surface for a host or a fork that keeps only its node file.
9
2
  export { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from "./waveform/envelope"
10
3
  export type { AudioEnvelope, EnvelopeBar } from "./waveform/envelope"
11
4
  export {
12
5
  VOICE_PALETTE,
13
6
  WAVEFORM_ALIGNMENTS,
14
7
  WAVEFORM_STYLES,
15
- clamp01,
16
8
  groupByPaint,
17
9
  layOutBars,
18
10
  livePaintKey,
package/src/nodes.ts CHANGED
@@ -1,17 +1,5 @@
1
1
  import { FullWaveform } from "./waveform/full-waveform";
2
2
  import { LiveWaveform } from "./waveform/live-waveform";
3
3
 
4
- /**
5
- * Every node type this package publishes.
6
- *
7
- * A host registers these by handing the array to an engine —
8
- * `new Engine(platform, { nodes: NODES })` — which reads each component's key.
9
- * Defining a component only *declares* that key: nothing is registered by
10
- * importing a module, so a registry holds exactly what its owner asked for.
11
- *
12
- * Listing the component **values** is also what survives bundling: this package
13
- * declares `sideEffects: false`, and a document names a node type by string, so
14
- * an import kept only for its side effect is exactly what gets shaken out.
15
- */
16
4
  // Typed by name, so the declaration names each component rather than spelling out its type through core.
17
5
  export const NODES: (typeof FullWaveform | typeof LiveWaveform)[] = [FullWaveform, LiveWaveform];
@@ -1,88 +1,32 @@
1
1
  /**
2
- * The shape of a recording, as the waveform nodes draw it.
3
- *
4
- * ## How an envelope reaches a node: the host supplies it
5
- *
6
- * Nothing in the engine, and nothing in this kit, decodes audio. A waveform node
7
- * does not read a `sound` node's `src`, declares no audio asset and is not tied
8
- * to a clip: it draws the {@link AudioEnvelope} it is handed in its `envelope`
9
- * prop. Decoding a file into an envelope is the host's job, done once per file
10
- * before the document is built. Where the recording sits on the audio lane is
11
- * the author's timing too, which is why `FullWaveform.follow` is opt-in.
12
- *
13
- * ### The JSON form
14
- *
15
- * A document holds the envelope as plain JSON, because a prop value in a
16
- * document is JSON. The typed arrays in {@link AudioEnvelope} are one spelling of
17
- * it; `ArrayLike<number>` also accepts the plain arrays a document stores:
18
- *
19
- * ```json
20
- * {
21
- * "magnitude": [0, 0.25, 0.5, 1, 0.5, 0],
22
- * "duration": 6,
23
- * "speaker": [0, 0, 0, 1, 1, -1],
24
- * "speakers": 2
25
- * }
26
- * ```
27
- *
28
- * `magnitude` is the peak loudness, `0`…`1`, of buckets that divide `duration`
29
- * seconds evenly. `speaker` is `null` when nobody was identified; otherwise it is parallel to `magnitude`, one voice
30
- * index per bucket, `-1` for "nobody". `speakers` is the voice count, `0` when
31
- * `speaker` is `null`. A `Float32Array` or `Int16Array` is not JSON-serialisable
32
- * (`JSON.stringify` writes an object of indices), so a host that holds the typed
33
- * form converts with `Array.from` before storing it in a document.
34
- *
35
- * ## Why the data is baked rather than listened to
36
- *
37
- * A scene has to render identically in the preview and in a headless export,
38
- * and an export has no audio device, no `AudioContext` and no playback to
39
- * analyse. So a "live" visualiser cannot read a Web Audio analyser: what it
40
- * reads is a *pre-computed envelope*, indexed by the scene clock. The picture is
41
- * then a pure function of time, which is what makes scrubbing backwards, jumping
42
- * to frame 900, and rendering out of order all produce the same frame.
43
- *
44
- * The decode happens in the host, once per file, and arrives here through the
45
- * build — the same arrangement a chart's rows and a protein's atoms already use,
46
- * and for the same reason: the build is synchronous and cannot await a fetch.
47
- *
48
- * ## Why magnitude rather than min/max
49
- *
50
- * An editor's waveform is drawn from per-bucket minima *and* maxima, because an
51
- * editor is a measuring instrument. These nodes are neither — they are a bar
52
- * chart of loudness that happens to be shaped like a recording — and audio is
53
- * near enough symmetric that a mirrored magnitude is indistinguishable at the
54
- * size a node draws. Half the numbers, and one array to reason about.
2
+ * The host decodes audio into an {@link AudioEnvelope} once per file, before the
3
+ * build, and hands it to the node; nothing here decodes or listens. It is baked
4
+ * because an export has no audio device or analyser, and the picture must be a
5
+ * pure function of time.
6
+ *
7
+ * In a document an envelope is plain JSON, which `ArrayLike<number>` accepts.
8
+ * A `Float32Array` is not JSON-serialisable (`JSON.stringify` writes an object
9
+ * of indices), so convert with `Array.from` before storing it.
55
10
  */
56
11
 
57
12
  /**
58
- * A decoded recording, reduced to a uniform grid of loudness — plus, when the
59
- * host knows it, who is talking in each bucket.
60
- *
61
- * The speaker track is **not** something a decoder can produce; it comes from a
62
- * diarized transcript and is folded in by the host before the build. It lives
63
- * here rather than in a parallel structure because every reader of it is also a
64
- * reader of the magnitudes at the same index, and two arrays that must stay the
65
- * same length are better as one object than as two arguments.
13
+ * A recording reduced to a uniform grid of loudness, plus who is talking in each
14
+ * bucket when the host knows. The speaker track comes from a diarized transcript,
15
+ * not the decoder; it lives here because every reader of it also reads the
16
+ * magnitudes at the same index.
66
17
  */
67
18
  export interface AudioEnvelope {
68
- /**
69
- * Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. A
70
- * `Float32Array`, or the plain `number[]` a document stores.
71
- */
19
+ /** Peak magnitude per bucket, 0 to 1; buckets span {@link duration} evenly. */
72
20
  magnitude: ArrayLike<number>
73
- /** The decoded length in seconds. */
21
+ /** Seconds. */
74
22
  duration: number
75
- /**
76
- * Which voice is speaking in each bucket, as an index into the recording's
77
- * own numbering, or `-1` for "nobody identified". Parallel to
78
- * {@link magnitude}, or `null` when the track was never diarized.
79
- */
23
+ /** Voice index per bucket (`-1` for nobody), parallel to {@link magnitude}, or `null` when never diarized. */
80
24
  speaker: ArrayLike<number> | null
81
- /** How many distinct voices the diarizer found. Zero when it did not run. */
25
+ /** Zero when the diarizer did not run. */
82
26
  speakers: number
83
27
  }
84
28
 
85
- /** The envelope of a track that hasn't loaded — or isn't there. */
29
+ /** The envelope of a track that has not loaded or is not there. */
86
30
  export const EMPTY_ENVELOPE: AudioEnvelope = {
87
31
  magnitude: new Float32Array(0),
88
32
  duration: 0,
@@ -90,28 +34,19 @@ export const EMPTY_ENVELOPE: AudioEnvelope = {
90
34
  speakers: 0,
91
35
  }
92
36
 
93
- /** One drawn bar: how loud, and whose. */
94
37
  export interface EnvelopeBar {
95
- /** 0…1. */
38
+ /** 0 to 1. */
96
39
  magnitude: number
97
- /** The voice index, or -1. */
40
+ /** Voice index, or -1. */
98
41
  speaker: number
99
42
  }
100
43
 
101
44
  /**
102
- * Resamples `[from, to)` seconds of an envelope down to `bars` columns.
103
- *
104
- * Peak-preserving rather than averaging, which is the whole difference between
105
- * a waveform and a blur: averaging a hundred buckets into one column flattens
106
- * every transient, and transients are what makes a drawn recording read as
107
- * speech rather than as noise. The speaker of a column is the speaker of its
108
- * *loudest* bucket, on the same reasoning — a column straddling a turn belongs
109
- * to whoever is actually audible in it.
110
- *
111
- * A window past either end of the recording yields silent, unattributed
112
- * columns rather than clamping onto the nearest real one. That matters for the
113
- * live visualiser, which is handed a window centred on the playhead and would
114
- * otherwise smear the first bucket across the whole run-up to a track's start.
45
+ * Resamples `[from, to)` seconds down to `bars` columns, keeping peaks rather
46
+ * than averaging: averaging flattens the transients that make a drawing read as
47
+ * speech. A column's speaker is that of its loudest bucket. A window past either
48
+ * end yields silent, unattributed columns rather than clamping onto the nearest
49
+ * real one, which would smear the first bucket across a live window's run-up.
115
50
  */
116
51
  export function sampleEnvelope(
117
52
  envelope: AudioEnvelope,
@@ -133,8 +68,7 @@ export function sampleEnvelope(
133
68
  const start = (from + span * i) * perSecond
134
69
  const end = (from + span * (i + 1)) * perSecond
135
70
 
136
- // At least one bucket per column, so a window zoomed in past the grid draws
137
- // the sample it is standing on rather than an empty band.
71
+ // At least one bucket per column, so a zoomed-in window draws the sample it stands on.
138
72
  const first = Math.floor(start)
139
73
  const last = Math.max(first, Math.ceil(end) - 1)
140
74
 
@@ -155,15 +89,9 @@ export function sampleEnvelope(
155
89
  }
156
90
 
157
91
  /**
158
- * The same window, with everything that isn't `voice` silenced.
159
- *
160
- * The point of the speaker filter, and the reason it silences rather than
161
- * *drops*: a podcast visualiser filtered to one host should go quiet while the
162
- * other one talks, not compress their turn out of existence. Keeping the bars
163
- * and zeroing them is what makes the picture read as "this person is not
164
- * speaking right now" instead of as a jump cut.
165
- *
166
- * `voice` of `null` returns the bars untouched, which is the unfiltered node.
92
+ * The same window with everything but `voice` silenced, not dropped, so a
93
+ * filtered visualiser goes quiet while the other host talks instead of cutting
94
+ * their turn out. `null` returns the bars untouched.
167
95
  */
168
96
  export function forSpeaker(
169
97
  bars: EnvelopeBar[],
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  Types,
3
+ clamp,
3
4
  component2d,
4
5
  easeInOut,
5
6
  fillOps,
@@ -14,88 +15,61 @@ import {
14
15
 
15
16
  import { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from "./envelope"
16
17
  import {
17
- clamp01,
18
18
  groupByPaint,
19
19
  layOutBars,
20
20
  paintBars,
21
- voiceColor,
21
+ voiceFill,
22
22
  type WaveformAlignment,
23
23
  } from "./shared"
24
24
 
25
- /** A paint entry as the waveforms declare theirs: resolved and tweened like a fill, its kind read off the default. */
25
+ /** A fill entry whose kind is read off the default. */
26
26
  const paint = (initial: string) =>
27
27
  Types.custom<Fill, FillResolved[]>(initial, { resolve: fillOps.resolve, lerp: fillOps.lerp })
28
28
 
29
29
  /**
30
- * A **waveform overview**: the whole recording's shape, drawn at once.
30
+ * The whole recording's shape drawn at once, with a played portion that
31
+ * advances: a picture of the file where {@link LiveWaveform} is of the moment.
31
32
  *
32
- * The picture every audio editor opens with, and the one a podcast clip puts
33
- * under its caption — a static silhouette of the file, with a played portion
34
- * that advances. It answers "how long is this and where are the loud parts",
35
- * which is a question about the *file*, not about the moment; its sibling
36
- * {@link LiveWaveform} answers the other one.
37
- *
38
- * ## Progress is authored, not assumed
39
- *
40
- * `follow` is off by default, and that default is the considered one. A
41
- * scene has no idea where the recording it is drawing sits on the video's audio
42
- * lane — a clip is placed, trimmed and sped up out there, and none of that is
43
- * visible from in here. So the honest default is that the fill is *animation*:
44
- * the author sweeps it with a command, the same as any other tween, and it lands
45
- * wherever they put it. `follow` is the shortcut for the common arrangement
46
- * where the node appears exactly as the track starts, and it says so by being a
47
- * switch someone has to turn on rather than a behaviour that quietly disagrees
48
- * with the mix.
49
- *
50
- * ## Voices instead of progress
51
- *
52
- * With `byVoice` on, the played/ahead split is replaced by one colour per
53
- * speaking voice, at a size you can read. For a two-hander it is a map of the conversation: who has the
54
- * floor, where the turns are, how long each one ran. It replaces the progress
55
- * colours rather than combining with them, because a bar cannot mean two things
56
- * at once and "played, and also Grace" is a legend nobody can hold in their
57
- * head.
33
+ * `follow` is off by default because a scene cannot see where the recording sits
34
+ * on the video's audio lane (placed, trimmed, sped up); the author sweeps
35
+ * `progress` with a command, and `follow` is the opt-in for a node that appears
36
+ * exactly as the track starts. `byVoice` replaces the played/ahead split with
37
+ * one colour per voice rather than combining with it: a bar cannot mean two
38
+ * things at once.
58
39
  */
59
40
  export const FullWaveform = component2d({
60
41
  key: "fullWaveform",
61
42
  state: {
62
- /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
43
+ /** The baked recording; see {@link AudioEnvelope}. */
63
44
  envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),
64
- /** How many columns the whole file is drawn as. */
65
45
  bars: 120,
66
- /** Space between bars, as a fraction of the pitch. See {@link layOutBars}. */
46
+ /** Space between bars as a fraction of the pitch; see {@link layOutBars}. */
67
47
  gap: 0.3,
68
- /** Corner radius of a bar, capped at half its width. */
48
+ /** Capped at half the bar's width. */
69
49
  radius: 2,
70
- /** How much of the file reads as played, `0`–`1`. Tweenable. */
50
+ /** The share of the file that reads as played, `0`-`1`. */
71
51
  progress: 0,
72
- /** Drive `progress` from the node's own clock instead of the prop. */
52
+ /** Drive `progress` from the node's own clock instead. */
73
53
  follow: false,
74
- /** Multiplies every magnitude — a quiet track pulled up to fill its box. */
54
+ /** Multiplies every magnitude, pulling a quiet track up to fill its box. */
75
55
  gain: 1,
76
- /** How much of the box a silent bar still occupies, `0`–`1`. */
56
+ /** The share of the box a silent bar still occupies, `0`-`1`. */
77
57
  floor: 0.015,
78
58
  align: Types.custom<WaveformAlignment>("center"),
79
- /** Colour each voice's turns rather than splitting on played / not played. */
80
59
  byVoice: false,
81
- /** A colour per voice index. Short or empty falls back to the palette. */
60
+ /** A colour per voice index; short or empty falls back to the palette. */
82
61
  voiceColors: Types.custom<string[]>([]),
83
62
  /** The bars ahead of the playhead. */
84
63
  fill: paint("#5b5b7a"),
85
- /** …and the bars behind it. */
86
64
  playedFill: paint("#6366f1"),
87
65
  },
88
- // Nothing declares a component's paints but itself: `fill` and `playedFill` both.
66
+ // Nothing declares a component's paints but itself.
89
67
  load: (loader, state) => {
90
68
  loader.fill(state.fill)
91
69
  loader.fill(state.playedFill)
92
70
  },
93
71
  commands: {
94
- /**
95
- * Fill the waveform in, left to right — the node's one command, since
96
- * everything else about it is expressible as an ordinary `to`. Eased in-out
97
- * unless it is given an easing of its own.
98
- */
72
+ /** Fill the waveform in, left to right; eased in-out unless given an easing. */
99
73
  sweep: (timeline, state) => {
100
74
  const ease = timeline.easing ? null : easeInOut()
101
75
  const from = state.progress
@@ -109,7 +83,6 @@ export const FullWaveform = component2d({
109
83
  })
110
84
 
111
85
  export type FullWaveform = NodeOf<typeof FullWaveform>
112
- export type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>
113
86
 
114
87
  interface FullState {
115
88
  readonly envelope: AudioEnvelope
@@ -143,9 +116,7 @@ function drawBars(ctx: RenderContext, s: FullState): void {
143
116
  })
144
117
  if (boxes.length === 0) return
145
118
 
146
- // One draw per colour rather than per bar — see {@link paintBars}. The key
147
- // is a string because it has to be comparable; the paint it stands for is
148
- // resolved after the grouping.
119
+ // One draw per colour, not per bar (see {@link paintBars}); the key is a string so it is comparable.
149
120
  const edge = played(ctx, s) * boxes.length
150
121
  const groups = groupByPaint(boxes, (i) =>
151
122
  s.byVoice
@@ -161,28 +132,15 @@ function drawBars(ctx: RenderContext, s: FullState): void {
161
132
  }
162
133
  }
163
134
 
164
- /** Where the fill has reached, from whichever source is in charge. */
165
135
  function played(ctx: RenderContext, s: FullState): number {
166
- if (!s.follow) return clamp01(s.progress)
136
+ if (!s.follow) return clamp(s.progress, 0, 1)
167
137
  const length = s.envelope.duration
168
138
  if (length <= 0) return 0
169
- return clamp01(ctx.time.elapsed / length)
139
+ return clamp(ctx.time.elapsed / length, 0, 1)
170
140
  }
171
141
 
172
- /**
173
- * The paint a group takes.
174
- *
175
- * A voice's colour is a **colour string**, never one of the node's two fill
176
- * props, and that is deliberate: a voice is identified by hue, so a gradient
177
- * or an image there would defeat the only thing the colour is for. It also
178
- * means these paints declare no assets, which is why `load` only has to account
179
- * for the two real fill props.
180
- */
181
142
  function paintFor(s: FullState, key: string): Fill {
182
143
  if (key === "played") return s.playedFill
183
144
  if (key === "ahead") return s.fill
184
-
185
- const voice = Number(key.slice("voice:".length))
186
- if (!Number.isFinite(voice) || voice < 0) return s.fill
187
- return s.voiceColors[voice] ?? voiceColor(voice)
145
+ return voiceFill(key, s.voiceColors, s.fill)
188
146
  }
@@ -1,21 +1,5 @@
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
1
  export { FullWaveform } from "./full-waveform"
16
- export type { FullWaveformProps } from "./full-waveform"
17
2
  export { LiveWaveform } from "./live-waveform"
18
- export type { LiveWaveformProps } from "./live-waveform"
19
3
  export { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from "./envelope"
20
4
  export type { AudioEnvelope, EnvelopeBar } from "./envelope"
21
5
  export {