@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,6 +1,37 @@
1
1
  /**
2
2
  * The shape of a recording, as the waveform nodes draw it.
3
3
  *
4
+ * ## How an envelope reaches a node: the host supplies it
5
+ *
6
+ * Nothing in the engine, and nothing in this kit, decodes audio. A waveform node
7
+ * does not read a `sound` node's `src`, declares no audio asset and is not tied
8
+ * to a clip: it draws the {@link AudioEnvelope} it is handed in its `envelope`
9
+ * prop. Decoding a file into an envelope is the host's job, done once per file
10
+ * before the document is built. Where the recording sits on the audio lane is
11
+ * the author's timing too, which is why `FullWaveform.follow` is opt-in.
12
+ *
13
+ * ### The JSON form
14
+ *
15
+ * A document holds the envelope as plain JSON, because a prop value in a
16
+ * document is JSON. The typed arrays in {@link AudioEnvelope} are one spelling of
17
+ * it; `ArrayLike<number>` also accepts the plain arrays a document stores:
18
+ *
19
+ * ```json
20
+ * {
21
+ * "magnitude": [0, 0.25, 0.5, 1, 0.5, 0],
22
+ * "duration": 6,
23
+ * "speaker": [0, 0, 0, 1, 1, -1],
24
+ * "speakers": 2
25
+ * }
26
+ * ```
27
+ *
28
+ * `magnitude` is the peak loudness, `0`…`1`, of buckets that divide `duration`
29
+ * seconds evenly. `speaker` is `null` when nobody was identified; otherwise it is parallel to `magnitude`, one voice
30
+ * index per bucket, `-1` for "nobody". `speakers` is the voice count, `0` when
31
+ * `speaker` is `null`. A `Float32Array` or `Int16Array` is not JSON-serialisable
32
+ * (`JSON.stringify` writes an object of indices), so a host that holds the typed
33
+ * form converts with `Array.from` before storing it in a document.
34
+ *
4
35
  * ## Why the data is baked rather than listened to
5
36
  *
6
37
  * A scene has to render identically in the preview and in a headless export,
@@ -10,10 +41,9 @@
10
41
  * then a pure function of time, which is what makes scrubbing backwards, jumping
11
42
  * to frame 900, and rendering out of order all produce the same frame.
12
43
  *
13
- * The decode happens in the app, once per file, and arrives here through the
14
- * build (see `SceneAsset.envelope`) — the same arrangement a chart's rows and a
15
- * protein's atoms already use, and for the same reason: the build is
16
- * synchronous and cannot await a fetch.
44
+ * The decode happens in the host, once per file, and arrives here through the
45
+ * build — the same arrangement a chart's rows and a protein's atoms already use,
46
+ * and for the same reason: the build is synchronous and cannot await a fetch.
17
47
  *
18
48
  * ## Why magnitude rather than min/max
19
49
  *
@@ -26,17 +56,20 @@
26
56
 
27
57
  /**
28
58
  * A decoded recording, reduced to a uniform grid of loudness — plus, when the
29
- * app knows it, who is talking in each bucket.
59
+ * host knows it, who is talking in each bucket.
30
60
  *
31
61
  * The speaker track is **not** something a decoder can produce; it comes from a
32
- * diarized transcript and is folded in by the app before the build. It lives
62
+ * diarized transcript and is folded in by the host before the build. It lives
33
63
  * here rather than in a parallel structure because every reader of it is also a
34
64
  * reader of the magnitudes at the same index, and two arrays that must stay the
35
65
  * same length are better as one object than as two arguments.
36
66
  */
37
67
  export interface AudioEnvelope {
38
- /** Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. */
39
- magnitude: Float32Array
68
+ /**
69
+ * Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. A
70
+ * `Float32Array`, or the plain `number[]` a document stores.
71
+ */
72
+ magnitude: ArrayLike<number>
40
73
  /** The decoded length in seconds. */
41
74
  duration: number
42
75
  /**
@@ -44,7 +77,7 @@ export interface AudioEnvelope {
44
77
  * own numbering, or `-1` for "nobody identified". Parallel to
45
78
  * {@link magnitude}, or `null` when the track was never diarized.
46
79
  */
47
- speaker: Int16Array | null
80
+ speaker: ArrayLike<number> | null
48
81
  /** How many distinct voices the diarizer found. Zero when it did not run. */
49
82
  speakers: number
50
83
  }
