@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/README.md
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
# @motionscript/audio
|
|
2
|
-
|
|
3
|
-
Waveform components for [Motion Script](https://motionscript.dev): `fullWaveform`
|
|
4
|
-
draws a whole recording as bars with a played portion that advances, and
|
|
5
|
-
`liveWaveform` draws a meter that moves with playback.
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import { FullWaveform, LiveWaveform } from '@motionscript/audio';
|
|
9
|
-
|
|
10
|
-
export const nodes = [FullWaveform, LiveWaveform];
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## The host supplies the envelope
|
|
14
|
-
|
|
15
|
-
Nothing in the engine or in this package decodes audio. Both nodes draw the
|
|
16
|
-
loudness `envelope` they are handed in their `envelope` prop. A render has no
|
|
17
|
-
audio device and no analyser, so the picture has to be a pure function of time:
|
|
18
|
-
scrubbing backwards, jumping ahead and exporting frames out of order all draw the
|
|
19
|
-
same frame. Decoding a file into an envelope is the host's job, done once per
|
|
20
|
-
file before the document is built. The nodes declare no audio source and are not
|
|
21
|
-
tied to a clip: where a recording sits on the audio lane is the author's timing,
|
|
22
|
-
which is why `follow` is opt-in.
|
|
23
|
-
|
|
24
|
-
### The JSON form
|
|
25
|
-
|
|
26
|
-
A document stores the envelope as plain JSON:
|
|
27
|
-
|
|
28
|
-
```json
|
|
29
|
-
{
|
|
30
|
-
"magnitude": [0, 0.25, 0.5, 1, 0.5, 0],
|
|
31
|
-
"duration": 6,
|
|
32
|
-
"speaker": [0, 0, 0, 1, 1, -1],
|
|
33
|
-
"speakers": 2
|
|
34
|
-
}
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
- `magnitude`: peak loudness per bucket, `0` to `1`. The buckets divide
|
|
38
|
-
`duration` seconds evenly.
|
|
39
|
-
- `duration`: the recording's length in seconds.
|
|
40
|
-
- `speaker`: one voice index per bucket (parallel to `magnitude`), `-1` for
|
|
41
|
-
nobody identified, or `null` when the recording was never diarized.
|
|
42
|
-
- `speakers`: how many distinct voices there are; `0` when `speaker` is `null`.
|
|
43
|
-
|
|
44
|
-
`Float32Array` and `Int16Array` are accepted too, but `JSON.stringify` writes a
|
|
45
|
-
typed array as an object of indices, so a host that holds the typed form converts
|
|
46
|
-
with `Array.from` before it puts the envelope in a document.
|
|
47
|
-
|
|
48
|
-
## Nodes
|
|
49
|
-
|
|
50
|
-
| Key | Made for (w x h) | Commands | Notes |
|
|
51
|
-
|---|---|---|---|
|
|
52
|
-
| `fullWaveform` | 900 x 160 | `sweep` | whole recording; `progress` is the played share, or with `follow` the node's elapsed time over `envelope.duration`. `sweep` is a `to` of `progress` to 1 |
|
|
53
|
-
| `liveWaveform` | 420 x 220 | none | a `window` of seconds centred on `offset + elapsed` (or frozen at `offset` when `playing` is off); `style` is `bars`, `line` or `blocks`; `speaker` (-1 is every voice) filters to one voice |
|
|
54
|
-
|
|
55
|
-
Both take `byVoice` and `voiceColors` (a missing colour takes `VOICE_PALETTE`, eight
|
|
56
|
-
hues), and `align` of `center` or `bottom`. A voice colour is a plain string, never a
|
|
57
|
-
gradient or an image, so a voice declares no asset.
|
|
58
|
-
|
|
59
|
-
- **A column is the loudest bucket in it, not the mean** (`sampleEnvelope`), so
|
|
60
|
-
transients survive downsampling.
|
|
61
|
-
- **A silent bar is a hairline, never a hole**: every bar is at least 1 px wide and
|
|
62
|
-
tall, and the minimum height is taken before the bar is positioned, so it stays
|
|
63
|
-
inside the box.
|
|
64
|
-
- **Bars are batched by paint**, so two voices are two draws however long the
|
|
65
|
-
recording.
|
|
66
|
-
- **The frame is a function of the document and the node's clock**, so a seek redraws
|
|
67
|
-
what was drawn the first time.
|
|
68
|
-
|
|
69
|
-
## Entry points
|
|
70
|
-
|
|
71
|
-
`@motionscript/audio` exports the two nodes, `NODES`, the envelope type and
|
|
72
|
-
`EMPTY_ENVELOPE`, and the pure helpers (`sampleEnvelope`, `forSpeaker`, `layOutBars`,
|
|
73
|
-
`paintBars`, `groupByPaint`, `voiceColor`). `@motionscript/audio/kit` (`src/kit.ts`)
|
|
74
|
-
exports the same helpers plus `clamp01` and `livePaintKey`.
|
|
75
|
-
`
|
|
76
|
-
`shared.ts` and the node); the copy's imports stay relative, so none is rewritten to
|
|
77
|
-
the kit specifier.
|
|
78
|
-
|
|
79
|
-
## Usage
|
|
80
|
-
|
|
81
|
-
```bash
|
|
82
|
-
npm install @motionscript/audio @motionscript/sdk
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## Development
|
|
86
|
-
|
|
87
|
-
From the monorepo root:
|
|
88
|
-
|
|
89
|
-
```bash
|
|
90
|
-
pnpm --filter @motionscript/audio build
|
|
91
|
-
pnpm --filter @motionscript/audio test
|
|
92
|
-
```
|
|
1
|
+
# @motionscript/audio
|
|
2
|
+
|
|
3
|
+
Waveform components for [Motion Script](https://motionscript.dev): `fullWaveform`
|
|
4
|
+
draws a whole recording as bars with a played portion that advances, and
|
|
5
|
+
`liveWaveform` draws a meter that moves with playback.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { FullWaveform, LiveWaveform } from '@motionscript/audio';
|
|
9
|
+
|
|
10
|
+
export const nodes = [FullWaveform, LiveWaveform];
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## The host supplies the envelope
|
|
14
|
+
|
|
15
|
+
Nothing in the engine or in this package decodes audio. Both nodes draw the
|
|
16
|
+
loudness `envelope` they are handed in their `envelope` prop. A render has no
|
|
17
|
+
audio device and no analyser, so the picture has to be a pure function of time:
|
|
18
|
+
scrubbing backwards, jumping ahead and exporting frames out of order all draw the
|
|
19
|
+
same frame. Decoding a file into an envelope is the host's job, done once per
|
|
20
|
+
file before the document is built. The nodes declare no audio source and are not
|
|
21
|
+
tied to a clip: where a recording sits on the audio lane is the author's timing,
|
|
22
|
+
which is why `follow` is opt-in.
|
|
23
|
+
|
|
24
|
+
### The JSON form
|
|
25
|
+
|
|
26
|
+
A document stores the envelope as plain JSON:
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"magnitude": [0, 0.25, 0.5, 1, 0.5, 0],
|
|
31
|
+
"duration": 6,
|
|
32
|
+
"speaker": [0, 0, 0, 1, 1, -1],
|
|
33
|
+
"speakers": 2
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- `magnitude`: peak loudness per bucket, `0` to `1`. The buckets divide
|
|
38
|
+
`duration` seconds evenly.
|
|
39
|
+
- `duration`: the recording's length in seconds.
|
|
40
|
+
- `speaker`: one voice index per bucket (parallel to `magnitude`), `-1` for
|
|
41
|
+
nobody identified, or `null` when the recording was never diarized.
|
|
42
|
+
- `speakers`: how many distinct voices there are; `0` when `speaker` is `null`.
|
|
43
|
+
|
|
44
|
+
`Float32Array` and `Int16Array` are accepted too, but `JSON.stringify` writes a
|
|
45
|
+
typed array as an object of indices, so a host that holds the typed form converts
|
|
46
|
+
with `Array.from` before it puts the envelope in a document.
|
|
47
|
+
|
|
48
|
+
## Nodes
|
|
49
|
+
|
|
50
|
+
| Key | Made for (w x h) | Commands | Notes |
|
|
51
|
+
|---|---|---|---|
|
|
52
|
+
| `fullWaveform` | 900 x 160 | `sweep` | whole recording; `progress` is the played share, or with `follow` the node's elapsed time over `envelope.duration`. `sweep` is a `to` of `progress` to 1 |
|
|
53
|
+
| `liveWaveform` | 420 x 220 | none | a `window` of seconds centred on `offset + elapsed` (or frozen at `offset` when `playing` is off); `style` is `bars`, `line` or `blocks`; `speaker` (-1 is every voice) filters to one voice |
|
|
54
|
+
|
|
55
|
+
Both take `byVoice` and `voiceColors` (a missing colour takes `VOICE_PALETTE`, eight
|
|
56
|
+
hues), and `align` of `center` or `bottom`. A voice colour is a plain string, never a
|
|
57
|
+
gradient or an image, so a voice declares no asset.
|
|
58
|
+
|
|
59
|
+
- **A column is the loudest bucket in it, not the mean** (`sampleEnvelope`), so
|
|
60
|
+
transients survive downsampling.
|
|
61
|
+
- **A silent bar is a hairline, never a hole**: every bar is at least 1 px wide and
|
|
62
|
+
tall, and the minimum height is taken before the bar is positioned, so it stays
|
|
63
|
+
inside the box.
|
|
64
|
+
- **Bars are batched by paint**, so two voices are two draws however long the
|
|
65
|
+
recording.
|
|
66
|
+
- **The frame is a function of the document and the node's clock**, so a seek redraws
|
|
67
|
+
what was drawn the first time.
|
|
68
|
+
|
|
69
|
+
## Entry points
|
|
70
|
+
|
|
71
|
+
`@motionscript/audio` exports the two nodes, `NODES`, the envelope type and
|
|
72
|
+
`EMPTY_ENVELOPE`, and the pure helpers (`sampleEnvelope`, `forSpeaker`, `layOutBars`,
|
|
73
|
+
`paintBars`, `groupByPaint`, `voiceColor`). `@motionscript/audio/kit` (`src/kit.ts`)
|
|
74
|
+
exports the same helpers plus `clamp01` and `livePaintKey`. A copy of `full-waveform` or
|
|
75
|
+
`live-waveform` (`registry.json`) takes three files into the project (`envelope.ts`,
|
|
76
|
+
`shared.ts` and the node); the copy's imports stay relative, so none is rewritten to
|
|
77
|
+
the kit specifier.
|
|
78
|
+
|
|
79
|
+
## Usage
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
npm install @motionscript/audio @motionscript/sdk
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
From the monorepo root:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
pnpm --filter @motionscript/audio build
|
|
91
|
+
pnpm --filter @motionscript/audio test
|
|
92
|
+
```
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
var E={magnitude:new Float32Array(0),duration:0,speaker:null,speakers:0};function y(t,r,e,o){let n=Math.max(0,Math.floor(o)),a=new Array(n);for(let i=0;i<n;i++)a[i]={magnitude:0,speaker:-1};let l=t.magnitude.length;if(n===0||l===0||t.duration<=0)return a;let m=l/t.duration,s=(e-r)/n;for(let i=0;i<n;i++){let f=(r+s*i)*m,x=(r+s*(i+1))*m,p=Math.floor(f),u=Math.max(p,Math.ceil(x)-1),g=0,d=-1;for(let c=p;c<=u;c++){if(c<0||c>=l)continue;let h=t.magnitude[c];h<g||(g=h,d=t.speaker?t.speaker[c]:-1)}a[i]={magnitude:g,speaker:d}}return a}function A(t,r){return r===null?t:t.map(e=>e.speaker===r?e:{magnitude:0,speaker:e.speaker})}import{Graphics2D as w}from"@motionscript/sdk";var S=["center","bottom"],T=["bars","line","blocks"],M=["#6366f1","#14b8a6","#f59e0b","#ec4899","#22c55e","#3b82f6","#f97316","#a855f7"];function L(t){return M[(t%M.length+M.length)%M.length]}function _(t,r){let{width:e,height:o,gap:n,align:a,gain:l,floor:m}=r,s=t.length;if(s===0||e<=0||o<=0)return[];let i=e/s,f=Math.max(1,i*(1-k(n))),x=a==="center"?o/2:o,p=new Array(s);for(let u=0;u<s;u++){let g=k(t[u].magnitude*l),d=Math.max(x*k(m),x*g),c=-e/2+i*u+(i-f)/2,h=Math.max(1,a==="center"?d*2:d);p[u]=a==="center"?{x:c,y:-h/2,width:f,height:h}:{x:c,y:o/2-h,width:f,height:h}}return p}function b(t,r,e){if(t.length===0||r.length===0)return null;let o=new w;for(let n of t)o.rect({x:n.x+n.width/2,y:-(n.y+n.height/2),width:n.width,height:n.height,cornerRadius:Math.min(e,n.width/2,n.height/2)});return o.fill(r)}function B(t,r){let e=new Map;for(let o=0;o<t.length;o++){let n=r(o),a=e.get(n);a?a.push(t[o]):e.set(n,[t[o]])}return e}function k(t){return t<0?0:t>1?1:t}function F(t,r,e){return e!==null?`voice:${e}`:r?`voice:${t}`:"all"}export{E as a,y as b,A as c,S as d,T as e,M as f,L as g,_ as h,b as i,B as j,k,F as l};
|
|
2
|
-
//# sourceMappingURL=chunk-
|
|
2
|
+
//# sourceMappingURL=chunk-DD3KHRUL.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/waveform/envelope.ts", "../../../src/waveform/shared.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * The shape of a recording, as the waveform nodes draw it.\n *\n * ## How an envelope reaches a node: the host supplies it\n *\n * Nothing in the engine, and nothing in this kit, decodes audio. A waveform node\n * does not read a `sound` node's `src`, declares no audio asset and is not tied\n * to a clip: it draws the {@link AudioEnvelope} it is handed in its `envelope`\n * prop. Decoding a file into an envelope is the host's job, done once per file\n * before the document is built. Where the recording sits on the audio lane is\n * the author's timing too, which is why `FullWaveform.follow` is opt-in.\n *\n * ### The JSON form\n *\n * A document holds the envelope as plain JSON, because a prop value in a\n * document is JSON. The typed arrays in {@link AudioEnvelope} are one spelling of\n * it; `ArrayLike<number>` also accepts the plain arrays a document stores:\n *\n * ```json\n * {\n * \"magnitude\": [0, 0.25, 0.5, 1, 0.5, 0],\n * \"duration\": 6,\n * \"speaker\": [0, 0, 0, 1, 1, -1],\n * \"speakers\": 2\n * }\n * ```\n *\n * `magnitude` is the peak loudness, `0`…`1`, of buckets that divide `duration`\n * seconds evenly. `speaker` is `null` when nobody was identified; otherwise it is parallel to `magnitude`, one voice\n * index per bucket, `-1` for \"nobody\". `speakers` is the voice count, `0` when\n * `speaker` is `null`. A `Float32Array` or `Int16Array` is not JSON-serialisable\n * (`JSON.stringify` writes an object of indices), so a host that holds the typed\n * form converts with `Array.from` before storing it in a document.\n *\n * ## Why the data is baked rather than listened to\n *\n * A scene has to render identically in the preview and in a headless export,\n * and an export has no audio device, no `AudioContext` and no playback to\n * analyse. So a \"live\" visualiser cannot read a Web Audio analyser: what it\n * reads is a *pre-computed envelope*, indexed by the scene clock. The picture is\n * then a pure function of time, which is what makes scrubbing backwards, jumping\n * to frame 900, and rendering out of order all produce the same frame.\n *\n * The decode happens in the host, once per file, and arrives here through the\n * build — the same arrangement a chart's rows and a protein's atoms already use,\n * and for the same reason: the build is synchronous and cannot await a fetch.\n *\n * ## Why magnitude rather than min/max\n *\n * An editor's waveform is drawn from per-bucket minima *and* maxima, because an\n * editor is a measuring instrument. These nodes are neither — they are a bar\n * chart of loudness that happens to be shaped like a recording — and audio is\n * near enough symmetric that a mirrored magnitude is indistinguishable at the\n * size a node draws. Half the numbers, and one array to reason about.\n */\n\n/**\n * A decoded recording, reduced to a uniform grid of loudness — plus, when the\n * host knows it, who is talking in each bucket.\n *\n * The speaker track is **not** something a decoder can produce; it comes from a\n * diarized transcript and is folded in by the host before the build. It lives\n * here rather than in a parallel structure because every reader of it is also a\n * reader of the magnitudes at the same index, and two arrays that must stay the\n * same length are better as one object than as two arguments.\n */\nexport interface AudioEnvelope {\n /**\n * Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. A\n * `Float32Array`, or the plain `number[]` a document stores.\n */\n magnitude: ArrayLike<number>\n /** The decoded length in seconds. */\n duration: number\n /**\n * Which voice is speaking in each bucket, as an index into the recording's\n * own numbering, or `-1` for \"nobody identified\". Parallel to\n * {@link magnitude}, or `null` when the track was never diarized.\n */\n speaker: ArrayLike<number> | null\n /** How many distinct voices the diarizer found. Zero when it did not run. */\n speakers: number\n}\n\n/** The envelope of a track that hasn't loaded — or isn't there. */\nexport const EMPTY_ENVELOPE: AudioEnvelope = {\n magnitude: new Float32Array(0),\n duration: 0,\n speaker: null,\n speakers: 0,\n}\n\n/** One drawn bar: how loud, and whose. */\nexport interface EnvelopeBar {\n /** 0…1. */\n magnitude: number\n /** The voice index, or -1. */\n speaker: number\n}\n\n/**\n * Resamples `[from, to)` seconds of an envelope down to `bars` columns.\n *\n * Peak-preserving rather than averaging, which is the whole difference between\n * a waveform and a blur: averaging a hundred buckets into one column flattens\n * every transient, and transients are what makes a drawn recording read as\n * speech rather than as noise. The speaker of a column is the speaker of its\n * *loudest* bucket, on the same reasoning — a column straddling a turn belongs\n * to whoever is actually audible in it.\n *\n * A window past either end of the recording yields silent, unattributed\n * columns rather than clamping onto the nearest real one. That matters for the\n * live visualiser, which is handed a window centred on the playhead and would\n * otherwise smear the first bucket across the whole run-up to a track's start.\n */\nexport function sampleEnvelope(\n envelope: AudioEnvelope,\n from: number,\n to: number,\n bars: number\n): EnvelopeBar[] {\n const count = Math.max(0, Math.floor(bars))\n const out: EnvelopeBar[] = new Array(count)\n for (let i = 0; i < count; i++) out[i] = { magnitude: 0, speaker: -1 }\n\n const buckets = envelope.magnitude.length\n if (count === 0 || buckets === 0 || envelope.duration <= 0) return out\n\n const perSecond = buckets / envelope.duration\n const span = (to - from) / count\n\n for (let i = 0; i < count; i++) {\n const start = (from + span * i) * perSecond\n const end = (from + span * (i + 1)) * perSecond\n\n // At least one bucket per column, so a window zoomed in past the grid draws\n // the sample it is standing on rather than an empty band.\n const first = Math.floor(start)\n const last = Math.max(first, Math.ceil(end) - 1)\n\n let loudest = 0\n let speaker = -1\n for (let b = first; b <= last; b++) {\n if (b < 0 || b >= buckets) continue\n const value = envelope.magnitude[b]\n if (value < loudest) continue\n loudest = value\n speaker = envelope.speaker ? envelope.speaker[b] : -1\n }\n\n out[i] = { magnitude: loudest, speaker }\n }\n\n return out\n}\n\n/**\n * The same window, with everything that isn't `voice` silenced.\n *\n * The point of the speaker filter, and the reason it silences rather than\n * *drops*: a podcast visualiser filtered to one host should go quiet while the\n * other one talks, not compress their turn out of existence. Keeping the bars\n * and zeroing them is what makes the picture read as \"this person is not\n * speaking right now\" instead of as a jump cut.\n *\n * `voice` of `null` returns the bars untouched, which is the unfiltered node.\n */\nexport function forSpeaker(\n bars: EnvelopeBar[],\n voice: number | null\n): EnvelopeBar[] {\n if (voice === null) return bars\n return bars.map((bar) =>\n bar.speaker === voice ? bar : { magnitude: 0, speaker: bar.speaker }\n )\n}\n", "import { Graphics2D, type Fill, type FillResolved } from \"@motionscript/sdk\"\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 * Eight hues that can be told apart at a glance, handed out by voice index. They\n * are literals rather than looked up, because a scene has to build in a\n * headless render with no database in scope; a host that names its voices\n * supplies its own colours instead.\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/**\n * Where one bar sits and how tall it came out: its top-left corner and size, in\n * the node's own pixels with **y pointing down** from the middle of the box.\n * {@link paintBars} turns that into the centred, y-up rect the renderer draws.\n */\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.\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 // A hairline minimum, taken before the position so the bar stays inside the\n // box: a silent bar 1px tall that kept the position of a zero-height one\n // would hang half a pixel (centred) or a whole pixel (bottom) outside it.\n const h = Math.max(1, align === \"center\" ? reach * 2 : reach)\n out[i] =\n align === \"center\"\n ? { x, y: -h / 2, width: barWidth, height: h }\n : { x, y: height / 2 - h, width: barWidth, height: h }\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 // The box is stated y-down and the rect is drawn y-up. A centred bar is\n // symmetric about the middle, so only a bottom-aligned one shows the flip.\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"],
|
|
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,KAAK,MAAMF,CAAK,EACxBG,EAAO,KAAK,IAAID,EAAO,KAAK,KAAKD,CAAG,EAAI,CAAC,EAE3CG,EAAU,EACVC,EAAU,GACd,QAASC,EAAIJ,EAAOI,GAAKH,EAAMG,IAAK,C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,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
|
+
}
|
package/dist/browser/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as p,b as s,c as w,d as W,e as L,f as T,g as f,h as c,i as u,j as m,k as g,l as B}from"./chunks/chunk-
|
|
1
|
+
import{a as p,b as s,c as w,d as W,e as L,f as T,g as f,h as c,i as u,j as m,k as g,l as B}from"./chunks/chunk-DD3KHRUL.js";import{Types as M,component2d as C,easeInOut as S,fillOps as N}from"@motionscript/sdk";var P=o=>M.custom(o,{resolve:N.resolve,lerp:N.lerp}),O=C({key:"fullWaveform",state:{envelope:M.custom(p),bars:120,gap:.3,radius:2,progress:0,follow:!1,gain:1,floor:.015,align:M.custom("center"),byVoice:!1,voiceColors:M.custom([]),fill:P("#5b5b7a"),playedFill:P("#6366f1")},load:(o,e)=>{o.fill(e.fill),o.fill(e.playedFill)},commands:{sweep:(o,e)=>{let r=o.easing?null:S(),n=e.progress;return o.animate(t=>({progress:n+(1-n)*(r?r(t):t)}))}},render:(o,e)=>{_(o,e),o.children()}});function _(o,e){let{width:r,height:n}=o.size;if(r<=0||n<=0)return;let t=Math.max(1,Math.round(e.bars)),a=s(e.envelope,0,e.envelope.duration,t),l=c(a,{width:r,height:n,gap:e.gap,align:e.align,gain:e.gain,floor:e.floor});if(l.length===0)return;let v=A(o,e)*l.length,h=m(l,i=>e.byVoice?`voice:${a[i].speaker}`:i<v?"played":"ahead");for(let[i,y]of h){let E=u(y,Y(e,i),e.radius);E&&o.draw(E)}}function A(o,e){if(!e.follow)return g(e.progress);let r=e.envelope.duration;return r<=0?0:g(o.time.elapsed/r)}function Y(o,e){if(e==="played")return o.playedFill;if(e==="ahead")return o.fill;let r=Number(e.slice(6));return!Number.isFinite(r)||r<0?o.fill:o.voiceColors[r]??f(r)}import{Types as d,component2d as I,fillOps as x}from"@motionscript/sdk";var F=I({key:"liveWaveform",state:{envelope:d.custom(p),bars:48,gap:.35,radius:4,window:3,offset:0,playing:!0,gain:1.4,floor:.04,align:d.custom("center"),style:d.custom("bars"),speaker:-1,byVoice:!1,voiceColors:d.custom([]),fill:d.custom("#6366f1",{resolve:x.resolve,lerp:x.lerp})},load:(o,e)=>o.fill(e.fill),render:(o,e)=>{D(o,e),o.children()}});function z(o,e){return e.offset+(e.playing?Math.max(0,o.time.elapsed):0)}function D(o,e){let{width:r,height:n}=o.size;if(r<=0||n<=0)return;let t=Math.max(1,Math.round(e.bars)),a=Math.max(.05,e.window),l=z(o,e),v=e.speaker<0?null:Math.round(e.speaker),h=w(s(e.envelope,l-a/2,l+a/2,t),v),i=e.style==="line"?G(h):h,y=c(i,{width:r,height:n,gap:e.style==="blocks"?.12:e.gap,align:e.align,gain:e.gain,floor:e.floor});if(y.length===0)return;let E=m(y,b=>B(i[b].speaker,e.byVoice,v));for(let[b,V]of E){let k=u(V,R(e,b),e.radius);k&&o.draw(k)}}function R(o,e){if(e==="all")return o.fill;let r=Number(e.slice(6));return!Number.isFinite(r)||r<0?o.fill:o.voiceColors[r]??f(r)}function G(o){let e=o.length;if(e<3)return[...o];let r=Math.max(1,Math.floor(e/5));return o.map((n,t)=>{let a=Math.min(t,e-1-t),l=g(a/r);return{magnitude:n.magnitude*l,speaker:n.speaker}})}var K=[O,F];export{p as EMPTY_ENVELOPE,O as FullWaveform,F as LiveWaveform,K as NODES,T as VOICE_PALETTE,W as WAVEFORM_ALIGNMENTS,L as WAVEFORM_STYLES,w as forSpeaker,m as groupByPaint,c as layOutBars,u as paintBars,s as sampleEnvelope,f as voiceColor};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/waveform/full-waveform.ts", "../../src/waveform/live-waveform.ts", "../../src/nodes.ts"],
|
|
4
|
-
"sourcesContent": ["import {\r\n Types,\r\n component2d,\r\n easeInOut,\r\n fillOps,\r\n type Fill,\r\n type FillResolved,\r\n type NodeOf,\r\n type RenderContext,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Node2D as _Node2D,\r\n type Node2DProps as _Node2DProps,\r\n} from \"@motionscript/sdk\"\r\n\r\nimport { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from \"./envelope\"\r\nimport {\r\n clamp01,\r\n groupByPaint,\r\n layOutBars,\r\n paintBars,\r\n voiceColor,\r\n type WaveformAlignment,\r\n} from \"./shared\"\r\n\r\n/** A paint entry as the waveforms declare theirs: resolved and tweened like a fill, its kind read off the default. */\r\nconst paint = (initial: string) =>\r\n Types.custom<Fill, FillResolved[]>(initial, { resolve: fillOps.resolve, lerp: fillOps.lerp })\r\n\r\n/**\r\n * A **waveform overview**: the whole recording's shape, drawn at once.\r\n *\r\n * The picture every audio editor opens with, and the one a podcast clip puts\r\n * under its caption — a static silhouette of the file, with a played portion\r\n * that advances. It answers \"how long is this and where are the loud parts\",\r\n * which is a question about the *file*, not about the moment; its sibling\r\n * {@link LiveWaveform} answers the other one.\r\n *\r\n * ## Progress is authored, not assumed\r\n *\r\n * `follow` is off by default, and that default is the considered one. A\r\n * scene has no idea where the recording it is drawing sits on the video's audio\r\n * lane — a clip is placed, trimmed and sped up out there, and none of that is\r\n * visible from in here. So the honest default is that the fill is *animation*:\r\n * the author sweeps it with a command, the same as any other tween, and it lands\r\n * wherever they put it. `follow` is the shortcut for the common arrangement\r\n * where the node appears exactly as the track starts, and it says so by being a\r\n * switch someone has to turn on rather than a behaviour that quietly disagrees\r\n * with the mix.\r\n *\r\n * ## Voices instead of progress\r\n *\r\n * With `byVoice` on, the played/ahead split is replaced by one colour per\r\n * speaking voice, at a size you can read. For a two-hander it is a map of the conversation: who has the\r\n * floor, where the turns are, how long each one ran. It replaces the progress\r\n * colours rather than combining with them, because a bar cannot mean two things\r\n * at once and \"played, and also Grace\" is a legend nobody can hold in their\r\n * head.\r\n */\r\nexport const FullWaveform = component2d({\r\n key: \"fullWaveform\",\r\n state: {\r\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\r\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\r\n /** How many columns the whole file is drawn as. */\r\n bars: 120,\r\n /** Space between bars, as a fraction of the pitch. See {@link layOutBars}. */\r\n gap: 0.3,\r\n /** Corner radius of a bar, capped at half its width. */\r\n radius: 2,\r\n /** How much of the file reads as played, `0`–`1`. Tweenable. */\r\n progress: 0,\r\n /** Drive `progress` from the node's own clock instead of the prop. */\r\n follow: false,\r\n /** Multiplies every magnitude — a quiet track pulled up to fill its box. */\r\n gain: 1,\r\n /** How much of the box a silent bar still occupies, `0`–`1`. */\r\n floor: 0.015,\r\n align: Types.custom<WaveformAlignment>(\"center\"),\r\n /** Colour each voice's turns rather than splitting on played / not played. */\r\n byVoice: false,\r\n /** A colour per voice index. Short or empty falls back to the palette. */\r\n voiceColors: Types.custom<string[]>([]),\r\n /** The bars ahead of the playhead. */\r\n fill: paint(\"#5b5b7a\"),\r\n /** …and the bars behind it. */\r\n playedFill: paint(\"#6366f1\"),\r\n },\r\n // Nothing declares a component's paints but itself: `fill` and `playedFill` both.\r\n load: (loader, state) => {\r\n loader.fill(state.fill)\r\n loader.fill(state.playedFill)\r\n },\r\n commands: {\r\n /**\r\n * Fill the waveform in, left to right — the node's one command, since\r\n * everything else about it is expressible as an ordinary `to`. Eased in-out\r\n * unless it is given an easing of its own.\r\n */\r\n sweep: (timeline, state) => {\r\n const ease = timeline.easing ? null : easeInOut()\r\n const from = state.progress\r\n return timeline.animate((t) => ({ progress: from + (1 - from) * (ease ? ease(t) : t) }))\r\n },\r\n },\r\n render: (ctx, state) => {\r\n drawBars(ctx, state)\r\n ctx.children()\r\n },\r\n})\r\n\r\nexport type FullWaveform = NodeOf<typeof FullWaveform>\r\nexport type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>\r\n\r\ninterface FullState {\r\n readonly envelope: AudioEnvelope\r\n readonly bars: number\r\n readonly gap: number\r\n readonly radius: number\r\n readonly progress: number\r\n readonly follow: boolean\r\n readonly gain: number\r\n readonly floor: number\r\n readonly align: WaveformAlignment\r\n readonly byVoice: boolean\r\n readonly voiceColors: string[]\r\n readonly fill: FillResolved[]\r\n readonly playedFill: FillResolved[]\r\n}\r\n\r\nfunction drawBars(ctx: RenderContext, s: FullState): void {\r\n const { width, height } = ctx.size\r\n if (width <= 0 || height <= 0) return\r\n\r\n const count = Math.max(1, Math.round(s.bars))\r\n const sampled = sampleEnvelope(s.envelope, 0, s.envelope.duration, count)\r\n const boxes = layOutBars(sampled, {\r\n width,\r\n height,\r\n gap: s.gap,\r\n align: s.align,\r\n gain: s.gain,\r\n floor: s.floor,\r\n })\r\n if (boxes.length === 0) return\r\n\r\n // One draw per colour rather than per bar — see {@link paintBars}. The key\r\n // is a string because it has to be comparable; the paint it stands for is\r\n // resolved after the grouping.\r\n const edge = played(ctx, s) * boxes.length\r\n const groups = groupByPaint(boxes, (i) =>\r\n s.byVoice\r\n ? `voice:${sampled[i].speaker}`\r\n : i < edge\r\n ? \"played\"\r\n : \"ahead\"\r\n )\r\n\r\n for (const [key, run] of groups) {\r\n const graphics = paintBars(run, paintFor(s, key), s.radius)\r\n if (graphics) ctx.draw(graphics)\r\n }\r\n}\r\n\r\n/** Where the fill has reached, from whichever source is in charge. */\r\nfunction played(ctx: RenderContext, s: FullState): number {\r\n if (!s.follow) return clamp01(s.progress)\r\n const length = s.envelope.duration\r\n if (length <= 0) return 0\r\n return clamp01(ctx.time.elapsed / length)\r\n}\r\n\r\n/**\r\n * The paint a group takes.\r\n *\r\n * A voice's colour is a **colour string**, never one of the node's two fill\r\n * props, and that is deliberate: a voice is identified by hue, so a gradient\r\n * or an image there would defeat the only thing the colour is for. It also\r\n * means these paints declare no assets, which is why `load` only has to account\r\n * for the two real fill props.\r\n */\r\nfunction paintFor(s: FullState, key: string): Fill {\r\n if (key === \"played\") return s.playedFill\r\n if (key === \"ahead\") return s.fill\r\n\r\n const voice = Number(key.slice(\"voice:\".length))\r\n if (!Number.isFinite(voice) || voice < 0) return s.fill\r\n return s.voiceColors[voice] ?? voiceColor(voice)\r\n}\r\n", "import {\r\n Types,\r\n component2d,\r\n fillOps,\r\n type Fill,\r\n type FillResolved,\r\n type NodeOf,\r\n type RenderContext,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Node2D as _Node2D,\r\n type Node2DProps as _Node2DProps,\r\n} from \"@motionscript/sdk\"\r\n\r\nimport {\r\n EMPTY_ENVELOPE,\r\n forSpeaker,\r\n sampleEnvelope,\r\n type AudioEnvelope,\r\n type EnvelopeBar,\r\n} from \"./envelope\"\r\nimport {\r\n clamp01,\r\n groupByPaint,\r\n layOutBars,\r\n livePaintKey,\r\n paintBars,\r\n voiceColor,\r\n type WaveformAlignment,\r\n type WaveformStyle,\r\n} from \"./shared\"\r\n\r\n/**\r\n * A **live audio visualiser**: bars that move with the recording as it plays.\r\n *\r\n * The thing every podcast-to-video tool puts beside the artwork. It is not a\r\n * picture of the file — that is {@link FullWaveform} — it is a picture of *this\r\n * moment*, and what it is for is making a static frame of a talking head feel\r\n * like it is playing.\r\n *\r\n * ## \"Live\" without listening\r\n *\r\n * Nothing here reads an audio device. The bars are sampled out of a baked\r\n * envelope at the node's own elapsed time, which is what makes the visualiser\r\n * survive the thing it exists for: an **export**, where there is no playback to\r\n * analyse and frames may be rendered out of order or in parallel. A frame is a\r\n * pure function of its time, so scrubbing back and forth over the visualiser\r\n * redraws exactly what it drew the first time — which an analyser node, by\r\n * construction, cannot promise. See {@link AudioEnvelope}.\r\n *\r\n * ## The speaker filter\r\n *\r\n * `speaker` narrows the node to one voice of a diarized recording, and it\r\n * is the reason this node knows about transcripts at all. Two visualisers beside\r\n * two headshots, each filtered to its own host, is a podcast edit that would\r\n * otherwise be done by hand for every turn — and because the filter *silences*\r\n * rather than skips (see {@link forSpeaker}), the quiet host visibly stops\r\n * talking instead of vanishing. Nothing about that requires the words: only who\r\n * is talking when, which is what the diarizer produces.\r\n *\r\n * A filter naming a voice the recording doesn't have draws a resting row rather\r\n * than everything — a picture of \"this person never speaks here\", which is\r\n * true, where falling back to the unfiltered mix would be a silent lie about\r\n * whose voice you were watching.\r\n */\r\nexport const LiveWaveform = component2d({\r\n key: \"liveWaveform\",\r\n state: {\r\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\r\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\r\n /** How many bars the visualiser is made of. */\r\n bars: 48,\r\n /** Space between bars, as a fraction of the pitch. */\r\n gap: 0.35,\r\n radius: 4,\r\n /** How many seconds of audio are on screen at once. */\r\n window: 3,\r\n /** Seconds into the file the node's first frame shows. */\r\n offset: 0,\r\n /** Advance with the scene clock. Off freezes the picture at `offset`. */\r\n playing: true,\r\n /** Multiplies every magnitude before it is clamped. */\r\n gain: 1.4,\r\n /** How much of the box a silent bar still occupies, `0`–`1`. */\r\n floor: 0.04,\r\n align: Types.custom<WaveformAlignment>(\"center\"),\r\n style: Types.custom<WaveformStyle>(\"bars\"),\r\n /**\r\n * Show only this voice's activity, by index into the recording's own\r\n * numbering. `-1` — the default — is every voice.\r\n */\r\n speaker: -1,\r\n /** Colour each bar by whoever is speaking in it. */\r\n byVoice: false,\r\n /** A colour per voice index. Short or empty falls back to the palette. */\r\n voiceColors: Types.custom<string[]>([]),\r\n fill: Types.custom<Fill, FillResolved[]>(\"#6366f1\", { resolve: fillOps.resolve, lerp: fillOps.lerp }),\r\n },\r\n // Nothing declares a component's paints but itself.\r\n load: (loader, state) => loader.fill(state.fill),\r\n render: (ctx, state) => {\r\n drawBars(ctx, state)\r\n ctx.children()\r\n },\r\n})\r\n\r\nexport type LiveWaveform = NodeOf<typeof LiveWaveform>\r\nexport type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>\r\n\r\ninterface LiveState {\r\n readonly envelope: AudioEnvelope\r\n readonly bars: number\r\n readonly gap: number\r\n readonly radius: number\r\n readonly window: number\r\n readonly offset: number\r\n readonly playing: boolean\r\n readonly gain: number\r\n readonly floor: number\r\n readonly align: WaveformAlignment\r\n readonly style: WaveformStyle\r\n readonly speaker: number\r\n readonly byVoice: boolean\r\n readonly voiceColors: string[]\r\n readonly fill: FillResolved[]\r\n}\r\n\r\n/**\r\n * The second of the recording the visualiser is showing.\r\n *\r\n * Off the node's own clock rather than the scene's, which is what \"from the\r\n * moment this node appeared\" means — the same clock the engine's own\r\n * `Video` times its picture from, and the same one that makes a node dropped\r\n * halfway through a clip start its recording at its entrance rather than\r\n * partway in.\r\n */\r\nfunction at(ctx: RenderContext, s: LiveState): number {\r\n return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0)\r\n}\r\n\r\nfunction drawBars(ctx: RenderContext, s: LiveState): void {\r\n const { width, height } = ctx.size\r\n if (width <= 0 || height <= 0) return\r\n\r\n const count = Math.max(1, Math.round(s.bars))\r\n const span = Math.max(0.05, s.window)\r\n const now = at(ctx, s)\r\n const voice = s.speaker < 0 ? null : Math.round(s.speaker)\r\n\r\n // The window is centred on the playhead, so the bars run *toward* the\r\n // middle and away again — the arrangement every mixer's meter uses, and the\r\n // one that reads as sound arriving rather than as a strip scrolling past.\r\n const sampled = forSpeaker(\r\n sampleEnvelope(s.envelope, now - span / 2, now + span / 2, count),\r\n voice\r\n )\r\n\r\n const shaped = s.style === \"line\" ? taper(sampled) : sampled\r\n const boxes = layOutBars(shaped, {\r\n width,\r\n height,\r\n gap: s.style === \"blocks\" ? 0.12 : s.gap,\r\n align: s.align,\r\n gain: s.gain,\r\n floor: s.floor,\r\n })\r\n if (boxes.length === 0) return\r\n\r\n const groups = groupByPaint(boxes, (i) =>\r\n livePaintKey(shaped[i].speaker, s.byVoice, voice)\r\n )\r\n\r\n for (const [key, run] of groups) {\r\n const graphics = paintBars(run, paintFor(s, key), s.radius)\r\n if (graphics) ctx.draw(graphics)\r\n }\r\n}\r\n\r\n/** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */\r\nfunction paintFor(s: LiveState, key: string): Fill {\r\n if (key === \"all\") return s.fill\r\n const voice = Number(key.slice(\"voice:\".length))\r\n if (!Number.isFinite(voice) || voice < 0) return s.fill\r\n return s.voiceColors[voice] ?? voiceColor(voice)\r\n}\r\n\r\n/**\r\n * Rolls the ends of a window off toward silence.\r\n *\r\n * What separates the `line` style from `bars`: a hard-edged window makes the\r\n * loud bar entering at the right edge appear from nothing, which reads as a\r\n * glitch rather than as sound arriving. Fading the outer fifth turns the same\r\n * data into a shape that grows out of the baseline — the profile a spectrum\r\n * display has, and the reason it looks like one object rather than a row of\r\n * separate meters.\r\n */\r\nfunction taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {\r\n const count = bars.length\r\n if (count < 3) return [...bars]\r\n const edge = Math.max(1, Math.floor(count / 5))\r\n\r\n return bars.map((bar, i) => {\r\n const fromEnd = Math.min(i, count - 1 - i)\r\n const weight = clamp01(fromEnd / edge)\r\n return { magnitude: bar.magnitude * weight, speaker: bar.speaker }\r\n })\r\n}\r\n", "import { FullWaveform } from \"./waveform/full-waveform\";\r\nimport { LiveWaveform } from \"./waveform/live-waveform\";\r\n\r\n/**\r\n * Every node type this package publishes.\r\n *\r\n * A host registers these by handing the array to an engine —\r\n * `new Engine(platform, { nodes: NODES })` — which reads each component's key.\r\n * Defining a component only *declares* that key: nothing is registered by\r\n * importing a module, so a registry holds exactly what its owner asked for.\r\n *\r\n * Listing the component **values** is also what survives bundling: this package\r\n * declares `sideEffects: false`, and a document names a node type by string, so\r\n * an import kept only for its side effect is exactly what gets shaken out.\r\n */\r\n// Typed by name, so the declaration names each component rather than spelling out its type through core.\r\nexport const NODES: (typeof FullWaveform | typeof LiveWaveform)[] = [FullWaveform, LiveWaveform];\r\n"],
|
|
4
|
+
"sourcesContent": ["import {\n Types,\n component2d,\n easeInOut,\n fillOps,\n type Fill,\n type FillResolved,\n type NodeOf,\n type RenderContext,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n} from \"@motionscript/sdk\"\n\nimport { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from \"./envelope\"\nimport {\n clamp01,\n groupByPaint,\n layOutBars,\n paintBars,\n voiceColor,\n type WaveformAlignment,\n} from \"./shared\"\n\n/** A paint entry as the waveforms declare theirs: resolved and tweened like a fill, its kind read off the default. */\nconst paint = (initial: string) =>\n Types.custom<Fill, FillResolved[]>(initial, { resolve: fillOps.resolve, lerp: fillOps.lerp })\n\n/**\n * A **waveform overview**: the whole recording's shape, drawn at once.\n *\n * The picture every audio editor opens with, and the one a podcast clip puts\n * under its caption — a static silhouette of the file, with a played portion\n * that advances. It answers \"how long is this and where are the loud parts\",\n * which is a question about the *file*, not about the moment; its sibling\n * {@link LiveWaveform} answers the other one.\n *\n * ## Progress is authored, not assumed\n *\n * `follow` is off by default, and that default is the considered one. A\n * scene has no idea where the recording it is drawing sits on the video's audio\n * lane — a clip is placed, trimmed and sped up out there, and none of that is\n * visible from in here. So the honest default is that the fill is *animation*:\n * the author sweeps it with a command, the same as any other tween, and it lands\n * wherever they put it. `follow` is the shortcut for the common arrangement\n * where the node appears exactly as the track starts, and it says so by being a\n * switch someone has to turn on rather than a behaviour that quietly disagrees\n * with the mix.\n *\n * ## Voices instead of progress\n *\n * With `byVoice` on, the played/ahead split is replaced by one colour per\n * speaking voice, at a size you can read. For a two-hander it is a map of the conversation: who has the\n * floor, where the turns are, how long each one ran. It replaces the progress\n * colours rather than combining with them, because a bar cannot mean two things\n * at once and \"played, and also Grace\" is a legend nobody can hold in their\n * head.\n */\nexport const FullWaveform = component2d({\n key: \"fullWaveform\",\n state: {\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\n /** How many columns the whole file is drawn as. */\n bars: 120,\n /** Space between bars, as a fraction of the pitch. See {@link layOutBars}. */\n gap: 0.3,\n /** Corner radius of a bar, capped at half its width. */\n radius: 2,\n /** How much of the file reads as played, `0`–`1`. Tweenable. */\n progress: 0,\n /** Drive `progress` from the node's own clock instead of the prop. */\n follow: false,\n /** Multiplies every magnitude — a quiet track pulled up to fill its box. */\n gain: 1,\n /** How much of the box a silent bar still occupies, `0`–`1`. */\n floor: 0.015,\n align: Types.custom<WaveformAlignment>(\"center\"),\n /** Colour each voice's turns rather than splitting on played / not played. */\n byVoice: false,\n /** A colour per voice index. Short or empty falls back to the palette. */\n voiceColors: Types.custom<string[]>([]),\n /** The bars ahead of the playhead. */\n fill: paint(\"#5b5b7a\"),\n /** …and the bars behind it. */\n playedFill: paint(\"#6366f1\"),\n },\n // Nothing declares a component's paints but itself: `fill` and `playedFill` both.\n load: (loader, state) => {\n loader.fill(state.fill)\n loader.fill(state.playedFill)\n },\n commands: {\n /**\n * Fill the waveform in, left to right — the node's one command, since\n * everything else about it is expressible as an ordinary `to`. Eased in-out\n * unless it is given an easing of its own.\n */\n sweep: (timeline, state) => {\n const ease = timeline.easing ? null : easeInOut()\n const from = state.progress\n return timeline.animate((t) => ({ progress: from + (1 - from) * (ease ? ease(t) : t) }))\n },\n },\n render: (ctx, state) => {\n drawBars(ctx, state)\n ctx.children()\n },\n})\n\nexport type FullWaveform = NodeOf<typeof FullWaveform>\nexport type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>\n\ninterface FullState {\n readonly envelope: AudioEnvelope\n readonly bars: number\n readonly gap: number\n readonly radius: number\n readonly progress: number\n readonly follow: boolean\n readonly gain: number\n readonly floor: number\n readonly align: WaveformAlignment\n readonly byVoice: boolean\n readonly voiceColors: string[]\n readonly fill: FillResolved[]\n readonly playedFill: FillResolved[]\n}\n\nfunction drawBars(ctx: RenderContext, s: FullState): void {\n const { width, height } = ctx.size\n if (width <= 0 || height <= 0) return\n\n const count = Math.max(1, Math.round(s.bars))\n const sampled = sampleEnvelope(s.envelope, 0, s.envelope.duration, count)\n const boxes = layOutBars(sampled, {\n width,\n height,\n gap: s.gap,\n align: s.align,\n gain: s.gain,\n floor: s.floor,\n })\n if (boxes.length === 0) return\n\n // One draw per colour rather than per bar — see {@link paintBars}. The key\n // is a string because it has to be comparable; the paint it stands for is\n // resolved after the grouping.\n const edge = played(ctx, s) * boxes.length\n const groups = groupByPaint(boxes, (i) =>\n s.byVoice\n ? `voice:${sampled[i].speaker}`\n : i < edge\n ? \"played\"\n : \"ahead\"\n )\n\n for (const [key, run] of groups) {\n const graphics = paintBars(run, paintFor(s, key), s.radius)\n if (graphics) ctx.draw(graphics)\n }\n}\n\n/** Where the fill has reached, from whichever source is in charge. */\nfunction played(ctx: RenderContext, s: FullState): number {\n if (!s.follow) return clamp01(s.progress)\n const length = s.envelope.duration\n if (length <= 0) return 0\n return clamp01(ctx.time.elapsed / length)\n}\n\n/**\n * The paint a group takes.\n *\n * A voice's colour is a **colour string**, never one of the node's two fill\n * props, and that is deliberate: a voice is identified by hue, so a gradient\n * or an image there would defeat the only thing the colour is for. It also\n * means these paints declare no assets, which is why `load` only has to account\n * for the two real fill props.\n */\nfunction paintFor(s: FullState, key: string): Fill {\n if (key === \"played\") return s.playedFill\n if (key === \"ahead\") return s.fill\n\n const voice = Number(key.slice(\"voice:\".length))\n if (!Number.isFinite(voice) || voice < 0) return s.fill\n return s.voiceColors[voice] ?? voiceColor(voice)\n}\n", "import {\n Types,\n component2d,\n fillOps,\n type Fill,\n type FillResolved,\n type NodeOf,\n type RenderContext,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n} from \"@motionscript/sdk\"\n\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\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 * `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 */\nexport const LiveWaveform = component2d({\n key: \"liveWaveform\",\n state: {\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\n /** How many bars the visualiser is made of. */\n bars: 48,\n /** Space between bars, as a fraction of the pitch. */\n gap: 0.35,\n radius: 4,\n /** How many seconds of audio are on screen at once. */\n window: 3,\n /** Seconds into the file the node's first frame shows. */\n offset: 0,\n /** Advance with the scene clock. Off freezes the picture at `offset`. */\n playing: true,\n /** Multiplies every magnitude before it is clamped. */\n gain: 1.4,\n /** How much of the box a silent bar still occupies, `0`–`1`. */\n floor: 0.04,\n align: Types.custom<WaveformAlignment>(\"center\"),\n style: Types.custom<WaveformStyle>(\"bars\"),\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: -1,\n /** Colour each bar by whoever is speaking in it. */\n byVoice: false,\n /** A colour per voice index. Short or empty falls back to the palette. */\n voiceColors: Types.custom<string[]>([]),\n fill: Types.custom<Fill, FillResolved[]>(\"#6366f1\", { resolve: fillOps.resolve, lerp: fillOps.lerp }),\n },\n // Nothing declares a component's paints but itself.\n load: (loader, state) => loader.fill(state.fill),\n render: (ctx, state) => {\n drawBars(ctx, state)\n ctx.children()\n },\n})\n\nexport type LiveWaveform = NodeOf<typeof LiveWaveform>\nexport type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>\n\ninterface LiveState {\n readonly envelope: AudioEnvelope\n readonly bars: number\n readonly gap: number\n readonly radius: number\n readonly window: number\n readonly offset: number\n readonly playing: boolean\n readonly gain: number\n readonly floor: number\n readonly align: WaveformAlignment\n readonly style: WaveformStyle\n readonly speaker: number\n readonly byVoice: boolean\n readonly voiceColors: string[]\n readonly fill: FillResolved[]\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 the engine's own\n * `Video` times its picture from, and the same one that makes a node dropped\n * halfway through a clip start its recording at its entrance rather than\n * partway in.\n */\nfunction at(ctx: RenderContext, s: LiveState): number {\n return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0)\n}\n\nfunction drawBars(ctx: RenderContext, s: LiveState): void {\n const { width, height } = ctx.size\n if (width <= 0 || height <= 0) return\n\n const count = Math.max(1, Math.round(s.bars))\n const span = Math.max(0.05, s.window)\n const now = at(ctx, s)\n const voice = s.speaker < 0 ? null : Math.round(s.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(s.envelope, now - span / 2, now + span / 2, count),\n voice\n )\n\n const shaped = s.style === \"line\" ? taper(sampled) : sampled\n const boxes = layOutBars(shaped, {\n width,\n height,\n gap: s.style === \"blocks\" ? 0.12 : s.gap,\n align: s.align,\n gain: s.gain,\n floor: s.floor,\n })\n if (boxes.length === 0) return\n\n const groups = groupByPaint(boxes, (i) =>\n livePaintKey(shaped[i].speaker, s.byVoice, voice)\n )\n\n for (const [key, run] of groups) {\n const graphics = paintBars(run, paintFor(s, key), s.radius)\n if (graphics) ctx.draw(graphics)\n }\n}\n\n/** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */\nfunction paintFor(s: LiveState, key: string): Fill {\n if (key === \"all\") return s.fill\n const voice = Number(key.slice(\"voice:\".length))\n if (!Number.isFinite(voice) || voice < 0) return s.fill\n return s.voiceColors[voice] ?? voiceColor(voice)\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", "import { FullWaveform } from \"./waveform/full-waveform\";\nimport { LiveWaveform } from \"./waveform/live-waveform\";\n\n/**\n * Every node type this package publishes.\n *\n * A host registers these by handing the array to an engine —\n * `new Engine(platform, { nodes: NODES })` — which reads each component's key.\n * Defining a component only *declares* that key: nothing is registered by\n * importing a module, so a registry holds exactly what its owner asked for.\n *\n * Listing the component **values** is also what survives bundling: this package\n * declares `sideEffects: false`, and a document names a node type by string, so\n * an import kept only for its side effect is exactly what gets shaken out.\n */\n// Typed by name, so the declaration names each component rather than spelling out its type through core.\nexport const NODES: (typeof FullWaveform | typeof LiveWaveform)[] = [FullWaveform, LiveWaveform];\n"],
|
|
5
5
|
"mappings": "4HAAA,OACE,SAAAA,EACA,eAAAC,EACA,aAAAC,EACA,WAAAC,MAQK,oBAaP,IAAMC,EAASC,GACbC,EAAM,OAA6BD,EAAS,CAAE,QAASE,EAAQ,QAAS,KAAMA,EAAQ,IAAI,CAAE,EAgCjFC,EAAeC,EAAY,CACtC,IAAK,eACL,MAAO,CAEL,SAAUH,EAAM,OAAsBI,CAAc,EAEpD,KAAM,IAEN,IAAK,GAEL,OAAQ,EAER,SAAU,EAEV,OAAQ,GAER,KAAM,EAEN,MAAO,KACP,MAAOJ,EAAM,OAA0B,QAAQ,EAE/C,QAAS,GAET,YAAaA,EAAM,OAAiB,CAAA,CAAE,EAEtC,KAAMF,EAAM,SAAS,EAErB,WAAYA,EAAM,SAAS,GAG7B,KAAM,CAACO,EAAQC,IAAS,CACtBD,EAAO,KAAKC,EAAM,IAAI,EACtBD,EAAO,KAAKC,EAAM,UAAU,CAC9B,EACA,SAAU,CAMR,MAAO,CAACC,EAAUD,IAAS,CACzB,IAAME,EAAOD,EAAS,OAAS,KAAOE,EAAS,EACzCC,EAAOJ,EAAM,SACnB,OAAOC,EAAS,QAAS,IAAO,CAAE,SAAUG,GAAQ,EAAIA,IAASF,EAAOA,EAAK,CAAC,EAAI,EAAE,EAAG,CACzF,GAEF,OAAQ,CAACG,EAAKL,IAAS,CACrBM,EAASD,EAAKL,CAAK,EACnB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ps", "paint", "initial", "Types", "fillOps", "FullWaveform", "component2d", "EMPTY_ENVELOPE", "loader", "state", "timeline", "ease", "easeInOut", "from", "ctx", "drawBars", "s", "width", "height", "count", "sampled", "sampleEnvelope", "boxes", "layOutBars", "edge", "played", "groups", "groupByPaint", "key", "run", "graphics", "paintBars", "paintFor", "clamp01", "length", "voice", "voiceColor", "Types", "component2d", "fillOps", "LiveWaveform", "component2d", "Types", "EMPTY_ENVELOPE", "fillOps", "loader", "state", "ctx", "drawBars", "at", "s", "width", "height", "count", "span", "now", "voice", "sampled", "forSpeaker", "sampleEnvelope", "shaped", "taper", "boxes", "layOutBars", "groups", "groupByPaint", "i", "livePaintKey", "key", "run", "graphics", "paintBars", "paintFor", "voiceColor", "bars", "edge", "bar", "fromEnd", "weight", "clamp01", "NODES", "FullWaveform", "LiveWaveform"]
|
|
7
7
|
}
|
package/dist/browser/kit.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a as E,b as e,c as o,d as r,e as a,f as p,g as l,h as t,i,j as O,k as P,l as T}from"./chunks/chunk-
|
|
1
|
+
import{a as E,b as e,c as o,d as r,e as a,f as p,g as l,h as t,i,j as O,k as P,l as T}from"./chunks/chunk-DD3KHRUL.js";export{E as EMPTY_ENVELOPE,p as VOICE_PALETTE,r as WAVEFORM_ALIGNMENTS,a as WAVEFORM_STYLES,P as clamp01,o as forSpeaker,O as groupByPaint,t as layOutBars,T as livePaintKey,i as paintBars,e as sampleEnvelope,l as voiceColor};
|
|
2
2
|
//# sourceMappingURL=kit.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC","sourcesContent":["export * from \"./waveform\"\
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC","sourcesContent":["export * from \"./waveform\"\nexport { NODES } from \"./nodes\";\n"]}
|
package/dist/kit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kit.js","sourceRoot":"","sources":["../src/kit.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEhF,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,OAAO,EACP,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,SAAS,EACT,UAAU,GACX,MAAM,mBAAmB,CAAA","sourcesContent":["/**\
|
|
1
|
+
{"version":3,"file":"kit.js","sourceRoot":"","sources":["../src/kit.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEhF,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,OAAO,EACP,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,SAAS,EACT,UAAU,GACX,MAAM,mBAAmB,CAAA","sourcesContent":["/**\n * What the two waveform nodes share, importable without copying it.\n *\n * A registry item copies `envelope.ts` and `shared.ts` beside its node: they\n * hold no class and no registered state, so a fork owning its own copy splits\n * no identity. This is the same surface for a host that samples envelopes or\n * lays bars out itself, and for a fork that keeps only its node file.\n */\nexport { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from \"./waveform/envelope\"\nexport type { AudioEnvelope, EnvelopeBar } from \"./waveform/envelope\"\nexport {\n VOICE_PALETTE,\n WAVEFORM_ALIGNMENTS,\n WAVEFORM_STYLES,\n clamp01,\n groupByPaint,\n layOutBars,\n livePaintKey,\n paintBars,\n voiceColor,\n} from \"./waveform/shared\"\nexport type { BarBox, WaveformAlignment, WaveformStyle } from \"./waveform/shared\"\n"]}
|
package/dist/nodes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodes.js","sourceRoot":"","sources":["../src/nodes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD;;;;;;;;;;;GAWG;AACH,yGAAyG;AACzG,MAAM,CAAC,MAAM,KAAK,GAAkD,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC","sourcesContent":["import { FullWaveform } from \"./waveform/full-waveform\";\
|
|
1
|
+
{"version":3,"file":"nodes.js","sourceRoot":"","sources":["../src/nodes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD;;;;;;;;;;;GAWG;AACH,yGAAyG;AACzG,MAAM,CAAC,MAAM,KAAK,GAAkD,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC","sourcesContent":["import { FullWaveform } from \"./waveform/full-waveform\";\nimport { LiveWaveform } from \"./waveform/live-waveform\";\n\n/**\n * Every node type this package publishes.\n *\n * A host registers these by handing the array to an engine —\n * `new Engine(platform, { nodes: NODES })` — which reads each component's key.\n * Defining a component only *declares* that key: nothing is registered by\n * importing a module, so a registry holds exactly what its owner asked for.\n *\n * Listing the component **values** is also what survives bundling: this package\n * declares `sideEffects: false`, and a document names a node type by string, so\n * an import kept only for its side effect is exactly what gets shaken out.\n */\n// Typed by name, so the declaration names each component rather than spelling out its type through core.\nexport const NODES: (typeof FullWaveform | typeof LiveWaveform)[] = [FullWaveform, LiveWaveform];\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"envelope.js","sourceRoot":"","sources":["../../src/waveform/envelope.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;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CAAA;IAC/B,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CACtB,GAAG,CAAC,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CACrE,CAAA;AACH,CAAC","sourcesContent":["/**\n * The shape of a recording, as the waveform nodes draw it.\n *\n * ## How an envelope reaches a node: the host supplies it\n *\n * Nothing in the engine, and nothing in this kit, decodes audio. A waveform node\n * does not read a `sound` node's `src`, declares no audio asset and is not tied\n * to a clip: it draws the {@link AudioEnvelope} it is handed in its `envelope`\n * prop. Decoding a file into an envelope is the host's job, done once per file\n * before the document is built. Where the recording sits on the audio lane is\n * the author's timing too, which is why `FullWaveform.follow` is opt-in.\n *\n * ### The JSON form\n *\n * A document holds the envelope as plain JSON, because a prop value in a\n * document is JSON. The typed arrays in {@link AudioEnvelope} are one spelling of\n * it; `ArrayLike<number>` also accepts the plain arrays a document stores:\n *\n * ```json\n * {\n * \"magnitude\": [0, 0.25, 0.5, 1, 0.5, 0],\n * \"duration\": 6,\n * \"speaker\": [0, 0, 0, 1, 1, -1],\n * \"speakers\": 2\n * }\n * ```\n *\n * `magnitude` is the peak loudness, `0`…`1`, of buckets that divide `duration`\n * seconds evenly. `speaker` is `null` when nobody was identified; otherwise it is parallel to `magnitude`, one voice\n * index per bucket, `-1` for \"nobody\". `speakers` is the voice count, `0` when\n * `speaker` is `null`. A `Float32Array` or `Int16Array` is not JSON-serialisable\n * (`JSON.stringify` writes an object of indices), so a host that holds the typed\n * form converts with `Array.from` before storing it in a document.\n *\n * ## Why the data is baked rather than listened to\n *\n * A scene has to render identically in the preview and in a headless export,\n * and an export has no audio device, no `AudioContext` and no playback to\n * analyse. So a \"live\" visualiser cannot read a Web Audio analyser: what it\n * reads is a *pre-computed envelope*, indexed by the scene clock. The picture is\n * then a pure function of time, which is what makes scrubbing backwards, jumping\n * to frame 900, and rendering out of order all produce the same frame.\n *\n * The decode happens in the host, once per file, and arrives here through the\n * build — the same arrangement a chart's rows and a protein's atoms already use,\n * and for the same reason: the build is synchronous and cannot await a fetch.\n *\n * ## Why magnitude rather than min/max\n *\n * An editor's waveform is drawn from per-bucket minima *and* maxima, because an\n * editor is a measuring instrument. These nodes are neither — they are a bar\n * chart of loudness that happens to be shaped like a recording — and audio is\n * near enough symmetric that a mirrored magnitude is indistinguishable at the\n * size a node draws. Half the numbers, and one array to reason about.\n */\n\n/**\n * A decoded recording, reduced to a uniform grid of loudness — plus, when the\n * host knows it, who is talking in each bucket.\n *\n * The speaker track is **not** something a decoder can produce; it comes from a\n * diarized transcript and is folded in by the host before the build. It lives\n * here rather than in a parallel structure because every reader of it is also a\n * reader of the magnitudes at the same index, and two arrays that must stay the\n * same length are better as one object than as two arguments.\n */\nexport interface AudioEnvelope {\n /**\n * Peak magnitude per bucket, 0…1. Buckets span {@link duration} evenly. A\n * `Float32Array`, or the plain `number[]` a document stores.\n */\n magnitude: ArrayLike<number>\n /** The decoded length in seconds. */\n duration: number\n /**\n * Which voice is speaking in each bucket, as an index into the recording's\n * own numbering, or `-1` for \"nobody identified\". Parallel to\n * {@link magnitude}, or `null` when the track was never diarized.\n */\n speaker: ArrayLike<number> | null\n /** How many distinct voices the diarizer found. Zero when it did not run. */\n speakers: number\n}\n\n/** The envelope of a track that hasn't loaded — or isn't there. */\nexport const EMPTY_ENVELOPE: AudioEnvelope = {\n magnitude: new Float32Array(0),\n duration: 0,\n speaker: null,\n speakers: 0,\n}\n\n/** One drawn bar: how loud, and whose. */\nexport interface EnvelopeBar {\n /** 0…1. */\n magnitude: number\n /** The voice index, or -1. */\n speaker: number\n}\n\n/**\n * Resamples `[from, to)` seconds of an envelope down to `bars` columns.\n *\n * Peak-preserving rather than averaging, which is the whole difference between\n * a waveform and a blur: averaging a hundred buckets into one column flattens\n * every transient, and transients are what makes a drawn recording read as\n * speech rather than as noise. The speaker of a column is the speaker of its\n * *loudest* bucket, on the same reasoning — a column straddling a turn belongs\n * to whoever is actually audible in it.\n *\n * A window past either end of the recording yields silent, unattributed\n * columns rather than clamping onto the nearest real one. That matters for the\n * live visualiser, which is handed a window centred on the playhead and would\n * otherwise smear the first bucket across the whole run-up to a track's start.\n */\nexport function sampleEnvelope(\n envelope: AudioEnvelope,\n from: number,\n to: number,\n bars: number\n): EnvelopeBar[] {\n const count = Math.max(0, Math.floor(bars))\n const out: EnvelopeBar[] = new Array(count)\n for (let i = 0; i < count; i++) out[i] = { magnitude: 0, speaker: -1 }\n\n const buckets = envelope.magnitude.length\n if (count === 0 || buckets === 0 || envelope.duration <= 0) return out\n\n const perSecond = buckets / envelope.duration\n const span = (to - from) / count\n\n for (let i = 0; i < count; i++) {\n const start = (from + span * i) * perSecond\n const end = (from + span * (i + 1)) * perSecond\n\n // At least one bucket per column, so a window zoomed in past the grid draws\n // the sample it is standing on rather than an empty band.\n const first = Math.floor(start)\n const last = Math.max(first, Math.ceil(end) - 1)\n\n let loudest = 0\n let speaker = -1\n for (let b = first; b <= last; b++) {\n if (b < 0 || b >= buckets) continue\n const value = envelope.magnitude[b]\n if (value < loudest) continue\n loudest = value\n speaker = envelope.speaker ? envelope.speaker[b] : -1\n }\n\n out[i] = { magnitude: loudest, speaker }\n }\n\n return out\n}\n\n/**\n * The same window, with everything that isn't `voice` silenced.\n *\n * The point of the speaker filter, and the reason it silences rather than\n * *drops*: a podcast visualiser filtered to one host should go quiet while the\n * other one talks, not compress their turn out of existence. Keeping the bars\n * and zeroing them is what makes the picture read as \"this person is not\n * speaking right now\" instead of as a jump cut.\n *\n * `voice` of `null` returns the bars untouched, which is the unfiltered node.\n */\nexport function forSpeaker(\n bars: EnvelopeBar[],\n voice: number | null\n): EnvelopeBar[] {\n if (voice === null) return bars\n return bars.map((bar) =>\n bar.speaker === voice ? bar : { magnitude: 0, speaker: bar.speaker }\n )\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,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,MAAM,GAAG,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAA;IAClC,IAAI,MAAM,IAAI,CAAC;QAAE,OAAO,CAAC,CAAA;IACzB,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,CAAA;AAC3C,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,QAAQ,CAAC,CAAY,EAAE,GAAW;IACzC,IAAI,GAAG,KAAK,QAAQ;QAAE,OAAO,CAAC,CAAC,UAAU,CAAA;IACzC,IAAI,GAAG,KAAK,OAAO;QAAE,OAAO,CAAC,CAAC,IAAI,CAAA;IAElC,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAA;IAChD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC,IAAI,CAAA;IACvD,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAA;AAClD,CAAC","sourcesContent":["import {\r\n Types,\r\n component2d,\r\n easeInOut,\r\n fillOps,\r\n type Fill,\r\n type FillResolved,\r\n type NodeOf,\r\n type RenderContext,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Node2D as _Node2D,\r\n type Node2DProps as _Node2DProps,\r\n} from \"@motionscript/sdk\"\r\n\r\nimport { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from \"./envelope\"\r\nimport {\r\n clamp01,\r\n groupByPaint,\r\n layOutBars,\r\n paintBars,\r\n voiceColor,\r\n type WaveformAlignment,\r\n} from \"./shared\"\r\n\r\n/** A paint entry as the waveforms declare theirs: resolved and tweened like a fill, its kind read off the default. */\r\nconst paint = (initial: string) =>\r\n Types.custom<Fill, FillResolved[]>(initial, { resolve: fillOps.resolve, lerp: fillOps.lerp })\r\n\r\n/**\r\n * A **waveform overview**: the whole recording's shape, drawn at once.\r\n *\r\n * The picture every audio editor opens with, and the one a podcast clip puts\r\n * under its caption — a static silhouette of the file, with a played portion\r\n * that advances. It answers \"how long is this and where are the loud parts\",\r\n * which is a question about the *file*, not about the moment; its sibling\r\n * {@link LiveWaveform} answers the other one.\r\n *\r\n * ## Progress is authored, not assumed\r\n *\r\n * `follow` is off by default, and that default is the considered one. A\r\n * scene has no idea where the recording it is drawing sits on the video's audio\r\n * lane — a clip is placed, trimmed and sped up out there, and none of that is\r\n * visible from in here. So the honest default is that the fill is *animation*:\r\n * the author sweeps it with a command, the same as any other tween, and it lands\r\n * wherever they put it. `follow` is the shortcut for the common arrangement\r\n * where the node appears exactly as the track starts, and it says so by being a\r\n * switch someone has to turn on rather than a behaviour that quietly disagrees\r\n * with the mix.\r\n *\r\n * ## Voices instead of progress\r\n *\r\n * With `byVoice` on, the played/ahead split is replaced by one colour per\r\n * speaking voice, at a size you can read. For a two-hander it is a map of the conversation: who has the\r\n * floor, where the turns are, how long each one ran. It replaces the progress\r\n * colours rather than combining with them, because a bar cannot mean two things\r\n * at once and \"played, and also Grace\" is a legend nobody can hold in their\r\n * head.\r\n */\r\nexport const FullWaveform = component2d({\r\n key: \"fullWaveform\",\r\n state: {\r\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\r\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\r\n /** How many columns the whole file is drawn as. */\r\n bars: 120,\r\n /** Space between bars, as a fraction of the pitch. See {@link layOutBars}. */\r\n gap: 0.3,\r\n /** Corner radius of a bar, capped at half its width. */\r\n radius: 2,\r\n /** How much of the file reads as played, `0`–`1`. Tweenable. */\r\n progress: 0,\r\n /** Drive `progress` from the node's own clock instead of the prop. */\r\n follow: false,\r\n /** Multiplies every magnitude — a quiet track pulled up to fill its box. */\r\n gain: 1,\r\n /** How much of the box a silent bar still occupies, `0`–`1`. */\r\n floor: 0.015,\r\n align: Types.custom<WaveformAlignment>(\"center\"),\r\n /** Colour each voice's turns rather than splitting on played / not played. */\r\n byVoice: false,\r\n /** A colour per voice index. Short or empty falls back to the palette. */\r\n voiceColors: Types.custom<string[]>([]),\r\n /** The bars ahead of the playhead. */\r\n fill: paint(\"#5b5b7a\"),\r\n /** …and the bars behind it. */\r\n playedFill: paint(\"#6366f1\"),\r\n },\r\n // Nothing declares a component's paints but itself: `fill` and `playedFill` both.\r\n load: (loader, state) => {\r\n loader.fill(state.fill)\r\n loader.fill(state.playedFill)\r\n },\r\n commands: {\r\n /**\r\n * Fill the waveform in, left to right — the node's one command, since\r\n * everything else about it is expressible as an ordinary `to`. Eased in-out\r\n * unless it is given an easing of its own.\r\n */\r\n sweep: (timeline, state) => {\r\n const ease = timeline.easing ? null : easeInOut()\r\n const from = state.progress\r\n return timeline.animate((t) => ({ progress: from + (1 - from) * (ease ? ease(t) : t) }))\r\n },\r\n },\r\n render: (ctx, state) => {\r\n drawBars(ctx, state)\r\n ctx.children()\r\n },\r\n})\r\n\r\nexport type FullWaveform = NodeOf<typeof FullWaveform>\r\nexport type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>\r\n\r\ninterface FullState {\r\n readonly envelope: AudioEnvelope\r\n readonly bars: number\r\n readonly gap: number\r\n readonly radius: number\r\n readonly progress: number\r\n readonly follow: boolean\r\n readonly gain: number\r\n readonly floor: number\r\n readonly align: WaveformAlignment\r\n readonly byVoice: boolean\r\n readonly voiceColors: string[]\r\n readonly fill: FillResolved[]\r\n readonly playedFill: FillResolved[]\r\n}\r\n\r\nfunction drawBars(ctx: RenderContext, s: FullState): void {\r\n const { width, height } = ctx.size\r\n if (width <= 0 || height <= 0) return\r\n\r\n const count = Math.max(1, Math.round(s.bars))\r\n const sampled = sampleEnvelope(s.envelope, 0, s.envelope.duration, count)\r\n const boxes = layOutBars(sampled, {\r\n width,\r\n height,\r\n gap: s.gap,\r\n align: s.align,\r\n gain: s.gain,\r\n floor: s.floor,\r\n })\r\n if (boxes.length === 0) return\r\n\r\n // One draw per colour rather than per bar — see {@link paintBars}. The key\r\n // is a string because it has to be comparable; the paint it stands for is\r\n // resolved after the grouping.\r\n const edge = played(ctx, s) * boxes.length\r\n const groups = groupByPaint(boxes, (i) =>\r\n s.byVoice\r\n ? `voice:${sampled[i].speaker}`\r\n : i < edge\r\n ? \"played\"\r\n : \"ahead\"\r\n )\r\n\r\n for (const [key, run] of groups) {\r\n const graphics = paintBars(run, paintFor(s, key), s.radius)\r\n if (graphics) ctx.draw(graphics)\r\n }\r\n}\r\n\r\n/** Where the fill has reached, from whichever source is in charge. */\r\nfunction played(ctx: RenderContext, s: FullState): number {\r\n if (!s.follow) return clamp01(s.progress)\r\n const length = s.envelope.duration\r\n if (length <= 0) return 0\r\n return clamp01(ctx.time.elapsed / length)\r\n}\r\n\r\n/**\r\n * The paint a group takes.\r\n *\r\n * A voice's colour is a **colour string**, never one of the node's two fill\r\n * props, and that is deliberate: a voice is identified by hue, so a gradient\r\n * or an image there would defeat the only thing the colour is for. It also\r\n * means these paints declare no assets, which is why `load` only has to account\r\n * for the two real fill props.\r\n */\r\nfunction paintFor(s: FullState, key: string): Fill {\r\n if (key === \"played\") return s.playedFill\r\n if (key === \"ahead\") return s.fill\r\n\r\n const voice = Number(key.slice(\"voice:\".length))\r\n if (!Number.isFinite(voice) || voice < 0) return s.fill\r\n return s.voiceColors[voice] ?? voiceColor(voice)\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"full-waveform.js","sourceRoot":"","sources":["../../src/waveform/full-waveform.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,WAAW,EACX,SAAS,EACT,OAAO,GAQR,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,cAAc,EAAE,cAAc,EAAsB,MAAM,YAAY,CAAA;AAC/E,OAAO,EACL,OAAO,EACP,YAAY,EACZ,UAAU,EACV,SAAS,EACT,UAAU,GAEX,MAAM,UAAU,CAAA;AAEjB,sHAAsH;AACtH,MAAM,KAAK,GAAG,CAAC,OA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OAAO;QAAE,OAAO,CAAC,CAAC,IAAI,CAAA;IAElC,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAA;IAChD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC,IAAI,CAAA;IACvD,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAA;AAClD,CAAC","sourcesContent":["import {\n Types,\n component2d,\n easeInOut,\n fillOps,\n type Fill,\n type FillResolved,\n type NodeOf,\n type RenderContext,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n} from \"@motionscript/sdk\"\n\nimport { EMPTY_ENVELOPE, sampleEnvelope, type AudioEnvelope } from \"./envelope\"\nimport {\n clamp01,\n groupByPaint,\n layOutBars,\n paintBars,\n voiceColor,\n type WaveformAlignment,\n} from \"./shared\"\n\n/** A paint entry as the waveforms declare theirs: resolved and tweened like a fill, its kind read off the default. */\nconst paint = (initial: string) =>\n Types.custom<Fill, FillResolved[]>(initial, { resolve: fillOps.resolve, lerp: fillOps.lerp })\n\n/**\n * A **waveform overview**: the whole recording's shape, drawn at once.\n *\n * The picture every audio editor opens with, and the one a podcast clip puts\n * under its caption — a static silhouette of the file, with a played portion\n * that advances. It answers \"how long is this and where are the loud parts\",\n * which is a question about the *file*, not about the moment; its sibling\n * {@link LiveWaveform} answers the other one.\n *\n * ## Progress is authored, not assumed\n *\n * `follow` is off by default, and that default is the considered one. A\n * scene has no idea where the recording it is drawing sits on the video's audio\n * lane — a clip is placed, trimmed and sped up out there, and none of that is\n * visible from in here. So the honest default is that the fill is *animation*:\n * the author sweeps it with a command, the same as any other tween, and it lands\n * wherever they put it. `follow` is the shortcut for the common arrangement\n * where the node appears exactly as the track starts, and it says so by being a\n * switch someone has to turn on rather than a behaviour that quietly disagrees\n * with the mix.\n *\n * ## Voices instead of progress\n *\n * With `byVoice` on, the played/ahead split is replaced by one colour per\n * speaking voice, at a size you can read. For a two-hander it is a map of the conversation: who has the\n * floor, where the turns are, how long each one ran. It replaces the progress\n * colours rather than combining with them, because a bar cannot mean two things\n * at once and \"played, and also Grace\" is a legend nobody can hold in their\n * head.\n */\nexport const FullWaveform = component2d({\n key: \"fullWaveform\",\n state: {\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\n /** How many columns the whole file is drawn as. */\n bars: 120,\n /** Space between bars, as a fraction of the pitch. See {@link layOutBars}. */\n gap: 0.3,\n /** Corner radius of a bar, capped at half its width. */\n radius: 2,\n /** How much of the file reads as played, `0`–`1`. Tweenable. */\n progress: 0,\n /** Drive `progress` from the node's own clock instead of the prop. */\n follow: false,\n /** Multiplies every magnitude — a quiet track pulled up to fill its box. */\n gain: 1,\n /** How much of the box a silent bar still occupies, `0`–`1`. */\n floor: 0.015,\n align: Types.custom<WaveformAlignment>(\"center\"),\n /** Colour each voice's turns rather than splitting on played / not played. */\n byVoice: false,\n /** A colour per voice index. Short or empty falls back to the palette. */\n voiceColors: Types.custom<string[]>([]),\n /** The bars ahead of the playhead. */\n fill: paint(\"#5b5b7a\"),\n /** …and the bars behind it. */\n playedFill: paint(\"#6366f1\"),\n },\n // Nothing declares a component's paints but itself: `fill` and `playedFill` both.\n load: (loader, state) => {\n loader.fill(state.fill)\n loader.fill(state.playedFill)\n },\n commands: {\n /**\n * Fill the waveform in, left to right — the node's one command, since\n * everything else about it is expressible as an ordinary `to`. Eased in-out\n * unless it is given an easing of its own.\n */\n sweep: (timeline, state) => {\n const ease = timeline.easing ? null : easeInOut()\n const from = state.progress\n return timeline.animate((t) => ({ progress: from + (1 - from) * (ease ? ease(t) : t) }))\n },\n },\n render: (ctx, state) => {\n drawBars(ctx, state)\n ctx.children()\n },\n})\n\nexport type FullWaveform = NodeOf<typeof FullWaveform>\nexport type FullWaveformProps = NonNullable<Parameters<typeof FullWaveform>[0]>\n\ninterface FullState {\n readonly envelope: AudioEnvelope\n readonly bars: number\n readonly gap: number\n readonly radius: number\n readonly progress: number\n readonly follow: boolean\n readonly gain: number\n readonly floor: number\n readonly align: WaveformAlignment\n readonly byVoice: boolean\n readonly voiceColors: string[]\n readonly fill: FillResolved[]\n readonly playedFill: FillResolved[]\n}\n\nfunction drawBars(ctx: RenderContext, s: FullState): void {\n const { width, height } = ctx.size\n if (width <= 0 || height <= 0) return\n\n const count = Math.max(1, Math.round(s.bars))\n const sampled = sampleEnvelope(s.envelope, 0, s.envelope.duration, count)\n const boxes = layOutBars(sampled, {\n width,\n height,\n gap: s.gap,\n align: s.align,\n gain: s.gain,\n floor: s.floor,\n })\n if (boxes.length === 0) return\n\n // One draw per colour rather than per bar — see {@link paintBars}. The key\n // is a string because it has to be comparable; the paint it stands for is\n // resolved after the grouping.\n const edge = played(ctx, s) * boxes.length\n const groups = groupByPaint(boxes, (i) =>\n s.byVoice\n ? `voice:${sampled[i].speaker}`\n : i < edge\n ? \"played\"\n : \"ahead\"\n )\n\n for (const [key, run] of groups) {\n const graphics = paintBars(run, paintFor(s, key), s.radius)\n if (graphics) ctx.draw(graphics)\n }\n}\n\n/** Where the fill has reached, from whichever source is in charge. */\nfunction played(ctx: RenderContext, s: FullState): number {\n if (!s.follow) return clamp01(s.progress)\n const length = s.envelope.duration\n if (length <= 0) return 0\n return clamp01(ctx.time.elapsed / length)\n}\n\n/**\n * The paint a group takes.\n *\n * A voice's colour is a **colour string**, never one of the node's two fill\n * props, and that is deliberate: a voice is identified by hue, so a gradient\n * or an image there would defeat the only thing the colour is for. It also\n * means these paints declare no assets, which is why `load` only has to account\n * for the two real fill props.\n */\nfunction paintFor(s: FullState, key: string): Fill {\n if (key === \"played\") return s.playedFill\n if (key === \"ahead\") return s.fill\n\n const voice = Number(key.slice(\"voice:\".length))\n if (!Number.isFinite(voice) || voice < 0) return s.fill\n return s.voiceColors[voice] ?? voiceColor(voice)\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/waveform/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEvE,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,UAAU,EACV,SAAS,EACT,UAAU,GACX,MAAM,UAAU,CAAA","sourcesContent":["/**\
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/waveform/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEvE,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,UAAU,EACV,SAAS,EACT,UAAU,GACX,MAAM,UAAU,CAAA","sourcesContent":["/**\n * The waveform family: one subject, two questions.\n *\n * - {@link FullWaveform} draws the **file** — the whole recording's silhouette,\n * with a played portion that advances. \"How long is this, and where are the\n * loud parts.\"\n * - {@link LiveWaveform} draws the **moment** — bars moving with playback, the\n * visualiser a podcast clip puts beside the artwork. \"Is anything happening\n * right now, and whose voice is it.\"\n *\n * Both read the same baked {@link AudioEnvelope}, which is why they cannot\n * disagree about a recording, and why neither listens to anything — see that\n * module for why a scene that has to export cannot use a live analyser.\n */\nexport { FullWaveform } from \"./full-waveform\"\nexport type { FullWaveformProps } from \"./full-waveform\"\nexport { LiveWaveform } from \"./live-waveform\"\nexport type { LiveWaveformProps } from \"./live-waveform\"\nexport { EMPTY_ENVELOPE, forSpeaker, sampleEnvelope } from \"./envelope\"\nexport type { AudioEnvelope, EnvelopeBar } from \"./envelope\"\nexport {\n VOICE_PALETTE,\n WAVEFORM_ALIGNMENTS,\n WAVEFORM_STYLES,\n groupByPaint,\n layOutBars,\n paintBars,\n voiceColor,\n} from \"./shared\"\nexport type { BarBox, WaveformAlignment, WaveformStyle } from \"./shared\"\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"live-waveform.js","sourceRoot":"","sources":["../../src/waveform/live-waveform.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,WAAW,EACX,OAAO,GAQR,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EACL,cAAc,EACd,UAAU,EACV,cAAc,GAGf,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,OAAO,EACP,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,SAAS,EACT,UAAU,GAGX,MAAM,UAAU,CAAA;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,WAAW,CAAC;IACtC,GAAG,EAAE,cAAc;IACnB,KAAK,EAAE;QACL,0EAA0E;QAC1E,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAgB,cAAc,CAAC;QACrD,+CAA+C;QAC/C,IAAI,EAAE,EAAE;QACR,sDAAsD;QACtD,GAAG,EAAE,IAAI;QACT,MAAM,EAAE,CAAC;QACT,uDAAuD;QACvD,MAAM,EAAE,CAAC;QACT,0DAA0D;QAC1D,MAAM,EAAE,CAAC;QACT,yEAAyE;QACzE,OAAO,EAAE,IAAI;QACb,uDAAuD;QACvD,IAAI,EAAE,GAAG;QACT,gEAAgE;QAChE,KAAK,EAAE,IAAI;QACX,KAAK,EAAE,KAAK,CAAC,MAAM,CAAoB,QAAQ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E,GAAG,CAAC,SAAS,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAA;IACpE,CAAC,CAAC,CAAA;AACJ,CAAC","sourcesContent":["import {\r\n Types,\r\n component2d,\r\n fillOps,\r\n type Fill,\r\n type FillResolved,\r\n type NodeOf,\r\n type RenderContext,\r\n // Named so the emitted declaration reaches these through the sdk, not core.\r\n type Node2D as _Node2D,\r\n type Node2DProps as _Node2DProps,\r\n} from \"@motionscript/sdk\"\r\n\r\nimport {\r\n EMPTY_ENVELOPE,\r\n forSpeaker,\r\n sampleEnvelope,\r\n type AudioEnvelope,\r\n type EnvelopeBar,\r\n} from \"./envelope\"\r\nimport {\r\n clamp01,\r\n groupByPaint,\r\n layOutBars,\r\n livePaintKey,\r\n paintBars,\r\n voiceColor,\r\n type WaveformAlignment,\r\n type WaveformStyle,\r\n} from \"./shared\"\r\n\r\n/**\r\n * A **live audio visualiser**: bars that move with the recording as it plays.\r\n *\r\n * The thing every podcast-to-video tool puts beside the artwork. It is not a\r\n * picture of the file — that is {@link FullWaveform} — it is a picture of *this\r\n * moment*, and what it is for is making a static frame of a talking head feel\r\n * like it is playing.\r\n *\r\n * ## \"Live\" without listening\r\n *\r\n * Nothing here reads an audio device. The bars are sampled out of a baked\r\n * envelope at the node's own elapsed time, which is what makes the visualiser\r\n * survive the thing it exists for: an **export**, where there is no playback to\r\n * analyse and frames may be rendered out of order or in parallel. A frame is a\r\n * pure function of its time, so scrubbing back and forth over the visualiser\r\n * redraws exactly what it drew the first time — which an analyser node, by\r\n * construction, cannot promise. See {@link AudioEnvelope}.\r\n *\r\n * ## The speaker filter\r\n *\r\n * `speaker` narrows the node to one voice of a diarized recording, and it\r\n * is the reason this node knows about transcripts at all. Two visualisers beside\r\n * two headshots, each filtered to its own host, is a podcast edit that would\r\n * otherwise be done by hand for every turn — and because the filter *silences*\r\n * rather than skips (see {@link forSpeaker}), the quiet host visibly stops\r\n * talking instead of vanishing. Nothing about that requires the words: only who\r\n * is talking when, which is what the diarizer produces.\r\n *\r\n * A filter naming a voice the recording doesn't have draws a resting row rather\r\n * than everything — a picture of \"this person never speaks here\", which is\r\n * true, where falling back to the unfiltered mix would be a silent lie about\r\n * whose voice you were watching.\r\n */\r\nexport const LiveWaveform = component2d({\r\n key: \"liveWaveform\",\r\n state: {\r\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\r\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\r\n /** How many bars the visualiser is made of. */\r\n bars: 48,\r\n /** Space between bars, as a fraction of the pitch. */\r\n gap: 0.35,\r\n radius: 4,\r\n /** How many seconds of audio are on screen at once. */\r\n window: 3,\r\n /** Seconds into the file the node's first frame shows. */\r\n offset: 0,\r\n /** Advance with the scene clock. Off freezes the picture at `offset`. */\r\n playing: true,\r\n /** Multiplies every magnitude before it is clamped. */\r\n gain: 1.4,\r\n /** How much of the box a silent bar still occupies, `0`–`1`. */\r\n floor: 0.04,\r\n align: Types.custom<WaveformAlignment>(\"center\"),\r\n style: Types.custom<WaveformStyle>(\"bars\"),\r\n /**\r\n * Show only this voice's activity, by index into the recording's own\r\n * numbering. `-1` — the default — is every voice.\r\n */\r\n speaker: -1,\r\n /** Colour each bar by whoever is speaking in it. */\r\n byVoice: false,\r\n /** A colour per voice index. Short or empty falls back to the palette. */\r\n voiceColors: Types.custom<string[]>([]),\r\n fill: Types.custom<Fill, FillResolved[]>(\"#6366f1\", { resolve: fillOps.resolve, lerp: fillOps.lerp }),\r\n },\r\n // Nothing declares a component's paints but itself.\r\n load: (loader, state) => loader.fill(state.fill),\r\n render: (ctx, state) => {\r\n drawBars(ctx, state)\r\n ctx.children()\r\n },\r\n})\r\n\r\nexport type LiveWaveform = NodeOf<typeof LiveWaveform>\r\nexport type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>\r\n\r\ninterface LiveState {\r\n readonly envelope: AudioEnvelope\r\n readonly bars: number\r\n readonly gap: number\r\n readonly radius: number\r\n readonly window: number\r\n readonly offset: number\r\n readonly playing: boolean\r\n readonly gain: number\r\n readonly floor: number\r\n readonly align: WaveformAlignment\r\n readonly style: WaveformStyle\r\n readonly speaker: number\r\n readonly byVoice: boolean\r\n readonly voiceColors: string[]\r\n readonly fill: FillResolved[]\r\n}\r\n\r\n/**\r\n * The second of the recording the visualiser is showing.\r\n *\r\n * Off the node's own clock rather than the scene's, which is what \"from the\r\n * moment this node appeared\" means — the same clock the engine's own\r\n * `Video` times its picture from, and the same one that makes a node dropped\r\n * halfway through a clip start its recording at its entrance rather than\r\n * partway in.\r\n */\r\nfunction at(ctx: RenderContext, s: LiveState): number {\r\n return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0)\r\n}\r\n\r\nfunction drawBars(ctx: RenderContext, s: LiveState): void {\r\n const { width, height } = ctx.size\r\n if (width <= 0 || height <= 0) return\r\n\r\n const count = Math.max(1, Math.round(s.bars))\r\n const span = Math.max(0.05, s.window)\r\n const now = at(ctx, s)\r\n const voice = s.speaker < 0 ? null : Math.round(s.speaker)\r\n\r\n // The window is centred on the playhead, so the bars run *toward* the\r\n // middle and away again — the arrangement every mixer's meter uses, and the\r\n // one that reads as sound arriving rather than as a strip scrolling past.\r\n const sampled = forSpeaker(\r\n sampleEnvelope(s.envelope, now - span / 2, now + span / 2, count),\r\n voice\r\n )\r\n\r\n const shaped = s.style === \"line\" ? taper(sampled) : sampled\r\n const boxes = layOutBars(shaped, {\r\n width,\r\n height,\r\n gap: s.style === \"blocks\" ? 0.12 : s.gap,\r\n align: s.align,\r\n gain: s.gain,\r\n floor: s.floor,\r\n })\r\n if (boxes.length === 0) return\r\n\r\n const groups = groupByPaint(boxes, (i) =>\r\n livePaintKey(shaped[i].speaker, s.byVoice, voice)\r\n )\r\n\r\n for (const [key, run] of groups) {\r\n const graphics = paintBars(run, paintFor(s, key), s.radius)\r\n if (graphics) ctx.draw(graphics)\r\n }\r\n}\r\n\r\n/** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */\r\nfunction paintFor(s: LiveState, key: string): Fill {\r\n if (key === \"all\") return s.fill\r\n const voice = Number(key.slice(\"voice:\".length))\r\n if (!Number.isFinite(voice) || voice < 0) return s.fill\r\n return s.voiceColors[voice] ?? voiceColor(voice)\r\n}\r\n\r\n/**\r\n * Rolls the ends of a window off toward silence.\r\n *\r\n * What separates the `line` style from `bars`: a hard-edged window makes the\r\n * loud bar entering at the right edge appear from nothing, which reads as a\r\n * glitch rather than as sound arriving. Fading the outer fifth turns the same\r\n * data into a shape that grows out of the baseline — the profile a spectrum\r\n * display has, and the reason it looks like one object rather than a row of\r\n * separate meters.\r\n */\r\nfunction taper(bars: readonly EnvelopeBar[]): EnvelopeBar[] {\r\n const count = bars.length\r\n if (count < 3) return [...bars]\r\n const edge = Math.max(1, Math.floor(count / 5))\r\n\r\n return bars.map((bar, i) => {\r\n const fromEnd = Math.min(i, count - 1 - i)\r\n const weight = clamp01(fromEnd / edge)\r\n return { magnitude: bar.magnitude * weight, speaker: bar.speaker }\r\n })\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"live-waveform.js","sourceRoot":"","sources":["../../src/waveform/live-waveform.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,WAAW,EACX,OAAO,GAQR,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EACL,cAAc,EACd,UAAU,EACV,cAAc,GAGf,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,OAAO,EACP,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,SAAS,EACT,UAAU,GAGX,MAAM,UAAU,CAAA;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,WAAW,CAAC;IACtC,GAAG,EAAE,cAAc;IACnB,KAAK,EAAE;QACL,0EAA0E;QAC1E,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAgB,cAAc,CAAC;QACrD,+CAA+C;QAC/C,IAAI,EAAE,EAAE;QACR,s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,CAAC,CAAC,CAAA;IAE/C,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE;QACzB,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,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 Types,\n component2d,\n fillOps,\n type Fill,\n type FillResolved,\n type NodeOf,\n type RenderContext,\n // Named so the emitted declaration reaches these through the sdk, not core.\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n} from \"@motionscript/sdk\"\n\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\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 * `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 */\nexport const LiveWaveform = component2d({\n key: \"liveWaveform\",\n state: {\n /** The baked recording. See {@link AudioEnvelope} for why it is baked. */\n envelope: Types.custom<AudioEnvelope>(EMPTY_ENVELOPE),\n /** How many bars the visualiser is made of. */\n bars: 48,\n /** Space between bars, as a fraction of the pitch. */\n gap: 0.35,\n radius: 4,\n /** How many seconds of audio are on screen at once. */\n window: 3,\n /** Seconds into the file the node's first frame shows. */\n offset: 0,\n /** Advance with the scene clock. Off freezes the picture at `offset`. */\n playing: true,\n /** Multiplies every magnitude before it is clamped. */\n gain: 1.4,\n /** How much of the box a silent bar still occupies, `0`–`1`. */\n floor: 0.04,\n align: Types.custom<WaveformAlignment>(\"center\"),\n style: Types.custom<WaveformStyle>(\"bars\"),\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: -1,\n /** Colour each bar by whoever is speaking in it. */\n byVoice: false,\n /** A colour per voice index. Short or empty falls back to the palette. */\n voiceColors: Types.custom<string[]>([]),\n fill: Types.custom<Fill, FillResolved[]>(\"#6366f1\", { resolve: fillOps.resolve, lerp: fillOps.lerp }),\n },\n // Nothing declares a component's paints but itself.\n load: (loader, state) => loader.fill(state.fill),\n render: (ctx, state) => {\n drawBars(ctx, state)\n ctx.children()\n },\n})\n\nexport type LiveWaveform = NodeOf<typeof LiveWaveform>\nexport type LiveWaveformProps = NonNullable<Parameters<typeof LiveWaveform>[0]>\n\ninterface LiveState {\n readonly envelope: AudioEnvelope\n readonly bars: number\n readonly gap: number\n readonly radius: number\n readonly window: number\n readonly offset: number\n readonly playing: boolean\n readonly gain: number\n readonly floor: number\n readonly align: WaveformAlignment\n readonly style: WaveformStyle\n readonly speaker: number\n readonly byVoice: boolean\n readonly voiceColors: string[]\n readonly fill: FillResolved[]\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 the engine's own\n * `Video` times its picture from, and the same one that makes a node dropped\n * halfway through a clip start its recording at its entrance rather than\n * partway in.\n */\nfunction at(ctx: RenderContext, s: LiveState): number {\n return s.offset + (s.playing ? Math.max(0, ctx.time.elapsed) : 0)\n}\n\nfunction drawBars(ctx: RenderContext, s: LiveState): void {\n const { width, height } = ctx.size\n if (width <= 0 || height <= 0) return\n\n const count = Math.max(1, Math.round(s.bars))\n const span = Math.max(0.05, s.window)\n const now = at(ctx, s)\n const voice = s.speaker < 0 ? null : Math.round(s.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(s.envelope, now - span / 2, now + span / 2, count),\n voice\n )\n\n const shaped = s.style === \"line\" ? taper(sampled) : sampled\n const boxes = layOutBars(shaped, {\n width,\n height,\n gap: s.style === \"blocks\" ? 0.12 : s.gap,\n align: s.align,\n gain: s.gain,\n floor: s.floor,\n })\n if (boxes.length === 0) return\n\n const groups = groupByPaint(boxes, (i) =>\n livePaintKey(shaped[i].speaker, s.byVoice, voice)\n )\n\n for (const [key, run] of groups) {\n const graphics = paintBars(run, paintFor(s, key), s.radius)\n if (graphics) ctx.draw(graphics)\n }\n}\n\n/** See `FullWaveform`'s paints — a voice is a hue, never a gradient. */\nfunction paintFor(s: LiveState, key: string): Fill {\n if (key === \"all\") return s.fill\n const voice = Number(key.slice(\"voice:\".length))\n if (!Number.isFinite(voice) || voice < 0) return s.fill\n return s.voiceColors[voice] ?? voiceColor(voice)\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"]}
|