@motionscript/audio 0.1.0-alpha.8 → 0.1.0

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,188 +1,188 @@
1
- import {
2
- Types,
3
- component2d,
4
- easeInOut,
5
- fillOps,
6
- type Fill,
7
- type FillResolved,
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"
14
-
15
- import { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from "./envelope"
16
- import {
17
- clamp01,
18
- groupByPaint,
19
- layOutBars,
20
- paintBars,
21
- voiceColor,
22
- type WaveformAlignment,
23
- } from "./shared"
24
-
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 })
28
-
29
- /**
30
- * A **waveform overview**: the whole recording's shape, drawn at once.
31
- *
32
- * The picture every audio editor opens with, and the one a podcast clip puts
33
- * under its caption — a static silhouette of the file, with a played portion
34
- * that advances. It answers "how long is this and where are the loud parts",
35
- * which is a question about the *file*, not about the moment; its sibling
36
- * {@link LiveWaveform} answers the other one.
37
- *
38
- * ## Progress is authored, not assumed
39
- *
40
- * `follow` is off by default, and that default is the considered one. A
41
- * scene has no idea where the recording it is drawing sits on the video's audio
42
- * lane — a clip is placed, trimmed and sped up out there, and none of that is
43
- * visible from in here. So the honest default is that the fill is *animation*:
44
- * the author sweeps it with a command, the same as any other tween, and it lands
45
- * wherever they put it. `follow` is the shortcut for the common arrangement
46
- * where the node appears exactly as the track starts, and it says so by being a
47
- * switch someone has to turn on rather than a behaviour that quietly disagrees
48
- * with the mix.
49
- *
50
- * ## Voices instead of progress
51
- *
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
54
- * floor, where the turns are, how long each one ran. It replaces the progress
55
- * colours rather than combining with them, because a bar cannot mean two things
56
- * at once and "played, and also Grace" is a legend nobody can hold in their
57
- * head.
58
- */
59
- export const FullWaveform = component2d({
60
- key: "fullWaveform",
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
- },
104
- },
105
- render: (ctx, state) => {
106
- drawBars(ctx, state)
107
- ctx.children()
108
- },
109
- })
110
-
111
- export type FullWaveform = NodeOf<typeof FullWaveform>
112
- export type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>
113
-
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
- }
129
-
130
- function drawBars(ctx: RenderContext, s: FullState): void {
131
- const { width, height } = ctx.size
132
- if (width <= 0 || height <= 0) return
133
-
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
145
-
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
- )
157
-
158
- for (const [key, run] of groups) {
159
- const graphics = paintBars(run, paintFor(s, key), s.radius)
160
- if (graphics) ctx.draw(graphics)
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
- }
171
-
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
184
-
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)
188
- }
1
+ import {
2
+ Types,
3
+ component2d,
4
+ easeInOut,
5
+ fillOps,
6
+ type Fill,
7
+ type FillResolved,
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"
14
+
15
+ import { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from "./envelope"
16
+ import {
17
+ clamp01,
18
+ groupByPaint,
19
+ layOutBars,
20
+ paintBars,
21
+ voiceColor,
22
+ type WaveformAlignment,
23
+ } from "./shared"
24
+
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 })
28
+
29
+ /**
30
+ * A **waveform overview**: the whole recording's shape, drawn at once.
31
+ *
32
+ * The picture every audio editor opens with, and the one a podcast clip puts
33
+ * under its caption — a static silhouette of the file, with a played portion
34
+ * that advances. It answers "how long is this and where are the loud parts",
35
+ * which is a question about the *file*, not about the moment; its sibling
36
+ * {@link LiveWaveform} answers the other one.
37
+ *
38
+ * ## Progress is authored, not assumed
39
+ *
40
+ * `follow` is off by default, and that default is the considered one. A
41
+ * scene has no idea where the recording it is drawing sits on the video's audio
42
+ * lane — a clip is placed, trimmed and sped up out there, and none of that is
43
+ * visible from in here. So the honest default is that the fill is *animation*:
44
+ * the author sweeps it with a command, the same as any other tween, and it lands
45
+ * wherever they put it. `follow` is the shortcut for the common arrangement
46
+ * where the node appears exactly as the track starts, and it says so by being a
47
+ * switch someone has to turn on rather than a behaviour that quietly disagrees
48
+ * with the mix.
49
+ *
50
+ * ## Voices instead of progress
51
+ *
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
54
+ * floor, where the turns are, how long each one ran. It replaces the progress
55
+ * colours rather than combining with them, because a bar cannot mean two things
56
+ * at once and "played, and also Grace" is a legend nobody can hold in their
57
+ * head.
58
+ */
59
+ export const FullWaveform = component2d({
60
+ key: "fullWaveform",
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
+ },
104
+ },
105
+ render: (ctx, state) => {
106
+ drawBars(ctx, state)
107
+ ctx.children()
108
+ },
109
+ })
110
+
111
+ export type FullWaveform = NodeOf<typeof FullWaveform>
112
+ export type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>
113
+
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
+ }
129
+
130
+ function drawBars(ctx: RenderContext, s: FullState): void {
131
+ const { width, height } = ctx.size
132
+ if (width <= 0 || height <= 0) return
133
+
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
145
+
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
+ )
157
+
158
+ for (const [key, run] of groups) {
159
+ const graphics = paintBars(run, paintFor(s, key), s.radius)
160
+ if (graphics) ctx.draw(graphics)
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
+ }
171
+
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
184
+
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)
188
+ }
@@ -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"