@motionscript/audio 0.1.0-alpha.3 → 0.1.0-alpha.6

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 (45) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +92 -0
  5. package/dist/browser/chunks/chunk-DAFIVUTT.js +2 -0
  6. package/dist/browser/chunks/chunk-DAFIVUTT.js.map +7 -0
  7. package/dist/browser/index.js +1 -1
  8. package/dist/browser/index.js.map +3 -3
  9. package/dist/browser/kit.js +1 -1
  10. package/dist/browser/manifest.json +3 -2
  11. package/dist/index.js.map +1 -1
  12. package/dist/kit.js.map +1 -1
  13. package/dist/nodes.d.ts +6 -9
  14. package/dist/nodes.d.ts.map +1 -1
  15. package/dist/nodes.js +7 -9
  16. package/dist/nodes.js.map +1 -1
  17. package/dist/waveform/envelope.d.ts +42 -9
  18. package/dist/waveform/envelope.d.ts.map +1 -1
  19. package/dist/waveform/envelope.js +34 -4
  20. package/dist/waveform/envelope.js.map +1 -1
  21. package/dist/waveform/full-waveform.d.ts +61 -75
  22. package/dist/waveform/full-waveform.d.ts.map +1 -1
  23. package/dist/waveform/full-waveform.js +115 -162
  24. package/dist/waveform/full-waveform.js.map +1 -1
  25. package/dist/waveform/index.js.map +1 -1
  26. package/dist/waveform/live-waveform.d.ts +63 -63
  27. package/dist/waveform/live-waveform.d.ts.map +1 -1
  28. package/dist/waveform/live-waveform.js +92 -134
  29. package/dist/waveform/live-waveform.js.map +1 -1
  30. package/dist/waveform/shared.d.ts +10 -8
  31. package/dist/waveform/shared.d.ts.map +1 -1
  32. package/dist/waveform/shared.js +14 -15
  33. package/dist/waveform/shared.js.map +1 -1
  34. package/package.json +7 -6
  35. package/registry.json +28 -28
  36. package/src/index.ts +2 -2
  37. package/src/kit.ts +22 -22
  38. package/src/nodes.ts +17 -19
  39. package/src/waveform/envelope.ts +176 -143
  40. package/src/waveform/full-waveform.ts +188 -218
  41. package/src/waveform/index.ts +30 -30
  42. package/src/waveform/live-waveform.ts +206 -221
  43. package/src/waveform/shared.ts +197 -194
  44. package/dist/browser/chunks/chunk-B3JXGNP6.js +0 -2
  45. package/dist/browser/chunks/chunk-B3JXGNP6.js.map +0 -7
