@motionscript/audio 0.1.0-alpha.7 → 0.1.0-alpha.9

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.
@@ -1,206 +1,206 @@
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
- }
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
+ }