@@ -1,20 +1,17 @@
1
1
  import {
2
- Node2D,
3
- node,
4
- command,
2
+ Types,
3
+ component2d,
5
4
  easeInOut,
6
5
  fillOps,
7
- property,
8
- type AssetScope,
9
- type CommandArgs,
10
6
  type Fill,
11
7
  type FillResolved,
12
- type Node2DProps,
13
- type RenderContext2D,
14
- } from "@motionscript/core"
8
+ type NodeOf,
9
+ type RenderContext,
10
+ // Named so the emitted declaration reaches these through the sdk, not core.
11
+ type Node2D as _Node2D,
12
+ type Node2DProps as _Node2DProps,
13
+ } from "@motionscript/sdk"
15
14
 
16
- import { declarePaints } from "@motionscript/core/component"
17
- import type { Seekable } from "@motionscript/core/component"
18
15
  import { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from "./envelope"
19
16
  import {
20
17
  clamp01,
@@ -25,33 +22,9 @@ import {
25
22
  type WaveformAlignment,
26
23
  } from "./shared"
27
24
 
28
- export interface FullWaveformProps extends Node2DProps {
29
- /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
30
- envelope: AudioEnvelope
31
- /** How many columns the whole file is drawn as. */
32
- bars: number
33
- /** Space between bars, as a fraction of the pitch. See {@link layOutBars}. */
34
- gap: number
35
- /** Corner radius of a bar, capped at half its width. */
36
- radius: number
37
- /** How much of the file reads as played, `0`–`1`. Tweenable. */
38
- progress: number
39
- /** Drive {@link progress} from the node's own clock instead of the prop. */
40
- follow: boolean
41
- /** Multiplies every magnitude — a quiet track pulled up to fill its box. */
42
- gain: number
43
- /** How much of the box a silent bar still occupies, `0`–`1`. */
44
- floor: number
45
- align: WaveformAlignment
46
- /** Colour each voice's turns rather than splitting on played / not played. */
47
- byVoice: boolean
48
- /** A colour per voice index. Short or empty falls back to the palette. */
49
- voiceColors: string[]
50
- /** The bars ahead of the playhead. */
51
- fill: Fill
52
- /** …and the bars behind it. */
53
- playedFill: Fill
54
- }
25
+ /** A paint entry as the waveforms declare theirs: resolved and tweened like a fill, its kind read off the default. */
26
+ const paint = (initial: string) =>
27
+ Types.custom<Fill, FillResolved[]>(initial, { resolve: fillOps.resolve, lerp: fillOps.lerp })
55
28
 
56
29
  /**
57
30
  * A **waveform overview**: the whole recording's shape, drawn at once.
@@ -64,7 +37,7 @@ export interface FullWaveformProps extends Node2DProps {
64
37
  *
65
38
  * ## Progress is authored, not assumed
66
39
  *
67
- * {@link follow} is off by default, and that default is the considered one. A
40
+ * `follow` is off by default, and that default is the considered one. A
68
41
  * scene has no idea where the recording it is drawing sits on the video's audio
69
42
  * lane — a clip is placed, trimmed and sped up out there, and none of that is
70
43
  * visible from in here. So the honest default is that the fill is *animation*:
@@ -76,143 +49,140 @@ export interface FullWaveformProps extends Node2DProps {
76
49
  *
77
50
  * ## Voices instead of progress
78
51
  *
79
- * With {@link byVoice} on, the played/ahead split is replaced by one colour per
80
- * speaking voice — the drawing the timeline's clip bars already make, at a size
81
- * you can read. For a two-hander it is a map of the conversation: who has the
52
+ * With `byVoice` on, the played/ahead split is replaced by one colour per
53
+ * speaking voice, at a size you can read. For a two-hander it is a map of the conversation: who has the
82
54
  * floor, where the turns are, how long each one ran. It replaces the progress
83
55
  * colours rather than combining with them, because a bar cannot mean two things
84
56
  * at once and "played, and also Grace" is a legend nobody can hold in their
85
57
  * head.
86
58
  */
87
- @node({
59
+ export const FullWaveform = component2d({
88
60
  key: "fullWaveform",
89
- parentKey: "node",
90
- forkable: true,
91
- layout: {
92
- children: "freeform",
93
- defaultWidthMode: "fixed",
94
- defaultHeightMode: "fixed",
95
- acceptsChildren: true,
61
+ state: {
62
+ /** The baked recording. See {@link AudioEnvelope} for why it is baked. */
63
+ envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),
64
+ /** How many columns the whole file is drawn as. */
65
+ bars: 120,
66
+ /** Space between bars, as a fraction of the pitch. See {@link layOutBars}. */
67
+ gap: 0.3,
68
+ /** Corner radius of a bar, capped at half its width. */
69
+ radius: 2,
70
+ /** How much of the file reads as played, `0`–`1`. Tweenable. */
71
+ progress: 0,
72
+ /** Drive `progress` from the node's own clock instead of the prop. */
73
+ follow: false,
74
+ /** Multiplies every magnitude — a quiet track pulled up to fill its box. */
75
+ gain: 1,
76
+ /** How much of the box a silent bar still occupies, `0`–`1`. */
77
+ floor: 0.015,
78
+ align: Types.custom<WaveformAlignment>("center"),
79
+ /** Colour each voice's turns rather than splitting on played / not played. */
80
+ byVoice: false,
81
+ /** A colour per voice index. Short or empty falls back to the palette. */
82
+ voiceColors: Types.custom<string[]>([]),
83
+ /** The bars ahead of the playhead. */
84
+ fill: paint("#5b5b7a"),
85
+ /** …and the bars behind it. */
86
+ playedFill: paint("#6366f1"),
87
+ },
88
+ // Nothing declares a component's paints but itself: `fill` and `playedFill` both.
89
+ load: (loader, state) => {
90
+ loader.fill(state.fill)
91
+ loader.fill(state.playedFill)
92
+ },
93
+ commands: {
94
+ /**
95
+ * Fill the waveform in, left to right — the node's one command, since
96
+ * everything else about it is expressible as an ordinary `to`. Eased in-out
97
+ * unless it is given an easing of its own.
98
+ */
99
+ sweep: (timeline, state) => {
100
+ const ease = timeline.easing ? null : easeInOut()
101
+ const from = state.progress
102
+ return timeline.animate((t) => ({ progress: from + (1 - from) * (ease ? ease(t) : t) }))
103
+ },
96
104
  },
97
- seed: {
98
- width: 900,
99
- height: 160,
105
+ render: (ctx, state) => {
106
+ drawBars(ctx, state)
107
+ ctx.children()
100
108
  },
101
109
  })
102
- export class FullWaveform extends Node2D<FullWaveformProps> {
103
- @property({ default: EMPTY_ENVELOPE }) declare envelope: AudioEnvelope
104
- @property({ default: 120 }) declare bars: number
105
- @property({ default: 0.3 }) declare gap: number
106
- @property({ default: 2 }) declare radius: number
107
- @property({ default: 0 }) declare progress: number
108
- @property({ default: false }) declare follow: boolean
109
- @property({ default: 1 }) declare gain: number
110
- @property({ default: 0.015 }) declare floor: number
111
- @property({ default: "center" }) declare align: WaveformAlignment
112
- @property({ default: false }) declare byVoice: boolean
113
- @property({ default: [] }) declare voiceColors: string[]
114
-
115
- @property({
116
- default: "#5b5b7a",
117
- mapper: fillOps.resolve,
118
- tween: fillOps.lerp,
119
- })
120
- declare fill: Fill
121
110
 
122
- @property({
123
- default: "#6366f1",
124
- mapper: fillOps.resolve,
125
- tween: fillOps.lerp,
126
- })
127
- declare playedFill: Fill
128
-
129
- /**
130
- * Fill the waveform in, left to right — the node's one command, since
131
- * everything else about it is expressible as an ordinary `to`.
132
- */
133
- @command()
134
- sweep(args: CommandArgs<Record<string, never>> & { duration: number }): Seekable {
135
- return this.to({
136
- data: { progress: 1 } as Partial<FullWaveformProps>,
137
- duration: args.duration,
138
- easing: args.easing ?? easeInOut(),
139
- })
140
- }
141
-
142
- /**
143
- * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
144
- * paint props it carries — see {@link declarePaints}. `playedFill` is named
145
- * past the conventional slots, so it is passed explicitly.
146
- */
147
- override declareAssets(assets: AssetScope): void {
148
- super.declareAssets(assets)
149
- declarePaints(this, assets, { fills: [this.playedFill as FillResolved[]] })
150
- }
111
+ export type FullWaveform = NodeOf<typeof FullWaveform>
112
+ export type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>
151
113
 
152
- /** Where the fill has reached, from whichever source is in charge. */
153
- private get played(): number {
154
- if (!this.follow) return clamp01(this.progress)
155
- const length = this.envelope.duration
156
- if (length <= 0) return 0
157
- return clamp01(this.time.elapsed / length)
158
- }
114
+ interface FullState {
115
+ readonly envelope: AudioEnvelope
116
+ readonly bars: number
117
+ readonly gap: number
118
+ readonly radius: number
119
+ readonly progress: number
120
+ readonly follow: boolean
121
+ readonly gain: number
122
+ readonly floor: number
123
+ readonly align: WaveformAlignment
124
+ readonly byVoice: boolean
125
+ readonly voiceColors: string[]
126
+ readonly fill: FillResolved[]
127
+ readonly playedFill: FillResolved[]
128
+ }
159
129
 
160
- protected renderSelf(ctx: RenderContext2D): void {
161
- const rect = this.layoutBounds
162
- const width = rect?.width ?? 0
163
- const height = rect?.height ?? 0
164
- if (width <= 0 || height <= 0) return
130
+ function drawBars(ctx: RenderContext, s: FullState): void {
131
+ const { width, height } = ctx.size
132
+ if (width <= 0 || height <= 0) return
165
133
 
166
- const count = Math.max(1, Math.round(this.bars))
167
- const sampled = sampleEnvelope(
168
- this.envelope,
169
- 0,
170
- this.envelope.duration,
171
- count
172
- )
173
- const boxes = layOutBars(sampled, {
174
- width,
175
- height,
176
- gap: this.gap,
177
- align: this.align,
178
- gain: this.gain,
179
- floor: this.floor,
180
- })
181
- if (boxes.length === 0) return
134
+ const count = Math.max(1, Math.round(s.bars))
135
+ const sampled = sampleEnvelope(s.envelope, 0, s.envelope.duration, count)
136
+ const boxes = layOutBars(sampled, {
137
+ width,
138
+ height,
139
+ gap: s.gap,
140
+ align: s.align,
141
+ gain: s.gain,
142
+ floor: s.floor,
143
+ })
144
+ if (boxes.length === 0) return
182
145
 
183
- // One draw per colour rather than per bar — see {@link paintBars}. The key
184
- // is a string because it has to be comparable; the paint it stands for is
185
- // resolved after the grouping.
186
- const edge = this.played * boxes.length
187
- const groups = groupByPaint(boxes, (i) =>
188
- this.byVoice
189
- ? `voice:${sampled[i].speaker}`
190
- : i < edge
191
- ? "played"
192
- : "ahead"
193
- )
146
+ // One draw per colour rather than per bar — see {@link paintBars}. The key
147
+ // is a string because it has to be comparable; the paint it stands for is
148
+ // resolved after the grouping.
149
+ const edge = played(ctx, s) * boxes.length
150
+ const groups = groupByPaint(boxes, (i) =>
151
+ s.byVoice
152
+ ? `voice:${sampled[i].speaker}`
153
+ : i < edge
154
+ ? "played"
155
+ : "ahead"
156
+ )
194
157
 
195
- for (const [key, run] of groups) {
196
- const graphics = paintBars(run, this.paintFor(key), this.radius)
197
- if (graphics) ctx.draw(graphics)
198
- }
158
+ for (const [key, run] of groups) {
159
+ const graphics = paintBars(run, paintFor(s, key), s.radius)
160
+ if (graphics) ctx.draw(graphics)
199
161
  }
162
+ }
163
+
164
+ /** Where the fill has reached, from whichever source is in charge. */
165
+ function played(ctx: RenderContext, s: FullState): number {
166
+ if (!s.follow) return clamp01(s.progress)
167
+ const length = s.envelope.duration
168
+ if (length <= 0) return 0
169
+ return clamp01(ctx.time.elapsed / length)
170
+ }
200
171
 
201
- /**
202
- * The paint a group takes.
203
- *
204
- * A voice's colour is a **colour string**, never one of the node's two fill
205
- * props, and that is deliberate: a voice is identified by hue, so a gradient
206
- * or an image there would defeat the only thing the colour is for. It also
207
- * means these paints declare no assets, which is why {@link declareAssets}
208
- * only has to account for the two real fill props.
209
- */
210
- private paintFor(key: string): Fill {
211
- if (key === "played") return this.playedFill
212
- if (key === "ahead") return this.fill
172
+ /**
173
+ * The paint a group takes.
174
+ *
175
+ * A voice's colour is a **colour string**, never one of the node's two fill
176
+ * props, and that is deliberate: a voice is identified by hue, so a gradient
177
+ * or an image there would defeat the only thing the colour is for. It also
178
+ * means these paints declare no assets, which is why `load` only has to account
179
+ * for the two real fill props.
180
+ */
181
+ function paintFor(s: FullState, key: string): Fill {
182
+ if (key === "played") return s.playedFill
183
+ if (key === "ahead") return s.fill
213
184
 
214
- const voice = Number(key.slice("voice:".length))
215
- if (!Number.isFinite(voice) || voice < 0) return this.fill
216
- return this.voiceColors[voice] ?? voiceColor(voice)
217
- }
185
+ const voice = Number(key.slice("voice:".length))
186
+ if (!Number.isFinite(voice) || voice < 0) return s.fill
187
+ return s.voiceColors[voice] ?? voiceColor(voice)
218
188
  }