@@ -1,221 +1,206 @@
1
- import {
2
- Node2D,
3
- node,
4
- fillOps,
5
- property,
6
- type AssetScope,
7
- type Fill,
8
- type Node2DProps,
9
- type RenderContext2D,
10
- } from "@motionscript/sdk"
11
-
12
- import { declarePaints } from "@motionscript/sdk/component"
13
- import {
14
- EMPTY_ENVELOPE,
15
- forSpeaker,
16
- sampleEnvelope,
17
- type AudioEnvelope,
18
- type EnvelopeBar,
19
- } from "./envelope"
20
- import {
21
- clamp01,
22
- groupByPaint,
23
- layOutBars,
24
- livePaintKey,
25
- paintBars,
26
- voiceColor,
27
- type WaveformAlignment,
28
- type WaveformStyle,
29
- } from "./shared"
30
-
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
- /**
64
- * A **live audio visualiser**: bars that move with the recording as it plays.
65
- *
66
- * The thing every podcast-to-video tool puts beside the artwork. It is not a
67
- * picture of the file — that is {@link FullWaveform} — it is a picture of *this
68
- * moment*, and what it is for is making a static frame of a talking head feel
69
- * like it is playing.
70
- *
71
- * ## "Live" without listening
72
- *
73
- * Nothing here reads an audio device. The bars are sampled out of a baked
74
- * envelope at the node's own elapsed time, which is what makes the visualiser
75
- * survive the thing it exists for: an **export**, where there is no playback to
76
- * analyse and frames may be rendered out of order or in parallel. A frame is a
77
- * pure function of its time, so scrubbing back and forth over the visualiser
78
- * redraws exactly what it drew the first time — which an analyser node, by
79
- * construction, cannot promise. See {@link AudioEnvelope}.
80
- *
81
- * ## The speaker filter
82
- *
83
- * {@link speaker} narrows the node to one voice of a diarized recording, and it
84
- * is the reason this node knows about transcripts at all. Two visualisers beside
85
- * two headshots, each filtered to its own host, is a podcast edit that would
86
- * otherwise be done by hand for every turn — and because the filter *silences*
87
- * rather than skips (see {@link forSpeaker}), the quiet host visibly stops
88
- * talking instead of vanishing. Nothing about that requires the words: only who
89
- * is talking when, which is what the diarizer produces.
90
- *
91
- * A filter naming a voice the recording doesn't have draws a resting row rather
92
- * than everything — a picture of "this person never speaks here", which is
93
- * true, where falling back to the unfiltered mix would be a silent lie about
94
- * whose voice you were watching.
95
- */
96
- @node({
97
- key: "liveWaveform",
98
- parentKey: "node",
99
- forkable: true,
100
- layout: {
101
- children: "freeform",
102
- defaultWidthMode: "fixed",
103
- defaultHeightMode: "fixed",
104
- acceptsChildren: true,
105
- },
106
- seed: {
107
- width: 420,
108
- height: 220,
109
- },
110
- })
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
-
127
- @property({
128
- default: "#6366f1",
129
- mapper: fillOps.resolve,
130
- tween: fillOps.lerp,
131
- })
132
- declare fill: Fill
133
-
134
- override declareAssets(assets: AssetScope): void {
135
- super.declareAssets(assets)
136
- declarePaints(this, assets)
137
- }
138
-
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
- }
151
-
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
157
-
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)
162
-
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
- )
170
-
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
181
-
182
- const groups = groupByPaint(boxes, (i) =>
183
- livePaintKey(shaped[i].speaker, this.byVoice, voice)
184
- )
185
-
186
- for (const [key, run] of groups) {
187
- const graphics = paintBars(run, this.paintFor(key), this.radius)
188
- if (graphics) ctx.draw(graphics)
189
- }
190
- }
191
-
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
- }
199
- }
200
-
201
- /**
202
- * Rolls the ends of a window off toward silence.
203
- *
204
- * What separates the `line` style from `bars`: a hard-edged window makes the
205
- * loud bar entering at the right edge appear from nothing, which reads as a
206
- * glitch rather than as sound arriving. Fading the outer fifth turns the same
207
- * data into a shape that grows out of the baseline — the profile a spectrum
208
- * display has, and the reason it looks like one object rather than a row of
209
- * separate meters.
210
- */
211
- function taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {
212
- const count = bars.length
213
- if (count < 3) return [...bars]
214
- const edge = Math.max(1, Math.floor(count / 5))
215
-
216
- return bars.map((bar, i) => {
217
- const fromEnd = Math.min(i, count - 1 - i)
218
- const weight = clamp01(fromEnd / edge)
219
- return { magnitude: bar.magnitude * weight, speaker: bar.speaker }
220
- })
221
- }
1
+ import {
2
+ Types,
3
+ component2d,
4
+ fillOps,
5
+ type Fill,
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"
13
+
14
+ import {
15
+ EMPTY_ENVELOPE,
16
+ forSpeaker,
17
+ sampleEnvelope,
18
+ type AudioEnvelope,
19
+ type EnvelopeBar,
20
+ } from "./envelope"
21
+ import {
22
+ clamp01,
23
+ groupByPaint,
24
+ layOutBars,
25
+ livePaintKey,
26
+ paintBars,
27
+ voiceColor,
28
+ type WaveformAlignment,
29
+ type WaveformStyle,
30
+ } from "./shared"
31
+
32
+ /**
33
+ * A **live audio visualiser**: bars that move with the recording as it plays.
34
+ *
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.
64
+ */
65
+ export const LiveWaveform = component2d({
66
+ key: "liveWaveform",
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 }),
97
+ },
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()
103
+ },
104
+ })
105
+
106
+ export type LiveWaveform = NodeOf<typeof LiveWaveform>
107
+ export type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>
108
+
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
+ }
126
+
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
+ }
139
+
140
+ function drawBars(ctx: RenderContext, s: LiveState): void {
141
+ const { width, height } = ctx.size
142
+ if (width <= 0 || height <= 0) return
143
+
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)
148
+
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
+ )
156
+
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
167
+
168
+ const groups = groupByPaint(boxes, (i) =>
169
+ livePaintKey(shaped[i].speaker, s.byVoice, voice)
170
+ )
171
+
172
+ for (const [key, run] of groups) {
173
+ const graphics = paintBars(run, paintFor(s, key), s.radius)
174
+ if (graphics) ctx.draw(graphics)
175
+ }
176
+ }
177
+
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)
184
+ }
185
+
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
+ */
196
+ function taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {
197
+ const count = bars.length
198
+ if (count < 3) return [...bars]
199
+ const edge = Math.max(1, Math.floor(count / 5))
200
+
201
+ return bars.map((bar, i) => {
202
+ const fromEnd = Math.min(i, count - 1 - i)
203
+ const weight = clamp01(fromEnd / edge)
204
+ return { magnitude: bar.magnitude * weight, speaker: bar.speaker }
205
+ })
206
+ }