@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.
- package/CHANGELOG.md +6 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +92 -92
- package/dist/browser/chunks/{chunk-DAFIVUTT.js → chunk-DD3KHRUL.js} +1 -1
- package/dist/browser/chunks/chunk-DD3KHRUL.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/waveform/envelope.js.map +1 -1
- package/dist/waveform/full-waveform.js.map +1 -1
- package/dist/waveform/index.js.map +1 -1
- package/dist/waveform/live-waveform.js.map +1 -1
- package/dist/waveform/shared.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +28 -28
- package/src/index.ts +2 -2
- package/src/kit.ts +22 -22
- package/src/nodes.ts +17 -17
- package/src/waveform/envelope.ts +176 -176
- package/src/waveform/full-waveform.ts +188 -188
- package/src/waveform/index.ts +30 -30
- package/src/waveform/live-waveform.ts +206 -206
- package/src/waveform/shared.ts +197 -197
- package/dist/browser/chunks/chunk-DAFIVUTT.js.map +0 -7
package/src/waveform/shared.ts
CHANGED
|
@@ -1,197 +1,197 @@
|
|
|
1
|
-
import { Graphics2D, type Fill, type FillResolved } from "@motionscript/sdk"
|
|
2
|
-
|
|
3
|
-
import type { EnvelopeBar } from "./envelope"
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Geometry and paint shared by the two waveform nodes.
|
|
7
|
-
*
|
|
8
|
-
* They draw the same object — a row of bars whose heights are loudness — and
|
|
9
|
-
* differ only in what decides the heights: the whole file for one, a window
|
|
10
|
-
* around the playhead for the other. Everything about *how a row of bars is
|
|
11
|
-
* laid out and painted* is therefore common, and lives here so the two cannot
|
|
12
|
-
* drift into looking like different components.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/** How a bar is grown out of its baseline. */
|
|
16
|
-
export const WAVEFORM_ALIGNMENTS = ["center", "bottom"] as const
|
|
17
|
-
export type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number]
|
|
18
|
-
|
|
19
|
-
/** What the bars are shaped like. */
|
|
20
|
-
export const WAVEFORM_STYLES = ["bars", "line", "blocks"] as const
|
|
21
|
-
export type WaveformStyle = (typeof WAVEFORM_STYLES)[number]
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The colours voices are drawn in when nobody has assigned any.
|
|
25
|
-
*
|
|
26
|
-
* Eight hues that can be told apart at a glance, handed out by voice index. They
|
|
27
|
-
* are literals rather than looked up, because a scene has to build in a
|
|
28
|
-
* headless render with no database in scope; a host that names its voices
|
|
29
|
-
* supplies its own colours instead.
|
|
30
|
-
*/
|
|
31
|
-
export const VOICE_PALETTE = [
|
|
32
|
-
"#6366f1",
|
|
33
|
-
"#14b8a6",
|
|
34
|
-
"#f59e0b",
|
|
35
|
-
"#ec4899",
|
|
36
|
-
"#22c55e",
|
|
37
|
-
"#3b82f6",
|
|
38
|
-
"#f97316",
|
|
39
|
-
"#a855f7",
|
|
40
|
-
] as const
|
|
41
|
-
|
|
42
|
-
/** The colour a voice takes, wrapping past the end of the palette. */
|
|
43
|
-
export function voiceColor(index: number): string {
|
|
44
|
-
return VOICE_PALETTE[
|
|
45
|
-
((index % VOICE_PALETTE.length) + VOICE_PALETTE.length) %
|
|
46
|
-
VOICE_PALETTE.length
|
|
47
|
-
]
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Where one bar sits and how tall it came out: its top-left corner and size, in
|
|
52
|
-
* the node's own pixels with **y pointing down** from the middle of the box.
|
|
53
|
-
* {@link paintBars} turns that into the centred, y-up rect the renderer draws.
|
|
54
|
-
*/
|
|
55
|
-
export interface BarBox {
|
|
56
|
-
x: number
|
|
57
|
-
y: number
|
|
58
|
-
width: number
|
|
59
|
-
height: number
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Lays a row of bars out across a box.
|
|
64
|
-
*
|
|
65
|
-
* The gap is stated as a *fraction of the pitch* rather than in pixels, and
|
|
66
|
-
* that is the one decision here worth stating out loud: the bar count is a
|
|
67
|
-
* control, so a gap in pixels would silently become a solid block at 200 bars
|
|
68
|
-
* and a picket fence at 20. As a fraction, the row keeps the same rhythm at
|
|
69
|
-
* every density — which is what lets the count be a slider anyone can drag.
|
|
70
|
-
*
|
|
71
|
-
* A bar is never thinner than a hairline and never shorter than one either: a
|
|
72
|
-
* silent passage has to read as "audio, and nothing here" rather than as a hole
|
|
73
|
-
* in the drawing.
|
|
74
|
-
*/
|
|
75
|
-
export function layOutBars(
|
|
76
|
-
bars: readonly EnvelopeBar[],
|
|
77
|
-
options: {
|
|
78
|
-
width: number
|
|
79
|
-
height: number
|
|
80
|
-
gap: number
|
|
81
|
-
align: WaveformAlignment
|
|
82
|
-
/** Multiplies every magnitude before it is clamped back into 0…1. */
|
|
83
|
-
gain: number
|
|
84
|
-
/** Fraction of the box a silent bar still occupies. */
|
|
85
|
-
floor: number
|
|
86
|
-
}
|
|
87
|
-
): BarBox[] {
|
|
88
|
-
const { width, height, gap, align, gain, floor } = options
|
|
89
|
-
const count = bars.length
|
|
90
|
-
if (count === 0 || width <= 0 || height <= 0) return []
|
|
91
|
-
|
|
92
|
-
const pitch = width / count
|
|
93
|
-
const barWidth = Math.max(1, pitch * (1 - clamp01(gap)))
|
|
94
|
-
// The tallest a bar may be. Centre-aligned bars grow both ways from the
|
|
95
|
-
// middle, so the *half* is what a magnitude of 1 fills.
|
|
96
|
-
const full = align === "center" ? height / 2 : height
|
|
97
|
-
|
|
98
|
-
const out: BarBox[] = new Array(count)
|
|
99
|
-
for (let i = 0; i < count; i++) {
|
|
100
|
-
const magnitude = clamp01(bars[i].magnitude * gain)
|
|
101
|
-
const reach = Math.max(full * clamp01(floor), full * magnitude)
|
|
102
|
-
const x = -width / 2 + pitch * i + (pitch - barWidth) / 2
|
|
103
|
-
|
|
104
|
-
// A hairline minimum, taken before the position so the bar stays inside the
|
|
105
|
-
// box: a silent bar 1px tall that kept the position of a zero-height one
|
|
106
|
-
// would hang half a pixel (centred) or a whole pixel (bottom) outside it.
|
|
107
|
-
const h = Math.max(1, align === "center" ? reach * 2 : reach)
|
|
108
|
-
out[i] =
|
|
109
|
-
align === "center"
|
|
110
|
-
? { x, y: -h / 2, width: barWidth, height: h }
|
|
111
|
-
: { x, y: height / 2 - h, width: barWidth, height: h }
|
|
112
|
-
}
|
|
113
|
-
return out
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Draws a run of bars in one paint.
|
|
118
|
-
*
|
|
119
|
-
* Batched by colour by the caller, and this is why: every `Graphics2D` handed to
|
|
120
|
-
* `ctx.draw` is a surface the renderer composites, so a bar apiece would be a
|
|
121
|
-
* thousand surfaces a frame. Chaining the rects of one colour into a single
|
|
122
|
-
* `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill
|
|
123
|
-
* maps across the whole run rather than restarting inside every bar, which is
|
|
124
|
-
* the behaviour anyone reaching for a gradient here actually wants.
|
|
125
|
-
*/
|
|
126
|
-
export function paintBars(
|
|
127
|
-
boxes: readonly BarBox[],
|
|
128
|
-
fill: Fill,
|
|
129
|
-
radius: number
|
|
130
|
-
): Graphics2D | null {
|
|
131
|
-
if (boxes.length === 0) return null
|
|
132
|
-
if ((fill as FillResolved[]).length === 0) return null
|
|
133
|
-
|
|
134
|
-
const graphics = new Graphics2D()
|
|
135
|
-
for (const box of boxes) {
|
|
136
|
-
graphics.rect({
|
|
137
|
-
x: box.x + box.width / 2,
|
|
138
|
-
// The box is stated y-down and the rect is drawn y-up. A centred bar is
|
|
139
|
-
// symmetric about the middle, so only a bottom-aligned one shows the flip.
|
|
140
|
-
y: -(box.y + box.height / 2),
|
|
141
|
-
width: box.width,
|
|
142
|
-
height: box.height,
|
|
143
|
-
// Never rounder than the bar is wide, or a thin bar becomes a lozenge
|
|
144
|
-
// whose ends taper away to nothing.
|
|
145
|
-
cornerRadius: Math.min(radius, box.width / 2, box.height / 2),
|
|
146
|
-
})
|
|
147
|
-
}
|
|
148
|
-
return graphics.fill(fill)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Groups bars by the paint they take, preserving order within each group.
|
|
153
|
-
*
|
|
154
|
-
* The seam that makes {@link paintBars} worth batching: a diarized waveform has
|
|
155
|
-
* as many colours as it has voices, not as many as it has bars, so a
|
|
156
|
-
* conversation of two people is two draws however long it runs.
|
|
157
|
-
*/
|
|
158
|
-
export function groupByPaint<K>(
|
|
159
|
-
boxes: readonly BarBox[],
|
|
160
|
-
keyOf: (index: number) => K
|
|
161
|
-
): Map<K, BarBox[]> {
|
|
162
|
-
const groups = new Map<K, BarBox[]>()
|
|
163
|
-
for (let i = 0; i < boxes.length; i++) {
|
|
164
|
-
const key = keyOf(i)
|
|
165
|
-
const run = groups.get(key)
|
|
166
|
-
if (run) run.push(boxes[i])
|
|
167
|
-
else groups.set(key, [boxes[i]])
|
|
168
|
-
}
|
|
169
|
-
return groups
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function clamp01(value: number): number {
|
|
173
|
-
return value < 0 ? 0 : value > 1 ? 1 : value
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* The {@link groupByPaint} key one bar of a live visualiser takes.
|
|
178
|
-
*
|
|
179
|
-
* Filtered to one voice, every bar is that voice regardless of `byVoice` or
|
|
180
|
-
* whose turn the bucket originally belonged to. That second part is the one
|
|
181
|
-
* worth spelling out: {@link forSpeaker} silences a bar by zeroing its
|
|
182
|
-
* magnitude but leaves `speaker` exactly as it was, so a bar sitting in the
|
|
183
|
-
* middle of a *different* voice's turn is still stamped with that voice's
|
|
184
|
-
* index — grouping by `byVoice`'s usual per-bar key would paint those silent
|
|
185
|
-
* stretches in whoever used to be talking there, in flickers too short to
|
|
186
|
-
* read as anything but noise. Forcing every bar under the filtered voice's own
|
|
187
|
-
* key is the true picture instead: the node is drawing exactly one person, on
|
|
188
|
-
* or off, never anyone else's colour.
|
|
189
|
-
*/
|
|
190
|
-
export function livePaintKey(
|
|
191
|
-
barSpeaker: number,
|
|
192
|
-
byVoice: boolean,
|
|
193
|
-
filteredVoice: number | null
|
|
194
|
-
): string {
|
|
195
|
-
if (filteredVoice !== null) return `voice:${filteredVoice}`
|
|
196
|
-
return byVoice ? `voice:${barSpeaker}` : "all"
|
|
197
|
-
}
|
|
1
|
+
import { Graphics2D, type Fill, type FillResolved } from "@motionscript/sdk"
|
|
2
|
+
|
|
3
|
+
import type { EnvelopeBar } from "./envelope"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Geometry and paint shared by the two waveform nodes.
|
|
7
|
+
*
|
|
8
|
+
* They draw the same object — a row of bars whose heights are loudness — and
|
|
9
|
+
* differ only in what decides the heights: the whole file for one, a window
|
|
10
|
+
* around the playhead for the other. Everything about *how a row of bars is
|
|
11
|
+
* laid out and painted* is therefore common, and lives here so the two cannot
|
|
12
|
+
* drift into looking like different components.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** How a bar is grown out of its baseline. */
|
|
16
|
+
export const WAVEFORM_ALIGNMENTS = ["center", "bottom"] as const
|
|
17
|
+
export type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number]
|
|
18
|
+
|
|
19
|
+
/** What the bars are shaped like. */
|
|
20
|
+
export const WAVEFORM_STYLES = ["bars", "line", "blocks"] as const
|
|
21
|
+
export type WaveformStyle = (typeof WAVEFORM_STYLES)[number]
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The colours voices are drawn in when nobody has assigned any.
|
|
25
|
+
*
|
|
26
|
+
* Eight hues that can be told apart at a glance, handed out by voice index. They
|
|
27
|
+
* are literals rather than looked up, because a scene has to build in a
|
|
28
|
+
* headless render with no database in scope; a host that names its voices
|
|
29
|
+
* supplies its own colours instead.
|
|
30
|
+
*/
|
|
31
|
+
export const VOICE_PALETTE = [
|
|
32
|
+
"#6366f1",
|
|
33
|
+
"#14b8a6",
|
|
34
|
+
"#f59e0b",
|
|
35
|
+
"#ec4899",
|
|
36
|
+
"#22c55e",
|
|
37
|
+
"#3b82f6",
|
|
38
|
+
"#f97316",
|
|
39
|
+
"#a855f7",
|
|
40
|
+
] as const
|
|
41
|
+
|
|
42
|
+
/** The colour a voice takes, wrapping past the end of the palette. */
|
|
43
|
+
export function voiceColor(index: number): string {
|
|
44
|
+
return VOICE_PALETTE[
|
|
45
|
+
((index % VOICE_PALETTE.length) + VOICE_PALETTE.length) %
|
|
46
|
+
VOICE_PALETTE.length
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Where one bar sits and how tall it came out: its top-left corner and size, in
|
|
52
|
+
* the node's own pixels with **y pointing down** from the middle of the box.
|
|
53
|
+
* {@link paintBars} turns that into the centred, y-up rect the renderer draws.
|
|
54
|
+
*/
|
|
55
|
+
export interface BarBox {
|
|
56
|
+
x: number
|
|
57
|
+
y: number
|
|
58
|
+
width: number
|
|
59
|
+
height: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Lays a row of bars out across a box.
|
|
64
|
+
*
|
|
65
|
+
* The gap is stated as a *fraction of the pitch* rather than in pixels, and
|
|
66
|
+
* that is the one decision here worth stating out loud: the bar count is a
|
|
67
|
+
* control, so a gap in pixels would silently become a solid block at 200 bars
|
|
68
|
+
* and a picket fence at 20. As a fraction, the row keeps the same rhythm at
|
|
69
|
+
* every density — which is what lets the count be a slider anyone can drag.
|
|
70
|
+
*
|
|
71
|
+
* A bar is never thinner than a hairline and never shorter than one either: a
|
|
72
|
+
* silent passage has to read as "audio, and nothing here" rather than as a hole
|
|
73
|
+
* in the drawing.
|
|
74
|
+
*/
|
|
75
|
+
export function layOutBars(
|
|
76
|
+
bars: readonly EnvelopeBar[],
|
|
77
|
+
options: {
|
|
78
|
+
width: number
|
|
79
|
+
height: number
|
|
80
|
+
gap: number
|
|
81
|
+
align: WaveformAlignment
|
|
82
|
+
/** Multiplies every magnitude before it is clamped back into 0…1. */
|
|
83
|
+
gain: number
|
|
84
|
+
/** Fraction of the box a silent bar still occupies. */
|
|
85
|
+
floor: number
|
|
86
|
+
}
|
|
87
|
+
): BarBox[] {
|
|
88
|
+
const { width, height, gap, align, gain, floor } = options
|
|
89
|
+
const count = bars.length
|
|
90
|
+
if (count === 0 || width <= 0 || height <= 0) return []
|
|
91
|
+
|
|
92
|
+
const pitch = width / count
|
|
93
|
+
const barWidth = Math.max(1, pitch * (1 - clamp01(gap)))
|
|
94
|
+
// The tallest a bar may be. Centre-aligned bars grow both ways from the
|
|
95
|
+
// middle, so the *half* is what a magnitude of 1 fills.
|
|
96
|
+
const full = align === "center" ? height / 2 : height
|
|
97
|
+
|
|
98
|
+
const out: BarBox[] = new Array(count)
|
|
99
|
+
for (let i = 0; i < count; i++) {
|
|
100
|
+
const magnitude = clamp01(bars[i].magnitude * gain)
|
|
101
|
+
const reach = Math.max(full * clamp01(floor), full * magnitude)
|
|
102
|
+
const x = -width / 2 + pitch * i + (pitch - barWidth) / 2
|
|
103
|
+
|
|
104
|
+
// A hairline minimum, taken before the position so the bar stays inside the
|
|
105
|
+
// box: a silent bar 1px tall that kept the position of a zero-height one
|
|
106
|
+
// would hang half a pixel (centred) or a whole pixel (bottom) outside it.
|
|
107
|
+
const h = Math.max(1, align === "center" ? reach * 2 : reach)
|
|
108
|
+
out[i] =
|
|
109
|
+
align === "center"
|
|
110
|
+
? { x, y: -h / 2, width: barWidth, height: h }
|
|
111
|
+
: { x, y: height / 2 - h, width: barWidth, height: h }
|
|
112
|
+
}
|
|
113
|
+
return out
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Draws a run of bars in one paint.
|
|
118
|
+
*
|
|
119
|
+
* Batched by colour by the caller, and this is why: every `Graphics2D` handed to
|
|
120
|
+
* `ctx.draw` is a surface the renderer composites, so a bar apiece would be a
|
|
121
|
+
* thousand surfaces a frame. Chaining the rects of one colour into a single
|
|
122
|
+
* `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill
|
|
123
|
+
* maps across the whole run rather than restarting inside every bar, which is
|
|
124
|
+
* the behaviour anyone reaching for a gradient here actually wants.
|
|
125
|
+
*/
|
|
126
|
+
export function paintBars(
|
|
127
|
+
boxes: readonly BarBox[],
|
|
128
|
+
fill: Fill,
|
|
129
|
+
radius: number
|
|
130
|
+
): Graphics2D | null {
|
|
131
|
+
if (boxes.length === 0) return null
|
|
132
|
+
if ((fill as FillResolved[]).length === 0) return null
|
|
133
|
+
|
|
134
|
+
const graphics = new Graphics2D()
|
|
135
|
+
for (const box of boxes) {
|
|
136
|
+
graphics.rect({
|
|
137
|
+
x: box.x + box.width / 2,
|
|
138
|
+
// The box is stated y-down and the rect is drawn y-up. A centred bar is
|
|
139
|
+
// symmetric about the middle, so only a bottom-aligned one shows the flip.
|
|
140
|
+
y: -(box.y + box.height / 2),
|
|
141
|
+
width: box.width,
|
|
142
|
+
height: box.height,
|
|
143
|
+
// Never rounder than the bar is wide, or a thin bar becomes a lozenge
|
|
144
|
+
// whose ends taper away to nothing.
|
|
145
|
+
cornerRadius: Math.min(radius, box.width / 2, box.height / 2),
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
return graphics.fill(fill)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Groups bars by the paint they take, preserving order within each group.
|
|
153
|
+
*
|
|
154
|
+
* The seam that makes {@link paintBars} worth batching: a diarized waveform has
|
|
155
|
+
* as many colours as it has voices, not as many as it has bars, so a
|
|
156
|
+
* conversation of two people is two draws however long it runs.
|
|
157
|
+
*/
|
|
158
|
+
export function groupByPaint<K>(
|
|
159
|
+
boxes: readonly BarBox[],
|
|
160
|
+
keyOf: (index: number) => K
|
|
161
|
+
): Map<K, BarBox[]> {
|
|
162
|
+
const groups = new Map<K, BarBox[]>()
|
|
163
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
164
|
+
const key = keyOf(i)
|
|
165
|
+
const run = groups.get(key)
|
|
166
|
+
if (run) run.push(boxes[i])
|
|
167
|
+
else groups.set(key, [boxes[i]])
|
|
168
|
+
}
|
|
169
|
+
return groups
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function clamp01(value: number): number {
|
|
173
|
+
return value < 0 ? 0 : value > 1 ? 1 : value
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The {@link groupByPaint} key one bar of a live visualiser takes.
|
|
178
|
+
*
|
|
179
|
+
* Filtered to one voice, every bar is that voice regardless of `byVoice` or
|
|
180
|
+
* whose turn the bucket originally belonged to. That second part is the one
|
|
181
|
+
* worth spelling out: {@link forSpeaker} silences a bar by zeroing its
|
|
182
|
+
* magnitude but leaves `speaker` exactly as it was, so a bar sitting in the
|
|
183
|
+
* middle of a *different* voice's turn is still stamped with that voice's
|
|
184
|
+
* index — grouping by `byVoice`'s usual per-bar key would paint those silent
|
|
185
|
+
* stretches in whoever used to be talking there, in flickers too short to
|
|
186
|
+
* read as anything but noise. Forcing every bar under the filtered voice's own
|
|
187
|
+
* key is the true picture instead: the node is drawing exactly one person, on
|
|
188
|
+
* or off, never anyone else's colour.
|
|
189
|
+
*/
|
|
190
|
+
export function livePaintKey(
|
|
191
|
+
barSpeaker: number,
|
|
192
|
+
byVoice: boolean,
|
|
193
|
+
filteredVoice: number | null
|
|
194
|
+
): string {
|
|
195
|
+
if (filteredVoice !== null) return `voice:${filteredVoice}`
|
|
196
|
+
return byVoice ? `voice:${barSpeaker}` : "all"
|
|
197
|
+
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/waveform/envelope.ts", "../../../src/waveform/shared.ts"],
|
|
4
|
-
"sourcesContent": ["/**\r\n * The shape of a recording, as the waveform nodes draw it.\r\n *\r\n * ## How an envelope reaches a node: the host supplies it\r\n *\r\n * Nothing in the engine, and nothing in this kit, decodes audio. A waveform node\r\n * does not read a `sound` node's `src`, declares no audio asset and is not tied\r\n * to a clip: it draws the {@link AudioEnvelope} it is handed in its `envelope`\r\n * prop. Decoding a file into an envelope is the host's job, done once per file\r\n * before the document is built. Where the recording sits on the audio lane is\r\n * the author's timing too, which is why `FullWaveform.follow` is opt-in.\r\n *\r\n * ### The JSON form\r\n *\r\n * A document holds the envelope as plain JSON, because a prop value in a\r\n * document is JSON. The typed arrays in {@link AudioEnvelope} are one spelling of\r\n * it; `ArrayLike<number>` also accepts the plain arrays a document stores:\r\n *\r\n * ```json\r\n * {\r\n * \"magnitude\": [0, 0.25, 0.5, 1, 0.5, 0],\r\n * \"duration\": 6,\r\n * \"speaker\": [0, 0, 0, 1, 1, -1],\r\n * \"speakers\": 2\r\n * }\r\n * ```\r\n *\r\n * `magnitude` is the peak loudness, `0`…`1`, of buckets that divide `duration`\r\n * seconds evenly. `speaker` is `null` when nobody was identified; otherwise it is parallel to `magnitude`, one voice\r\n * index per bucket, `-1` for \"nobody\". `speakers` is the voice count, `0` when\r\n * `speaker` is `null`. A `Float32Array` or `Int16Array` is not JSON-serialisable\r\n * (`JSON.stringify` writes an object of indices), so a host that holds the typed\r\n * form converts with `Array.from` before storing it in a document.\r\n *\r\n * ## Why the data is baked rather than listened to\r\n *\r\n * A scene has to render identically in the preview and in a headless export,\r\n * and an export has no audio device, no `AudioContext` and no playback to\r\n * analyse. So a \"live\" visualiser cannot read a Web Audio analyser: what it\r\n * reads is a *pre-computed envelope*, indexed by the scene clock. The picture is\r\n * then a pure function of time, which is what makes scrubbing backwards, jumping\r\n * to frame 900, and rendering out of order all produce the same frame.\r\n *\r\n * The decode happens in the host, once per file, and arrives here through the\r\n * build — the same arrangement a chart's rows and a protein's atoms already use,\r\n * and for the same reason: the build is synchronous and cannot await a fetch.\r\n *\r\n * ## Why magnitude rather than min/max\r\n *\r\n * An editor's waveform is drawn from per-bucket minima *and* maxima, because an\r\n * editor is a measuring instrument. These nodes are neither — they are a bar\r\n * chart of loudness that happens to be shaped like a recording — and audio is\r\n * near enough symmetric that a mirrored magnitude is indistinguishable at the\r\n * size a node draws. Half the numbers, and one array to reason about.\r\n */\r\n\r\n/**\r\n * A decoded recording, reduced to a uniform grid of loudness — plus, when the\r\n * host knows it, who is talking in each bucket.\r\n *\r\n * The speaker track is **not** something a decoder can produce; it comes from a\r\n * diarized transcript and is folded in by the host before the build. It lives\r\n * here rather than in a parallel structure because every reader of it is also a\r\n * reader of the magnitudes at the same index, and two arrays that must stay the\r\n * same length are better as one object than as two arguments.\r\n */\r\nexport interface AudioEnvelope {\r\n /**\r\n * Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. A\r\n * `Float32Array`, or the plain `number[]` a document stores.\r\n */\r\n magnitude: ArrayLike<number>\r\n /** The decoded length in seconds. */\r\n duration: number\r\n /**\r\n * Which voice is speaking in each bucket, as an index into the recording's\r\n * own numbering, or `-1` for \"nobody identified\". Parallel to\r\n * {@link magnitude}, or `null` when the track was never diarized.\r\n */\r\n speaker: ArrayLike<number> | null\r\n /** How many distinct voices the diarizer found. Zero when it did not run. */\r\n speakers: number\r\n}\r\n\r\n/** The envelope of a track that hasn't loaded — or isn't there. */\r\nexport const EMPTY_ENVELOPE: AudioEnvelope = {\r\n magnitude: new Float32Array(0),\r\n duration: 0,\r\n speaker: null,\r\n speakers: 0,\r\n}\r\n\r\n/** One drawn bar: how loud, and whose. */\r\nexport interface EnvelopeBar {\r\n /** 0…1. */\r\n magnitude: number\r\n /** The voice index, or -1. */\r\n speaker: number\r\n}\r\n\r\n/**\r\n * Resamples `[from, to)` seconds of an envelope down to `bars` columns.\r\n *\r\n * Peak-preserving rather than averaging, which is the whole difference between\r\n * a waveform and a blur: averaging a hundred buckets into one column flattens\r\n * every transient, and transients are what makes a drawn recording read as\r\n * speech rather than as noise. The speaker of a column is the speaker of its\r\n * *loudest* bucket, on the same reasoning — a column straddling a turn belongs\r\n * to whoever is actually audible in it.\r\n *\r\n * A window past either end of the recording yields silent, unattributed\r\n * columns rather than clamping onto the nearest real one. That matters for the\r\n * live visualiser, which is handed a window centred on the playhead and would\r\n * otherwise smear the first bucket across the whole run-up to a track's start.\r\n */\r\nexport function sampleEnvelope(\r\n envelope: AudioEnvelope,\r\n from: number,\r\n to: number,\r\n bars: number\r\n): EnvelopeBar[] {\r\n const count = Math.max(0, Math.floor(bars))\r\n const out: EnvelopeBar[] = new Array(count)\r\n for (let i = 0; i < count; i++) out[i] = { magnitude: 0, speaker: -1 }\r\n\r\n const buckets = envelope.magnitude.length\r\n if (count === 0 || buckets === 0 || envelope.duration <= 0) return out\r\n\r\n const perSecond = buckets / envelope.duration\r\n const span = (to - from) / count\r\n\r\n for (let i = 0; i < count; i++) {\r\n const start = (from + span * i) * perSecond\r\n const end = (from + span * (i + 1)) * perSecond\r\n\r\n // At least one bucket per column, so a window zoomed in past the grid draws\r\n // the sample it is standing on rather than an empty band.\r\n const first = Math.floor(start)\r\n const last = Math.max(first, Math.ceil(end) - 1)\r\n\r\n let loudest = 0\r\n let speaker = -1\r\n for (let b = first; b <= last; b++) {\r\n if (b < 0 || b >= buckets) continue\r\n const value = envelope.magnitude[b]\r\n if (value < loudest) continue\r\n loudest = value\r\n speaker = envelope.speaker ? envelope.speaker[b] : -1\r\n }\r\n\r\n out[i] = { magnitude: loudest, speaker }\r\n }\r\n\r\n return out\r\n}\r\n\r\n/**\r\n * The same window, with everything that isn't `voice` silenced.\r\n *\r\n * The point of the speaker filter, and the reason it silences rather than\r\n * *drops*: a podcast visualiser filtered to one host should go quiet while the\r\n * other one talks, not compress their turn out of existence. Keeping the bars\r\n * and zeroing them is what makes the picture read as \"this person is not\r\n * speaking right now\" instead of as a jump cut.\r\n *\r\n * `voice` of `null` returns the bars untouched, which is the unfiltered node.\r\n */\r\nexport function forSpeaker(\r\n bars: EnvelopeBar[],\r\n voice: number | null\r\n): EnvelopeBar[] {\r\n if (voice === null) return bars\r\n return bars.map((bar) =>\r\n bar.speaker === voice ? bar : { magnitude: 0, speaker: bar.speaker }\r\n )\r\n}\r\n", "import { Graphics2D, type Fill, type FillResolved } from \"@motionscript/sdk\"\r\n\r\nimport type { EnvelopeBar } from \"./envelope\"\r\n\r\n/**\r\n * Geometry and paint shared by the two waveform nodes.\r\n *\r\n * They draw the same object — a row of bars whose heights are loudness — and\r\n * differ only in what decides the heights: the whole file for one, a window\r\n * around the playhead for the other. Everything about *how a row of bars is\r\n * laid out and painted* is therefore common, and lives here so the two cannot\r\n * drift into looking like different components.\r\n */\r\n\r\n/** How a bar is grown out of its baseline. */\r\nexport const WAVEFORM_ALIGNMENTS = [\"center\", \"bottom\"] as const\r\nexport type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number]\r\n\r\n/** What the bars are shaped like. */\r\nexport const WAVEFORM_STYLES = [\"bars\", \"line\", \"blocks\"] as const\r\nexport type WaveformStyle = (typeof WAVEFORM_STYLES)[number]\r\n\r\n/**\r\n * The colours voices are drawn in when nobody has assigned any.\r\n *\r\n * Eight hues that can be told apart at a glance, handed out by voice index. They\r\n * are literals rather than looked up, because a scene has to build in a\r\n * headless render with no database in scope; a host that names its voices\r\n * supplies its own colours instead.\r\n */\r\nexport const VOICE_PALETTE = [\r\n \"#6366f1\",\r\n \"#14b8a6\",\r\n \"#f59e0b\",\r\n \"#ec4899\",\r\n \"#22c55e\",\r\n \"#3b82f6\",\r\n \"#f97316\",\r\n \"#a855f7\",\r\n] as const\r\n\r\n/** The colour a voice takes, wrapping past the end of the palette. */\r\nexport function voiceColor(index: number): string {\r\n return VOICE_PALETTE[\r\n ((index % VOICE_PALETTE.length) + VOICE_PALETTE.length) %\r\n VOICE_PALETTE.length\r\n ]\r\n}\r\n\r\n/**\r\n * Where one bar sits and how tall it came out: its top-left corner and size, in\r\n * the node's own pixels with **y pointing down** from the middle of the box.\r\n * {@link paintBars} turns that into the centred, y-up rect the renderer draws.\r\n */\r\nexport interface BarBox {\r\n x: number\r\n y: number\r\n width: number\r\n height: number\r\n}\r\n\r\n/**\r\n * Lays a row of bars out across a box.\r\n *\r\n * The gap is stated as a *fraction of the pitch* rather than in pixels, and\r\n * that is the one decision here worth stating out loud: the bar count is a\r\n * control, so a gap in pixels would silently become a solid block at 200 bars\r\n * and a picket fence at 20. As a fraction, the row keeps the same rhythm at\r\n * every density — which is what lets the count be a slider anyone can drag.\r\n *\r\n * A bar is never thinner than a hairline and never shorter than one either: a\r\n * silent passage has to read as \"audio, and nothing here\" rather than as a hole\r\n * in the drawing.\r\n */\r\nexport function layOutBars(\r\n bars: readonly EnvelopeBar[],\r\n options: {\r\n width: number\r\n height: number\r\n gap: number\r\n align: WaveformAlignment\r\n /** Multiplies every magnitude before it is clamped back into 0…1. */\r\n gain: number\r\n /** Fraction of the box a silent bar still occupies. */\r\n floor: number\r\n }\r\n): BarBox[] {\r\n const { width, height, gap, align, gain, floor } = options\r\n const count = bars.length\r\n if (count === 0 || width <= 0 || height <= 0) return []\r\n\r\n const pitch = width / count\r\n const barWidth = Math.max(1, pitch * (1 - clamp01(gap)))\r\n // The tallest a bar may be. Centre-aligned bars grow both ways from the\r\n // middle, so the *half* is what a magnitude of 1 fills.\r\n const full = align === \"center\" ? height / 2 : height\r\n\r\n const out: BarBox[] = new Array(count)\r\n for (let i = 0; i < count; i++) {\r\n const magnitude = clamp01(bars[i].magnitude * gain)\r\n const reach = Math.max(full * clamp01(floor), full * magnitude)\r\n const x = -width / 2 + pitch * i + (pitch - barWidth) / 2\r\n\r\n // A hairline minimum, taken before the position so the bar stays inside the\r\n // box: a silent bar 1px tall that kept the position of a zero-height one\r\n // would hang half a pixel (centred) or a whole pixel (bottom) outside it.\r\n const h = Math.max(1, align === \"center\" ? reach * 2 : reach)\r\n out[i] =\r\n align === \"center\"\r\n ? { x, y: -h / 2, width: barWidth, height: h }\r\n : { x, y: height / 2 - h, width: barWidth, height: h }\r\n }\r\n return out\r\n}\r\n\r\n/**\r\n * Draws a run of bars in one paint.\r\n *\r\n * Batched by colour by the caller, and this is why: every `Graphics2D` handed to\r\n * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a\r\n * thousand surfaces a frame. Chaining the rects of one colour into a single\r\n * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill\r\n * maps across the whole run rather than restarting inside every bar, which is\r\n * the behaviour anyone reaching for a gradient here actually wants.\r\n */\r\nexport function paintBars(\r\n boxes: readonly BarBox[],\r\n fill: Fill,\r\n radius: number\r\n): Graphics2D | null {\r\n if (boxes.length === 0) return null\r\n if ((fill as FillResolved[]).length === 0) return null\r\n\r\n const graphics = new Graphics2D()\r\n for (const box of boxes) {\r\n graphics.rect({\r\n x: box.x + box.width / 2,\r\n // The box is stated y-down and the rect is drawn y-up. A centred bar is\r\n // symmetric about the middle, so only a bottom-aligned one shows the flip.\r\n y: -(box.y + box.height / 2),\r\n width: box.width,\r\n height: box.height,\r\n // Never rounder than the bar is wide, or a thin bar becomes a lozenge\r\n // whose ends taper away to nothing.\r\n cornerRadius: Math.min(radius, box.width / 2, box.height / 2),\r\n })\r\n }\r\n return graphics.fill(fill)\r\n}\r\n\r\n/**\r\n * Groups bars by the paint they take, preserving order within each group.\r\n *\r\n * The seam that makes {@link paintBars} worth batching: a diarized waveform has\r\n * as many colours as it has voices, not as many as it has bars, so a\r\n * conversation of two people is two draws however long it runs.\r\n */\r\nexport function groupByPaint<K>(\r\n boxes: readonly BarBox[],\r\n keyOf: (index: number) => K\r\n): Map<K, BarBox[]> {\r\n const groups = new Map<K, BarBox[]>()\r\n for (let i = 0; i < boxes.length; i++) {\r\n const key = keyOf(i)\r\n const run = groups.get(key)\r\n if (run) run.push(boxes[i])\r\n else groups.set(key, [boxes[i]])\r\n }\r\n return groups\r\n}\r\n\r\nexport function clamp01(value: number): number {\r\n return value < 0 ? 0 : value > 1 ? 1 : value\r\n}\r\n\r\n/**\r\n * The {@link groupByPaint} key one bar of a live visualiser takes.\r\n *\r\n * Filtered to one voice, every bar is that voice regardless of `byVoice` or\r\n * whose turn the bucket originally belonged to. That second part is the one\r\n * worth spelling out: {@link forSpeaker} silences a bar by zeroing its\r\n * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the\r\n * middle of a *different* voice's turn is still stamped with that voice's\r\n * index — grouping by `byVoice`'s usual per-bar key would paint those silent\r\n * stretches in whoever used to be talking there, in flickers too short to\r\n * read as anything but noise. Forcing every bar under the filtered voice's own\r\n * key is the true picture instead: the node is drawing exactly one person, on\r\n * or off, never anyone else's colour.\r\n */\r\nexport function livePaintKey(\r\n barSpeaker: number,\r\n byVoice: boolean,\r\n filteredVoice: number | null\r\n): string {\r\n if (filteredVoice !== null) return `voice:${filteredVoice}`\r\n return byVoice ? `voice:${barSpeaker}` : \"all\"\r\n}\r\n"],
|
|
5
|
-
"mappings": "AAqFO,IAAMA,EAAgC,CAC3C,UAAW,IAAI,aAAa,CAAC,EAC7B,SAAU,EACV,QAAS,KACT,SAAU,GA0BN,SAAUC,EACdC,EACAC,EACAC,EACAC,EAAY,CAEZ,IAAMC,EAAQ,KAAK,IAAI,EAAG,KAAK,MAAMD,CAAI,CAAC,EACpCE,EAAqB,IAAI,MAAMD,CAAK,EAC1C,QAAS,EAAI,EAAG,EAAIA,EAAO,IAAKC,EAAI,CAAC,EAAI,CAAE,UAAW,EAAG,QAAS,EAAE,EAEpE,IAAMC,EAAUN,EAAS,UAAU,OACnC,GAAII,IAAU,GAAKE,IAAY,GAAKN,EAAS,UAAY,EAAG,OAAOK,EAEnE,IAAME,EAAYD,EAAUN,EAAS,SAC/BQ,GAAQN,EAAKD,GAAQG,EAE3B,QAAS,EAAI,EAAG,EAAIA,EAAO,IAAK,CAC9B,IAAMK,GAASR,EAAOO,EAAO,GAAKD,EAC5BG,GAAOT,EAAOO,GAAQ,EAAI,IAAMD,EAIhCI,EAAQ,KAA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EAAQkB,EAAa,CACnC,OAAOA,EAAQ,EAAI,EAAIA,EAAQ,EAAI,EAAIA,CACzC,CAgBM,SAAUC,EACdC,EACAC,EACAC,EAA4B,CAE5B,OAAIA,IAAkB,KAAa,SAASA,CAAa,GAClDD,EAAU,SAASD,CAAU,GAAK,KAC3C",
|
|
6
|
-
"names": ["EMPTY_ENVELOPE", "sampleEnvelope", "envelope", "from", "to", "bars", "count", "out", "buckets", "perSecond", "span", "start", "end", "first", "last", "loudest", "speaker", "b", "value", "forSpeaker", "voice", "bar", "Graphics2D", "WAVEFORM_ALIGNMENTS", "WAVEFORM_STYLES", "VOICE_PALETTE", "voiceColor", "index", "layOutBars", "bars", "options", "width", "height", "gap", "align", "gain", "floor", "count", "pitch", "barWidth", "clamp01", "full", "out", "i", "magnitude", "reach", "x", "paintBars", "boxes", "fill", "radius", "graphics", "box", "groupByPaint", "keyOf", "groups", "key", "run", "value", "livePaintKey", "barSpeaker", "byVoice", "filteredVoice"]
|
|
7
|
-
}
|