@motionscript/audio 0.1.0 → 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 +19 -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,5 +1,6 @@
1
1
  import {
2
2
  Types,
3
+ clamp,
3
4
  component2d,
4
5
  fillOps,
5
6
  type Fill,
@@ -19,83 +20,55 @@ import {
19
20
  type EnvelopeBar,
20
21
  } from "./envelope"
21
22
  import {
22
- clamp01,
23
23
  groupByPaint,
24
24
  layOutBars,
25
25
  livePaintKey,
26
26
  paintBars,
27
- voiceColor,
27
+ voiceFill,
28
28
  type WaveformAlignment,
29
29
  type WaveformStyle,
30
30
  } from "./shared"
31
31
 
32
32
  /**
33
- * A **live audio visualiser**: bars that move with the recording as it plays.
33
+ * A visualiser whose bars move with the recording: a picture of this moment
34
+ * where {@link FullWaveform} is of the file. The bars are sampled from a baked
35
+ * envelope at the node's own elapsed time, so a frame is a pure function of time
36
+ * and an export, which has no playback to analyse and may render out of order,
37
+ * draws what the preview drew.
34
38
  *
35
- * The thing every podcast-to-video tool puts beside the artwork. It is not a
36
- * picture of the file — that is {@link FullWaveform} — it is a picture of *this
37
- * moment*, and what it is for is making a static frame of a talking head feel
38
- * like it is playing.
39
- *
40
- * ## "Live" without listening
41
- *
42
- * Nothing here reads an audio device. The bars are sampled out of a baked
43
- * envelope at the node's own elapsed time, which is what makes the visualiser
44
- * survive the thing it exists for: an **export**, where there is no playback to
45
- * analyse and frames may be rendered out of order or in parallel. A frame is a
46
- * pure function of its time, so scrubbing back and forth over the visualiser
47
- * redraws exactly what it drew the first time — which an analyser node, by
48
- * construction, cannot promise. See {@link AudioEnvelope}.
49
- *
50
- * ## The speaker filter
51
- *
52
- * `speaker` narrows the node to one voice of a diarized recording, and it
53
- * is the reason this node knows about transcripts at all. Two visualisers beside
54
- * two headshots, each filtered to its own host, is a podcast edit that would
55
- * otherwise be done by hand for every turn — and because the filter *silences*
56
- * rather than skips (see {@link forSpeaker}), the quiet host visibly stops
57
- * talking instead of vanishing. Nothing about that requires the words: only who
58
- * is talking when, which is what the diarizer produces.
59
- *
60
- * A filter naming a voice the recording doesn't have draws a resting row rather
61
- * than everything — a picture of "this person never speaks here", which is
62
- * true, where falling back to the unfiltered mix would be a silent lie about
63
- * whose voice you were watching.
39
+ * `speaker` narrows the node to one voice of a diarized recording; it silences
40
+ * the other voices (see {@link forSpeaker}) so the quiet host visibly stops. A
41
+ * voice the recording does not have draws a resting row, not the unfiltered mix,
42
+ * which would be a silent lie about whose voice you are watching.
64
43
  */
65
44
  export const LiveWaveform = component2d({
66
45
  key: "liveWaveform",
67
46
  state: {
68
- /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
47
+ /** The baked recording; see {@link AudioEnvelope}. */
69
48
  envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),
70
- /** How many bars the visualiser is made of. */
71
49
  bars: 48,
72
- /** Space between bars, as a fraction of the pitch. */
50
+ /** Space between bars as a fraction of the pitch. */
73
51
  gap: 0.35,
74
52
  radius: 4,
75
- /** How many seconds of audio are on screen at once. */
53
+ /** Seconds of audio on screen at once. */
76
54
  window: 3,
77
- /** Seconds into the file the node's first frame shows. */
55
+ /** Seconds into the file the first frame shows. */
78
56
  offset: 0,
79
- /** Advance with the scene clock. Off freezes the picture at `offset`. */
57
+ /** Advance with the scene clock; off freezes the picture at `offset`. */
80
58
  playing: true,
81
- /** Multiplies every magnitude before it is clamped. */
82
59
  gain: 1.4,
83
- /** How much of the box a silent bar still occupies, `0`–`1`. */
60
+ /** The share of the box a silent bar still occupies, `0`-`1`. */
84
61
  floor: 0.04,
85
62
  align: Types.custom<WaveformAlignment>("center"),
86
63
  style: Types.custom<WaveformStyle>("bars"),
87
- /**
88
- * Show only this voice's activity, by index into the recording's own
89
- * numbering. `-1` — the default — is every voice.
90
- */
64
+ /** Show only this voice, by index into the recording's own numbering; `-1` is every voice. */
91
65
  speaker: -1,
92
66
  /** Colour each bar by whoever is speaking in it. */
93
67
  byVoice: false,
94
- /** A colour per voice index. Short or empty falls back to the palette. */
68
+ /** A colour per voice index; short or empty falls back to the palette. */
95
69
  voiceColors: Types.custom<string[]>([]),
96
70
  fill: Types.custom<Fill, FillResolved[]>("#6366f1", { resolve: fillOps.resolve, lerp: fillOps.lerp }),
97
71
  },
98
- // Nothing declares a component's paints but itself.
99
72
  load: (loader, state) => loader.fill(state.fill),
100
73
  render: (ctx, state) => {
101
74
  drawBars(ctx, state)
@@ -104,7 +77,6 @@ export const LiveWaveform = component2d({
104
77
  })
105
78
 
106
79
  export type LiveWaveform = NodeOf<typeof LiveWaveform>
107
- export type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>
108
80
 
109
81
  interface LiveState {
110
82
  readonly envelope: AudioEnvelope
@@ -124,15 +96,7 @@ interface LiveState {
124
96
  readonly fill: FillResolved[]
125
97
  }
126
98
 
127
- /**
128
- * The second of the recording the visualiser is showing.
129
- *
130
- * Off the node's own clock rather than the scene's, which is what "from the
131
- * moment this node appeared" means — the same clock the engine's own
132
- * `Video` times its picture from, and the same one that makes a node dropped
133
- * halfway through a clip start its recording at its entrance rather than
134
- * partway in.
135
- */
99
+ /** The second of the recording on show, off the node's own clock (as `Video`'s is), so a node entering halfway through a clip starts its recording at its entrance. */
136
100
  function at(ctx: RenderContext, s: LiveState): number {
137
101
  return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0)
138
102
  }
@@ -146,9 +110,7 @@ function drawBars(ctx: RenderContext, s: LiveState): void {
146
110
  const now = at(ctx, s)
147
111
  const voice = s.speaker < 0 ? null : Math.round(s.speaker)
148
112
 
149
- // The window is centred on the playhead, so the bars run *toward* the
150
- // middle and away again — the arrangement every mixer's meter uses, and the
151
- // one that reads as sound arriving rather than as a strip scrolling past.
113
+ // Centred on the playhead, so sound reads as arriving rather than a strip scrolling past.
152
114
  const sampled = forSpeaker(
153
115
  sampleEnvelope(s.envelope, now - span / 2, now + span / 2, count),
154
116
  voice
@@ -175,24 +137,12 @@ function drawBars(ctx: RenderContext, s: LiveState): void {
175
137
  }
176
138
  }
177
139
 
178
- /** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */
179
140
  function paintFor(s: LiveState, key: string): Fill {
180
141
  if (key === "all") return s.fill
181
- const voice = Number(key.slice("voice:".length))
182
- if (!Number.isFinite(voice) || voice < 0) return s.fill
183
- return s.voiceColors[voice] ?? voiceColor(voice)
142
+ return voiceFill(key, s.voiceColors, s.fill)
184
143
  }
185
144
 
186
- /**
187
- * Rolls the ends of a window off toward silence.
188
- *
189
- * What separates the `line` style from `bars`: a hard-edged window makes the
190
- * loud bar entering at the right edge appear from nothing, which reads as a
191
- * glitch rather than as sound arriving. Fading the outer fifth turns the same
192
- * data into a shape that grows out of the baseline — the profile a spectrum
193
- * display has, and the reason it looks like one object rather than a row of
194
- * separate meters.
195
- */
145
+ /** Fades the outer fifth of the window to silence, the `line` style: a hard edge makes a loud bar appear from nothing, which reads as a glitch. */
196
146
  function taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {
197
147
  const count = bars.length
198
148
  if (count < 3) return [...bars]
@@ -200,7 +150,7 @@ function taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {
200
150
 
201
151
  return bars.map((bar, i) => {
202
152
  const fromEnd = Math.min(i, count - 1 - i)
203
- const weight = clamp01(fromEnd / edge)
153
+ const weight = clamp(fromEnd / edge, 0, 1)
204
154
  return { magnitude: bar.magnitude * weight, speaker: bar.speaker }
205
155
  })
206
156
  }
@@ -1,33 +1,15 @@
1
- import { Graphics2D, type Fill, type FillResolved } from "@motionscript/sdk"
1
+ import { Graphics2D, clamp, type Fill, type FillResolved } from "@motionscript/sdk"
2
2
 
3
3
  import type { EnvelopeBar } from "./envelope"
4
4
 
5
- /**
6
- * Geometry and paint shared by the two waveform nodes.
7
- *
8
- * They draw the same object — a row of bars whose heights are loudness — and
9
- * differ only in what decides the heights: the whole file for one, a window
10
- * around the playhead for the other. Everything about *how a row of bars is
11
- * laid out and painted* is therefore common, and lives here so the two cannot
12
- * drift into looking like different components.
13
- */
14
-
15
5
  /** How a bar is grown out of its baseline. */
16
6
  export const WAVEFORM_ALIGNMENTS = ["center", "bottom"] as const
17
7
  export type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number]
18
8
 
19
- /** What the bars are shaped like. */
20
9
  export const WAVEFORM_STYLES = ["bars", "line", "blocks"] as const
21
10
  export type WaveformStyle = (typeof WAVEFORM_STYLES)[number]
22
11
 
23
- /**
24
- * The colours voices are drawn in when nobody has assigned any.
25
- *
26
- * Eight hues that can be told apart at a glance, handed out by voice index. They
27
- * are literals rather than looked up, because a scene has to build in a
28
- * headless render with no database in scope; a host that names its voices
29
- * supplies its own colours instead.
30
- */
12
+ /** 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. */
31
13
  export const VOICE_PALETTE = [
32
14
  "#6366f1",
33
15
  "#14b8a6",
@@ -47,11 +29,7 @@ export function voiceColor(index: number): string {
47
29
  ]
48
30
  }
49
31
 
50
- /**
51
- * Where one bar sits and how tall it came out: its top-left corner and size, in
52
- * the node's own pixels with **y pointing down** from the middle of the box.
53
- * {@link paintBars} turns that into the centred, y-up rect the renderer draws.
54
- */
32
+ /** 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. */
55
33
  export interface BarBox {
56
34
  x: number
57
35
  y: number
@@ -60,17 +38,10 @@ export interface BarBox {
60
38
  }
61
39
 
62
40
  /**
63
- * Lays a row of bars out across a box.
64
- *
65
- * The gap is stated as a *fraction of the pitch* rather than in pixels, and
66
- * that is the one decision here worth stating out loud: the bar count is a
67
- * control, so a gap in pixels would silently become a solid block at 200 bars
68
- * and a picket fence at 20. As a fraction, the row keeps the same rhythm at
69
- * every density — which is what lets the count be a slider anyone can drag.
70
- *
71
- * A bar is never thinner than a hairline and never shorter than one either: a
72
- * silent passage has to read as "audio, and nothing here" rather than as a hole
73
- * in the drawing.
41
+ * Lays a row of bars across a box. The gap is a fraction of the pitch, not px:
42
+ * the bar count is a control, and a px gap would become a solid block at 200
43
+ * bars and a picket fence at 20. A bar is never thinner or shorter than a
44
+ * hairline, so a silent passage reads as audio with nothing in it, not a hole.
74
45
  */
75
46
  export function layOutBars(
76
47
  bars: readonly EnvelopeBar[],
@@ -79,7 +50,6 @@ export function layOutBars(
79
50
  height: number
80
51
  gap: number
81
52
  align: WaveformAlignment
82
- /** Multiplies every magnitude before it is clamped back into 0…1. */
83
53
  gain: number
84
54
  /** Fraction of the box a silent bar still occupies. */
85
55
  floor: number
@@ -90,20 +60,17 @@ export function layOutBars(
90
60
  if (count === 0 || width <= 0 || height <= 0) return []
91
61
 
92
62
  const pitch = width / count
93
- const barWidth = Math.max(1, pitch * (1 - clamp01(gap)))
94
- // The tallest a bar may be. Centre-aligned bars grow both ways from the
95
- // middle, so the *half* is what a magnitude of 1 fills.
63
+ const barWidth = Math.max(1, pitch * (1 - clamp(gap, 0, 1)))
64
+ // Centre-aligned bars grow both ways, so half the height is what a magnitude of 1 fills.
96
65
  const full = align === "center" ? height / 2 : height
97
66
 
98
67
  const out: BarBox[] = new Array(count)
99
68
  for (let i = 0; i < count; i++) {
100
- const magnitude = clamp01(bars[i].magnitude * gain)
101
- const reach = Math.max(full * clamp01(floor), full * magnitude)
69
+ const magnitude = clamp(bars[i].magnitude * gain, 0, 1)
70
+ const reach = Math.max(full * clamp(floor, 0, 1), full * magnitude)
102
71
  const x = -width / 2 + pitch * i + (pitch - barWidth) / 2
103
72
 
104
- // A hairline minimum, taken before the position so the bar stays inside the
105
- // box: a silent bar 1px tall that kept the position of a zero-height one
106
- // would hang half a pixel (centred) or a whole pixel (bottom) outside it.
73
+ // The hairline minimum is taken before the position, or a 1px bar placed as if zero-height would hang outside the box.
107
74
  const h = Math.max(1, align === "center" ? reach * 2 : reach)
108
75
  out[i] =
109
76
  align === "center"
@@ -114,14 +81,10 @@ export function layOutBars(
114
81
  }
115
82
 
116
83
  /**
117
- * Draws a run of bars in one paint.
118
- *
119
- * Batched by colour by the caller, and this is why: every `Graphics2D` handed to
120
- * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a
121
- * thousand surfaces a frame. Chaining the rects of one colour into a single
122
- * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill
123
- * maps across the whole run rather than restarting inside every bar, which is
124
- * the behaviour anyone reaching for a gradient here actually wants.
84
+ * Draws a run of bars in one paint. Callers batch by colour: every `Graphics2D`
85
+ * handed to `ctx.draw` is a surface the renderer composites, so a bar apiece
86
+ * would be a thousand surfaces a frame. One `Graphics2D` per run also maps a
87
+ * gradient across the whole run instead of restarting inside every bar.
125
88
  */
126
89
  export function paintBars(
127
90
  boxes: readonly BarBox[],
@@ -135,26 +98,18 @@ export function paintBars(
135
98
  for (const box of boxes) {
136
99
  graphics.rect({
137
100
  x: box.x + box.width / 2,
138
- // The box is stated y-down and the rect is drawn y-up. A centred bar is
139
- // symmetric about the middle, so only a bottom-aligned one shows the flip.
101
+ // The box is y-down and the rect y-up; only a bottom-aligned bar shows the flip.
140
102
  y: -(box.y + box.height / 2),
141
103
  width: box.width,
142
104
  height: box.height,
143
- // Never rounder than the bar is wide, or a thin bar becomes a lozenge
144
- // whose ends taper away to nothing.
105
+ // A thin bar rounder than it is wide becomes a lozenge.
145
106
  cornerRadius: Math.min(radius, box.width / 2, box.height / 2),
146
107
  })
147
108
  }
148
109
  return graphics.fill(fill)
149
110
  }
150
111
 
151
- /**
152
- * Groups bars by the paint they take, preserving order within each group.
153
- *
154
- * The seam that makes {@link paintBars} worth batching: a diarized waveform has
155
- * as many colours as it has voices, not as many as it has bars, so a
156
- * conversation of two people is two draws however long it runs.
157
- */
112
+ /** 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. */
158
113
  export function groupByPaint<K>(
159
114
  boxes: readonly BarBox[],
160
115
  keyOf: (index: number) => K
@@ -169,23 +124,23 @@ export function groupByPaint<K>(
169
124
  return groups
170
125
  }
171
126
 
172
- export function clamp01(value: number): number {
173
- return value < 0 ? 0 : value > 1 ? 1 : value
127
+ /**
128
+ * The colour of a `voice:N` group key, or `fallback` for a bad index. Always a
129
+ * colour string, never one of the node's fills: a voice is identified by hue, so
130
+ * a gradient or image would defeat the point, and it keeps these paints from
131
+ * declaring assets.
132
+ */
133
+ export function voiceFill(key: string, voiceColors: readonly string[], fallback: Fill): Fill {
134
+ const voice = Number(key.slice("voice:".length))
135
+ if (!Number.isFinite(voice) || voice < 0) return fallback
136
+ return voiceColors[voice] ?? voiceColor(voice)
174
137
  }
175
138
 
176
139
  /**
177
- * The {@link groupByPaint} key one bar of a live visualiser takes.
178
- *
179
- * Filtered to one voice, every bar is that voice regardless of `byVoice` or
180
- * whose turn the bucket originally belonged to. That second part is the one
181
- * worth spelling out: {@link forSpeaker} silences a bar by zeroing its
182
- * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the
183
- * middle of a *different* voice's turn is still stamped with that voice's
184
- * index — grouping by `byVoice`'s usual per-bar key would paint those silent
185
- * stretches in whoever used to be talking there, in flickers too short to
186
- * read as anything but noise. Forcing every bar under the filtered voice's own
187
- * key is the true picture instead: the node is drawing exactly one person, on
188
- * or off, never anyone else's colour.
140
+ * The {@link groupByPaint} key of one live bar. Filtered to a voice, every bar
141
+ * takes that voice's key: {@link forSpeaker} zeroes a bar's magnitude but leaves
142
+ * its `speaker`, so the usual per-bar key would paint the silenced stretches in
143
+ * whoever used to talk there, in flickers too short to read as anything but noise.
189
144
  */
190
145
  export function livePaintKey(
191
146
  barSpeaker: number,
@@ -1,2 +0,0 @@
1
- var E={magnitude:new Float32Array(0),duration:0,speaker:null,speakers:0};function y(t,r,e,o){let n=Math.max(0,Math.floor(o)),a=new Array(n);for(let i=0;i<n;i++)a[i]={magnitude:0,speaker:-1};let l=t.magnitude.length;if(n===0||l===0||t.duration<=0)return a;let m=l/t.duration,s=(e-r)/n;for(let i=0;i<n;i++){let f=(r+s*i)*m,x=(r+s*(i+1))*m,p=Math.floor(f),u=Math.max(p,Math.ceil(x)-1),g=0,d=-1;for(let c=p;c<=u;c++){if(c<0||c>=l)continue;let h=t.magnitude[c];h<g||(g=h,d=t.speaker?t.speaker[c]:-1)}a[i]={magnitude:g,speaker:d}}return a}function A(t,r){return r===null?t:t.map(e=>e.speaker===r?e:{magnitude:0,speaker:e.speaker})}import{Graphics2D as w}from"@motionscript/sdk";var S=["center","bottom"],T=["bars","line","blocks"],M=["#6366f1","#14b8a6","#f59e0b","#ec4899","#22c55e","#3b82f6","#f97316","#a855f7"];function L(t){return M[(t%M.length+M.length)%M.length]}function _(t,r){let{width:e,height:o,gap:n,align:a,gain:l,floor:m}=r,s=t.length;if(s===0||e<=0||o<=0)return[];let i=e/s,f=Math.max(1,i*(1-k(n))),x=a==="center"?o/2:o,p=new Array(s);for(let u=0;u<s;u++){let g=k(t[u].magnitude*l),d=Math.max(x*k(m),x*g),c=-e/2+i*u+(i-f)/2,h=Math.max(1,a==="center"?d*2:d);p[u]=a==="center"?{x:c,y:-h/2,width:f,height:h}:{x:c,y:o/2-h,width:f,height:h}}return p}function b(t,r,e){if(t.length===0||r.length===0)return null;let o=new w;for(let n of t)o.rect({x:n.x+n.width/2,y:-(n.y+n.height/2),width:n.width,height:n.height,cornerRadius:Math.min(e,n.width/2,n.height/2)});return o.fill(r)}function B(t,r){let e=new Map;for(let o=0;o<t.length;o++){let n=r(o),a=e.get(n);a?a.push(t[o]):e.set(n,[t[o]])}return e}function k(t){return t<0?0:t>1?1:t}function F(t,r,e){return e!==null?`voice:${e}`:r?`voice:${t}`:"all"}export{E as a,y as b,A as c,S as d,T as e,M as f,L as g,_ as h,b as i,B as j,k,F as l};
2
- //# sourceMappingURL=chunk-DD3KHRUL.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/waveform/envelope.ts", "../../../src/waveform/shared.ts"],
4
- "sourcesContent": ["/**\n * The shape of a recording, as the waveform nodes draw it.\n *\n * ## How an envelope reaches a node: the host supplies it\n *\n * Nothing in the engine, and nothing in this kit, decodes audio. A waveform node\n * does not read a `sound` node's `src`, declares no audio asset and is not tied\n * to a clip: it draws the {@link AudioEnvelope} it is handed in its `envelope`\n * prop. Decoding a file into an envelope is the host's job, done once per file\n * before the document is built. Where the recording sits on the audio lane is\n * the author's timing too, which is why `FullWaveform.follow` is opt-in.\n *\n * ### The JSON form\n *\n * A document holds the envelope as plain JSON, because a prop value in a\n * document is JSON. The typed arrays in {@link AudioEnvelope} are one spelling of\n * it; `ArrayLike<number>` also accepts the plain arrays a document stores:\n *\n * ```json\n * {\n * \"magnitude\": [0, 0.25, 0.5, 1, 0.5, 0],\n * \"duration\": 6,\n * \"speaker\": [0, 0, 0, 1, 1, -1],\n * \"speakers\": 2\n * }\n * ```\n *\n * `magnitude` is the peak loudness, `0`…`1`, of buckets that divide `duration`\n * seconds evenly. `speaker` is `null` when nobody was identified; otherwise it is parallel to `magnitude`, one voice\n * index per bucket, `-1` for \"nobody\". `speakers` is the voice count, `0` when\n * `speaker` is `null`. A `Float32Array` or `Int16Array` is not JSON-serialisable\n * (`JSON.stringify` writes an object of indices), so a host that holds the typed\n * form converts with `Array.from` before storing it in a document.\n *\n * ## Why the data is baked rather than listened to\n *\n * A scene has to render identically in the preview and in a headless export,\n * and an export has no audio device, no `AudioContext` and no playback to\n * analyse. So a \"live\" visualiser cannot read a Web Audio analyser: what it\n * reads is a *pre-computed envelope*, indexed by the scene clock. The picture is\n * then a pure function of time, which is what makes scrubbing backwards, jumping\n * to frame 900, and rendering out of order all produce the same frame.\n *\n * The decode happens in the host, once per file, and arrives here through the\n * build — the same arrangement a chart's rows and a protein's atoms already use,\n * and for the same reason: the build is synchronous and cannot await a fetch.\n *\n * ## Why magnitude rather than min/max\n *\n * An editor's waveform is drawn from per-bucket minima *and* maxima, because an\n * editor is a measuring instrument. These nodes are neither — they are a bar\n * chart of loudness that happens to be shaped like a recording — and audio is\n * near enough symmetric that a mirrored magnitude is indistinguishable at the\n * size a node draws. Half the numbers, and one array to reason about.\n */\n\n/**\n * A decoded recording, reduced to a uniform grid of loudness — plus, when the\n * host knows it, who is talking in each bucket.\n *\n * The speaker track is **not** something a decoder can produce; it comes from a\n * diarized transcript and is folded in by the host before the build. It lives\n * here rather than in a parallel structure because every reader of it is also a\n * reader of the magnitudes at the same index, and two arrays that must stay the\n * same length are better as one object than as two arguments.\n */\nexport interface AudioEnvelope {\n /**\n * Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. A\n * `Float32Array`, or the plain `number[]` a document stores.\n */\n magnitude: ArrayLike<number>\n /** The decoded length in seconds. */\n duration: number\n /**\n * Which voice is speaking in each bucket, as an index into the recording's\n * own numbering, or `-1` for \"nobody identified\". Parallel to\n * {@link magnitude}, or `null` when the track was never diarized.\n */\n speaker: ArrayLike<number> | null\n /** How many distinct voices the diarizer found. Zero when it did not run. */\n speakers: number\n}\n\n/** The envelope of a track that hasn't loaded — or isn't there. */\nexport const EMPTY_ENVELOPE: AudioEnvelope = {\n magnitude: new Float32Array(0),\n duration: 0,\n speaker: null,\n speakers: 0,\n}\n\n/** One drawn bar: how loud, and whose. */\nexport interface EnvelopeBar {\n /** 0…1. */\n magnitude: number\n /** The voice index, or -1. */\n speaker: number\n}\n\n/**\n * Resamples `[from, to)` seconds of an envelope down to `bars` columns.\n *\n * Peak-preserving rather than averaging, which is the whole difference between\n * a waveform and a blur: averaging a hundred buckets into one column flattens\n * every transient, and transients are what makes a drawn recording read as\n * speech rather than as noise. The speaker of a column is the speaker of its\n * *loudest* bucket, on the same reasoning — a column straddling a turn belongs\n * to whoever is actually audible in it.\n *\n * A window past either end of the recording yields silent, unattributed\n * columns rather than clamping onto the nearest real one. That matters for the\n * live visualiser, which is handed a window centred on the playhead and would\n * otherwise smear the first bucket across the whole run-up to a track's start.\n */\nexport function sampleEnvelope(\n envelope: AudioEnvelope,\n from: number,\n to: number,\n bars: number\n): EnvelopeBar[] {\n const count = Math.max(0, Math.floor(bars))\n const out: EnvelopeBar[] = new Array(count)\n for (let i = 0; i < count; i++) out[i] = { magnitude: 0, speaker: -1 }\n\n const buckets = envelope.magnitude.length\n if (count === 0 || buckets === 0 || envelope.duration <= 0) return out\n\n const perSecond = buckets / envelope.duration\n const span = (to - from) / count\n\n for (let i = 0; i < count; i++) {\n const start = (from + span * i) * perSecond\n const end = (from + span * (i + 1)) * perSecond\n\n // At least one bucket per column, so a window zoomed in past the grid draws\n // the sample it is standing on rather than an empty band.\n const first = Math.floor(start)\n const last = Math.max(first, Math.ceil(end) - 1)\n\n let loudest = 0\n let speaker = -1\n for (let b = first; b <= last; b++) {\n if (b < 0 || b >= buckets) continue\n const value = envelope.magnitude[b]\n if (value < loudest) continue\n loudest = value\n speaker = envelope.speaker ? envelope.speaker[b] : -1\n }\n\n out[i] = { magnitude: loudest, speaker }\n }\n\n return out\n}\n\n/**\n * The same window, with everything that isn't `voice` silenced.\n *\n * The point of the speaker filter, and the reason it silences rather than\n * *drops*: a podcast visualiser filtered to one host should go quiet while the\n * other one talks, not compress their turn out of existence. Keeping the bars\n * and zeroing them is what makes the picture read as \"this person is not\n * speaking right now\" instead of as a jump cut.\n *\n * `voice` of `null` returns the bars untouched, which is the unfiltered node.\n */\nexport function forSpeaker(\n bars: EnvelopeBar[],\n voice: number | null\n): EnvelopeBar[] {\n if (voice === null) return bars\n return bars.map((bar) =>\n bar.speaker === voice ? bar : { magnitude: 0, speaker: bar.speaker }\n )\n}\n", "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"],
5
- "mappings": "AAqFO,IAAMA,EAAgC,CAC3C,UAAW,IAAI,aAAa,CAAC,EAC7B,SAAU,EACV,QAAS,KACT,SAAU,GA0BN,SAAUC,EACdC,EACAC,EACAC,EACAC,EAAY,CAEZ,IAAMC,EAAQ,KAAK,IAAI,EAAG,KAAK,MAAMD,CAAI,CAAC,EACpCE,EAAqB,IAAI,MAAMD,CAAK,EAC1C,QAAS,EAAI,EAAG,EAAIA,EAAO,IAAKC,EAAI,CAAC,EAAI,CAA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,KAAKF,CAAI,CAC3B,CASM,SAAUI,EACdL,EACAM,EAA2B,CAE3B,IAAMC,EAAS,IAAI,IACnB,QAASZ,EAAI,EAAGA,EAAIK,EAAM,OAAQL,IAAK,CACrC,IAAMa,EAAMF,EAAMX,CAAC,EACbc,EAAMF,EAAO,IAAIC,CAAG,EACtBC,EAAKA,EAAI,KAAKT,EAAML,CAAC,CAAC,EACrBY,EAAO,IAAIC,EAAK,CAACR,EAAML,CAAC,CAAC,CAAC,CACjC,CACA,OAAOY,CACT,CAEM,SAAUf,EAAQkB,EAAa,CACnC,OAAOA,EAAQ,EAAI,EAAIA,EAAQ,EAAI,EAAIA,CACzC,CAgBM,SAAUC,EACdC,EACAC,EACAC,EAA4B,CAE5B,OAAIA,IAAkB,KAAa,SAASA,CAAa,GAClDD,EAAU,SAASD,CAAU,GAAK,KAC3C",
6
- "names": ["EMPTY_ENVELOPE", "sampleEnvelope", "envelope", "from", "to", "bars", "count", "out", "buckets", "perSecond", "span", "start", "end", "first", "last", "loudest", "speaker", "b", "value", "forSpeaker", "voice", "bar", "Graphics2D", "WAVEFORM_ALIGNMENTS", "WAVEFORM_STYLES", "VOICE_PALETTE", "voiceColor", "index", "layOutBars", "bars", "options", "width", "height", "gap", "align", "gain", "floor", "count", "pitch", "barWidth", "clamp01", "full", "out", "i", "magnitude", "reach", "x", "paintBars", "boxes", "fill", "radius", "graphics", "box", "groupByPaint", "keyOf", "groups", "key", "run", "value", "livePaintKey", "barSpeaker", "byVoice", "filteredVoice"]
7
- }