@motionscript/audio 0.1.0-alpha.0 → 0.1.0-alpha.5

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 (42) hide show
  1. package/CHANGELOG.md +40 -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 +2 -2
  9. package/dist/browser/kit.js +1 -1
  10. package/dist/browser/manifest.json +4 -3
  11. package/dist/index.js.map +1 -1
  12. package/dist/kit.js.map +1 -1
  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 +3 -4
  19. package/dist/waveform/full-waveform.d.ts.map +1 -1
  20. package/dist/waveform/full-waveform.js +3 -4
  21. package/dist/waveform/full-waveform.js.map +1 -1
  22. package/dist/waveform/index.js.map +1 -1
  23. package/dist/waveform/live-waveform.d.ts +3 -3
  24. package/dist/waveform/live-waveform.d.ts.map +1 -1
  25. package/dist/waveform/live-waveform.js +4 -4
  26. package/dist/waveform/live-waveform.js.map +1 -1
  27. package/dist/waveform/shared.d.ts +11 -9
  28. package/dist/waveform/shared.d.ts.map +1 -1
  29. package/dist/waveform/shared.js +15 -16
  30. package/dist/waveform/shared.js.map +1 -1
  31. package/package.json +7 -5
  32. package/registry.json +28 -28
  33. package/src/index.ts +2 -2
  34. package/src/kit.ts +22 -22
  35. package/src/nodes.ts +19 -19
  36. package/src/waveform/envelope.ts +176 -143
  37. package/src/waveform/full-waveform.ts +217 -218
  38. package/src/waveform/index.ts +30 -30
  39. package/src/waveform/live-waveform.ts +221 -221
  40. package/src/waveform/shared.ts +197 -194
  41. package/dist/browser/chunks/chunk-U5OUQJHQ.js +0 -2
  42. package/dist/browser/chunks/chunk-U5OUQJHQ.js.map +0 -7
@@ -1,221 +1,221 @@
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/core"
11
-
12
- import { declarePaints } from "@motionscript/core/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
+ 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 the engine's own
144
+ * `Video` times its picture from, and the same one that makes a node dropped
145
+ * halfway through a clip 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
+ }