@motionscript/audio 0.0.0-stage → 0.1.0-alpha.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +201 -0
  3. package/dist/browser/chunks/chunk-U5OUQJHQ.js +2 -0
  4. package/dist/browser/chunks/chunk-U5OUQJHQ.js.map +7 -0
  5. package/dist/browser/index.js +2 -0
  6. package/dist/browser/index.js.map +7 -0
  7. package/dist/browser/kit.js +2 -0
  8. package/dist/browser/kit.js.map +7 -0
  9. package/dist/browser/manifest.json +12 -0
  10. package/dist/index.d.ts +3 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +3 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/kit.d.ts +13 -0
  15. package/dist/kit.d.ts.map +1 -0
  16. package/dist/kit.js +11 -0
  17. package/dist/kit.js.map +1 -0
  18. package/dist/nodes.d.ts +19 -0
  19. package/dist/nodes.d.ts.map +1 -0
  20. package/dist/nodes.js +19 -0
  21. package/dist/nodes.js.map +1 -0
  22. package/dist/waveform/envelope.d.ts +87 -0
  23. package/dist/waveform/envelope.d.ts.map +1 -0
  24. package/dist/waveform/envelope.js +96 -0
  25. package/dist/waveform/envelope.js.map +1 -0
  26. package/dist/waveform/full-waveform.d.ts +104 -0
  27. package/dist/waveform/full-waveform.d.ts.map +1 -0
  28. package/dist/waveform/full-waveform.js +193 -0
  29. package/dist/waveform/full-waveform.js.map +1 -0
  30. package/dist/waveform/index.d.ts +23 -0
  31. package/dist/waveform/index.d.ts.map +1 -0
  32. package/dist/waveform/index.js +19 -0
  33. package/dist/waveform/index.js.map +1 -0
  34. package/dist/waveform/live-waveform.d.ts +99 -0
  35. package/dist/waveform/live-waveform.d.ts.map +1 -0
  36. package/dist/waveform/live-waveform.js +191 -0
  37. package/dist/waveform/live-waveform.js.map +1 -0
  38. package/dist/waveform/shared.d.ts +96 -0
  39. package/dist/waveform/shared.d.ts.map +1 -0
  40. package/dist/waveform/shared.js +150 -0
  41. package/dist/waveform/shared.js.map +1 -0
  42. package/package.json +69 -3
  43. package/registry.json +28 -0
  44. package/src/index.ts +2 -0
  45. package/src/kit.ts +22 -0
  46. package/src/nodes.ts +19 -0
  47. package/src/waveform/envelope.ts +143 -0
  48. package/src/waveform/full-waveform.ts +218 -0
  49. package/src/waveform/index.ts +30 -0
  50. package/src/waveform/live-waveform.ts +221 -0
  51. package/src/waveform/shared.ts +194 -0
  52. package/README.md +0 -4
