@motionscript/audio 0.1.0-alpha.0 → 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 (37) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/NOTICE +49 -0
  3. package/README.md +92 -0
  4. package/dist/browser/chunks/chunk-DD3KHRUL.js +2 -0
  5. package/dist/browser/chunks/chunk-DD3KHRUL.js.map +7 -0
  6. package/dist/browser/index.js +1 -1
  7. package/dist/browser/index.js.map +3 -3
  8. package/dist/browser/kit.js +1 -1
  9. package/dist/browser/manifest.json +4 -3
  10. package/dist/nodes.d.ts +6 -9
  11. package/dist/nodes.d.ts.map +1 -1
  12. package/dist/nodes.js +7 -9
  13. package/dist/nodes.js.map +1 -1
  14. package/dist/waveform/envelope.d.ts +42 -9
  15. package/dist/waveform/envelope.d.ts.map +1 -1
  16. package/dist/waveform/envelope.js +34 -4
  17. package/dist/waveform/envelope.js.map +1 -1
  18. package/dist/waveform/full-waveform.d.ts +89 -75
  19. package/dist/waveform/full-waveform.d.ts.map +1 -1
  20. package/dist/waveform/full-waveform.js +115 -162
  21. package/dist/waveform/full-waveform.js.map +1 -1
  22. package/dist/waveform/live-waveform.d.ts +93 -63
  23. package/dist/waveform/live-waveform.d.ts.map +1 -1
  24. package/dist/waveform/live-waveform.js +92 -134
  25. package/dist/waveform/live-waveform.js.map +1 -1
  26. package/dist/waveform/shared.d.ts +11 -9
  27. package/dist/waveform/shared.d.ts.map +1 -1
  28. package/dist/waveform/shared.js +15 -16
  29. package/dist/waveform/shared.js.map +1 -1
  30. package/package.json +8 -5
  31. package/src/nodes.ts +8 -10
  32. package/src/waveform/envelope.ts +42 -9
  33. package/src/waveform/full-waveform.ts +130 -160
  34. package/src/waveform/live-waveform.ts +114 -129
  35. package/src/waveform/shared.ts +20 -17
  36. package/dist/browser/chunks/chunk-U5OUQJHQ.js +0 -2
  37. package/dist/browser/chunks/chunk-U5OUQJHQ.js.map +0 -7
@@ -1,15 +1,16 @@
1
1
  import {
2
- Node2D,
3
- node,
2
+ Types,
3
+ component2d,
4
4
  fillOps,
5
- property,
6
- type AssetScope,
7
5
  type Fill,
8
- type Node2DProps,
9
- type RenderContext2D,
10
- } from "@motionscript/core"
6
+ type FillResolved,
7
+ type NodeOf,
8
+ type RenderContext,
9
+ // Named so the emitted declaration reaches these through the sdk, not core.
10
+ type Node2D as _Node2D,
11
+ type Node2DProps as _Node2DProps,
12
+ } from "@motionscript/sdk"
11
13
 
