@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,218 +1,217 @@
1
- import {
2
- Node2D,
3
- node,
4
- command,
5
- easeInOut,
6
- fillOps,
7
- property,
8
- type AssetScope,
9
- type CommandArgs,
10
- type Fill,
11
- type FillResolved,
12
- type Node2DProps,
13
- type RenderContext2D,
14
- } from "@motionscript/core"
15
-
16
- import { declarePaints } from "@motionscript/core/component"
17
- import type { Seekable } from "@motionscript/core/component"
18
- import { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from "./envelope"
19
- import {
20
- clamp01,
21
- groupByPaint,
22
- layOutBars,
23
- paintBars,
24
- voiceColor,
25
- type WaveformAlignment,
26
- } from "./shared"
27
-
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
- }
55
-
56
- /**
57
- * A **waveform overview**: the whole recording's shape, drawn at once.
58
- *
59
- * The picture every audio editor opens with, and the one a podcast clip puts
60
- * under its caption — a static silhouette of the file, with a played portion
61
- * that advances. It answers "how long is this and where are the loud parts",
62
- * which is a question about the *file*, not about the moment; its sibling
63
- * {@link LiveWaveform} answers the other one.
64
- *
65
- * ## Progress is authored, not assumed
66
- *
67
- * {@link follow} is off by default, and that default is the considered one. A
68
- * scene has no idea where the recording it is drawing sits on the video's audio
69
- * lane — a clip is placed, trimmed and sped up out there, and none of that is
70
- * visible from in here. So the honest default is that the fill is *animation*:
71
- * the author sweeps it with a command, the same as any other tween, and it lands
72
- * wherever they put it. `follow` is the shortcut for the common arrangement
73
- * where the node appears exactly as the track starts, and it says so by being a
74
- * switch someone has to turn on rather than a behaviour that quietly disagrees
75
- * with the mix.
76
- *
77
- * ## Voices instead of progress
78
- *
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
82
- * floor, where the turns are, how long each one ran. It replaces the progress
83
- * colours rather than combining with them, because a bar cannot mean two things
84
- * at once and "played, and also Grace" is a legend nobody can hold in their
85
- * head.
86
- */
87
- @node({
88
- key: "fullWaveform",
89
- parentKey: "node",
90
- forkable: true,
91
- layout: {
92
- children: "freeform",
93
- defaultWidthMode: "fixed",
94
- defaultHeightMode: "fixed",
95
- acceptsChildren: true,
96
- },
97
- seed: {
98
- width: 900,
99
- height: 160,
100
- },
101
- })
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
-
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
- }
151
-
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
- }
159
-
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
165
-
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
182
-
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
- )
194
-
195
- for (const [key, run] of groups) {
196
- const graphics = paintBars(run, this.paintFor(key), this.radius)
197
- if (graphics) ctx.draw(graphics)
198
- }
199
- }
200
-
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
213
-
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
- }
218
- }
1
+ import {
2
+ Node2D,
3
+ node,
4
+ command,
5
+ easeInOut,
6
+ fillOps,
7
+ property,
8
+ type AssetScope,
9
+ type CommandArgs,
10
+ type Fill,
11
+ type FillResolved,
12
+ type Node2DProps,
13
+ type RenderContext2D,
14
+ } from "@motionscript/sdk"
15
+
16
+ import { declarePaints } from "@motionscript/sdk/component"
17
+ import type { Seekable } from "@motionscript/sdk/component"
18
+ import { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from "./envelope"
19
+ import {
20
+ clamp01,
21
+ groupByPaint,
22
+ layOutBars,
23
+ paintBars,
24
+ voiceColor,
25
+ type WaveformAlignment,
26
+ } from "./shared"
27
+
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
+ }
55
+
56
+ /**
57
+ * A **waveform overview**: the whole recording's shape, drawn at once.
58
+ *
59
+ * The picture every audio editor opens with, and the one a podcast clip puts
60
+ * under its caption — a static silhouette of the file, with a played portion
61
+ * that advances. It answers "how long is this and where are the loud parts",
62
+ * which is a question about the *file*, not about the moment; its sibling
63
+ * {@link LiveWaveform} answers the other one.
64
+ *
65
+ * ## Progress is authored, not assumed
66
+ *
67
+ * {@link follow} is off by default, and that default is the considered one. A
68
+ * scene has no idea where the recording it is drawing sits on the video's audio
69
+ * lane — a clip is placed, trimmed and sped up out there, and none of that is
70
+ * visible from in here. So the honest default is that the fill is *animation*:
71
+ * the author sweeps it with a command, the same as any other tween, and it lands
72
+ * wherever they put it. `follow` is the shortcut for the common arrangement
73
+ * where the node appears exactly as the track starts, and it says so by being a
74
+ * switch someone has to turn on rather than a behaviour that quietly disagrees
75
+ * with the mix.
76
+ *
77
+ * ## Voices instead of progress
78
+ *
79
+ * With {@link byVoice} on, the played/ahead split is replaced by one colour per
80
+ * speaking voice, at a size you can read. For a two-hander it is a map of the conversation: who has the
81
+ * floor, where the turns are, how long each one ran. It replaces the progress
82
+ * colours rather than combining with them, because a bar cannot mean two things
83
+ * at once and "played, and also Grace" is a legend nobody can hold in their
84
+ * head.
85
+ */
86
+ @node({
87
+ key: "fullWaveform",
88
+ parentKey: "node",
89
+ forkable: true,
90
+ layout: {
91
+ children: "freeform",
92
+ defaultWidthMode: "fixed",
93
+ defaultHeightMode: "fixed",
94
+ acceptsChildren: true,
95
+ },
96
+ seed: {
97
+ width: 900,
98
+ height: 160,
99
+ },
100
+ })
101
+ export class FullWaveform extends Node2D<FullWaveformProps> {
102
+ @property({ default: EMPTY_ENVELOPE }) declare envelope: AudioEnvelope
103
+ @property({ default: 120 }) declare bars: number
104
+ @property({ default: 0.3 }) declare gap: number
105
+ @property({ default: 2 }) declare radius: number
106
+ @property({ default: 0 }) declare progress: number
107
+ @property({ default: false }) declare follow: boolean
108
+ @property({ default: 1 }) declare gain: number
109
+ @property({ default: 0.015 }) declare floor: number
110
+ @property({ default: "center" }) declare align: WaveformAlignment
111
+ @property({ default: false }) declare byVoice: boolean
112
+ @property({ default: [] }) declare voiceColors: string[]
113
+
114
+ @property({
115
+ default: "#5b5b7a",
116
+ mapper: fillOps.resolve,
117
+ tween: fillOps.lerp,
118
+ })
119
+ declare fill: Fill
120
+
121
+ @property({
122
+ default: "#6366f1",
123
+ mapper: fillOps.resolve,
124
+ tween: fillOps.lerp,
125
+ })
126
+ declare playedFill: Fill
127
+
128
+ /**
129
+ * Fill the waveform in, left to right — the node's one command, since
130
+ * everything else about it is expressible as an ordinary `to`.
131
+ */
132
+ @command()
133
+ sweep(args: CommandArgs<Record<string, never>> & { duration: number }): Seekable {
134
+ return this.to({
135
+ data: { progress: 1 } as Partial<FullWaveformProps>,
136
+ duration: args.duration,
137
+ easing: args.easing ?? easeInOut(),
138
+ })
139
+ }
140
+
141
+ /**
142
+ * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
143
+ * paint props it carries — see {@link declarePaints}. `playedFill` is named
144
+ * past the conventional slots, so it is passed explicitly.
145
+ */
146
+ override declareAssets(assets: AssetScope): void {
147
+ super.declareAssets(assets)
148
+ declarePaints(this, assets, { fills: [this.playedFill as FillResolved[]] })
149
+ }
150
+
151
+ /** Where the fill has reached, from whichever source is in charge. */
152
+ private get played(): number {
153
+ if (!this.follow) return clamp01(this.progress)
154
+ const length = this.envelope.duration
155
+ if (length <= 0) return 0
156
+ return clamp01(this.time.elapsed / length)
157
+ }
158
+
159
+ protected renderSelf(ctx: RenderContext2D): void {
160
+ const rect = this.layoutBounds
161
+ const width = rect?.width ?? 0
162
+ const height = rect?.height ?? 0
163
+ if (width <= 0 || height <= 0) return
164
+
165
+ const count = Math.max(1, Math.round(this.bars))
166
+ const sampled = sampleEnvelope(
167
+ this.envelope,
168
+ 0,
169
+ this.envelope.duration,
170
+ count
171
+ )
172
+ const boxes = layOutBars(sampled, {
173
+ width,
174
+ height,
175
+ gap: this.gap,
176
+ align: this.align,
177
+ gain: this.gain,
178
+ floor: this.floor,
179
+ })
180
+ if (boxes.length === 0) return
181
+
182
+ // One draw per colour rather than per bar — see {@link paintBars}. The key
183
+ // is a string because it has to be comparable; the paint it stands for is
184
+ // resolved after the grouping.
185
+ const edge = this.played * boxes.length
186
+ const groups = groupByPaint(boxes, (i) =>
187
+ this.byVoice
188
+ ? `voice:${sampled[i].speaker}`
189
+ : i < edge
190
+ ? "played"
191
+ : "ahead"
192
+ )
193
+
194
+ for (const [key, run] of groups) {
195
+ const graphics = paintBars(run, this.paintFor(key), this.radius)
196
+ if (graphics) ctx.draw(graphics)
197
+ }
198
+ }
199
+
200
+ /**
201
+ * The paint a group takes.
202
+ *
203
+ * A voice's colour is a **colour string**, never one of the node's two fill
204
+ * props, and that is deliberate: a voice is identified by hue, so a gradient
205
+ * or an image there would defeat the only thing the colour is for. It also
206
+ * means these paints declare no assets, which is why {@link declareAssets}
207
+ * only has to account for the two real fill props.
208
+ */
209
+ private paintFor(key: string): Fill {
210
+ if (key === "played") return this.playedFill
211
+ if (key === "ahead") return this.fill
212
+
213
+ const voice = Number(key.slice("voice:".length))
214
+ if (!Number.isFinite(voice) || voice < 0) return this.fill
215
+ return this.voiceColors[voice] ?? voiceColor(voice)
216
+ }
217
+ }
@@ -1,30 +1,30 @@
1
- /**
2
- * The waveform family: one subject, two questions.
3
- *
4
- * - {@link FullWaveform} draws the **file** — the whole recording's silhouette,
5
- * with a played portion that advances. "How long is this, and where are the
6
- * loud parts."
7
- * - {@link LiveWaveform} draws the **moment** — bars moving with playback, the
8
- * visualiser a podcast clip puts beside the artwork. "Is anything happening
9
- * right now, and whose voice is it."
10
- *
11
- * Both read the same baked {@link AudioEnvelope}, which is why they cannot
12
- * disagree about a recording, and why neither listens to anything — see that
13
- * module for why a scene that has to export cannot use a live analyser.
14
- */
15
- export { FullWaveform } from "./full-waveform"
16
- export type { FullWaveformProps } from "./full-waveform"
17
- export { LiveWaveform } from "./live-waveform"
18
- export type { LiveWaveformProps } from "./live-waveform"
19
- export { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from "./envelope"
20
- export type { AudioEnvelope, EnvelopeBar } from "./envelope"
21
- export {
22
- VOICE_PALETTE,
23
- WAVEFORM_ALIGNMENTS,
24
- WAVEFORM_STYLES,
25
- groupByPaint,
26
- layOutBars,
27
- paintBars,
28
- voiceColor,
29
- } from "./shared"
30
- export type { BarBox, WaveformAlignment, WaveformStyle } from "./shared"
1
+ /**
2
+ * The waveform family: one subject, two questions.
3
+ *
4
+ * - {@link FullWaveform} draws the **file** — the whole recording's silhouette,
5
+ * with a played portion that advances. "How long is this, and where are the
6
+ * loud parts."
7
+ * - {@link LiveWaveform} draws the **moment** — bars moving with playback, the
8
+ * visualiser a podcast clip puts beside the artwork. "Is anything happening
9
+ * right now, and whose voice is it."
10
+ *
11
+ * Both read the same baked {@link AudioEnvelope}, which is why they cannot
12
+ * disagree about a recording, and why neither listens to anything — see that
13
+ * module for why a scene that has to export cannot use a live analyser.
14
+ */
15
+ export { FullWaveform } from "./full-waveform"
16
+ export type { FullWaveformProps } from "./full-waveform"
17
+ export { LiveWaveform } from "./live-waveform"
18
+ export type { LiveWaveformProps } from "./live-waveform"
19
+ export { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from "./envelope"
20
+ export type { AudioEnvelope, EnvelopeBar } from "./envelope"
21
+ export {
22
+ VOICE_PALETTE,
23
+ WAVEFORM_ALIGNMENTS,
24
+ WAVEFORM_STYLES,
25
+ groupByPaint,
26
+ layOutBars,
27
+ paintBars,
28
+ voiceColor,
29
+ } from "./shared"
30
+ export type { BarBox, WaveformAlignment, WaveformStyle } from "./shared"