@@ -0,0 +1,191 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Node2D, node, fillOps, property, } from "@motionscript/core";
8
+ import { declarePaints } from "@motionscript/core/component";
9
+ import { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope, } from "./envelope.js";
10
+ import { clamp01, groupByPaint, layOutBars, livePaintKey, paintBars, voiceColor, } from "./shared.js";
11
+ /**
12
+ * A **live audio visualiser**: bars that move with the recording as it plays.
13
+ *
14
+ * The thing every podcast-to-video tool puts beside the artwork. It is not a
15
+ * picture of the file — that is {@link FullWaveform} — it is a picture of *this
16
+ * moment*, and what it is for is making a static frame of a talking head feel
17
+ * like it is playing.
18
+ *
19
+ * ## "Live" without listening
20
+ *
21
+ * Nothing here reads an audio device. The bars are sampled out of a baked
22
+ * envelope at the node's own elapsed time, which is what makes the visualiser
23
+ * survive the thing it exists for: an **export**, where there is no playback to
24
+ * analyse and frames may be rendered out of order or in parallel. A frame is a
25
+ * pure function of its time, so scrubbing back and forth over the visualiser
26
+ * redraws exactly what it drew the first time — which an analyser node, by
27
+ * construction, cannot promise. See {@link AudioEnvelope}.
28
+ *
29
+ * ## The speaker filter
30
+ *
31
+ * {@link speaker} narrows the node to one voice of a diarized recording, and it
32
+ * is the reason this node knows about transcripts at all. Two visualisers beside
33
+ * two headshots, each filtered to its own host, is a podcast edit that would
34
+ * otherwise be done by hand for every turn — and because the filter *silences*
35
+ * rather than skips (see {@link forSpeaker}), the quiet host visibly stops
36
+ * talking instead of vanishing. Nothing about that requires the words: only who
37
+ * is talking when, which is what the diarizer produces.
38
+ *
39
+ * A filter naming a voice the recording doesn't have draws a resting row rather
40
+ * than everything — a picture of "this person never speaks here", which is
41
+ * true, where falling back to the unfiltered mix would be a silent lie about
42
+ * whose voice you were watching.
43
+ */
44
+ let LiveWaveform = class LiveWaveform extends Node2D {
45
+ declareAssets(assets) {
46
+ super.declareAssets(assets);
47
+ declarePaints(this, assets);
48
+ }
49
+ /**
50
+ * The second of the recording the visualiser is showing.
51
+ *
52
+ * Off the node's own clock rather than the scene's, which is what "from the
53
+ * moment this node appeared" means — the same clock motion-script's own
54
+ * `Video` times its picture from, and the same one that makes a node dropped
55
+ * halfway through a scene start its recording at its entrance rather than
56
+ * partway in.
57
+ */
58
+ get at() {
59
+ return this.offset + (this.playing ? Math.max(0, this.time.elapsed) : 0);
60
+ }
61
+ renderSelf(ctx) {
62
+ const rect = this.layoutBounds;
63
+ const width = rect?.width ?? 0;
64
+ const height = rect?.height ?? 0;
65
+ if (width <= 0 || height <= 0)
66
+ return;
67
+ const count = Math.max(1, Math.round(this.bars));
68
+ const span = Math.max(0.05, this.window);
69
+ const at = this.at;
70
+ const voice = this.speaker < 0 ? null : Math.round(this.speaker);
71
+ // The window is centred on the playhead, so the bars run *toward* the
72
+ // middle and away again — the arrangement every mixer's meter uses, and the
73
+ // one that reads as sound arriving rather than as a strip scrolling past.
74
+ const sampled = forSpeaker(sampleEnvelope(this.envelope, at - span / 2, at + span / 2, count), voice);
75
+ const shaped = this.style === "line" ? taper(sampled) : sampled;
76
+ const boxes = layOutBars(shaped, {
77
+ width,
78
+ height,
79
+ gap: this.style === "blocks" ? 0.12 : this.gap,
80
+ align: this.align,
81
+ gain: this.gain,
82
+ floor: this.floor,
83
+ });
84
+ if (boxes.length === 0)
85
+ return;
86
+ const groups = groupByPaint(boxes, (i) => livePaintKey(shaped[i].speaker, this.byVoice, voice));
87
+ for (const [key, run] of groups) {
88
+ const graphics = paintBars(run, this.paintFor(key), this.radius);
89
+ if (graphics)
90
+ ctx.draw(graphics);
91
+ }
92
+ }
93
+ /** See {@link FullWaveform.paintFor} — a voice is a hue, never a gradient. */
94
+ paintFor(key) {
95
+ if (key === "all")
96
+ return this.fill;
97
+ const voice = Number(key.slice("voice:".length));
98
+ if (!Number.isFinite(voice) || voice < 0)
99
+ return this.fill;
100
+ return this.voiceColors[voice] ?? voiceColor(voice);
101
+ }
102
+ };
103
+ __decorate([
104
+ property({ default: EMPTY_ENVELOPE })
105
+ ], LiveWaveform.prototype, "envelope", void 0);
106
+ __decorate([
107
+ property({ default: 48 })
108
+ ], LiveWaveform.prototype, "bars", void 0);
109
+ __decorate([
110
+ property({ default: 0.35 })
111
+ ], LiveWaveform.prototype, "gap", void 0);
112
+ __decorate([
113
+ property({ default: 4 })
114
+ ], LiveWaveform.prototype, "radius", void 0);
115
+ __decorate([
116
+ property({ default: 3 })
117
+ ], LiveWaveform.prototype, "window", void 0);
118
+ __decorate([
119
+ property({ default: 0 })
120
+ ], LiveWaveform.prototype, "offset", void 0);
121
+ __decorate([
122
+ property({ default: true })
123
+ ], LiveWaveform.prototype, "playing", void 0);
124
+ __decorate([
125
+ property({ default: 1.4 })
126
+ ], LiveWaveform.prototype, "gain", void 0);
127
+ __decorate([
128
+ property({ default: 0.04 })
129
+ ], LiveWaveform.prototype, "floor", void 0);
130
+ __decorate([
131
+ property({ default: "center" })
132
+ ], LiveWaveform.prototype, "align", void 0);
133
+ __decorate([
134
+ property({ default: "bars" })
135
+ ], LiveWaveform.prototype, "style", void 0);
136
+ __decorate([
137
+ property({ default: -1 })
138
+ ], LiveWaveform.prototype, "speaker", void 0);
139
+ __decorate([
140
+ property({ default: false })
141
+ ], LiveWaveform.prototype, "byVoice", void 0);
142
+ __decorate([
143
+ property({ default: [] })
144
+ ], LiveWaveform.prototype, "voiceColors", void 0);
145
+ __decorate([
146
+ property({
147
+ default: "#6366f1",
148
+ mapper: fillOps.resolve,
149
+ tween: fillOps.lerp,
150
+ })
151
+ ], LiveWaveform.prototype, "fill", void 0);
152
+ LiveWaveform = __decorate([
153
+ node({
154
+ key: "liveWaveform",
155
+ parentKey: "node",
156
+ forkable: true,
157
+ layout: {
158
+ children: "freeform",
159
+ defaultWidthMode: "fixed",
160
+ defaultHeightMode: "fixed",
161
+ acceptsChildren: true,
162
+ },
163
+ seed: {
164
+ width: 420,
165
+ height: 220,
166
+ },
167
+ })
168
+ ], LiveWaveform);
169
+ export { LiveWaveform };
170
+ /**
171
+ * Rolls the ends of a window off toward silence.
172
+ *
173
+ * What separates the `line` style from `bars`: a hard-edged window makes the
174
+ * loud bar entering at the right edge appear from nothing, which reads as a
175
+ * glitch rather than as sound arriving. Fading the outer fifth turns the same
176
+ * data into a shape that grows out of the baseline — the profile a spectrum
177
+ * display has, and the reason it looks like one object rather than a row of
178
+ * separate meters.
179
+ */
180
+ function taper(bars) {
181
+ const count = bars.length;
182
+ if (count < 3)
183
+ return [...bars];
184
+ const edge = Math.max(1, Math.floor(count / 5));
185
+ return bars.map((bar, i) => {
186
+ const fromEnd = Math.min(i, count - 1 - i);
187
+ const weight = clamp01(fromEnd / edge);
188
+ return { magnitude: bar.magnitude * weight, speaker: bar.speaker };
189
+ });
190
+ }
191
+ //# sourceMappingURL=live-waveform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-waveform.js","sourceRoot":"","sources":["../../src/waveform/live-waveform.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,MAAM,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AE,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;QAC1C,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAAA;QACtC,OAAO,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAA;IACpE,CAAC,CAAC,CAAA;AACJ,CAAC","sourcesContent":["import {\n Node2D,\n node,\n fillOps,\n property,\n type AssetScope,\n type Fill,\n type Node2DProps,\n type RenderContext2D,\n} from \"@motionscript/core\"\n\nimport { declarePaints } from \"@motionscript/core/component\"\nimport {\n EMPTY_ENVELOPE,\n forSpeaker,\n sampleEnvelope,\n type AudioEnvelope,\n type EnvelopeBar,\n} from \"./envelope\"\nimport {\n clamp01,\n groupByPaint,\n layOutBars,\n livePaintKey,\n paintBars,\n voiceColor,\n type WaveformAlignment,\n type WaveformStyle,\n} from \"./shared\"\n\nexport interface LiveWaveformProps extends Node2DProps {\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\n envelope: AudioEnvelope\n /** How many bars the visualiser is made of. */\n bars: number\n /** Space between bars, as a fraction of the pitch. */\n gap: number\n radius: number\n /** How many seconds of audio are on screen at once. */\n window: number\n /** Seconds into the file the node's first frame shows. */\n offset: number\n /** Advance with the scene clock. Off freezes the picture at {@link offset}. */\n playing: boolean\n /** Multiplies every magnitude before it is clamped. */\n gain: number\n /** How much of the box a silent bar still occupies, `0`–`1`. */\n floor: number\n align: WaveformAlignment\n style: WaveformStyle\n /**\n * Show only this voice's activity, by index into the recording's own\n * numbering. `-1` — the default — is every voice.\n */\n speaker: number\n /** Colour each bar by whoever is speaking in it. */\n byVoice: boolean\n /** A colour per voice index. Short or empty falls back to the palette. */\n voiceColors: string[]\n fill: Fill\n}\n\n/**\n * A **live audio visualiser**: bars that move with the recording as it plays.\n *\n * The thing every podcast-to-video tool puts beside the artwork. It is not a\n * picture of the file — that is {@link FullWaveform} — it is a picture of *this\n * moment*, and what it is for is making a static frame of a talking head feel\n * like it is playing.\n *\n * ## \"Live\" without listening\n *\n * Nothing here reads an audio device. The bars are sampled out of a baked\n * envelope at the node's own elapsed time, which is what makes the visualiser\n * survive the thing it exists for: an **export**, where there is no playback to\n * analyse and frames may be rendered out of order or in parallel. A frame is a\n * pure function of its time, so scrubbing back and forth over the visualiser\n * redraws exactly what it drew the first time — which an analyser node, by\n * construction, cannot promise. See {@link AudioEnvelope}.\n *\n * ## The speaker filter\n *\n * {@link speaker} narrows the node to one voice of a diarized recording, and it\n * is the reason this node knows about transcripts at all. Two visualisers beside\n * two headshots, each filtered to its own host, is a podcast edit that would\n * otherwise be done by hand for every turn — and because the filter *silences*\n * rather than skips (see {@link forSpeaker}), the quiet host visibly stops\n * talking instead of vanishing. Nothing about that requires the words: only who\n * is talking when, which is what the diarizer produces.\n *\n * A filter naming a voice the recording doesn't have draws a resting row rather\n * than everything — a picture of \"this person never speaks here\", which is\n * true, where falling back to the unfiltered mix would be a silent lie about\n * whose voice you were watching.\n */\n@node({\n key: \"liveWaveform\",\n parentKey: \"node\",\n forkable: true,\n layout: {\n children: \"freeform\",\n defaultWidthMode: \"fixed\",\n defaultHeightMode: \"fixed\",\n acceptsChildren: true,\n },\n seed: {\n width: 420,\n height: 220,\n },\n})\nexport class LiveWaveform extends Node2D<LiveWaveformProps> {\n @property({ default: EMPTY_ENVELOPE }) declare envelope: AudioEnvelope\n @property({ default: 48 }) declare bars: number\n @property({ default: 0.35 }) declare gap: number\n @property({ default: 4 }) declare radius: number\n @property({ default: 3 }) declare window: number\n @property({ default: 0 }) declare offset: number\n @property({ default: true }) declare playing: boolean\n @property({ default: 1.4 }) declare gain: number\n @property({ default: 0.04 }) declare floor: number\n @property({ default: \"center\" }) declare align: WaveformAlignment\n @property({ default: \"bars\" }) declare style: WaveformStyle\n @property({ default: -1 }) declare speaker: number\n @property({ default: false }) declare byVoice: boolean\n @property({ default: [] }) declare voiceColors: string[]\n\n @property({\n default: \"#6366f1\",\n mapper: fillOps.resolve,\n tween: fillOps.lerp,\n })\n declare fill: Fill\n\n override declareAssets(assets: AssetScope): void {\n super.declareAssets(assets)\n declarePaints(this, assets)\n }\n\n /**\n * The second of the recording the visualiser is showing.\n *\n * Off the node's own clock rather than the scene's, which is what \"from the\n * moment this node appeared\" means — the same clock motion-script's own\n * `Video` times its picture from, and the same one that makes a node dropped\n * halfway through a scene start its recording at its entrance rather than\n * partway in.\n */\n private get at(): number {\n return this.offset + (this.playing ? Math.max(0, this.time.elapsed) : 0)\n }\n\n protected renderSelf(ctx: RenderContext2D): void {\n const rect = this.layoutBounds\n const width = rect?.width ?? 0\n const height = rect?.height ?? 0\n if (width <= 0 || height <= 0) return\n\n const count = Math.max(1, Math.round(this.bars))\n const span = Math.max(0.05, this.window)\n const at = this.at\n const voice = this.speaker < 0 ? null : Math.round(this.speaker)\n\n // The window is centred on the playhead, so the bars run *toward* the\n // middle and away again — the arrangement every mixer's meter uses, and the\n // one that reads as sound arriving rather than as a strip scrolling past.\n const sampled = forSpeaker(\n sampleEnvelope(this.envelope, at - span / 2, at + span / 2, count),\n voice\n )\n\n const shaped = this.style === \"line\" ? taper(sampled) : sampled\n const boxes = layOutBars(shaped, {\n width,\n height,\n gap: this.style === \"blocks\" ? 0.12 : this.gap,\n align: this.align,\n gain: this.gain,\n floor: this.floor,\n })\n if (boxes.length === 0) return\n\n const groups = groupByPaint(boxes, (i) =>\n livePaintKey(shaped[i].speaker, this.byVoice, voice)\n )\n\n for (const [key, run] of groups) {\n const graphics = paintBars(run, this.paintFor(key), this.radius)\n if (graphics) ctx.draw(graphics)\n }\n }\n\n /** See {@link FullWaveform.paintFor} — a voice is a hue, never a gradient. */\n private paintFor(key: string): Fill {\n if (key === \"all\") return this.fill\n const voice = Number(key.slice(\"voice:\".length))\n if (!Number.isFinite(voice) || voice < 0) return this.fill\n return this.voiceColors[voice] ?? voiceColor(voice)\n }\n}\n\n/**\n * Rolls the ends of a window off toward silence.\n *\n * What separates the `line` style from `bars`: a hard-edged window makes the\n * loud bar entering at the right edge appear from nothing, which reads as a\n * glitch rather than as sound arriving. Fading the outer fifth turns the same\n * data into a shape that grows out of the baseline — the profile a spectrum\n * display has, and the reason it looks like one object rather than a row of\n * separate meters.\n */\nfunction taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {\n const count = bars.length\n if (count < 3) return [...bars]\n const edge = Math.max(1, Math.floor(count / 5))\n\n return bars.map((bar, i) => {\n const fromEnd = Math.min(i, count - 1 - i)\n const weight = clamp01(fromEnd / edge)\n return { magnitude: bar.magnitude * weight, speaker: bar.speaker }\n })\n}\n"]}
@@ -0,0 +1,96 @@
1
+ import { Graphics2D, type Fill } from "@motionscript/core";
2
+ import type { EnvelopeBar } from "./envelope.js";
3
+ /**
4
+ * Geometry and paint shared by the two waveform nodes.
5
+ *
6
+ * They draw the same object — a row of bars whose heights are loudness — and
7
+ * differ only in what decides the heights: the whole file for one, a window
8
+ * around the playhead for the other. Everything about *how a row of bars is
9
+ * laid out and painted* is therefore common, and lives here so the two cannot
10
+ * drift into looking like different components.
11
+ */
12
+ /** How a bar is grown out of its baseline. */
13
+ export declare const WAVEFORM_ALIGNMENTS: readonly ["center", "bottom"];
14
+ export type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number];
15
+ /** What the bars are shaped like. */
16
+ export declare const WAVEFORM_STYLES: readonly ["bars", "line", "blocks"];
17
+ export type WaveformStyle = (typeof WAVEFORM_STYLES)[number];
18
+ /**
19
+ * The colours voices are drawn in when nobody has assigned any.
20
+ *
21
+ * The same eight the app's speaker library suggests, in the same order, so a
22
+ * transcript whose voices are still unnamed already draws in the colours the
23
+ * transcript panel is showing them in. Duplicated as literals rather than
24
+ * imported because `scene-core` knows nothing about the app's speaker table —
25
+ * and must not, since a scene has to build in a headless render with no
26
+ * database in scope.
27
+ */
28
+ export declare const VOICE_PALETTE: readonly ["#6366f1", "#14b8a6", "#f59e0b", "#ec4899", "#22c55e", "#3b82f6", "#f97316", "#a855f7"];
29
+ /** The colour a voice takes, wrapping past the end of the palette. */
30
+ export declare function voiceColor(index: number): string;
31
+ /** Where one bar sits and how tall it came out. */
32
+ export interface BarBox {
33
+ x: number;
34
+ y: number;
35
+ width: number;
36
+ height: number;
37
+ }
38
+ /**
39
+ * Lays a row of bars out across a box.
40
+ *
41
+ * The gap is stated as a *fraction of the pitch* rather than in pixels, and
42
+ * that is the one decision here worth stating out loud: the bar count is a
43
+ * control, so a gap in pixels would silently become a solid block at 200 bars
44
+ * and a picket fence at 20. As a fraction, the row keeps the same rhythm at
45
+ * every density — which is what lets the count be a slider anyone can drag.
46
+ *
47
+ * A bar is never thinner than a hairline and never shorter than one either: a
48
+ * silent passage has to read as "audio, and nothing here" rather than as a hole
49
+ * in the drawing, exactly as it does on the timeline's clip bars.
50
+ */
51
+ export declare function layOutBars(bars: readonly EnvelopeBar[], options: {
52
+ width: number;
53
+ height: number;
54
+ gap: number;
55
+ align: WaveformAlignment;
56
+ /** Multiplies every magnitude before it is clamped back into 0…1. */
57
+ gain: number;
58
+ /** Fraction of the box a silent bar still occupies. */
59
+ floor: number;
60
+ }): BarBox[];
61
+ /**
62
+ * Draws a run of bars in one paint.
63
+ *
64
+ * Batched by colour by the caller, and this is why: every `Graphics2D` handed to
65
+ * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a
66
+ * thousand surfaces a frame. Chaining the rects of one colour into a single
67
+ * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill
68
+ * maps across the whole run rather than restarting inside every bar, which is
69
+ * the behaviour anyone reaching for a gradient here actually wants.
70
+ */
71
+ export declare function paintBars(boxes: readonly BarBox[], fill: Fill, radius: number): Graphics2D | null;
72
+ /**
73
+ * Groups bars by the paint they take, preserving order within each group.
74
+ *
75
+ * The seam that makes {@link paintBars} worth batching: a diarized waveform has
76
+ * as many colours as it has voices, not as many as it has bars, so a
77
+ * conversation of two people is two draws however long it runs.
78
+ */
79
+ export declare function groupByPaint<K>(boxes: readonly BarBox[], keyOf: (index: number) => K): Map<K, BarBox[]>;
80
+ export declare function clamp01(value: number): number;
81
+ /**
82
+ * The {@link groupByPaint} key one bar of a live visualiser takes.
83
+ *
84
+ * Filtered to one voice, every bar is that voice regardless of `byVoice` or
85
+ * whose turn the bucket originally belonged to. That second part is the one
86
+ * worth spelling out: {@link forSpeaker} silences a bar by zeroing its
87
+ * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the
88
+ * middle of a *different* voice's turn is still stamped with that voice's
89
+ * index — grouping by `byVoice`'s usual per-bar key would paint those silent
90
+ * stretches in whoever used to be talking there, in flickers too short to
91
+ * read as anything but noise. Forcing every bar under the filtered voice's own
92
+ * key is the true picture instead: the node is drawing exactly one person, on
93
+ * or off, never anyone else's colour.
94
+ */
95
+ export declare function livePaintKey(barSpeaker: number, byVoice: boolean, filteredVoice: number | null): string;
96
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/waveform/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,IAAI,EAAqB,MAAM,oBAAoB,CAAA;AAE7E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;;;;;;;GAQG;AAEH,8CAA8C;AAC9C,eAAO,MAAM,mBAAmB,+BAAgC,CAAA;AAChE,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAA;AAEpE,qCAAqC;AACrC,eAAO,MAAM,eAAe,qCAAsC,CAAA;AAClE,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAA;AAE5D;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,mGAShB,CAAA;AAEV,sEAAsE;AACtE,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKhD;AAED,mDAAmD;AACnD,MAAM,WAAW,MAAM;IACrB,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,OAAO,EAAE;IACP,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,iBAAiB,CAAA;IACxB,qEAAqE;IACrE,IAAI,EAAE,MAAM,CAAA;IACZ,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAA;CACd,GACA,MAAM,EAAE,CA4BV;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,MAAM,GACb,UAAU,GAAG,IAAI,CAiBnB;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC5B,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,GAC1B,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CASlB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAC1B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,OAAO,EAChB,aAAa,EAAE,MAAM,GAAG,IAAI,GAC3B,MAAM,CAGR"}
@@ -0,0 +1,150 @@
1
+ import { Graphics2D } from "@motionscript/core";
2
+ /**
3
+ * Geometry and paint shared by the two waveform nodes.
4
+ *
5
+ * They draw the same object — a row of bars whose heights are loudness — and
6
+ * differ only in what decides the heights: the whole file for one, a window
7
+ * around the playhead for the other. Everything about *how a row of bars is
8
+ * laid out and painted* is therefore common, and lives here so the two cannot
9
+ * drift into looking like different components.
10
+ */
11
+ /** How a bar is grown out of its baseline. */
12
+ export const WAVEFORM_ALIGNMENTS = ["center", "bottom"];
13
+ /** What the bars are shaped like. */
14
+ export const WAVEFORM_STYLES = ["bars", "line", "blocks"];
15
+ /**
16
+ * The colours voices are drawn in when nobody has assigned any.
17
+ *
18
+ * The same eight the app's speaker library suggests, in the same order, so a
19
+ * transcript whose voices are still unnamed already draws in the colours the
20
+ * transcript panel is showing them in. Duplicated as literals rather than
21
+ * imported because `scene-core` knows nothing about the app's speaker table —
22
+ * and must not, since a scene has to build in a headless render with no
23
+ * database in scope.
24
+ */
25
+ export const VOICE_PALETTE = [
26
+ "#6366f1",
27
+ "#14b8a6",
28
+ "#f59e0b",
29
+ "#ec4899",
30
+ "#22c55e",
31
+ "#3b82f6",
32
+ "#f97316",
33
+ "#a855f7",
34
+ ];
35
+ /** The colour a voice takes, wrapping past the end of the palette. */
36
+ export function voiceColor(index) {
37
+ return VOICE_PALETTE[((index % VOICE_PALETTE.length) + VOICE_PALETTE.length) %
38
+ VOICE_PALETTE.length];
39
+ }
40
+ /**
41
+ * Lays a row of bars out across a box.
42
+ *
43
+ * The gap is stated as a *fraction of the pitch* rather than in pixels, and
44
+ * that is the one decision here worth stating out loud: the bar count is a
45
+ * control, so a gap in pixels would silently become a solid block at 200 bars
46
+ * and a picket fence at 20. As a fraction, the row keeps the same rhythm at
47
+ * every density — which is what lets the count be a slider anyone can drag.
48
+ *
49
+ * A bar is never thinner than a hairline and never shorter than one either: a
50
+ * silent passage has to read as "audio, and nothing here" rather than as a hole
51
+ * in the drawing, exactly as it does on the timeline's clip bars.
52
+ */
53
+ export function layOutBars(bars, options) {
54
+ const { width, height, gap, align, gain, floor } = options;
55
+ const count = bars.length;
56
+ if (count === 0 || width <= 0 || height <= 0)
57
+ return [];
58
+ const pitch = width / count;
59
+ const barWidth = Math.max(1, pitch * (1 - clamp01(gap)));
60
+ // The tallest a bar may be. Centre-aligned bars grow both ways from the
61
+ // middle, so the *half* is what a magnitude of 1 fills.
62
+ const full = align === "center" ? height / 2 : height;
63
+ const out = new Array(count);
64
+ for (let i = 0; i < count; i++) {
65
+ const magnitude = clamp01(bars[i].magnitude * gain);
66
+ const reach = Math.max(full * clamp01(floor), full * magnitude);
67
+ const x = -width / 2 + pitch * i + (pitch - barWidth) / 2;
68
+ out[i] =
69
+ align === "center"
70
+ ? { x, y: -reach, width: barWidth, height: Math.max(1, reach * 2) }
71
+ : {
72
+ x,
73
+ y: height / 2 - reach,
74
+ width: barWidth,
75
+ height: Math.max(1, reach),
76
+ };
77
+ }
78
+ return out;
79
+ }
80
+ /**
81
+ * Draws a run of bars in one paint.
82
+ *
83
+ * Batched by colour by the caller, and this is why: every `Graphics2D` handed to
84
+ * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a
85
+ * thousand surfaces a frame. Chaining the rects of one colour into a single
86
+ * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill
87
+ * maps across the whole run rather than restarting inside every bar, which is
88
+ * the behaviour anyone reaching for a gradient here actually wants.
89
+ */
90
+ export function paintBars(boxes, fill, radius) {
91
+ if (boxes.length === 0)
92
+ return null;
93
+ if (fill.length === 0)
94
+ return null;
95
+ const graphics = new Graphics2D();
96
+ for (const box of boxes) {
97
+ graphics.rect({
98
+ x: box.x + box.width / 2,
99
+ y: box.y + box.height / 2,
100
+ width: box.width,
101
+ height: box.height,
102
+ // Never rounder than the bar is wide, or a thin bar becomes a lozenge
103
+ // whose ends taper away to nothing.
104
+ cornerRadius: Math.min(radius, box.width / 2, box.height / 2),
105
+ });
106
+ }
107
+ return graphics.fill(fill);
108
+ }
109
+ /**
110
+ * Groups bars by the paint they take, preserving order within each group.
111
+ *
112
+ * The seam that makes {@link paintBars} worth batching: a diarized waveform has
113
+ * as many colours as it has voices, not as many as it has bars, so a
114
+ * conversation of two people is two draws however long it runs.
115
+ */
116
+ export function groupByPaint(boxes, keyOf) {
117
+ const groups = new Map();
118
+ for (let i = 0; i < boxes.length; i++) {
119
+ const key = keyOf(i);
120
+ const run = groups.get(key);
121
+ if (run)
122
+ run.push(boxes[i]);
123
+ else
124
+ groups.set(key, [boxes[i]]);
125
+ }
126
+ return groups;
127
+ }
128
+ export function clamp01(value) {
129
+ return value < 0 ? 0 : value > 1 ? 1 : value;
130
+ }
131
+ /**
132
+ * The {@link groupByPaint} key one bar of a live visualiser takes.
133
+ *
134
+ * Filtered to one voice, every bar is that voice regardless of `byVoice` or
135
+ * whose turn the bucket originally belonged to. That second part is the one
136
+ * worth spelling out: {@link forSpeaker} silences a bar by zeroing its
137
+ * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the
138
+ * middle of a *different* voice's turn is still stamped with that voice's
139
+ * index — grouping by `byVoice`'s usual per-bar key would paint those silent
140
+ * stretches in whoever used to be talking there, in flickers too short to
141
+ * read as anything but noise. Forcing every bar under the filtered voice's own
142
+ * key is the true picture instead: the node is drawing exactly one person, on
143
+ * or off, never anyone else's colour.
144
+ */
145
+ export function livePaintKey(barSpeaker, byVoice, filteredVoice) {
146
+ if (filteredVoice !== null)
147
+ return `voice:${filteredVoice}`;
148
+ return byVoice ? `voice:${barSpeaker}` : "all";
149
+ }
150
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/waveform/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAgC,MAAM,oBAAoB,CAAA;AAI7E;;;;;;;;GAQG;AAEH,8CAA8C;AAC9C,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAU,CAAA;AAGhE,qCAAqC;AACrC,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAU,CAAA;AAGlE;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;I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Graphics2D, type Fill, type FillResolved } from \"@motionscript/core\"\n\nimport type { EnvelopeBar } from \"./envelope\"\n\n/**\n * Geometry and paint shared by the two waveform nodes.\n *\n * They draw the same object — a row of bars whose heights are loudness — and\n * differ only in what decides the heights: the whole file for one, a window\n * around the playhead for the other. Everything about *how a row of bars is\n * laid out and painted* is therefore common, and lives here so the two cannot\n * drift into looking like different components.\n */\n\n/** How a bar is grown out of its baseline. */\nexport const WAVEFORM_ALIGNMENTS = [\"center\", \"bottom\"] as const\nexport type WaveformAlignment = (typeof WAVEFORM_ALIGNMENTS)[number]\n\n/** What the bars are shaped like. */\nexport const WAVEFORM_STYLES = [\"bars\", \"line\", \"blocks\"] as const\nexport type WaveformStyle = (typeof WAVEFORM_STYLES)[number]\n\n/**\n * The colours voices are drawn in when nobody has assigned any.\n *\n * The same eight the app's speaker library suggests, in the same order, so a\n * transcript whose voices are still unnamed already draws in the colours the\n * transcript panel is showing them in. Duplicated as literals rather than\n * imported because `scene-core` knows nothing about the app's speaker table —\n * and must not, since a scene has to build in a headless render with no\n * database in scope.\n */\nexport const VOICE_PALETTE = [\n \"#6366f1\",\n \"#14b8a6\",\n \"#f59e0b\",\n \"#ec4899\",\n \"#22c55e\",\n \"#3b82f6\",\n \"#f97316\",\n \"#a855f7\",\n] as const\n\n/** The colour a voice takes, wrapping past the end of the palette. */\nexport function voiceColor(index: number): string {\n return VOICE_PALETTE[\n ((index % VOICE_PALETTE.length) + VOICE_PALETTE.length) %\n VOICE_PALETTE.length\n ]\n}\n\n/** Where one bar sits and how tall it came out. */\nexport interface BarBox {\n x: number\n y: number\n width: number\n height: number\n}\n\n/**\n * Lays a row of bars out across a box.\n *\n * The gap is stated as a *fraction of the pitch* rather than in pixels, and\n * that is the one decision here worth stating out loud: the bar count is a\n * control, so a gap in pixels would silently become a solid block at 200 bars\n * and a picket fence at 20. As a fraction, the row keeps the same rhythm at\n * every density — which is what lets the count be a slider anyone can drag.\n *\n * A bar is never thinner than a hairline and never shorter than one either: a\n * silent passage has to read as \"audio, and nothing here\" rather than as a hole\n * in the drawing, exactly as it does on the timeline's clip bars.\n */\nexport function layOutBars(\n bars: readonly EnvelopeBar[],\n options: {\n width: number\n height: number\n gap: number\n align: WaveformAlignment\n /** Multiplies every magnitude before it is clamped back into 0…1. */\n gain: number\n /** Fraction of the box a silent bar still occupies. */\n floor: number\n }\n): BarBox[] {\n const { width, height, gap, align, gain, floor } = options\n const count = bars.length\n if (count === 0 || width <= 0 || height <= 0) return []\n\n const pitch = width / count\n const barWidth = Math.max(1, pitch * (1 - clamp01(gap)))\n // The tallest a bar may be. Centre-aligned bars grow both ways from the\n // middle, so the *half* is what a magnitude of 1 fills.\n const full = align === \"center\" ? height / 2 : height\n\n const out: BarBox[] = new Array(count)\n for (let i = 0; i < count; i++) {\n const magnitude = clamp01(bars[i].magnitude * gain)\n const reach = Math.max(full * clamp01(floor), full * magnitude)\n const x = -width / 2 + pitch * i + (pitch - barWidth) / 2\n\n out[i] =\n align === \"center\"\n ? { x, y: -reach, width: barWidth, height: Math.max(1, reach * 2) }\n : {\n x,\n y: height / 2 - reach,\n width: barWidth,\n height: Math.max(1, reach),\n }\n }\n return out\n}\n\n/**\n * Draws a run of bars in one paint.\n *\n * Batched by colour by the caller, and this is why: every `Graphics2D` handed to\n * `ctx.draw` is a surface the renderer composites, so a bar apiece would be a\n * thousand surfaces a frame. Chaining the rects of one colour into a single\n * `Graphics2D` before filling makes it one — and, as a bonus, a gradient fill\n * maps across the whole run rather than restarting inside every bar, which is\n * the behaviour anyone reaching for a gradient here actually wants.\n */\nexport function paintBars(\n boxes: readonly BarBox[],\n fill: Fill,\n radius: number\n): Graphics2D | null {\n if (boxes.length === 0) return null\n if ((fill as FillResolved[]).length === 0) return null\n\n const graphics = new Graphics2D()\n for (const box of boxes) {\n graphics.rect({\n x: box.x + box.width / 2,\n y: box.y + box.height / 2,\n width: box.width,\n height: box.height,\n // Never rounder than the bar is wide, or a thin bar becomes a lozenge\n // whose ends taper away to nothing.\n cornerRadius: Math.min(radius, box.width / 2, box.height / 2),\n })\n }\n return graphics.fill(fill)\n}\n\n/**\n * Groups bars by the paint they take, preserving order within each group.\n *\n * The seam that makes {@link paintBars} worth batching: a diarized waveform has\n * as many colours as it has voices, not as many as it has bars, so a\n * conversation of two people is two draws however long it runs.\n */\nexport function groupByPaint<K>(\n boxes: readonly BarBox[],\n keyOf: (index: number) => K\n): Map<K, BarBox[]> {\n const groups = new Map<K, BarBox[]>()\n for (let i = 0; i < boxes.length; i++) {\n const key = keyOf(i)\n const run = groups.get(key)\n if (run) run.push(boxes[i])\n else groups.set(key, [boxes[i]])\n }\n return groups\n}\n\nexport function clamp01(value: number): number {\n return value < 0 ? 0 : value > 1 ? 1 : value\n}\n\n/**\n * The {@link groupByPaint} key one bar of a live visualiser takes.\n *\n * Filtered to one voice, every bar is that voice regardless of `byVoice` or\n * whose turn the bucket originally belonged to. That second part is the one\n * worth spelling out: {@link forSpeaker} silences a bar by zeroing its\n * magnitude but leaves `speaker` exactly as it was, so a bar sitting in the\n * middle of a *different* voice's turn is still stamped with that voice's\n * index — grouping by `byVoice`'s usual per-bar key would paint those silent\n * stretches in whoever used to be talking there, in flickers too short to\n * read as anything but noise. Forcing every bar under the filtered voice's own\n * key is the true picture instead: the node is drawing exactly one person, on\n * or off, never anyone else's colour.\n */\nexport function livePaintKey(\n barSpeaker: number,\n byVoice: boolean,\n filteredVoice: number | null\n): string {\n if (filteredVoice !== null) return `voice:${filteredVoice}`\n return byVoice ? `voice:${barSpeaker}` : \"all\"\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,72 @@
1
1
  {
2
2
  "name": "@motionscript/audio",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-alpha.0",
4
+ "type": "module",
5
+ "description": "Waveform components for Motion Script: a full recording drawn as bars, and a live meter that responds to a signal.",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/motionscript-dev/motionscript.git",
9
+ "directory": "packages/components/audio"
10
+ },
11
+ "homepage": "https://motionscript.dev",
12
+ "bugs": {
13
+ "url": "https://github.com/motionscript-dev/motionscript/issues"
14
+ },
15
+ "main": "dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "sideEffects": false,
18
+ "files": [
19
+ "dist",
20
+ "src",
21
+ "registry.json",
22
+ "CHANGELOG.md",
23
+ "!dist/**/*.tsbuildinfo",
24
+ "!src/**/tests/**",
25
+ "!src/**/*.test.ts",
26
+ "!src/**/*.test.tsx",
27
+ "!src/**/*.fixtures.ts"
28
+ ],
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "default": "./dist/index.js"
36
+ },
37
+ "./kit": {
38
+ "types": "./dist/kit.d.ts",
39
+ "default": "./dist/kit.js"
40
+ }
41
+ },
42
+ "keywords": [
43
+ "motion-script",
44
+ "motion script",
45
+ "audio",
46
+ "waveform",
47
+ "animation"
48
+ ],
49
+ "license": "Apache-2.0",
50
+ "peerDependencies": {
51
+ "@motionscript/core": "^0.1.0-alpha.0"
52
+ },
53
+ "devDependencies": {
54
+ "@types/node": "^25.6.2",
55
+ "eslint": "^9.39.4",
56
+ "eslint-import-resolver-typescript": "^4.4.4",
57
+ "eslint-plugin-import-x": "^4.16.1",
58
+ "tsc-alias": "^1.8.16",
59
+ "typescript": "^6.0.3",
60
+ "typescript-eslint": "^8.48.0",
61
+ "vitest": "^4.1.5",
62
+ "@motionscript/core": "0.1.0-alpha.0"
63
+ },
64
+ "scripts": {
65
+ "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json --resolve-full-paths && node ../../../scripts/build-browser.mjs",
66
+ "dev": "tsc -p tsconfig.build.json --watch & tsc-alias -p tsconfig.build.json --resolve-full-paths -w",
67
+ "clean": "rimraf --glob dist .turbo *.tsbuildinfo",
68
+ "test": "vitest --passWithNoTests",
69
+ "lint": "eslint .",
70
+ "typecheck": "tsc -p tsconfig.json --noEmit"
71
+ }
6
72
  }