12
- import { declarePaints } from "@motionscript/core/component"
13
14
  import {
14
15
  EMPTY_ENVELOPE,
15
16
  forSpeaker,
@@ -28,38 +29,6 @@ import {
28
29
  type WaveformStyle,
29
30
  } from "./shared"
30
31
 
31
- export interface LiveWaveformProps extends Node2DProps {
32
- /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
33
- envelope: AudioEnvelope
34
- /** How many bars the visualiser is made of. */
35
- bars: number
36
- /** Space between bars, as a fraction of the pitch. */
37
- gap: number
38
- radius: number
39
- /** How many seconds of audio are on screen at once. */
40
- window: number
41
- /** Seconds into the file the node's first frame shows. */
42
- offset: number
43
- /** Advance with the scene clock. Off freezes the picture at {@link offset}. */
44
- playing: boolean
45
- /** Multiplies every magnitude before it is clamped. */
46
- gain: number
47
- /** How much of the box a silent bar still occupies, `0`–`1`. */
48
- floor: number
49
- align: WaveformAlignment
50
- style: WaveformStyle
51
- /**
52
- * Show only this voice's activity, by index into the recording's own
53
- * numbering. `-1` — the default — is every voice.
54
- */
55
- speaker: number
56
- /** Colour each bar by whoever is speaking in it. */
57
- byVoice: boolean
58
- /** A colour per voice index. Short or empty falls back to the palette. */
59
- voiceColors: string[]
60
- fill: Fill
61
- }
62
-
63
32
  /**
64
33
  * A **live audio visualiser**: bars that move with the recording as it plays.
65
34
  *
@@ -80,7 +49,7 @@ export interface LiveWaveformProps extends Node2DProps {
80
49
  *
81
50
  * ## The speaker filter
82
51
  *
83
- * {@link speaker} narrows the node to one voice of a diarized recording, and it
52
+ * `speaker` narrows the node to one voice of a diarized recording, and it
84
53
  * is the reason this node knows about transcripts at all. Two visualisers beside
85
54
  * two headshots, each filtered to its own host, is a podcast edit that would
86
55
  * otherwise be done by hand for every turn — and because the filter *silences*
@@ -93,109 +62,125 @@ export interface LiveWaveformProps extends Node2DProps {
93
62
  * true, where falling back to the unfiltered mix would be a silent lie about
94
63
  * whose voice you were watching.
95
64
  */
96
- @node({
65
+ export const LiveWaveform = component2d({
97
66
  key: "liveWaveform",
98
- parentKey: "node",
99
- forkable: true,
100
- layout: {
101
- children: "freeform",
102
- defaultWidthMode: "fixed",
103
- defaultHeightMode: "fixed",
104
- acceptsChildren: true,
67
+ state: {
68
+ /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
69
+ envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),
70
+ /** How many bars the visualiser is made of. */
71
+ bars: 48,
72
+ /** Space between bars, as a fraction of the pitch. */
73
+ gap: 0.35,
74
+ radius: 4,
75
+ /** How many seconds of audio are on screen at once. */
76
+ window: 3,
77
+ /** Seconds into the file the node's first frame shows. */
78
+ offset: 0,
79
+ /** Advance with the scene clock. Off freezes the picture at `offset`. */
80
+ playing: true,
81
+ /** Multiplies every magnitude before it is clamped. */
82
+ gain: 1.4,
83
+ /** How much of the box a silent bar still occupies, `0`–`1`. */
84
+ floor: 0.04,
85
+ align: Types.custom<WaveformAlignment>("center"),
86
+ 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
+ */
91
+ speaker: -1,
92
+ /** Colour each bar by whoever is speaking in it. */
93
+ byVoice: false,
94
+ /** A colour per voice index. Short or empty falls back to the palette. */
95
+ voiceColors: Types.custom<string[]>([]),
96
+ fill: Types.custom<Fill, FillResolved[]>("#6366f1", { resolve: fillOps.resolve, lerp: fillOps.lerp }),
105
97
  },
106
- seed: {
107
- width: 420,
108
- height: 220,
98
+ // Nothing declares a component's paints but itself.
99
+ load: (loader, state) => loader.fill(state.fill),
100
+ render: (ctx, state) => {
101
+ drawBars(ctx, state)
102
+ ctx.children()
109
103
  },
110
104
  })
111
- export class LiveWaveform extends Node2D<LiveWaveformProps> {
112
- @property({ default: EMPTY_ENVELOPE }) declare envelope: AudioEnvelope
113
- @property({ default: 48 }) declare bars: number
114
- @property({ default: 0.35 }) declare gap: number
115
- @property({ default: 4 }) declare radius: number
116
- @property({ default: 3 }) declare window: number
117
- @property({ default: 0 }) declare offset: number
118
- @property({ default: true }) declare playing: boolean
119
- @property({ default: 1.4 }) declare gain: number
120
- @property({ default: 0.04 }) declare floor: number
121
- @property({ default: "center" }) declare align: WaveformAlignment
122
- @property({ default: "bars" }) declare style: WaveformStyle
123
- @property({ default: -1 }) declare speaker: number
124
- @property({ default: false }) declare byVoice: boolean
125
- @property({ default: [] }) declare voiceColors: string[]
126
105
 
127
- @property({
128
- default: "#6366f1",
129
- mapper: fillOps.resolve,
130
- tween: fillOps.lerp,
131
- })
132
- declare fill: Fill
106
+ export type LiveWaveform = NodeOf<typeof LiveWaveform>
107
+ export type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>
133
108
 
134
- override declareAssets(assets: AssetScope): void {
135
- super.declareAssets(assets)
136
- declarePaints(this, assets)
137
- }
109
+ interface LiveState {
110
+ readonly envelope: AudioEnvelope
111
+ readonly bars: number
112
+ readonly gap: number
113
+ readonly radius: number
114
+ readonly window: number
115
+ readonly offset: number
116
+ readonly playing: boolean
117
+ readonly gain: number
118
+ readonly floor: number
119
+ readonly align: WaveformAlignment
120
+ readonly style: WaveformStyle
121
+ readonly speaker: number
122
+ readonly byVoice: boolean
123
+ readonly voiceColors: string[]
124
+ readonly fill: FillResolved[]
125
+ }
138
126
 
139
- /**
140
- * The second of the recording the visualiser is showing.
141
- *
142
- * Off the node's own clock rather than the scene's, which is what "from the
143
- * moment this node appeared" means — the same clock motion-script's own
144
- * `Video` times its picture from, and the same one that makes a node dropped
145
- * halfway through a scene start its recording at its entrance rather than
146
- * partway in.
147
- */
148
- private get at(): number {
149
- return this.offset + (this.playing ? Math.max(0, this.time.elapsed) : 0)
150
- }
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
+ */
136
+ function at(ctx: RenderContext, s: LiveState): number {
137
+ return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0)
138
+ }
151
139
 
152
- protected renderSelf(ctx: RenderContext2D): void {
153
- const rect = this.layoutBounds
154
- const width = rect?.width ?? 0
155
- const height = rect?.height ?? 0
156
- if (width <= 0 || height <= 0) return
140
+ function drawBars(ctx: RenderContext, s: LiveState): void {
141
+ const { width, height } = ctx.size
142
+ if (width <= 0 || height <= 0) return
157
143
 
158
- const count = Math.max(1, Math.round(this.bars))
159
- const span = Math.max(0.05, this.window)
160
- const at = this.at
161
- const voice = this.speaker < 0 ? null : Math.round(this.speaker)
144
+ const count = Math.max(1, Math.round(s.bars))
145
+ const span = Math.max(0.05, s.window)
146
+ const now = at(ctx, s)
147
+ const voice = s.speaker < 0 ? null : Math.round(s.speaker)
162
148
 
163
- // The window is centred on the playhead, so the bars run *toward* the
164
- // middle and away again — the arrangement every mixer's meter uses, and the
165
- // one that reads as sound arriving rather than as a strip scrolling past.
166
- const sampled = forSpeaker(
167
- sampleEnvelope(this.envelope, at - span / 2, at + span / 2, count),
168
- voice
169
- )
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.
152
+ const sampled = forSpeaker(
153
+ sampleEnvelope(s.envelope, now - span / 2, now + span / 2, count),
154
+ voice
155
+ )
170
156
 
171
- const shaped = this.style === "line" ? taper(sampled) : sampled
172
- const boxes = layOutBars(shaped, {
173
- width,
174
- height,
175
- gap: this.style === "blocks" ? 0.12 : this.gap,
176
- align: this.align,
177
- gain: this.gain,
178
- floor: this.floor,
179
- })
180
- if (boxes.length === 0) return
157
+ const shaped = s.style === "line" ? taper(sampled) : sampled
158
+ const boxes = layOutBars(shaped, {
159
+ width,
160
+ height,
161
+ gap: s.style === "blocks" ? 0.12 : s.gap,
162
+ align: s.align,
163
+ gain: s.gain,
164
+ floor: s.floor,
165
+ })
166
+ if (boxes.length === 0) return
181
167
 
182
- const groups = groupByPaint(boxes, (i) =>
183
- livePaintKey(shaped[i].speaker, this.byVoice, voice)
184
- )
168
+ const groups = groupByPaint(boxes, (i) =>
169
+ livePaintKey(shaped[i].speaker, s.byVoice, voice)
170
+ )
185
171
 
186
- for (const [key, run] of groups) {
187
- const graphics = paintBars(run, this.paintFor(key), this.radius)
188
- if (graphics) ctx.draw(graphics)
189
- }
172
+ for (const [key, run] of groups) {
173
+ const graphics = paintBars(run, paintFor(s, key), s.radius)
174
+ if (graphics) ctx.draw(graphics)
190
175
  }
176
+ }
191
177
 
192
- /** See {@link FullWaveform.paintFor} — a voice is a hue, never a gradient. */
193
- private paintFor(key: string): Fill {
194
- if (key === "all") return this.fill
195
- const voice = Number(key.slice("voice:".length))
196
- if (!Number.isFinite(voice) || voice < 0) return this.fill
197
- return this.voiceColors[voice] ?? voiceColor(voice)
198
- }
178
+ /** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */
179
+ function paintFor(s: LiveState, key: string): Fill {
180
+ 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)
199
184
  }
200
185
 
201
186
  /**
@@ -1,4 +1,4 @@
1
- import { Graphics2D, type Fill, type FillResolved } from "@motionscript/core"
1
+ import { Graphics2D, type Fill, type FillResolved } from "@motionscript/sdk"
2
2
 
3
3
  import type { EnvelopeBar } from "./envelope"
4
4
 
@@ -23,12 +23,10 @@ export type WaveformStyle = (typeof WAVEFORM_STYLES)[number]
23
23
  /**
24
24
  * The colours voices are drawn in when nobody has assigned any.
25
25
  *
26
- * The same eight the app's speaker library suggests, in the same order, so a
27
- * transcript whose voices are still unnamed already draws in the colours the
28
- * transcript panel is showing them in. Duplicated as literals rather than
29
- * imported because `scene-core` knows nothing about the app's speaker table —
30
- * and must not, since a scene has to build in a headless render with no
31
- * database in scope.
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.
32
30
  */
33
31
  export const VOICE_PALETTE = [
34
32
  "#6366f1",
@@ -49,7 +47,11 @@ export function voiceColor(index: number): string {
49
47
  ]
50
48
  }
51
49
 
52
- /** Where one bar sits and how tall it came out. */
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
+ */
53
55
  export interface BarBox {
54
56
  x: number
55
57
  y: number
@@ -68,7 +70,7 @@ export interface BarBox {
68
70
  *
69
71
  * A bar is never thinner than a hairline and never shorter than one either: a
70
72
  * silent passage has to read as "audio, and nothing here" rather than as a hole
71
- * in the drawing, exactly as it does on the timeline's clip bars.
73
+ * in the drawing.
72
74
  */
73
75
  export function layOutBars(
74
76
  bars: readonly EnvelopeBar[],
@@ -99,15 +101,14 @@ export function layOutBars(
99
101
  const reach = Math.max(full * clamp01(floor), full * magnitude)
100
102
  const x = -width / 2 + pitch * i + (pitch - barWidth) / 2
101
103
 
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.
107
+ const h = Math.max(1, align === "center" ? reach * 2 : reach)
102
108
  out[i] =
103
109
  align === "center"
104
- ? { x, y: -reach, width: barWidth, height: Math.max(1, reach * 2) }
105
- : {
106
- x,
107
- y: height / 2 - reach,
108
- width: barWidth,
109
- height: Math.max(1, reach),
110
- }
110
+ ? { x, y: -h / 2, width: barWidth, height: h }
111
+ : { x, y: height / 2 - h, width: barWidth, height: h }
111
112
  }
112
113
  return out
113
114
  }
@@ -134,7 +135,9 @@ export function paintBars(
134
135
  for (const box of boxes) {
135
136
  graphics.rect({
136
137
  x: box.x + box.width / 2,
137
- y: box.y + box.height / 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.
140
+ y: -(box.y + box.height / 2),
138
141
  width: box.width,
139
142
  height: box.height,
140
143
  // Never rounder than the bar is wide, or a thin bar becomes a lozenge
@@ -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 d=l/t.duration,u=(e-r)/n;for(let i=0;i<n;i++){let f=(r+u*i)*d,m=(r+u*(i+1))*d,p=Math.floor(f),s=Math.max(p,Math.ceil(m)-1),g=0,h=-1;for(let c=p;c<=s;c++){if(c<0||c>=l)continue;let k=t.magnitude[c];k<g||(g=k,h=t.speaker?t.speaker[c]:-1)}a[i]={magnitude:g,speaker:h}}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/core";var S=["center","bottom"],T=["bars","line","blocks"],x=["#6366f1","#14b8a6","#f59e0b","#ec4899","#22c55e","#3b82f6","#f97316","#a855f7"];function L(t){return x[(t%x.length+x.length)%x.length]}function _(t,r){let{width:e,height:o,gap:n,align:a,gain:l,floor:d}=r,u=t.length;if(u===0||e<=0||o<=0)return[];let i=e/u,f=Math.max(1,i*(1-M(n))),m=a==="center"?o/2:o,p=new Array(u);for(let s=0;s<u;s++){let g=M(t[s].magnitude*l),h=Math.max(m*M(d),m*g),c=-e/2+i*s+(i-f)/2;p[s]=a==="center"?{x:c,y:-h,width:f,height:Math.max(1,h*2)}:{x:c,y:o/2-h,width:f,height:Math.max(1,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 M(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,x as f,L as g,_ as h,b as i,B as j,M as k,F as l};
2
- //# sourceMappingURL=chunk-U5OUQJHQ.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 * ## 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 app, once per file, and arrives here through the\n * build (see `SceneAsset.envelope`) — the same arrangement a chart's rows and a\n * protein's atoms already use, and for the same reason: the build is\n * 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 * app 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 app 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 /** Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. */\n magnitude: Float32Array\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: Int16Array | 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/core\"\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 * The same eight the app's speaker library suggests, in the same order, so a\n * transcript whose voices are still unnamed already draws in the colours the\n * transcript panel is showing them in. Duplicated as literals rather than\n * imported because `scene-core` knows nothing about the app's speaker table —\n * and must not, since a scene has to build in a headless render with no\n * database in scope.\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/** Where one bar sits and how tall it came out. */\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, exactly as it does on the timeline's clip bars.\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 out[i] =\n align === \"center\"\n ? { x, y: -reach, width: barWidth, height: Math.max(1, reach * 2) }\n : {\n x,\n y: height / 2 - reach,\n width: barWidth,\n height: Math.max(1, reach),\n }\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 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": "AAoDO,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,CAAE,UAAW,EAAG,QAAS,EAAE,EAEpE,IAAMC,EAAUN,EAAS,UAAU,OACnC,GAAII,IAAU,GAAKE,IAAY,GAAKN,EAAS,UAAY,EAAG,OAAOK,EAEnE,IAAME,EAAYD,EAAUN,EAAS,SAC/BQ,GAAQN,EAAKD,GAAQG,EAE3B,QAAS,EAAI,EAAG,EAAIA,EAAO,IAAK,CAC9B,IAAMK,GAASR,EAAOO,EAAO,GAAKD,EAC5BG,GAAOT,EAAOO,GAAQ,EAAI,IAAMD,EAIhCI,EAAQ,KAAK,MAAMF,CAAK,EACxBG,E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I,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
- }