package/registry.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "kit": {
3
+ "barrel": "src/kit.ts",
4
+ "specifier": "@motionscript/audio/kit"
5
+ },
6
+ "items": [
7
+ {
8
+ "name": "full-waveform",
9
+ "description": "A whole recording drawn as bars, with a played portion that advances.",
10
+ "root": "src/waveform",
11
+ "files": [
12
+ "envelope.ts",
13
+ "full-waveform.ts",
14
+ "shared.ts"
15
+ ]
16
+ },
17
+ {
18
+ "name": "live-waveform",
19
+ "description": "A visualiser whose bars move with the recording as it plays, coloured by voice.",
20
+ "root": "src/waveform",
21
+ "files": [
22
+ "envelope.ts",
23
+ "live-waveform.ts",
24
+ "shared.ts"
25
+ ]
26
+ }
27
+ ]
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./waveform"
2
+ export { NODES } from "./nodes";
package/src/kit.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * What the two waveform nodes share, importable without copying it.
3
+ *
4
+ * A registry item copies `envelope.ts` and `shared.ts` beside its node: they
5
+ * hold no class and no registered state, so a fork owning its own copy splits
6
+ * no identity. This is the same surface for a host that samples envelopes or
7
+ * lays bars out itself, and for a fork that keeps only its node file.
8
+ */
9
+ export { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from "./waveform/envelope"
10
+ export type { AudioEnvelope, EnvelopeBar } from "./waveform/envelope"
11
+ export {
12
+ VOICE_PALETTE,
13
+ WAVEFORM_ALIGNMENTS,
14
+ WAVEFORM_STYLES,
15
+ clamp01,
16
+ groupByPaint,
17
+ layOutBars,
18
+ livePaintKey,
19
+ paintBars,
20
+ voiceColor,
21
+ } from "./waveform/shared"
22
+ export type { BarBox, WaveformAlignment, WaveformStyle } from "./waveform/shared"
package/src/nodes.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { FullWaveform } from "./waveform/full-waveform";
2
+ import { LiveWaveform } from "./waveform/live-waveform";
3
+
4
+ /**
5
+ * Every node type this package publishes.
6
+ *
7
+ * A host registers these by handing the array to an engine —
8
+ * `new Engine(platform, { nodes: NODES })` — which reads each class's `@node()`
9
+ * key. The decorator only *declares* that key: nothing is registered by the mere
10
+ * act of importing a module, so a registry holds exactly what its owner asked
11
+ * for and two engines in one process can differ about it.
12
+ *
13
+ * Listing class **values** is also what survives bundling. This package declares
14
+ * `sideEffects: false`, which licenses a bundler to drop a module nothing
15
+ * imports a value from, and a document names a node type by string — so
16
+ * `import "./waveform/full-waveform"` for its side effect is exactly what gets shaken out, where an
17
+ * array of bindings is a data dependency that cannot be.
18
+ */
19
+ export const NODES = [FullWaveform, LiveWaveform];