@hussmartinez/audio-sdk 0.5.0 → 0.7.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/README.md CHANGED
@@ -1,62 +1,144 @@
1
1
  # audio-sdk
2
2
 
3
- A client-side audio SDK: trim, split, fade, adjust gain and export audio entirely in the browser. Audio never leaves the device. Its headless core is published; a React editor (`AudioEditor`) and a `<hm-audio-editor>` custom element come later.
3
+ A client-side audio SDK: layer recordings on tracks, trim, split, fade, adjust gain, play and export audio entirely in the browser. Audio never leaves the device. Its headless core is published; a React editor (`AudioEditor`) and a `<hm-audio-editor>` custom element come later.
4
4
 
5
5
  This repository publishes **[`@hussmartinez/audio-sdk`](https://www.npmjs.com/package/@hussmartinez/audio-sdk)**. It follows the family standards in [`sdk-core`'s `STANDARDS.md`](https://github.com/ie-huss-martinez/sdk-core/blob/main/STANDARDS.md), and in particular the decisions on [hostile input and no network](https://github.com/ie-huss-martinez/sdk-core/blob/main/docs/adr/0012-hostile-input-and-no-network.md), [workers and assets](https://github.com/ie-huss-martinez/sdk-core/blob/main/docs/adr/0011-worker-messaging-and-assets.md) and [ports and adapters](https://github.com/ie-huss-martinez/sdk-core/blob/main/docs/adr/0008-ports-adapters-and-session.md).
6
6
 
7
- > Status: pre-1.0. The headless core is published: opening, editing with undo, and exporting to WAV. Playback and the editor UI are next, and the API may change between minor versions until 1.0.
7
+ > Status: pre-1.0. The headless core is published: opening, multitrack editing with undo, playback, and exporting to WAV ([ADR 0006](docs/adr/0006-multitrack.md)). The editor UI is next, and the API may change between minor versions until 1.0.
8
8
 
9
9
  ## Editing and exporting
10
10
 
11
- `createAudioSession()` holds the recording being edited: a clip plan over decoded sources, edited with commands, with undo and redo ([ADR 0003](docs/adr/0003-clip-plan-document-model.md)). It applies the file policy for you, and `export()` renders the result in a worker:
11
+ `createAudioSession()` holds what's being edited: a plan of tracks, each with clips of decoded sources placed in time, edited with commands, with undo and redo ([ADR 0003](docs/adr/0003-clip-plan-document-model.md), [ADR 0006](docs/adr/0006-multitrack.md)). It applies the file policy for you, and `export()` mixes the tracks in a worker:
12
12
 
13
13
  ```ts
14
14
  import {
15
+ audioErrorText,
15
16
  audioMessages,
16
- createAudioEngine,
17
- createAudioProcessor,
17
+ audioWarningText,
18
+ createAudioAdapters,
18
19
  createAudioSession,
19
20
  } from "@hussmartinez/audio-sdk";
20
21
  import { createMessages } from "@hussmartinez/sdk-core";
21
22
 
22
23
  const messages = createMessages(audioMessages, { locale: "en" });
24
+ // The engine, processor and output, made together; yours to destroy.
25
+ const adapters = createAudioAdapters(); // or { assetsUrl } to self-host the worker
23
26
  const session = createAudioSession({
24
- engine: createAudioEngine(),
25
- processor: createAudioProcessor(),
27
+ engine: adapters.engine,
28
+ processor: adapters.processor,
26
29
  filePolicy: { maxDuration: 30 * 60 * 1_000 }, // or leave it out for the defaults
27
30
  onEvent: (event) => analytics.track(event), // anonymous: no names, samples or metadata
28
31
  });
29
- session.subscribe(() => render(session.getSnapshot())); // or useSyncExternalStore in React
32
+ session.subscribe(() => render(session.getSnapshot()));
33
+ // Or in React: useSyncExternalStore(session.subscribe, session.getSnapshot).
30
34
 
31
35
  try {
32
36
  await session.open(file, { onProgress: showProgress });
33
37
 
34
- // Cut the first second, and fade the rest in over half a second.
38
+ // The recording is one clip on one track. Cut its first second, closing
39
+ // the gap, and fade the rest in over half a second.
35
40
  const { plan } = session.getSnapshot();
36
41
  const rate = plan?.sampleRate ?? 48_000;
37
- session.execute({ type: "audio.deleteRange", start: 0, end: rate, newKey: session.createKey() });
38
- const [clip] = session.getSnapshot().plan?.clips ?? [];
42
+ const [voice] = plan?.tracks ?? [];
43
+ if (voice) {
44
+ session.execute({
45
+ type: "audio.deleteRange",
46
+ start: 0,
47
+ end: rate,
48
+ tracks: [voice.key],
49
+ ripple: true,
50
+ });
51
+ }
52
+ const [clip] = session.getSnapshot().plan?.tracks[0]?.clips ?? [];
39
53
  if (clip) {
40
54
  session.execute({ type: "audio.setFade", key: clip.key, edge: "in", frames: rate / 2, curve: "equalPower" });
41
55
  }
56
+
57
+ // Lay music under it, on a track of its own, 12 dB down.
58
+ await session.insert(music, { onProgress: showProgress });
59
+ const bed = session.getSnapshot().plan?.tracks.at(-1);
60
+ if (bed) session.execute({ type: "audio.setTrack", key: bed.key, name: "Music", gainDb: -12 });
61
+
42
62
  session.execute({ type: "audio.setMetadata", field: "title", value: "Interview" });
43
63
  session.undo(); // and session.redo()
44
64
 
45
- const wav = await session.export({
65
+ const { blob, peakDb, clippedFrames } = await session.export({
46
66
  format: { container: "wav", sampleFormat: "pcm24" },
47
67
  onProgress: showProgress,
48
68
  });
49
- offerDownload(wav); // a Blob: the session never downloads anything itself
69
+ if (clippedFrames > 0) warnOfClipping(peakDb); // the mix went this far over full scale
70
+ offerDownload(blob); // the session never downloads anything itself
50
71
  } catch (error) {
51
- showError(messages.error(error)); // text by code, never the error's message
72
+ showError(audioErrorText(messages, error)); // text by code and reason, never the error's message
52
73
  }
53
74
  ```
54
75
 
55
- - **The snapshot** has the `plan` (clips in frames, and the metadata), the `sources`, `canUndo` and `canRedo`, `busy`, the last `error`, and `warnings`: limits past their warning thresholds, and `truncated` for a WAV or AIFF file cut short, which opens with the audio it holds. Show their text from `warnings.<name>` in `audioMessages`.
56
- - **Commands** are plain data, such as `audio.splitClip` or `audio.setGain`, with an exact inverse for undo. One that doesn't apply throws `INVALID_COMMAND` with a stable `details.reason`, such as `unknown-clip`. `session.group(fn)` makes several commands, such as a drag, one undo step.
57
- - **`session.insert(file, { at })`** adds another recording at a frame, as one undo step. It must have the session's sample rate, for now.
58
- - **Exports** render the plan as it is when the export starts, as 16-bit (the default) or 24-bit PCM, or 32-bit float WAV, with the metadata in the file. They run under `maxProcessingTime`, and the `signal` option cancels them.
59
- - **Plans are portable JSON:** `toAudioPlanDocument` and `readAudioPlanDocument` write and read the `hm-audio-plan` format, whose JSON Schema ships in the package, at `@hussmartinez/audio-sdk/formats/hm-audio-plan/v1.schema.json` ([format record](docs/formats/hm-audio-plan.md)).
76
+ - **The snapshot** has the `plan` (its tracks, their clips in frames, the master gain and the metadata), the `sources`, the `selection`, `canUndo` and `canRedo`, `busy`, the last `error`, and `warnings`: limits past their warning thresholds, and `truncated` for a WAV or AIFF file cut short, which opens with the audio it holds. Show each one's text with `audioWarningText(messages, warning)`.
77
+ - **Tracks** play together. Each has a name, a gain, mute and solo, and clips placed in time that never overlap: two sounds at once are two tracks. `audibleTracks(plan)` says which ones sound.
78
+ - **Commands** are plain data, such as `audio.splitClip`, `audio.moveClips` or `audio.setTrack`, with an exact inverse for undo. Clip edits never move another clip: they fail with `clips-overlap` rather than push one. Time edits, `audio.deleteRange`, `audio.keepRange` and `audio.insertTime`, move what's after them on the tracks they name ([the format record](docs/formats/hm-audio-plan.md) lists every command). One that doesn't apply throws `INVALID_COMMAND` with a stable `details.reason`, such as `unknown-clip`. `session.group(fn)` makes several commands, such as a drag, one undo step.
79
+ - **Keys you leave out, the session gives:** a split's `newKey`, a new track's `key`, and the `newKeys` of a time edit, one for every track it names. The history records them, so undo and redo bring back the same keys. Pass your own, from `session.createKey()`, when you need to know them before the command runs.
80
+ - **A command that changes nothing,** such as a gain that's already set, succeeds without an undo step.
81
+ - **The selection** is a range of frames on some tracks, `{ start, end, tracks }`, set with `session.select()`. A time edit clears it, because it moves what was selected. Other edits, undo and redo keep it, clamped to the plan and to its tracks, and cleared when nothing's left.
82
+ - **Recordings have names,** for showing people: a `File`'s own name, or the `name` you pass to `open()` or `insert()`, on each entry in `snapshot.sources`. A file's name is hostile input, so it's cleaned as pdf-sdk cleans one: the last path segment, without control, bidirectional formatting or Windows-refused characters, in NFC, without leading dots or surrounding space, and at most 128 code points. Names never go into events, the plan or a document. Show them as text, never as HTML.
83
+ - **The session's methods work on their own,** without `session.`, so they can be passed around as `useSyncExternalStore` takes them. So do the output's.
84
+ - **`session.insert(file, { track, at })`** adds another recording, as one undo step: on a new track of its own, or on the track you name, where it needs free space. A recording at another sample rate is converted to the session's, in the worker, and its entry in `snapshot.sources` gives the file's own rate as `resampledFrom`. One with more channels widens the plan to its count.
85
+ - **Exports** mix the audible tracks as they are when the export starts, up to where the last audible clip ends, as 16-bit (the default) or 24-bit PCM, or 32-bit float WAV, with the metadata in the file. Tracks sum at unity, and the mix is clamped to full scale, never limited or normalised: the result's `peakDb` and `clippedFrames` say how far over it went, so you can lower the master gain with `audio.setMasterGain` and export again. They run under `maxProcessingTime`, and the `signal` option cancels them.
86
+ - **Plans are portable JSON:** `toAudioPlanDocument` and `readAudioPlanDocument` write and read the `hm-audio-plan` format, version 2, whose JSON Schema ships in the package, at `@hussmartinez/audio-sdk/formats/hm-audio-plan/v2.schema.json` ([format record](docs/formats/hm-audio-plan.md)). Version 1 documents are read too, as one track, and `migrateAudioCommand` turns a stored version 1 command into version 2's.
87
+
88
+ ## Playback
89
+
90
+ `createAudioOutput()` plays a plan through Web Audio, and `connectOutput()` keeps it in step with a session, so playback follows every edit, undo and insert, even while playing ([ADR 0002](docs/adr/0002-workers-and-assets.md), [ADR 0003](docs/adr/0003-clip-plan-document-model.md)):
91
+
92
+ ```ts
93
+ import {
94
+ audioErrorText,
95
+ connectOutput,
96
+ formatTimecode,
97
+ isAudioError,
98
+ watchPlayhead,
99
+ } from "@hussmartinez/audio-sdk";
100
+
101
+ const { output } = adapters; // or createAudioOutput() on its own
102
+ const disconnect = connectOutput(session, output);
103
+
104
+ playButton.onclick = async () => {
105
+ if (output.getSnapshot().state === "playing") return output.pause();
106
+ try {
107
+ await output.play(); // from the playhead, or from the start when it's at the end
108
+ } catch (error) {
109
+ // An autoplay rule kept sound from starting: a press of Play starts it.
110
+ if (isAudioError(error, "UNSUPPORTED_ENVIRONMENT")) showNotice(audioErrorText(messages, error));
111
+ }
112
+ };
113
+ output.subscribe(() => showState(output.getSnapshot().state)); // "stopped", "playing" or "paused"
114
+
115
+ // The playhead isn't in the snapshot: watch it, on animation frames while
116
+ // playing and whenever it jumps. It's in whole frames, at the plan's rate.
117
+ const rate = session.getSnapshot().plan?.sampleRate ?? 48_000;
118
+ const unwatch = watchPlayhead(output, (frame) => {
119
+ drawPlayhead(frame);
120
+ time.textContent = formatTimecode(frame, rate); // "0:12.40"
121
+ });
122
+
123
+ output.seek(5 * 48_000); // while playing, playback carries on from there
124
+
125
+ // When you're done: stop watching, disconnect, close, then release the adapters.
126
+ unwatch();
127
+ disconnect();
128
+ await session.close();
129
+ await adapters.destroy(); // the engine, the processor and the output
130
+ ```
131
+
132
+ - **Start playback from a user gesture.** Browsers block audio until someone interacts with the page. The first `play()` creates the one `AudioContext`; if it doesn't start within two seconds, `play()` rejects with `UNSUPPORTED_ENVIRONMENT` and `details.reason` set to `"autoplay-blocked"`, and nothing changes. `audioErrorText` gives it its own text: sound needs a press of Play.
133
+ - **The playhead** is a whole number of frames, read from the audio clock by `output.position()`. It isn't in the snapshot, because it changes on every audio frame, so subscribers aren't called as it moves. They are called when it jumps, after a seek, a stop or a plan that moves it, with a new snapshot in the same state. `watchPlayhead(output, listener)` does the rest: it reads it on animation frames while playing, asks for none otherwise, and calls you only when it changes.
134
+ - **`formatTimecode(frames, sampleRate)`** writes a position as an editor shows it: `0:12.40`, and `1:02:05.00` from an hour on. Hundredths are rounded down, so the time shown is never ahead of what's heard.
135
+ - **`peakColumns(peaks, { from, to, columns })`** gives a waveform's columns from `session.getPeaks(source)`: each column's lowest and highest sample over every channel, from the overview level that fits, so drawing costs the same for an hour as for a minute.
136
+ - **At the end,** the state becomes `stopped` and the playhead stays at the end. `stop()` goes back to the start.
137
+ - **Playback is what export renders:** the same audible tracks, track gains and master gain; each clip's gain and fades come from the same `gainAt`, and sources mix into the plan's channels by the same rules. One difference: playback isn't clamped, so the device clips at full scale where export clamps.
138
+ - **A mute, a solo, a track's gain and the master gain are heard at once.** A plan that differs only in those is applied to what's playing over 5 ms, without rescheduling a clip, so there's no gap and no click. Any other edit reschedules from the playhead.
139
+ - **Opening another recording, or closing,** stops the output at the start. An insert doesn't.
140
+ - **The session doesn't own what you give it.** `session.close()` releases the recording, not the engine, the processor or the output: destroy them yourself, as above, with `adapters.destroy()`, or each one's `destroy()` if you made them with `createAudioEngine`, `createAudioProcessor` and `createAudioOutput`.
141
+ - **No Content Security Policy directive** is needed for playback: it uses no worker and makes no requests.
60
142
 
61
143
  ## Using the engine directly
62
144
 
@@ -117,7 +199,7 @@ try {
117
199
  | Mode | How | Directives |
118
200
  |---|---|---|
119
201
  | Inline (the default) | `createAudioEngine()` and `createAudioProcessor()`: the worker is inlined and starts from a `blob:` URL | `worker-src blob:` |
120
- | Self-hosted | `createAudioEngine({ assetsUrl: "/audio-sdk/" })`, and the same option for `createAudioProcessor`: serve the package's `dist/assets/` there; the worker is `audio/audio.worker.js` | `worker-src 'self'` |
202
+ | Self-hosted | `createAudioAdapters({ assetsUrl: "/audio-sdk/" })`, or the same option for `createAudioEngine` and `createAudioProcessor`: serve the package's `dist/assets/` there; the worker is `audio/audio.worker.js` | `worker-src 'self'` |
121
203
 
122
204
  Neither mode needs `'unsafe-eval'` or `'wasm-unsafe-eval'`, and the SDK makes no network requests of its own ([ADR 0002](docs/adr/0002-workers-and-assets.md)).
123
205
 
@@ -142,8 +224,9 @@ The session adds:
142
224
 
143
225
  | Code | When |
144
226
  |---|---|
145
- | `TOO_MANY_FILES`, `SESSION_TOO_LARGE`, `TOO_MANY_CLIPS` | An insert or an edit would exceed `maxFiles`, `maxSessionSize` or `maxClips` |
146
- | `INVALID_COMMAND` | A command doesn't apply, or there's nothing to export; `details.reason` says why |
227
+ | `TOO_MANY_FILES`, `SESSION_TOO_LARGE`, `TOO_MANY_CLIPS`, `TOO_MANY_TRACKS` | An insert or an edit would exceed `maxFiles`, `maxSessionSize`, `maxClips` or `maxTracks` |
228
+ | `DURATION_TOO_LONG` | An insert or an edit would make the plan longer than `maxDuration`: clips can be placed anywhere, so the plan's length has the limit a file's has |
229
+ | `INVALID_COMMAND` | A command doesn't apply, or there's nothing to export; `details.reason` says why, such as `clips-overlap`, `empty-plan` or `nothing-audible` |
147
230
  | `INVALID_DOCUMENT` | A plan document isn't valid; `details.reason` says why. Another format or a later version is `UNSUPPORTED_FORMAT` |
148
231
 
149
232
  ## Development
@@ -1 +1 @@
1
- var e=/* @__PURE__ */ Symbol.for(`@hussmartinez/sdk-core/SdkError`),t=class extends Error{name=`SdkError`;code;constructor(t,n,r={}){super(n,`cause`in r?{cause:r.cause}:void 0),this.code=t,Object.defineProperty(this,e,{value:!0}),r.details!==void 0&&(this.details=r.details)}};function n(n,r){if(typeof n!=`object`||!n)return!1;let i=n;return(n instanceof t||i[e]===!0)&&typeof i.code==`string`&&typeof i.message==`string`&&(r===void 0||i.code===r)}var r=1<<20;function i(e,t,n){if(!Number.isSafeInteger(t)||t<n)throw RangeError(`The ${e} must be an integer of at least ${n}, not ${String(t)}.`)}function a(e,t={}){let n=t.windowSize??r;i(`window size`,n,1);let a=e.size,o=0,s=/* @__PURE__ */ new Uint8Array;return{size:a,async bytes(t,r){i(`offset`,t,0),i(`length`,r,0);let c=Math.min(t,a),l=Math.min(a,c+r);if(c<o||l>o+s.length){let t=Math.min(a,c+Math.max(r,n));s=new Uint8Array(await e.slice(c,t).arrayBuffer()),o=c}return s.subarray(c-o,l-o)}}}var o=8,s=16,c=`UNKNOWN`;function l(e,t={}){let n=t.includeStack??!1,r=h(t.maxDepth),i=/* @__PURE__ */ new Set,a=(e,t)=>{i.add(e);let o=u(e),s=n&&p(e)?e.stack:void 0;return typeof s==`string`&&(o.stack=s),p(e)&&`cause`in e&&t<r&&!i.has(e.cause)&&(o.cause=a(e.cause,t+1)),o};return a(e,0)}function u(e){if(n(e)){let t={name:e.name,code:e.code,message:e.message},n=d(e.details);return n!==void 0&&(t.details=n),t}if(p(e)&&typeof e.message==`string`){let t=typeof e.name==`string`?e.name:`Error`;return{name:`SdkError`,code:c,message:e.message,details:{originalName:t}}}return typeof e==`string`?{name:`SdkError`,code:c,message:e,details:{thrownType:`string`}}:{name:`SdkError`,code:c,message:`A value that isn't an Error was thrown.`,details:{thrownType:e===null?`null`:typeof e}}}function d(e){if(!m(e))return;let t=f(e,0);return m(t)?t:void 0}function f(e,t){if(e===null||typeof e==`string`||typeof e==`boolean`)return e;if(typeof e==`number`)return Number.isFinite(e)?e:void 0;if(!(t>=s)){if(Array.isArray(e))return e.flatMap(e=>{let n=f(e,t+1);return n===void 0?[]:[n]});if(m(e)){let n={};for(let[r,i]of Object.entries(e)){let e=f(i,t+1);e!==void 0&&(n[r]=e)}return n}}}function p(e){return typeof e==`object`&&!!e}function m(e){if(typeof e!=`object`||!e)return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function h(e){if(e===void 0)return o;if(!Number.isInteger(e)||e<0)throw RangeError(`maxDepth must be a non-negative integer, received ${e}.`);return e}[...Array.from({length:12},(e,t)=>`F${t+1}`)];var g=`hm-rpc`;function _(e){return{protocol:g,version:1,...e}}function v(e){if(typeof e!=`object`||!e)return;let t=e;if(t.protocol!==`hm-rpc`||typeof t.version!=`number`)return;let n=Number.isSafeInteger(t.id);switch(t.type){case`ready`:return t;case`call`:return n&&typeof t.method==`string`?t:void 0;case`cancel`:case`progress`:case`result`:return n?t:void 0;case`error`:return n?t:void 0;case`crash`:return t;default:return}}function y(e,t){let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Map,i=e=>{if(typeof e!=`object`||!e)return e;if(r.has(e))return r.get(e);if(ArrayBuffer.isView(e)){let i=b(e)?e:x(e);return t&&i===e&&n.add(e.buffer),r.set(e,i),i}return C(e)?(t&&n.add(e),r.set(e,e),e):Array.isArray(e)?a(e,()=>[],(e,t)=>{t.forEach((t,n)=>{e[n]=i(t)})}):e instanceof Map?a(e,()=>/* @__PURE__ */ new Map,(e,t)=>{for(let[n,r]of t)e.set(i(n),i(r))}):e instanceof Set?a(e,()=>/* @__PURE__ */ new Set,(e,t)=>{for(let n of t)e.add(i(n))}):w(e)?a(e,()=>({}),(e,t)=>{for(let[n,r]of Object.entries(t))e[n]=i(r)}):(r.set(e,e),e)},a=(e,t,n)=>{let i=t();return r.set(e,i),n(i,e),T(e,i)?(r.set(e,e),e):i};return{value:i(e),transferables:[...n]}}function ee(e,t,n){if(n.length===0)e(t,[]);else try{e(t,n)}catch{e(t,[])}}function b(e){return e.buffer instanceof ArrayBuffer&&e.byteOffset===0&&e.byteLength===e.buffer.byteLength}function x(e){let t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength).slice();if(e instanceof DataView)return new DataView(t.buffer);let n=e.constructor;return new n(t.buffer)}var S=[`ArrayBuffer`,`MessagePort`,`ImageBitmap`,`OffscreenCanvas`,`ReadableStream`,`WritableStream`,`TransformStream`,`VideoFrame`,`AudioData`];function C(e){let t=globalThis;return S.some(n=>{let r=t[n];return typeof r==`function`&&e instanceof r})}function w(e){let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function T(e,t){if(Array.isArray(e)){let n=t;return e.length===n.length&&e.every((e,t)=>e===n[t])}if(e instanceof Map){let n=[...t];return[...e].every(([e,t],r)=>n[r]?.[0]===e&&n[r]?.[1]===t)}if(e instanceof Set){let n=[...t];return[...e].every((e,t)=>n[t]===e)}let n=t;return Object.entries(e).every(([e,t])=>n[e]===t)}function E(e,n={}){let r=n.scope??globalThis,i=n.includeStack??!1,a=/* @__PURE__ */ new Map,o=(e,t=[])=>{r.postMessage(e,t)},s=(e,t)=>{o(_({type:`error`,id:e,error:l(t,{includeStack:i})}))},c=(e,n,i)=>{let a=y(i,!0);try{ee((e,t)=>r.postMessage(e,t),_({type:`result`,id:e,output:a.value}),a.transferables)}catch(r){s(e,new t(`WORKER_DATA_NOT_CLONEABLE`,`The result of "${n}" can't be sent to the page.`,{cause:r,details:{method:n}}))}},u=n=>{let{id:r,method:i}=n,l=Object.hasOwn(e,i)?e[i]:void 0;if(typeof l!=`function`){s(r,new t(`WORKER_METHOD_NOT_FOUND`,`The worker has no method "${i}".`,{details:{method:i}}));return}let u=new AbortController;a.set(r,u);let d={signal:u.signal,progress:e=>{a.get(r)===u&&o(_({type:`progress`,id:r,value:e}))}};Promise.resolve().then(()=>l.call(e,n.input,d)).then(e=>{a.delete(r),c(r,i,e)},e=>{a.delete(r),s(r,e)})},d=e=>{o(_({type:`crash`,error:l(e,{includeStack:i})}))};r.addEventListener(`message`,e=>{let t=v(e.data);t!==void 0&&t.version===1&&(t.type===`call`?u(t):t.type===`cancel`&&a.get(t.id)?.abort())}),r.addEventListener(`error`,e=>{let t=e;d(t.error??Error(t.message))}),r.addEventListener(`unhandledrejection`,e=>{d(e.reason)}),o(_({type:`ready`,...n.packageVersion===void 0?{}:{packageVersion:n.packageVersion}}))}var D=class extends t{name=`AudioError`},te=1024;function O(e,t){return new D(`INVALID_AUDIO`,`Invalid ${e} file: ${t}.`,{details:{container:e,reason:t}})}function k(e,t){return new D(`UNSUPPORTED_FORMAT`,`Unsupported sample format in ${e}: ${t}.`,{details:{container:e,codec:t}})}async function A(e,t,n){let r=await e.slice(t,t+n).arrayBuffer();return new DataView(r)}function j(e,t=0){return e.byteLength<t+4?``:String.fromCharCode(e.getUint8(t),e.getUint8(t+1),e.getUint8(t+2),e.getUint8(t+3))}function ne(e){let{container:t,channels:n,sampleRate:r,bytesPerSample:i,blockAlign:a,frames:o,dataOffset:s}=e;if(!Number.isInteger(n)||n<1||n>te)throw O(t,`channel count out of range`);if(!Number.isInteger(r)||r<1)throw O(t,`sample rate out of range`);if(![1,2,3,4,8].includes(i))throw O(t,`sample size out of range`);if(a!==n*i)throw O(t,`frame size doesn't match the channels and sample size`);if(!Number.isSafeInteger(o)||o<0||!Number.isSafeInteger(s))throw O(t,`length out of range`);return e}var re={NONE:{encoding:`int`,littleEndian:!1},twos:{encoding:`int`,littleEndian:!1},sowt:{encoding:`int`,littleEndian:!0},in24:{encoding:`int`,littleEndian:!1,bits:24},"42ni":{encoding:`int`,littleEndian:!0,bits:24},in32:{encoding:`int`,littleEndian:!1,bits:32},"23ni":{encoding:`int`,littleEndian:!0,bits:32},fl32:{encoding:`float`,littleEndian:!1,bits:32},FL32:{encoding:`float`,littleEndian:!1,bits:32},fl64:{encoding:`float`,littleEndian:!1,bits:64},FL64:{encoding:`float`,littleEndian:!1,bits:64}};function ie(e,t){let n=e.getUint16(t,!1),r=n&32767;return n&32768||r===0||r===32767?NaN:(e.getUint32(t+2,!1)*2**32+e.getUint32(t+6,!1))*2**(r-16383-63)}function ae(e,t){if(e.byteLength<(t?22:18))throw O(`aiff`,`truncated COMM chunk`);let n=ie(e,8);if(!Number.isInteger(n)||n<1||n>2**31)throw O(`aiff`,`sample rate out of range`);return{channels:e.getInt16(0,!1),frames:e.getUint32(2,!1),bits:e.getInt16(6,!1),sampleRate:n,type:t?j(e,18):`NONE`}}async function oe(e){let t=await A(e,0,12),n=j(t,8);if(j(t)!==`FORM`||n!==`AIFF`&&n!==`AIFC`)throw O(`aiff`,`missing FORM header`);let r=n===`AIFC`,i,a,o=12;for(let t=0;o+8<=e.size;t+=1){if(t>=1024)throw O(`aiff`,`too many chunks`);let n=await A(e,o,8),s=j(n),c=n.getUint32(4,!1),l=o+8,u=e.size-l;if(s===`COMM`){if(c>u)throw O(`aiff`,`truncated COMM chunk`);i=ae(await A(e,l,Math.min(c,64)),r)}else if(s===`SSND`){let t=await A(e,l,8);if(c<8||t.byteLength<8)throw O(`aiff`,`truncated SSND chunk`);let n=l+8+t.getUint32(0,!1),r=Math.min(l+c,e.size);if(n>r)throw O(`aiff`,`sound data offset past the chunk`);a={offset:n,size:r-n}}if(i&&a)break;o=l+c+c%2}if(!i)throw O(`aiff`,`missing COMM chunk`);if(!a)throw O(`aiff`,`missing SSND chunk`);let s=re[i.type];if(!s)throw k(`aiff`,i.type||`unknown`);let c=s.bits??i.bits;if(c<1||c>64)throw O(`aiff`,`sample size out of range`);if(s.encoding===`float`&&c!==32&&c!==64)throw k(`aiff`,`${c}-bit float`);if(s.encoding===`int`&&c>32)throw k(`aiff`,`${c}-bit integer`);let l=Math.ceil(c/8),u=ne({container:`aiff`,sampleRate:i.sampleRate,channels:i.channels,encoding:s.encoding,bytesPerSample:l,littleEndian:s.littleEndian,dataOffset:a.offset,blockAlign:i.channels*l,frames:i.frames});return u.frames*u.blockAlign>a.size?{...u,frames:Math.floor(a.size/u.blockAlign),declaredFrames:u.frames}:u}function se(e,t,n){let{channels:r,bytesPerSample:i,blockAlign:a,encoding:o,littleEndian:s}=t;if(e.byteLength<n*a)throw RangeError(`Expected ${n*a} bytes, got ${e.byteLength}.`);let c=Array.from({length:r},()=>new Float32Array(n)),l=ce(o,i,s);for(let t=0;t<r;t+=1){let r=c[t],o=t*i;for(let t=0;t<n;t+=1)r[t]=l(e,o),o+=a}return c}function ce(e,t,n){if(e===`float`)return t===8?(e,t)=>le(Math.fround(e.getFloat64(t,n))):(e,t)=>le(e.getFloat32(t,n));if(e===`uint`)return(e,t)=>(e.getUint8(t)-128)/128;switch(t){case 1:return(e,t)=>e.getInt8(t)/128;case 2:return(e,t)=>e.getInt16(t,n)/32768;case 3:return(e,t)=>{let r=e.getUint8(t),i=e.getUint8(t+1),a=e.getUint8(t+2);return((n?r|i<<8|a<<16:r<<16|i<<8|a)<<8>>8)/8388608};default:return(e,t)=>e.getInt32(t,n)/2147483648}}function le(e){return Number.isFinite(e)?e:0}var ue=1,de=3,fe=65534,pe=[0,0,0,0,16,0,128,0,0,170,0,56,155,113],me=4294967295;async function he(e,t){let n=await A(e,t,8);if(n.byteLength<8)return!1;for(let e=0;e<4;e+=1){let t=n.getUint8(e);if(t<32||t>126)return!1}return n.getUint32(4,!0)<=e.size-t-8}function ge(e){if(e.byteLength<16)throw O(`wav`,`truncated fmt chunk`);let t=e.getUint16(0,!0),n={channels:e.getUint16(2,!0),sampleRate:e.getUint32(4,!0),blockAlign:e.getUint16(12,!0),bitsPerSample:e.getUint16(14,!0)};if(t===fe){if(e.byteLength<40)throw O(`wav`,`truncated extensible fmt chunk`);t=e.getUint16(24,!0);for(let[t,n]of pe.entries())if(e.getUint8(26+t)!==n)throw k(`wav`,`extensible subformat`)}return{tag:t,...n}}function _e(e,t,n,r){let{tag:i,channels:a,sampleRate:o,blockAlign:s,bitsPerSample:c}=e;if(c%8!=0)throw O(`wav`,`sample size isn't whole bytes`);let l=c/8,u;if(i===ue&&[1,2,3,4].includes(l))u=l===1?`uint`:`int`;else if(i===de&&[4,8].includes(l))u=`float`;else throw k(`wav`,`format ${i} at ${c} bits`);if(s<1)throw O(`wav`,`frame size is zero`);return ne({container:`wav`,sampleRate:o,channels:a,encoding:u,bytesPerSample:l,littleEndian:!0,dataOffset:t,blockAlign:s,frames:Math.floor(n/s),...r===void 0?{}:{declaredFrames:Math.floor(r/s)}})}async function ve(e){let t=await A(e,0,12),n=j(t);if(n!==`RIFF`&&n!==`RF64`||j(t,8)!==`WAVE`)throw O(`wav`,`missing RIFF header`);let r,i,a,o=12;for(let t=0;o+8<=e.size;t+=1){if(t>=1024)throw O(`wav`,`too many chunks`);let s=await A(e,o,8),c=j(s),l=s.getUint32(4,!0),u=o+8,d=e.size-u;if(c===`ds64`&&n===`RF64`){let t=await A(e,u,16);if(t.byteLength<16)throw O(`wav`,`truncated ds64 chunk`);let n=t.getUint32(12,!0);if(n>=2**21)throw O(`wav`,`data size out of range`);a=n*2**32+t.getUint32(8,!0)}else if(c===`fmt `){if(l>d)throw O(`wav`,`truncated fmt chunk`);r=ge(await A(e,u,Math.min(l,64)))}else if(c===`data`){let t=l;if(n===`RF64`&&l===me){if(a===void 0)throw O(`wav`,`RF64 file without ds64 chunk`);t=a}else(l===me||l===0&&!await he(e,u))&&(t=d);if(i={offset:u,size:Math.min(t,d),declared:t>d?t:void 0},r)break;o=u+t+t%2;continue}if(r&&i)break;o=u+l+l%2}if(!r)throw O(`wav`,`missing fmt chunk`);if(!i)throw O(`wav`,`missing data chunk`);return _e(r,i.offset,i.size,i.declared)}async function ye(e){let t=await A(e,0,12),n=j(t),r=j(t,8),i;if((n===`RIFF`||n===`RF64`)&&r===`WAVE`)i=await ve(e);else if(n===`FORM`&&(r===`AIFF`||r===`AIFC`))i=await oe(e);else throw new D(`UNSUPPORTED_FORMAT`,`The file's container isn't recognised.`,{details:{container:`unknown`}});if(i.frames===0)throw O(i.container,`no audio frames`);return{layout:i,probe:{container:i.container,codec:`pcm`,sampleRate:i.sampleRate,channels:i.channels,frameBound:i.frames,expectedFrames:i.frames,...i.declaredFrames===void 0?{}:{declaredFrames:i.declaredFrames},frameTolerance:0}}}async function be(e,t,n,r){if(!Number.isSafeInteger(n)||!Number.isSafeInteger(r)||n<0||r<0||n+r>t.frames)throw RangeError(`Frames ${n} to ${n+r} are outside the file.`);let i=r*t.blockAlign,a=await A(e,t.dataOffset+n*t.blockAlign,i);if(a.byteLength<i)throw O(t.container,`file shorter than probed`);return se(a,t,r)}function xe(e,t){let n=[],r=[];for(let i of e){let e=Math.ceil(i.length/t),a=new Float32Array(e),o=new Float32Array(e);for(let n=0;n<e;n+=1){let e=Math.min(i.length,(n+1)*t),r=1/0,s=-1/0;for(let a=n*t;a<e;a+=1){let e=i[a];e<r&&(r=e),e>s&&(s=e)}a[n]=r,o[n]=s}n.push(a),r.push(o)}return{min:n,max:r}}var M=1<<20;async function Se(e,t,n,r,i,a){let o=t;for(let s=t;s<e.size;s+=M){let t=await e.bytes(s,M+n),c=Math.min(M,e.size-s);for(let e=0;e<c;e+=1){if(t[e]!==255)continue;let n=r(t,e);if(n===void 0)continue;let c=s+e,l=c>=o;l&&(o=c+Math.max(1,i(n))),a(n,l)}}}async function N(e,t,n,r,i,a,o=3){let s=await e.bytes(t,n+65536);for(let c=0;c<Math.min(n,s.length);c+=1){if(s[c]!==255)continue;let n=r(s,c);if(n===void 0)continue;let l=c,u=n;for(let d=1;;d+=1){if(d===o)return{offset:t+c,first:n};let f=l+i(u);if(f===s.length&&t+s.length===e.size)return{offset:t+c,first:n};let p=r(s,f);if(p===void 0||!a(n,p))break;l=f,u=p}}}var Ce=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350];function we(e,t){let n=0,r=0,i=0,a=a=>{let o=0;for(let s=0;s<a;s+=1){if(n===0){if(i>=e.length)throw O(t,`truncated AudioSpecificConfig`);r=e[i],i+=1,n=8}--n,o=o*2+(r>>n&1)}return o};a(5)===31&&a(6);let o=a(4),s=o===15?a(24):Ce[o];if(s===void 0||s===0)throw O(t,`AAC sample rate out of range`);return{coreRate:s,channelConfiguration:a(4)}}function P(e){return e===0||e===7?8:Math.max(2,e)}function F(e){return e<=24e3?e*2:e}function I(e,t){let n=n=>e[t+n],r=n(1),i=n(2),a=n(3),o=n(4),s=n(5),c=n(6);if(n(0)!==255||r===void 0||i===void 0||a===void 0||o===void 0||s===void 0||c===void 0||(r&246)!=240)return;let l=Ce[i>>2&15];if(l===void 0)return;let u=(i&1)<<2|a>>6,d=(a&3)<<11|o<<3|s>>5;if(!(d<(r&1?7:9)))return{sampleRate:l,channelConfiguration:u,blocks:(c&3)+1,frameLength:d}}var Te=(e,t)=>e.sampleRate===t.sampleRate&&e.channelConfiguration===t.channelConfiguration;async function Ee(e,t){return N(a(e),t,65536,I,e=>e.frameLength,Te)}async function De(e,t){let n=a(e),r=await N(n,t,65536,I,e=>e.frameLength,Te);if(!r)throw O(`adts`,`no frames`);let i=r.first.sampleRate,o=F(i),s=P(r.first.channelConfiguration),c=1024*Math.ceil(o/i),l=0,u=0;if(await Se(n,r.offset,7,I,e=>e.frameLength,(e,t)=>{let n=Math.max(1,Math.ceil(o/e.sampleRate)),r=Math.ceil(P(e.channelConfiguration)/s);l+=e.blocks*1024*n*r,t&&(u+=e.blocks)}),l===0)throw O(`adts`,`no frames`);return{container:`adts`,codec:`aac`,sampleRate:o,channels:s,frameBound:l,expectedFrames:Math.min(u*c,l),frameTolerance:2*c}}function L(e,t){let n=2**(e-1),r=2**e-1,i=/* @__PURE__ */ new Uint32Array(256);for(let a=0;a<256;a+=1){let o=a*2**(e-8);for(let e=0;e<8;e+=1)o=o>=n?(o*2^t)&r:o*2&r,o>>>=0;i[a]=o>>>0}return i}var Oe=L(8,7);L(16,32773),L(32,79764919);function ke(e,t=0,n=e.length){let r=0;for(let i=t;i<n;i+=1)r=Oe[r^e[i]];return r}function R(e,t){if(e.length<34)throw O(t,`truncated STREAMINFO`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=n.getUint16(0),i=n.getUint16(2),a=n.getUint32(10),o=a>>>12,s=(a>>>9&7)+1,c=(a>>>4&31)+1,l=(a&15)*2**32+n.getUint32(14);if(r<16||i<r)throw O(t,`block sizes out of range`);if(o===0)throw O(t,`sample rate is zero`);if(c<4)throw O(t,`sample size out of range`);return{minBlockSize:r,maxBlockSize:i,sampleRate:o,channels:s,bitsPerSample:c,totalFrames:l}}var Ae=[void 0,88200,176400,192e3,8e3,16e3,22050,24e3,32e3,44100,48e3,96e3];function z(e,t){let n=n=>e[t+n];if(n(0)!==255||((n(1)??0)&254)!=248)return;let r=n(2),i=n(3);if(r===void 0||i===void 0)return;let a=r>>4,o=r&15,s=i>>4,c=i>>1&7;if(a===0||o===15||s>10||c===3||i&1)return;let l=4,u=n(l);if(u===void 0)return;let d;if(u<128)d=0;else if(u>=192&&u<224)d=1;else if(u>=224&&u<240)d=2;else if(u>=240&&u<248)d=3;else if(u>=248&&u<252)d=4;else if(u>=252&&u<254)d=5;else if(u===254)d=6;else return;l+=1;for(let e=0;e<d;e+=1,l+=1){let e=n(l);if(e===void 0||(e&192)!=128)return}let f;if(a===1)f=192;else if(a<=5)f=576*2**(a-2);else if(a===6||a===7){let e=n(l);if(e===void 0)return;if(a===6)f=e+1,l+=1;else{let t=n(l+1);if(t===void 0)return;f=e*256+t+1,l+=2}}else f=256*2**(a-8);let p=Ae[o];if(o>=12){let e=n(l);if(e===void 0)return;if(o===12)p=e*1e3,l+=1;else{let t=n(l+1);if(t===void 0)return;p=o===13?e*256+t:(e*256+t)*10,l+=2}if(p===0)return}let m=n(l);if(m!==void 0&&ke(e,t,t+l)===m)return{blockSize:f,sampleRate:p,channels:s<8?s+1:2,length:l+1}}function je(e){if(e.length<10||e[0]!==73||e[1]!==68||e[2]!==51)return 0;let t=0;for(let n=6;n<10;n+=1){let r=e[n];if(r&128)return 0;t=t*128+r}let n=e[5]&16?10:0;return 10+t+n}var B=1<<20;function Me(e,t){let n=e.sampleRate!==void 0&&e.sampleRate<t.sampleRate?Math.ceil(t.sampleRate/e.sampleRate):1,r=Math.ceil(e.channels/t.channels);return e.blockSize*n*r}async function Ne(e){let t=a(e),n=je(await t.bytes(0,10)),r=await t.bytes(n,4);if(String.fromCharCode(...r)!==`fLaC`)throw O(`flac`,`missing fLaC marker`);n+=4;let i;for(let e=0;;e+=1){if(e>=1024)throw O(`flac`,`too many metadata blocks`);let r=await t.bytes(n,4);if(r.length<4)throw O(`flac`,`truncated metadata`);let a=!!(r[0]&128),o=r[0]&127,s=r[1]<<16|r[2]<<8|r[3];if(o===127)throw O(`flac`,`invalid metadata block type`);if(e===0){if(o!==0||s!==34)throw O(`flac`,`STREAMINFO isn't first`);i=R(await t.bytes(n+4,34),`flac`)}if(n+=4+s,n>t.size)throw O(`flac`,`metadata exceeds the file`);if(a)break}if(!i)throw O(`flac`,`missing STREAMINFO`);let o=0,s=0;for(let e=n;e<t.size;e+=B){let n=await t.bytes(e,B+16),r=Math.min(B,t.size-e);for(let e=0;e<r;e+=1){if(n[e]!==255)continue;let t=z(n,e);t&&(o+=Me(t,i),s+=1)}}if(s===0)throw O(`flac`,`no audio frames`);if(i.totalFrames>o)throw O(`flac`,`STREAMINFO claims more than the frames carry`);return{container:`flac`,codec:`flac`,sampleRate:i.sampleRate,channels:i.channels,frameBound:o,expectedFrames:i.totalFrames>0?i.totalFrames:o,frameTolerance:i.maxBlockSize}}var V=27,Pe=65307,Fe=-1;async function H(e,t){let n=a(e),r,i=Fe,o=[],s=0,c=0,l=0;for(;c+V<=n.size;){let e=await n.bytes(c,Pe);if(e[0]!==79||e[1]!==103||e[2]!==103||e[3]!==83){if(l===0)throw O(`ogg`,`missing first page`);c+=1;continue}let a=new DataView(e.buffer,e.byteOffset,e.byteLength),u=a.getUint8(4),d=a.getUint8(5),f=a.getUint8(26);if(u!==0||e.length<V+f){if(l===0)throw O(`ogg`,`malformed first page`);c+=1;continue}let p=0;for(let t=0;t<f;t+=1)p+=e[V+t];let m=V+f+p;if(m>e.length)break;let h=a.getUint32(14,!0),g=!!(d&2);if(r===void 0){if(!g)throw O(`ogg`,`first page doesn't begin a stream`);r=h}else if(h!==r){if(g)throw new D(`UNSUPPORTED_FORMAT`,`Ogg files with several streams aren't supported.`,{details:{container:`ogg`,reason:`multiplexed streams`}});c+=m,l+=1;continue}d&1||(o=[],s=0);let _=V+f;for(let n=0;n<f;n+=1){let r=e[V+n];if(o.length<64){let t=Math.min(r,64-o.length);for(let n=0;n<t;n+=1)o.push(e[_+n])}s+=r,_+=r,r<255&&(t({head:Uint8Array.from(o),length:s}),o=[],s=0)}let v=a.getInt32(10,!0),y=a.getUint32(6,!0);(v!==-1||y!==4294967295)&&(i=v*2**32+y),c+=m,l+=1}if(l===0)throw O(`ogg`,`missing first page`);return{lastGranule:i}}var Ie=13;function Le(e){return e[0]===127&&e[1]===70&&e[2]===76&&e[3]===65&&e[4]===67}async function Re(e){let t,n=0,r=0,i=0,a=0,{lastGranule:o}=await H(e,({head:e})=>{if(n+=1,n===1){if(!Le(e)||e[5]!==1)throw O(`ogg`,`not a FLAC mapping version 1`);let n=String.fromCharCode(...e.subarray(9,13)),i=(e[Ie]??127)&127;if(n!==`fLaC`||i!==0)throw O(`ogg`,`missing FLAC STREAMINFO`);t=R(e.subarray(17,51),`ogg`),r=(e[7]??0)<<8|(e[8]??0);return}if(!t)return;let o=e[0]===255&&((e[1]??0)&254)==248;if(n-1<=r||r===0&&!o&&a===0)return;a+=1;let s=z(e,0);if(!s)i+=t.maxBlockSize;else{if(s.channels!==t.channels)throw O(`ogg`,`a frame's channel count differs from STREAMINFO`);if(s.sampleRate!==void 0&&s.sampleRate!==t.sampleRate)throw O(`ogg`,`a frame's sample rate differs from STREAMINFO`);i+=s.blockSize}});if(!t)throw O(`ogg`,`missing FLAC header`);if(a===0)throw O(`ogg`,`no audio frames`);if(t.totalFrames>i||o>i)throw O(`ogg`,`a declared length exceeds the frames`);let s=o>0?o:t.totalFrames;return{container:`ogg`,codec:`flac`,sampleRate:t.sampleRate,channels:t.channels,frameBound:i,expectedFrames:s>0?s:i,frameTolerance:t.maxBlockSize}}var ze=48e3,U=5760;function Be(e){return String.fromCharCode(...e.subarray(0,8))===`OpusHead`}function Ve(e){return e<12?[480,960,1920,2880][e%4]:e<16?[480,960][e%2]:[120,240,480,960][e%4]}function He(e,t){let n=e[0];if(t===0||n===void 0)return U;let r=Ve(n>>3),i=n&3,a;if(i===0)a=1;else if(i===1||i===2)a=2;else{let t=e[1];if(t===void 0)return U;a=t&63}let o=a*r;return o===0||o>5760?U:o}function Ue(e,t){if(!Be(e)||e.length<19)throw O(t,`malformed OpusHead`);if(e[8]&240)throw O(t,`unsupported OpusHead version`);let n=e[9],r=e[10]|e[11]<<8,i=e[18];if(n===0)throw O(t,`no channels`);if(i===0?n>2:i!==1||n>8)throw new D(`UNSUPPORTED_FORMAT`,`Unsupported Opus channel mapping.`,{details:{container:t,codec:`opus`,reason:`channel mapping family ${i}`}});return{channels:n,preSkip:r}}async function We(e){let t=0,n=0,r=0,i=0,{lastGranule:a}=await H(e,({head:e,length:a})=>{if(r+=1,r===1)({channels:t,preSkip:n}=Ue(e,`ogg`));else if(r===2){if(String.fromCharCode(...e.subarray(0,8))!==`OpusTags`)throw O(`ogg`,`missing OpusTags`)}else i+=He(e,a)});if(r<2)throw O(`ogg`,`missing Opus headers`);if(i===0)throw O(`ogg`,`no audio packets`);if(a>i)throw O(`ogg`,`a declared length exceeds the packets`);let o=a>0?Math.max(0,a-n):i;return{container:`ogg`,codec:`opus`,sampleRate:ze,channels:t,frameBound:i,expectedFrames:o,frameTolerance:Math.max(U,n)}}var Ge=`vorbis`;function Ke(e){return e[0]===1&&String.fromCharCode(...e.subarray(1,7))===Ge}function qe(e,t){if(!Ke(e)||e.length<30)throw O(t,`malformed Vorbis header`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint32(7,!0)!==0)throw O(t,`unsupported Vorbis version`);let r=n.getUint8(11),i=n.getUint32(12,!0),a=n.getUint8(28),o=a&15,s=a>>4;if(o<6||s>13||o>s||(n.getUint8(29)&1)!=1)throw O(t,`Vorbis block sizes out of range`);if(r===0||i===0)throw O(t,`Vorbis format out of range`);return{channels:r,sampleRate:i,largeBlock:2**s}}async function Je(e){let t=0,n=0,r=0,i=0,a=0,{lastGranule:o}=await H(e,({head:e})=>{if(i+=1,i===1)({channels:t,sampleRate:n,largeBlock:r}=qe(e,`ogg`));else if(i===2||i===3){let t=i===2?3:5;if(e[0]!==t||String.fromCharCode(...e.subarray(1,7))!==Ge)throw O(`ogg`,`missing Vorbis comment or setup header`)}else a+=1});if(i<3)throw O(`ogg`,`missing Vorbis headers`);if(a===0)throw O(`ogg`,`no audio packets`);let s=r/2*a;if(o>s)throw O(`ogg`,`a declared length exceeds the packets`);return{container:`ogg`,codec:`vorbis`,sampleRate:n,channels:t,frameBound:s,expectedFrames:o>0?o:s,frameTolerance:r}}var W=440786851,Ye=17026,Xe=408125543,Ze=357149030,Qe=2807729,$e=17545,et=374648427,tt=174,nt=215,rt=131,it=134,at=25506,ot=524531317,st=163,ct=160,lt=161,ut=/* @__PURE__ */ new Set([ot,475249515,307544935,272869232,423732329,290298740,Ze,et,W]);function G(e,t){return new D(`UNSUPPORTED_FORMAT`,`Unsupported Matroska: ${e}.`,{details:t===void 0?{container:`webm`,reason:e}:{container:`webm`,codec:t,reason:e}})}function dt(e,t){let n=e[t];if(n===void 0||n===0)return;let r=Math.clz32(n)-23;if(r>4||t+r>e.length)return;let i=0;for(let n=0;n<r;n+=1)i=i*256+e[t+n];return{id:i,length:r}}function K(e,t){let n=e[t];if(n===void 0||n===0)return;let r=Math.clz32(n)-23;if(t+r>e.length)return;let i=n&255>>r,a=i===255>>r;for(let n=1;n<r;n+=1){let r=e[t+n];i=i*256+r,r!==255&&(a=!1)}return{value:i,length:r,unknown:a}}async function q(e,t){let n=await e.bytes(t,12),r=dt(n,0);if(!r)return;let i=K(n,r.length);if(i){if(!i.unknown&&!Number.isSafeInteger(i.value))throw O(`webm`,`element size out of range`);return{id:r.id,body:t+r.length+i.length,size:i.unknown?void 0:i.value}}}function J(e){let t=[],n=0;for(;n<e.length;){let r=dt(e,n),i=r&&K(e,n+r.length);if(!r||!i||i.unknown)throw O(`webm`,`malformed element`);let a=n+r.length+i.length;if(a+i.value>e.length)throw O(`webm`,`element exceeds its parent`);t.push({id:r.id,data:e.subarray(a,a+i.value)}),n=a+i.value}return t}var Y=e=>e.reduce((e,t)=>e*256+t,0);function ft(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength);return e.length===4?t.getFloat32(0):e.length===8?t.getFloat64(0):NaN}function pt(e,t){if(e===`A_OPUS`){if(!t)throw O(`webm`,`missing OpusHead`);let e=Ue(t,`webm`);return{codec:`opus`,sampleRate:ze,channels:e.channels,tolerance:Math.max(U,e.preSkip),frameSamples:He}}if(e===`A_VORBIS`){if(t?.[0]!==2)throw O(`webm`,`malformed Vorbis headers`);let e=1;for(let n=0;n<2;n+=1){for(;t[e]===255;)e+=1;if(t[e]===void 0)throw O(`webm`,`malformed Vorbis headers`);e+=1}let n=qe(t.subarray(e),`webm`),r=n.largeBlock/2;return{codec:`vorbis`,sampleRate:n.sampleRate,channels:n.channels,tolerance:n.largeBlock,frameSamples:()=>r}}if(e===`A_FLAC`){let e=t&&String.fromCharCode(...t.subarray(0,4));if(!t||e!==`fLaC`||(t[4]??127)&127)throw O(`webm`,`missing FLAC STREAMINFO`);let n=R(t.subarray(8,42),`webm`);return{codec:`flac`,sampleRate:n.sampleRate,channels:n.channels,tolerance:n.maxBlockSize,frameSamples(e){let t=z(e,0);if(!t)return n.maxBlockSize;if(t.channels!==n.channels)throw O(`webm`,`a frame's channel count differs from STREAMINFO`);if(t.sampleRate!==void 0&&t.sampleRate!==n.sampleRate)throw O(`webm`,`a frame's sample rate differs from STREAMINFO`);return t.blockSize}}}if(e.startsWith(`A_AAC`)){if(!t)throw G(`AAC without an AudioSpecificConfig`,`aac`);let e=we(t,`webm`),n=F(e.coreRate),r=1024*Math.ceil(n/e.coreRate);return{codec:`aac`,sampleRate:n,channels:P(e.channelConfiguration),tolerance:2*r,frameSamples:()=>r}}throw G(`codec ${e}`,e)}function mt(e){let t=[];for(let n of J(e)){if(n.id!==tt)continue;let e=J(n.data),r=t=>e.find(e=>e.id===t)?.data,i=r(rt);if(!i||Y(i)!==2)continue;let a=r(nt),o=r(it);if(!a||!o)throw O(`webm`,`incomplete audio track`);let s=String.fromCharCode(...o).replace(/\0+$/,``);t.push({number:Y(a),codec:pt(s,r(at))})}if(t.length===0)throw O(`webm`,`no audio track`);if(t.length>1)throw G(`several audio tracks`);return t[0]}function ht(e,t){let n=K(e,0);if(!n||n.unknown)throw O(`webm`,`malformed block`);if(n.value!==t.number)return 0;let r=n.length+2,i=e[r];if(i===void 0)throw O(`webm`,`truncated block`);r+=1;let a=i>>1&3,o=[];if(a===0)o.push(e.length-r);else{let t=e[r];if(t===void 0)throw O(`webm`,`truncated block`);let n=t+1;if(r+=1,a===1)for(let t=0;t<n-1;t+=1){let t=0;for(;;){let n=e[r];if(n===void 0)throw O(`webm`,`truncated lacing`);if(r+=1,t+=n,n<255)break}o.push(t)}else if(a===3){let t=K(e,r);if(!t||t.unknown)throw O(`webm`,`malformed lacing`);r+=t.length,o.push(t.value);for(let t=1;t<n-1;t+=1){let n=K(e,r);if(!n||n.unknown)throw O(`webm`,`malformed lacing`);r+=n.length;let i=2**(7*n.length-1)-1;o.push(o[t-1]+n.value-i)}}let i=e.length-r;if(a===2){if(i%n!==0)throw O(`webm`,`fixed lacing doesn't divide the block`);for(let e=0;e<n;e+=1)o.push(i/n)}else{let e=o.reduce((e,t)=>e+t,0);o.push(i-e)}}if(o.some(e=>e<0)||r+o.reduce((e,t)=>e+t,0)>e.length)throw O(`webm`,`lacing exceeds the block`);let s=0;for(let n of o)s+=t.codec.frameSamples(e.subarray(r,r+Math.min(n,16)),n),r+=n;return s}async function gt(e){let t=a(e),n=await q(t,0);if(!n||n.id!==W||n.size===void 0)throw O(`webm`,`missing EBML header`);let r=J(await t.bytes(n.body,n.size)).find(e=>e.id===Ye),i=r?String.fromCharCode(...r.data).replace(/\0+$/,``):``;if(i!==`webm`&&i!==`matroska`)throw G(`document type ${i||`missing`}`);let o=await q(t,n.body+n.size);if(!o||o.id!==Xe)throw O(`webm`,`missing Segment`);let s=Math.min(t.size,o.body+(o.size??t.size)),c,l=1e6,u,d=0,f=0,p,m=o.body;for(;m<s;){let e=await q(t,m);if(!e)throw O(`webm`,`malformed element`);if(p!==void 0&&(m>=p||ut.has(e.id))&&(p=void 0),p===void 0){if(e.id===W)throw G(`several segments`);if(e.id===ot){if(!c)throw O(`webm`,`clusters before the tracks`);if(p=e.size===void 0?1/0:e.body+e.size,p!==1/0&&p>s)throw O(`webm`,`element exceeds its parent`);m=e.body;continue}if(e.size===void 0)throw O(`webm`,`unknown size outside a Segment or Cluster`);let n=e.body+e.size;if(n>s)throw O(`webm`,`element exceeds its parent`);if(e.id===Ze){let n=J(await t.bytes(e.body,e.size)),r=n.find(e=>e.id===Qe),i=n.find(e=>e.id===$e);r&&(l=Y(r.data)),i&&(u=ft(i.data))}else if(e.id===et){if(c)throw G(`several Tracks elements`);c=mt(await t.bytes(e.body,e.size))}m=n;continue}if(e.size===void 0)throw O(`webm`,`unknown size inside a Cluster`);let n=e.body+e.size;if(n>Math.min(p,s))throw O(`webm`,`element exceeds its parent`);let r=c;if(e.id===st)d+=ht(await t.bytes(e.body,e.size),r),f+=1;else if(e.id===ct)for(let n of J(await t.bytes(e.body,e.size)))n.id===lt&&(d+=ht(n.data,r),f+=1);m=n}if(!c)throw O(`webm`,`missing Tracks`);if(f===0||d===0)throw O(`webm`,`no audio blocks`);let{codec:h}=c,g=d;if(u!==void 0){if(!Number.isFinite(u)||u<0)throw O(`webm`,`Duration out of range`);if(g=Math.round(u*l/1e9*h.sampleRate),g>d)throw O(`webm`,`the Duration claims more than the blocks carry`)}return{container:`webm`,codec:h.codec,sampleRate:h.sampleRate,channels:h.channels,frameBound:d,expectedFrames:g,frameTolerance:h.tolerance}}var _t=1e5;function X(e,t){return new D(`UNSUPPORTED_FORMAT`,`Unsupported MP4: ${e}.`,{details:t===void 0?{container:`mp4`,reason:e}:{container:`mp4`,codec:t,reason:e}})}var vt=class{boxes=0;reader;constructor(e){this.reader=e}async u8(e){let t=await this.reader.bytes(e,1);if(t.length<1)throw O(`mp4`,`truncated box`);return t[0]}async view(e,t){let n=await this.reader.bytes(e,t);if(n.length<t)throw O(`mp4`,`truncated box`);return new DataView(n.buffer,n.byteOffset,n.byteLength)}async children(e,t,n=!1){let r=[],i=e;for(;i+8<=t;){if(this.boxes+=1,this.boxes>_t)throw O(`mp4`,`too many boxes`);let e=await this.view(i,8),a=e.getUint32(0),o=String.fromCharCode(e.getUint8(4),e.getUint8(5),e.getUint8(6),e.getUint8(7)),s=8;if(a===1){let e=await this.view(i+8,8),t=e.getUint32(0);if(t>=2**21)throw O(`mp4`,`box size out of range`);a=t*2**32+e.getUint32(4),s=16}else if(a===0){if(!n)throw O(`mp4`,`open-ended box inside another`);a=t-i}if(a<s||i+a>t)throw O(`mp4`,`box exceeds its parent`);r.push({type:o,body:i+s,end:i+a}),i+=a}return r}async child(e,t){return(await this.children(e.body,e.end)).find(e=>e.type===t)}async need(e,t){let n=await this.child(e,t);if(!n)throw O(`mp4`,`missing ${t} box`);return n}};async function yt(e,t){let n=0;for(let r=0;r<4;r+=1){let i=await e.u8(t+r);if(n=n*128+(i&127),!(i&128))return[n,r+1]}throw O(`mp4`,`malformed descriptor length`)}async function bt(e,t){let n=t.body+4,r=async r=>{if(await e.u8(n)!==r)throw X(`unexpected ES descriptor`);let[i,a]=await yt(e,n+1);if(n+=1+a,n+i>t.end)throw O(`mp4`,`descriptor exceeds its box`)};await r(3);let i=await e.u8(n+2);n+=3,i&128&&(n+=2),i&64&&(n+=1+await e.u8(n)),i&32&&(n+=2),await r(4);let a=await e.u8(n);if(![64,102,103,104].includes(a))throw X(`object type ${a}`,a===107||a===105?`mp3`:`unknown`);return n+=13,await r(5),we(await e.reader.bytes(n,Math.min(5,t.end-n)),`mp4`)}async function xt(e){let t=a(e),n=new vt(t),r=await n.children(0,t.size,!0);if(r.some(e=>e.type===`moof`))throw X(`fragmented MP4`);let i=r.find(e=>e.type===`moov`);if(!i)throw O(`mp4`,`missing moov box`);let o=[];for(let e of(await n.children(i.body,i.end)).filter(e=>e.type===`trak`)){let t=await n.child(e,`mdia`),r=t&&await n.child(t,`hdlr`);t&&r&&(await n.view(r.body+8,4)).getUint32(0)===1936684398&&o.push({trak:e,mdia:t})}if(o.length===0)throw O(`mp4`,`no audio track`);if(o.length>1)throw X(`several audio tracks`);let{trak:s,mdia:c}=o[0],l=await n.need(c,`mdhd`),u=await n.u8(l.body),d=await n.view(l.body+4,u===1?28:16),f=d.getUint32(u===1?16:8),p=u===1?d.getUint32(20)*2**32+d.getUint32(24):d.getUint32(12);if(f===0)throw O(`mp4`,`timescale is zero`);let m=await n.need(await n.need(c,`minf`),`stbl`),h=await n.need(m,`stsd`),[g]=await n.children(h.body+8,h.end);if(!g)throw O(`mp4`,`empty sample description`);let _=await n.view(g.body,28),v=_.getUint16(8);if(v>1)throw X(`sound sample entry version 2`);let y=_.getUint16(16),ee=_.getUint32(24)>>>16,b=g.body+28+(v===1?16:0),x,S,C,w;if(g.type===`mp4a`){let e=(await n.children(b,g.end)).find(e=>e.type===`esds`);if(!e)throw O(`mp4`,`missing esds box`);let{coreRate:t,channelConfiguration:r}=await bt(n,e);x=`aac`,S=F(t),C=P(r||y),w=1024*Math.ceil(S/t)}else if(g.type===`alac`){let e=(await n.children(b,g.end)).find(e=>e.type===`alac`);if(!e)throw O(`mp4`,`missing ALAC configuration`);let t=await n.view(e.body+4,24);if(x=`alac`,w=t.getUint32(0),C=t.getUint8(9),S=t.getUint32(20)||ee,w===0||w>1<<20)throw O(`mp4`,`ALAC frame length out of range`);if(C===0||S===0)throw O(`mp4`,`ALAC format out of range`)}else throw X(`codec ${g.type}`,g.type);await St(n,i,s,{timescale:f,duration:p,decodeRate:S,tolerance:2*w});let T=await Ct(n,m,t.size);if(T===0)throw O(`mp4`,`no audio samples`);let E=T*w,D=p===0?E:Math.round(p*S/f);if(D>E)throw O(`mp4`,`the duration claims more than the samples carry`);return{container:`mp4`,codec:x,sampleRate:S,channels:C,frameBound:E,expectedFrames:D,frameTolerance:2*w}}async function St(e,t,n,r){let i=(await e.children(n.body,n.end)).filter(e=>e.type===`edts`);if(i.length===0)return;if(i.length>1)throw X(`several edit lists`);let a=(await e.children(i[0].body,i[0].end)).filter(e=>e.type===`elst`);if(a.length===0)return;if(a.length>1)throw X(`several edit lists`);let o=a[0],s=await e.need(t,`mvhd`),c=await e.u8(s.body),l=(await e.view(s.body+(c===1?20:12),4)).getUint32(0);if(l===0)throw O(`mp4`,`movie timescale is zero`);let u=await e.u8(o.body),d=(await e.view(o.body+4,4)).getUint32(0),f=u===1?20:12;if(o.body+8+d*f>o.end)throw O(`mp4`,`edit list exceeds its box`);if(d>2)throw X(`edit list with more than two edits`);let p=!1;for(let t=0;t<d;t+=1){let n=await e.view(o.body+8+t*f,f),i=u===1?n.getUint32(0)*2**32+n.getUint32(4):n.getUint32(0),a=u===1?n.getInt32(8)*2**32+n.getUint32(12):n.getInt32(4),s=n.getInt16(u===1?16:8),c=n.getUint16(u===1?18:10);if(a===-1){if(p||t>0)throw X(`edit list with an empty edit after another edit`);if(i*r.decodeRate/l>r.tolerance)throw X(`edit list with a long empty edit`)}else{if(p)throw X(`edit list with several media edits`);if(p=!0,s!==1||c!==0)throw X(`edit list with a media rate other than 1`);if(a<0||r.duration===0||a+Math.ceil(i*r.timescale/l)>r.duration)throw X(`edit list beyond the media`)}}}async function Ct(e,t,n){let r=await e.child(t,`stsz`);if(!r)throw await e.child(t,`stz2`)?X(`compact sample sizes`):O(`mp4`,`missing stsz box`);let i=await e.view(r.body+4,8),a=i.getUint32(0),o=i.getUint32(4);if(a===0&&r.body+12+o*4>r.end)throw O(`mp4`,`stsz table exceeds its box`);let s=await e.need(t,`stsc`),c=(await e.view(s.body+4,4)).getUint32(0);if(s.body+8+c*12>s.end)throw O(`mp4`,`stsc table exceeds its box`);let l=await e.child(t,`stco`)??await e.child(t,`co64`);if(!l)throw O(`mp4`,`missing chunk offsets`);let u=l.type===`co64`,d=(await e.view(l.body+4,4)).getUint32(0);if(l.body+8+d*(u?8:4)>l.end)throw O(`mp4`,`chunk offsets exceed their box`);let f=0,p=0,m=0;for(let t=1;t<=d;t+=1){for(;p<c;){let n=await e.view(s.body+8+p*12,12);if(n.getUint32(0)>t)break;m=n.getUint32(4),p+=1}let i=await e.view(l.body+8+(t-1)*(u?8:4),u?8:4),d=u?i.getUint32(0)*2**32+i.getUint32(4):i.getUint32(0);for(let t=0;t<m;t+=1){if(f>=o)throw O(`mp4`,`the sample tables disagree`);let t=a===0?(await e.view(r.body+12+f*4,4)).getUint32(0):a;if(d+t>n)throw O(`mp4`,`a sample lies outside the file`);d+=t,f+=1}}if(f!==o)throw O(`mp4`,`the sample tables disagree`);return o}var wt={1:[44100,48e3,32e3],2:[22050,24e3,16e3],2.5:[11025,12e3,8e3]},Tt={"1-1":[0,32,64,96,128,160,192,224,256,288,320,352,384,416,448],"1-2":[0,32,48,56,64,80,96,112,128,160,192,224,256,320,384],"1-3":[0,32,40,48,56,64,80,96,112,128,160,192,224,256,320],"2-1":[0,32,48,56,64,80,96,112,128,144,160,176,192,224,256],"2-2":[0,8,16,24,32,40,48,56,64,80,96,112,128,144,160],"2-3":[0,8,16,24,32,40,48,56,64,80,96,112,128,144,160]};function Z(e,t){let n=e[t],r=e[t+1],i=e[t+2],a=e[t+3];if(n!==255||r===void 0||i===void 0||a===void 0||(r&224)!=224)return;let o=r>>3&3,s=r>>1&3,c=i>>4,l=i>>2&3;if(o===1||s===0||c===0||c===15||l===3||(a&3)==2)return;let u=o===3?1:o===2?2:2.5,d=4-s,f=wt[u]?.[l],p=Tt[`${u===1?1:2}-${d}`]?.[c]*1e3,m=i>>1&1,h=a>>6==3?1:2,g,_;if(d===1?(g=384,_=(Math.floor(12*p/f)+m)*4):d===2||u===1?(g=1152,_=Math.floor(144*p/f)+m):(g=576,_=Math.floor(72*p/f)+m),!(_<4))return{version:u,layer:d,sampleRate:f,channels:h,samplesPerFrame:g,frameLength:_,protected:!(r&1)}}function Et(e,t){let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=t=>t+4<=e.length?String.fromCharCode(...e.subarray(t,t+4)):``,i=t.version===1?t.channels===1?17:32:t.channels===1?9:17,a=4+(t.protected?2:0)+i,o=r(a);if(o===`Xing`||o===`Info`){if(a+8>e.length)return;let t=n.getUint32(a+4);a+=8;let i;if(t&1){if(a+4>e.length)return;i=n.getUint32(a),a+=4}if(t&2&&(a+=4),t&4&&(a+=100),t&8&&(a+=4),a+24<=e.length&&r(a)===`LAME`){let t=e[a+21]*65536+e[a+22]*256+e[a+23];return{...i===void 0?{}:{frames:i},delay:t>>12,padding:t&4095}}return i===void 0?{}:{frames:i}}if(r(36)===`VBRI`&&54<=e.length)return{delay:n.getUint16(42),frames:n.getUint32(50)}}var Dt=65536,Ot=(e,t)=>e.version===t.version&&e.layer===t.layer&&e.sampleRate===t.sampleRate;async function kt(e,t){return N(a(e),t,Dt,Z,e=>e.frameLength,Ot)}function At(e,t,n){let r=e.sampleRate<t?Math.ceil(t/e.sampleRate):1;return e.samplesPerFrame*r*Math.ceil(e.channels/n)}async function jt(e,t){let n=a(e),r=await N(n,t,Dt,Z,e=>e.frameLength,Ot);if(!r)throw O(`mp3`,`no frames`);let{sampleRate:i,channels:o,samplesPerFrame:s}=r.first,c=Et(await n.bytes(r.offset,r.first.frameLength),r.first),l=0,u=0;if(await Se(n,r.offset,4,Z,e=>e.frameLength,(e,t)=>{l+=At(e,i,o),t&&(u+=1)}),l===0)throw O(`mp3`,`no frames`);let d=c?.delay??0,f=c?.padding??0,p=u*s;if(c?.frames!==void 0){let e=c.frames*s;if(e>l)throw O(`mp3`,`the tag claims more than the frames carry`);p=Math.max(0,e-d-f)}return{container:`mp3`,codec:`mp3`,sampleRate:i,channels:o,frameBound:l,expectedFrames:Math.min(p,l),frameTolerance:2*s+d+f}}function Mt(e,t){return new D(`UNSUPPORTED_FORMAT`,`This container or codec isn't supported.`,{details:t===void 0?{container:e}:{container:e,codec:t}})}async function Nt(e){let t=a(e),n=await t.bytes(0,64),r=(e,t)=>String.fromCharCode(...n.subarray(e,t)),i=r(0,4),o=r(8,12);if((i===`RIFF`||i===`RF64`)&&o===`WAVE`||i===`FORM`&&(o===`AIFF`||o===`AIFC`))return ye(e);if(i===`fLaC`)return{probe:await Ne(e)};if(n[0]===26&&n[1]===69&&n[2]===223&&n[3]===163)return{probe:await gt(e)};if([`ftyp`,`moov`,`mdat`,`free`,`skip`,`wide`].includes(r(4,8)))return{probe:await xt(e)};let s=je(n);if(s>0){let n=await t.bytes(s,4);if(String.fromCharCode(...n)===`fLaC`)return{probe:await Ne(e)}}if(i===`OggS`){let r=n[26]??0,i=await t.bytes(27+r,8);if(Le(i))return{probe:await Re(e)};if(Be(i))return{probe:await We(e)};if(Ke(i))return{probe:await Je(e)};throw Mt(`ogg`)}let[c,l]=await Promise.all([kt(e,s),Ee(e,s)]);if(c&&(!l||c.offset<=l.offset))return{probe:await jt(e,s)};if(l)return{probe:await De(e,s)};throw Mt(`unknown`)}function Pt(e){return 10**(e/20)}function Ft(e,t){return e===`equalPower`?Math.sin(t*Math.PI/2):t}function It(e,t){let n=e.end-e.start,r=Pt(e.gainDb),{fadeIn:i,fadeOut:a}=e;i.frames>0&&t<i.frames&&(r*=Ft(i.curve,t/i.frames));let o=n-1-t;return a.frames>0&&o<a.frames&&(r*=Ft(a.curve,Math.max(0,o)/a.frames)),r}function Lt(e,t){let n=Array.from({length:t},()=>Array(e).fill(0)),r=(e,t,r)=>{n[e][t]=r},i=Math.SQRT1_2;switch(`${e}>${t}`){case`1>2`:r(0,0,1),r(1,0,1);break;case`1>4`:r(0,0,1),r(1,0,1);break;case`1>6`:r(2,0,1);break;case`2>4`:case`2>6`:r(0,0,1),r(1,1,1);break;case`4>6`:r(0,0,1),r(1,1,1),r(4,2,1),r(5,3,1);break;case`2>1`:r(0,0,.5),r(0,1,.5);break;case`4>1`:for(let e=0;e<4;e+=1)r(0,e,.25);break;case`6>1`:r(0,0,i),r(0,1,i),r(0,2,1),r(0,4,.5),r(0,5,.5);break;case`4>2`:r(0,0,.5),r(0,2,.5),r(1,1,.5),r(1,3,.5);break;case`6>2`:r(0,0,1),r(0,2,i),r(0,4,i),r(1,1,1),r(1,2,i),r(1,5,i);break;case`6>4`:r(0,0,1),r(0,2,i),r(1,1,1),r(1,2,i),r(2,4,1),r(3,5,1);break;default:for(let n=0;n<Math.min(e,t);n+=1)r(n,n,1)}return n}var Q={pcm16:2,pcm24:3,float32:4},Rt=1,zt=3,Bt=65534,Vt=[0,0,0,0,16,0,128,0,0,170,0,56,155,113],Ht={1:4,2:3,4:51,6:63},$=4294967295,Ut=[[`title`,`INAM`],[`artist`,`IART`],[`album`,`IPRD`],[`year`,`ICRD`],[`comment`,`ICMT`]];function Wt(e){return e.frames*e.channels*Q[e.sampleFormat]}var Gt=class{bytes;view;at=0;constructor(e){this.bytes=new Uint8Array(e),this.view=new DataView(this.bytes.buffer)}text(e){for(let t=0;t<e.length;t+=1)this.bytes[this.at+t]=e.charCodeAt(t);this.at+=e.length}u16(e){this.view.setUint16(this.at,e,!0),this.at+=2}u32(e){this.view.setUint32(this.at,e,!0),this.at+=4}u64(e){this.u32(e%2**32),this.u32(Math.floor(e/2**32))}raw(e){this.bytes.set(e,this.at),this.at+=e.length}};function Kt(e){let t=new TextEncoder,n=Ut.flatMap(([n,r])=>{let i=e[n];return i===``?[]:[{id:r,value:t.encode(`${i}\0`)}]});if(n.length===0)return;let r=new Gt(4+n.reduce((e,{value:t})=>e+8+t.length+t.length%2,0));r.text(`INFO`);for(let{id:e,value:t}of n)r.text(e),r.u32(t.length),r.raw(t),t.length%2==1&&(r.at+=1);return r.bytes}function qt(e){let{sampleRate:t,channels:n,frames:r,sampleFormat:i,metadata:a}=e,o=Q[i],s=i===`float32`,c=n>2||o>2,l=c?40:s?18:16,u=Kt(a),d=Wt(e),f=d%2,p=8+l+(s?12:0)+(u?8+u.length:0)+8+d+f,m=4+p,h=m>$||d>$||r>$,g=new Gt(12+(h?36:0)+p-d-f);g.text(h?`RF64`:`RIFF`),g.u32(h?$:m),g.text(`WAVE`),h&&(g.text(`ds64`),g.u32(28),g.u64(m+36),g.u64(d),g.u64(r),g.u32(0)),g.text(`fmt `),g.u32(l);let _=s?zt:Rt;if(g.u16(c?Bt:_),g.u16(n),g.u32(t),g.u32(t*n*o),g.u16(n*o),g.u16(o*8),c){g.u16(22),g.u16(o*8),g.u32(Ht[n]??0),g.u16(_);for(let e of Vt)g.bytes[g.at++]=e}else s&&g.u16(0);return s&&(g.text(`fact`),g.u32(4),g.u32(h?$:r)),u&&(g.text(`LIST`),g.u32(u.length),g.raw(u)),g.text(`data`),g.u32(h?$:d),g.bytes}function Jt(e){return e<0?-Math.round(-e):Math.round(e)}function Yt(e,t){let n=Number.isFinite(e)?Math.max(-1,Math.min(1,e)):0;if(t===`float32`)return Math.fround(n);let r=t===`pcm16`?32768:8388608;return Math.min(r-1,Jt(n*r))+0}function Xt(e,t,n){let r=Q[n],i=new Uint8Array(t*e.length*r),a=new DataView(i.buffer),o=0;for(let i=0;i<t;i+=1)for(let t of e){let e=Yt(t[i]??0,n);n===`float32`?a.setFloat32(o,e,!0):n===`pcm16`?a.setInt16(o,e,!0):(a.setUint8(o,e&255),a.setUint8(o+1,e>>8&255),a.setUint8(o+2,e>>16&255)),o+=r}return i}function Zt(e){let{channels:t,frames:n,sampleFormat:r,segments:i}=e,a=Array.from({length:t},()=>new Float32Array(n)),o=new Float64Array(t);for(let e of i){let n=Lt(e.samples.length,t);for(let r=0;r<e.frames;r+=1){let i=It(e.clip,e.within+r);o.fill(0);for(let i=0;i<e.samples.length;i+=1){let a=e.samples[i][r];for(let e=0;e<t;e+=1)o[e]=o[e]+n[e][i]*a}for(let n=0;n<t;n+=1)a[n][e.at+r]=o[n]*i}}return Xt(a,n,r)}function Qt(e){E({probe:({file:e})=>Nt(e),async readBlock({file:e,layout:t,start:n,frames:r,bucket:i}){let a=await be(e,t,n,r);return{channels:a,...xe(a,i)}},peaks:({channels:e,bucket:t})=>xe(e,t),wavHeader:e=>qt(e),renderBlock:e=>Zt(e)},{...e,packageVersion:`0.5.0`})}Qt();
1
+ var e=!1;function t(){e||=!0}var n=/* @__PURE__ */ Symbol.for(`@hussmartinez/sdk-core/SdkError`),r=class extends Error{name=`SdkError`;code;constructor(e,r,i={}){super(r,`cause`in i?{cause:i.cause}:void 0),t(),this.code=e,Object.defineProperty(this,n,{value:!0}),i.details!==void 0&&(this.details=i.details)}};function i(e,t){if(typeof e!=`object`||!e)return!1;let i=e;return(e instanceof r||i[n]===!0)&&typeof i.code==`string`&&typeof i.message==`string`&&(t===void 0||i.code===t)}var a=1<<20;function o(e,t,n){if(!Number.isSafeInteger(t)||t<n)throw RangeError(`The ${e} must be an integer of at least ${n}, not ${String(t)}.`)}function s(e,t={}){let n=t.windowSize??a;o(`window size`,n,1);let r=e.size,i=0,s=/* @__PURE__ */ new Uint8Array;return{size:r,async bytes(t,a){o(`offset`,t,0),o(`length`,a,0);let c=Math.min(t,r),l=Math.min(r,c+a);if(c<i||l>i+s.length){let t=Math.min(r,c+Math.max(a,n));s=new Uint8Array(await e.slice(c,t).arrayBuffer()),i=c}return s.subarray(c-i,l-i)}}}var c=8,l=16,u=`UNKNOWN`;function d(e,t={}){let n=t.includeStack??!1,r=_(t.maxDepth),i=/* @__PURE__ */ new Set,a=(e,t)=>{i.add(e);let o=f(e),s=n&&h(e)?e.stack:void 0;return typeof s==`string`&&(o.stack=s),h(e)&&`cause`in e&&t<r&&!i.has(e.cause)&&(o.cause=a(e.cause,t+1)),o};return a(e,0)}function f(e){if(i(e)){let t={name:e.name,code:e.code,message:e.message},n=p(e.details);return n!==void 0&&(t.details=n),t}if(h(e)&&typeof e.message==`string`){let t=typeof e.name==`string`?e.name:`Error`;return{name:`SdkError`,code:u,message:e.message,details:{originalName:t}}}return typeof e==`string`?{name:`SdkError`,code:u,message:e,details:{thrownType:`string`}}:{name:`SdkError`,code:u,message:`A value that isn't an Error was thrown.`,details:{thrownType:e===null?`null`:typeof e}}}function p(e){if(!g(e))return;let t=m(e,0);return g(t)?t:void 0}function m(e,t){if(e===null||typeof e==`string`||typeof e==`boolean`)return e;if(typeof e==`number`)return Number.isFinite(e)?e:void 0;if(!(t>=l)){if(Array.isArray(e))return e.flatMap(e=>{let n=m(e,t+1);return n===void 0?[]:[n]});if(g(e)){let n={};for(let[r,i]of Object.entries(e)){let e=m(i,t+1);e!==void 0&&(n[r]=e)}return n}}}function h(e){return typeof e==`object`&&!!e}function g(e){if(typeof e!=`object`||!e)return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function _(e){if(e===void 0)return c;if(!Number.isInteger(e)||e<0)throw RangeError(`maxDepth must be a non-negative integer, received ${e}.`);return e}[...Array.from({length:12},(e,t)=>`F${t+1}`)];var v=`hm-rpc`;function y(e){return{protocol:v,version:1,...e}}function b(e){if(typeof e!=`object`||!e)return;let t=e;if(t.protocol!==`hm-rpc`||typeof t.version!=`number`)return;let n=Number.isSafeInteger(t.id);switch(t.type){case`ready`:return t;case`call`:return n&&typeof t.method==`string`?t:void 0;case`cancel`:case`progress`:case`result`:return n?t:void 0;case`error`:return n?t:void 0;case`crash`:return t;default:return}}function x(e,t){let n=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Map,i=e=>{if(typeof e!=`object`||!e)return e;if(r.has(e))return r.get(e);if(ArrayBuffer.isView(e)){let i=C(e)?e:w(e);return t&&i===e&&n.add(e.buffer),r.set(e,i),i}return E(e)?(t&&n.add(e),r.set(e,e),e):Array.isArray(e)?a(e,()=>[],(e,t)=>{t.forEach((t,n)=>{e[n]=i(t)})}):e instanceof Map?a(e,()=>/* @__PURE__ */ new Map,(e,t)=>{for(let[n,r]of t)e.set(i(n),i(r))}):e instanceof Set?a(e,()=>/* @__PURE__ */ new Set,(e,t)=>{for(let n of t)e.add(i(n))}):D(e)?a(e,()=>({}),(e,t)=>{for(let[n,r]of Object.entries(t))e[n]=i(r)}):(r.set(e,e),e)},a=(e,t,n)=>{let i=t();return r.set(e,i),n(i,e),ee(e,i)?(r.set(e,e),e):i};return{value:i(e),transferables:[...n]}}function S(e,t,n){if(n.length===0)e(t,[]);else try{e(t,n)}catch{e(t,[])}}function C(e){return e.buffer instanceof ArrayBuffer&&e.byteOffset===0&&e.byteLength===e.buffer.byteLength}function w(e){let t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength).slice();if(e instanceof DataView)return new DataView(t.buffer);let n=e.constructor;return new n(t.buffer)}var T=[`ArrayBuffer`,`MessagePort`,`ImageBitmap`,`OffscreenCanvas`,`ReadableStream`,`WritableStream`,`TransformStream`,`VideoFrame`,`AudioData`];function E(e){let t=globalThis;return T.some(n=>{let r=t[n];return typeof r==`function`&&e instanceof r})}function D(e){let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function ee(e,t){if(Array.isArray(e)){let n=t;return e.length===n.length&&e.every((e,t)=>e===n[t])}if(e instanceof Map){let n=[...t];return[...e].every(([e,t],r)=>n[r]?.[0]===e&&n[r]?.[1]===t)}if(e instanceof Set){let n=[...t];return[...e].every((e,t)=>n[t]===e)}let n=t;return Object.entries(e).every(([e,t])=>n[e]===t)}function te(e,t={}){let n=t.scope??globalThis,i=t.includeStack??!1,a=/* @__PURE__ */ new Map,o=(e,t=[])=>{n.postMessage(e,t)},s=(e,t)=>{o(y({type:`error`,id:e,error:d(t,{includeStack:i})}))},c=(e,t,i)=>{let a=x(i,!0);try{S((e,t)=>n.postMessage(e,t),y({type:`result`,id:e,output:a.value}),a.transferables)}catch(n){s(e,new r(`WORKER_DATA_NOT_CLONEABLE`,`The result of "${t}" can't be sent to the page.`,{cause:n,details:{method:t}}))}},l=t=>{let{id:n,method:i}=t,l=Object.hasOwn(e,i)?e[i]:void 0;if(typeof l!=`function`){s(n,new r(`WORKER_METHOD_NOT_FOUND`,`The worker has no method "${i}".`,{details:{method:i}}));return}let u=new AbortController;a.set(n,u);let d={signal:u.signal,progress:e=>{a.get(n)===u&&o(y({type:`progress`,id:n,value:e}))}};Promise.resolve().then(()=>l.call(e,t.input,d)).then(e=>{a.delete(n),c(n,i,e)},e=>{a.delete(n),s(n,e)})},u=e=>{o(y({type:`crash`,error:d(e,{includeStack:i})}))};n.addEventListener(`message`,e=>{let t=b(e.data);t!==void 0&&t.version===1&&(t.type===`call`?l(t):t.type===`cancel`&&a.get(t.id)?.abort())}),n.addEventListener(`error`,e=>{let t=e;u(t.error??Error(t.message))}),n.addEventListener(`unhandledrejection`,e=>{u(e.reason)}),o(y({type:`ready`,...t.packageVersion===void 0?{}:{packageVersion:t.packageVersion}}))}var O=class extends r{name=`AudioError`},ne=1024;function k(e,t){return new O(`INVALID_AUDIO`,`Invalid ${e} file: ${t}.`,{details:{container:e,reason:t}})}function A(e,t){return new O(`UNSUPPORTED_FORMAT`,`Unsupported sample format in ${e}: ${t}.`,{details:{container:e,codec:t}})}async function j(e,t,n){let r=await e.slice(t,t+n).arrayBuffer();return new DataView(r)}function M(e,t=0){return e.byteLength<t+4?``:String.fromCharCode(e.getUint8(t),e.getUint8(t+1),e.getUint8(t+2),e.getUint8(t+3))}function re(e){let{container:t,channels:n,sampleRate:r,bytesPerSample:i,blockAlign:a,frames:o,dataOffset:s}=e;if(!Number.isInteger(n)||n<1||n>ne)throw k(t,`channel count out of range`);if(!Number.isInteger(r)||r<1)throw k(t,`sample rate out of range`);if(![1,2,3,4,8].includes(i))throw k(t,`sample size out of range`);if(a!==n*i)throw k(t,`frame size doesn't match the channels and sample size`);if(!Number.isSafeInteger(o)||o<0||!Number.isSafeInteger(s))throw k(t,`length out of range`);return e}var ie={NONE:{encoding:`int`,littleEndian:!1},twos:{encoding:`int`,littleEndian:!1},sowt:{encoding:`int`,littleEndian:!0},in24:{encoding:`int`,littleEndian:!1,bits:24},"42ni":{encoding:`int`,littleEndian:!0,bits:24},in32:{encoding:`int`,littleEndian:!1,bits:32},"23ni":{encoding:`int`,littleEndian:!0,bits:32},fl32:{encoding:`float`,littleEndian:!1,bits:32},FL32:{encoding:`float`,littleEndian:!1,bits:32},fl64:{encoding:`float`,littleEndian:!1,bits:64},FL64:{encoding:`float`,littleEndian:!1,bits:64}};function ae(e,t){let n=e.getUint16(t,!1),r=n&32767;return n&32768||r===0||r===32767?NaN:(e.getUint32(t+2,!1)*2**32+e.getUint32(t+6,!1))*2**(r-16383-63)}function oe(e,t){if(e.byteLength<(t?22:18))throw k(`aiff`,`truncated COMM chunk`);let n=ae(e,8);if(!Number.isInteger(n)||n<1||n>2**31)throw k(`aiff`,`sample rate out of range`);return{channels:e.getInt16(0,!1),frames:e.getUint32(2,!1),bits:e.getInt16(6,!1),sampleRate:n,type:t?M(e,18):`NONE`}}async function se(e){let t=await j(e,0,12),n=M(t,8);if(M(t)!==`FORM`||n!==`AIFF`&&n!==`AIFC`)throw k(`aiff`,`missing FORM header`);let r=n===`AIFC`,i,a,o=12;for(let t=0;o+8<=e.size;t+=1){if(t>=1024)throw k(`aiff`,`too many chunks`);let n=await j(e,o,8),s=M(n),c=n.getUint32(4,!1),l=o+8,u=e.size-l;if(s===`COMM`){if(c>u)throw k(`aiff`,`truncated COMM chunk`);i=oe(await j(e,l,Math.min(c,64)),r)}else if(s===`SSND`){let t=await j(e,l,8);if(c<8||t.byteLength<8)throw k(`aiff`,`truncated SSND chunk`);let n=l+8+t.getUint32(0,!1),r=Math.min(l+c,e.size);if(n>r)throw k(`aiff`,`sound data offset past the chunk`);a={offset:n,size:r-n}}if(i&&a)break;o=l+c+c%2}if(!i)throw k(`aiff`,`missing COMM chunk`);if(!a)throw k(`aiff`,`missing SSND chunk`);let s=ie[i.type];if(!s)throw A(`aiff`,i.type||`unknown`);let c=s.bits??i.bits;if(c<1||c>64)throw k(`aiff`,`sample size out of range`);if(s.encoding===`float`&&c!==32&&c!==64)throw A(`aiff`,`${c}-bit float`);if(s.encoding===`int`&&c>32)throw A(`aiff`,`${c}-bit integer`);let l=Math.ceil(c/8),u=re({container:`aiff`,sampleRate:i.sampleRate,channels:i.channels,encoding:s.encoding,bytesPerSample:l,littleEndian:s.littleEndian,dataOffset:a.offset,blockAlign:i.channels*l,frames:i.frames});return u.frames*u.blockAlign>a.size?{...u,frames:Math.floor(a.size/u.blockAlign),declaredFrames:u.frames}:u}function ce(e,t,n){let{channels:r,bytesPerSample:i,blockAlign:a,encoding:o,littleEndian:s}=t;if(e.byteLength<n*a)throw RangeError(`Expected ${n*a} bytes, got ${e.byteLength}.`);let c=Array.from({length:r},()=>new Float32Array(n)),l=le(o,i,s);for(let t=0;t<r;t+=1){let r=c[t],o=t*i;for(let t=0;t<n;t+=1)r[t]=l(e,o),o+=a}return c}function le(e,t,n){if(e===`float`)return t===8?(e,t)=>ue(Math.fround(e.getFloat64(t,n))):(e,t)=>ue(e.getFloat32(t,n));if(e===`uint`)return(e,t)=>(e.getUint8(t)-128)/128;switch(t){case 1:return(e,t)=>e.getInt8(t)/128;case 2:return(e,t)=>e.getInt16(t,n)/32768;case 3:return(e,t)=>{let r=e.getUint8(t),i=e.getUint8(t+1),a=e.getUint8(t+2);return((n?r|i<<8|a<<16:r<<16|i<<8|a)<<8>>8)/8388608};default:return(e,t)=>e.getInt32(t,n)/2147483648}}function ue(e){return Number.isFinite(e)?e:0}var de=1,fe=3,pe=65534,me=[0,0,0,0,16,0,128,0,0,170,0,56,155,113],he=4294967295;async function ge(e,t){let n=await j(e,t,8);if(n.byteLength<8)return!1;for(let e=0;e<4;e+=1){let t=n.getUint8(e);if(t<32||t>126)return!1}return n.getUint32(4,!0)<=e.size-t-8}function _e(e){if(e.byteLength<16)throw k(`wav`,`truncated fmt chunk`);let t=e.getUint16(0,!0),n={channels:e.getUint16(2,!0),sampleRate:e.getUint32(4,!0),blockAlign:e.getUint16(12,!0),bitsPerSample:e.getUint16(14,!0)};if(t===pe){if(e.byteLength<40)throw k(`wav`,`truncated extensible fmt chunk`);t=e.getUint16(24,!0);for(let[t,n]of me.entries())if(e.getUint8(26+t)!==n)throw A(`wav`,`extensible subformat`)}return{tag:t,...n}}function ve(e,t,n,r){let{tag:i,channels:a,sampleRate:o,blockAlign:s,bitsPerSample:c}=e;if(c%8!=0)throw k(`wav`,`sample size isn't whole bytes`);let l=c/8,u;if(i===de&&[1,2,3,4].includes(l))u=l===1?`uint`:`int`;else if(i===fe&&[4,8].includes(l))u=`float`;else throw A(`wav`,`format ${i} at ${c} bits`);if(s<1)throw k(`wav`,`frame size is zero`);return re({container:`wav`,sampleRate:o,channels:a,encoding:u,bytesPerSample:l,littleEndian:!0,dataOffset:t,blockAlign:s,frames:Math.floor(n/s),...r===void 0?{}:{declaredFrames:Math.floor(r/s)}})}async function ye(e){let t=await j(e,0,12),n=M(t);if(n!==`RIFF`&&n!==`RF64`||M(t,8)!==`WAVE`)throw k(`wav`,`missing RIFF header`);let r,i,a,o=12;for(let t=0;o+8<=e.size;t+=1){if(t>=1024)throw k(`wav`,`too many chunks`);let s=await j(e,o,8),c=M(s),l=s.getUint32(4,!0),u=o+8,d=e.size-u;if(c===`ds64`&&n===`RF64`){let t=await j(e,u,16);if(t.byteLength<16)throw k(`wav`,`truncated ds64 chunk`);let n=t.getUint32(12,!0);if(n>=2**21)throw k(`wav`,`data size out of range`);a=n*2**32+t.getUint32(8,!0)}else if(c===`fmt `){if(l>d)throw k(`wav`,`truncated fmt chunk`);r=_e(await j(e,u,Math.min(l,64)))}else if(c===`data`){let t=l;if(n===`RF64`&&l===he){if(a===void 0)throw k(`wav`,`RF64 file without ds64 chunk`);t=a}else(l===he||l===0&&!await ge(e,u))&&(t=d);if(i={offset:u,size:Math.min(t,d),declared:t>d?t:void 0},r)break;o=u+t+t%2;continue}if(r&&i)break;o=u+l+l%2}if(!r)throw k(`wav`,`missing fmt chunk`);if(!i)throw k(`wav`,`missing data chunk`);return ve(r,i.offset,i.size,i.declared)}async function be(e){let t=await j(e,0,12),n=M(t),r=M(t,8),i;if((n===`RIFF`||n===`RF64`)&&r===`WAVE`)i=await ye(e);else if(n===`FORM`&&(r===`AIFF`||r===`AIFC`))i=await se(e);else throw new O(`UNSUPPORTED_FORMAT`,`The file's container isn't recognised.`,{details:{container:`unknown`}});if(i.frames===0)throw k(i.container,`no audio frames`);return{layout:i,probe:{container:i.container,codec:`pcm`,sampleRate:i.sampleRate,channels:i.channels,frameBound:i.frames,expectedFrames:i.frames,...i.declaredFrames===void 0?{}:{declaredFrames:i.declaredFrames},frameTolerance:0}}}async function xe(e,t,n,r){if(!Number.isSafeInteger(n)||!Number.isSafeInteger(r)||n<0||r<0||n+r>t.frames)throw RangeError(`Frames ${n} to ${n+r} are outside the file.`);let i=r*t.blockAlign,a=await j(e,t.dataOffset+n*t.blockAlign,i);if(a.byteLength<i)throw k(t.container,`file shorter than probed`);return ce(a,t,r)}function Se(e,t){let n=[],r=[];for(let i of e){let e=Math.ceil(i.length/t),a=new Float32Array(e),o=new Float32Array(e);for(let n=0;n<e;n+=1){let e=Math.min(i.length,(n+1)*t),r=1/0,s=-1/0;for(let a=n*t;a<e;a+=1){let e=i[a];e<r&&(r=e),e>s&&(s=e)}a[n]=r,o[n]=s}n.push(a),r.push(o)}return{min:n,max:r}}var N=1<<20;async function Ce(e,t,n,r,i,a){let o=t;for(let s=t;s<e.size;s+=N){let t=await e.bytes(s,N+n),c=Math.min(N,e.size-s);for(let e=0;e<c;e+=1){if(t[e]!==255)continue;let n=r(t,e);if(n===void 0)continue;let c=s+e,l=c>=o;l&&(o=c+Math.max(1,i(n))),a(n,l)}}}async function P(e,t,n,r,i,a,o=3){let s=await e.bytes(t,n+65536);for(let c=0;c<Math.min(n,s.length);c+=1){if(s[c]!==255)continue;let n=r(s,c);if(n===void 0)continue;let l=c,u=n;for(let d=1;;d+=1){if(d===o)return{offset:t+c,first:n};let f=l+i(u);if(f===s.length&&t+s.length===e.size)return{offset:t+c,first:n};let p=r(s,f);if(p===void 0||!a(n,p))break;l=f,u=p}}}var we=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350];function Te(e,t){let n=0,r=0,i=0,a=a=>{let o=0;for(let s=0;s<a;s+=1){if(n===0){if(i>=e.length)throw k(t,`truncated AudioSpecificConfig`);r=e[i],i+=1,n=8}--n,o=o*2+(r>>n&1)}return o};a(5)===31&&a(6);let o=a(4),s=o===15?a(24):we[o];if(s===void 0||s===0)throw k(t,`AAC sample rate out of range`);return{coreRate:s,channelConfiguration:a(4)}}function F(e){return e===0||e===7?8:Math.max(2,e)}function I(e){return e<=24e3?e*2:e}function L(e,t){let n=n=>e[t+n],r=n(1),i=n(2),a=n(3),o=n(4),s=n(5),c=n(6);if(n(0)!==255||r===void 0||i===void 0||a===void 0||o===void 0||s===void 0||c===void 0||(r&246)!=240)return;let l=we[i>>2&15];if(l===void 0)return;let u=(i&1)<<2|a>>6,d=(a&3)<<11|o<<3|s>>5;if(!(d<(r&1?7:9)))return{sampleRate:l,channelConfiguration:u,blocks:(c&3)+1,frameLength:d}}var Ee=(e,t)=>e.sampleRate===t.sampleRate&&e.channelConfiguration===t.channelConfiguration;async function De(e,t){return P(s(e),t,65536,L,e=>e.frameLength,Ee)}async function Oe(e,t){let n=s(e),r=await P(n,t,65536,L,e=>e.frameLength,Ee);if(!r)throw k(`adts`,`no frames`);let i=r.first.sampleRate,a=I(i),o=F(r.first.channelConfiguration),c=1024*Math.ceil(a/i),l=0,u=0;if(await Ce(n,r.offset,7,L,e=>e.frameLength,(e,t)=>{let n=Math.max(1,Math.ceil(a/e.sampleRate)),r=Math.ceil(F(e.channelConfiguration)/o);l+=e.blocks*1024*n*r,t&&(u+=e.blocks)}),l===0)throw k(`adts`,`no frames`);return{container:`adts`,codec:`aac`,sampleRate:a,channels:o,frameBound:l,expectedFrames:Math.min(u*c,l),frameTolerance:2*c}}function R(e,t){let n=2**(e-1),r=2**e-1,i=/* @__PURE__ */ new Uint32Array(256);for(let a=0;a<256;a+=1){let o=a*2**(e-8);for(let e=0;e<8;e+=1)o=o>=n?(o*2^t)&r:o*2&r,o>>>=0;i[a]=o>>>0}return i}var ke=R(8,7);R(16,32773),R(32,79764919);function Ae(e,t=0,n=e.length){let r=0;for(let i=t;i<n;i+=1)r=ke[r^e[i]];return r}function z(e,t){if(e.length<34)throw k(t,`truncated STREAMINFO`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=n.getUint16(0),i=n.getUint16(2),a=n.getUint32(10),o=a>>>12,s=(a>>>9&7)+1,c=(a>>>4&31)+1,l=(a&15)*2**32+n.getUint32(14);if(r<16||i<r)throw k(t,`block sizes out of range`);if(o===0)throw k(t,`sample rate is zero`);if(c<4)throw k(t,`sample size out of range`);return{minBlockSize:r,maxBlockSize:i,sampleRate:o,channels:s,bitsPerSample:c,totalFrames:l}}var je=[void 0,88200,176400,192e3,8e3,16e3,22050,24e3,32e3,44100,48e3,96e3];function B(e,t){let n=n=>e[t+n];if(n(0)!==255||((n(1)??0)&254)!=248)return;let r=n(2),i=n(3);if(r===void 0||i===void 0)return;let a=r>>4,o=r&15,s=i>>4,c=i>>1&7;if(a===0||o===15||s>10||c===3||i&1)return;let l=4,u=n(l);if(u===void 0)return;let d;if(u<128)d=0;else if(u>=192&&u<224)d=1;else if(u>=224&&u<240)d=2;else if(u>=240&&u<248)d=3;else if(u>=248&&u<252)d=4;else if(u>=252&&u<254)d=5;else if(u===254)d=6;else return;l+=1;for(let e=0;e<d;e+=1,l+=1){let e=n(l);if(e===void 0||(e&192)!=128)return}let f;if(a===1)f=192;else if(a<=5)f=576*2**(a-2);else if(a===6||a===7){let e=n(l);if(e===void 0)return;if(a===6)f=e+1,l+=1;else{let t=n(l+1);if(t===void 0)return;f=e*256+t+1,l+=2}}else f=256*2**(a-8);let p=je[o];if(o>=12){let e=n(l);if(e===void 0)return;if(o===12)p=e*1e3,l+=1;else{let t=n(l+1);if(t===void 0)return;p=o===13?e*256+t:(e*256+t)*10,l+=2}if(p===0)return}let m=n(l);if(m!==void 0&&Ae(e,t,t+l)===m)return{blockSize:f,sampleRate:p,channels:s<8?s+1:2,length:l+1}}function Me(e){if(e.length<10||e[0]!==73||e[1]!==68||e[2]!==51)return 0;let t=0;for(let n=6;n<10;n+=1){let r=e[n];if(r&128)return 0;t=t*128+r}let n=e[5]&16?10:0;return 10+t+n}var V=1<<20;function Ne(e,t){let n=e.sampleRate!==void 0&&e.sampleRate<t.sampleRate?Math.ceil(t.sampleRate/e.sampleRate):1,r=Math.ceil(e.channels/t.channels);return e.blockSize*n*r}async function Pe(e){let t=s(e),n=Me(await t.bytes(0,10)),r=await t.bytes(n,4);if(String.fromCharCode(...r)!==`fLaC`)throw k(`flac`,`missing fLaC marker`);n+=4;let i;for(let e=0;;e+=1){if(e>=1024)throw k(`flac`,`too many metadata blocks`);let r=await t.bytes(n,4);if(r.length<4)throw k(`flac`,`truncated metadata`);let a=!!(r[0]&128),o=r[0]&127,s=r[1]<<16|r[2]<<8|r[3];if(o===127)throw k(`flac`,`invalid metadata block type`);if(e===0){if(o!==0||s!==34)throw k(`flac`,`STREAMINFO isn't first`);i=z(await t.bytes(n+4,34),`flac`)}if(n+=4+s,n>t.size)throw k(`flac`,`metadata exceeds the file`);if(a)break}if(!i)throw k(`flac`,`missing STREAMINFO`);let a=0,o=0;for(let e=n;e<t.size;e+=V){let n=await t.bytes(e,V+16),r=Math.min(V,t.size-e);for(let e=0;e<r;e+=1){if(n[e]!==255)continue;let t=B(n,e);t&&(a+=Ne(t,i),o+=1)}}if(o===0)throw k(`flac`,`no audio frames`);if(i.totalFrames>a)throw k(`flac`,`STREAMINFO claims more than the frames carry`);return{container:`flac`,codec:`flac`,sampleRate:i.sampleRate,channels:i.channels,frameBound:a,expectedFrames:i.totalFrames>0?i.totalFrames:a,frameTolerance:i.maxBlockSize}}var H=27,Fe=65307,Ie=-1;async function U(e,t){let n=s(e),r,i=Ie,a=[],o=0,c=0,l=0;for(;c+H<=n.size;){let e=await n.bytes(c,Fe);if(e[0]!==79||e[1]!==103||e[2]!==103||e[3]!==83){if(l===0)throw k(`ogg`,`missing first page`);c+=1;continue}let s=new DataView(e.buffer,e.byteOffset,e.byteLength),u=s.getUint8(4),d=s.getUint8(5),f=s.getUint8(26);if(u!==0||e.length<H+f){if(l===0)throw k(`ogg`,`malformed first page`);c+=1;continue}let p=0;for(let t=0;t<f;t+=1)p+=e[H+t];let m=H+f+p;if(m>e.length)break;let h=s.getUint32(14,!0),g=!!(d&2);if(r===void 0){if(!g)throw k(`ogg`,`first page doesn't begin a stream`);r=h}else if(h!==r){if(g)throw new O(`UNSUPPORTED_FORMAT`,`Ogg files with several streams aren't supported.`,{details:{container:`ogg`,reason:`multiplexed streams`}});c+=m,l+=1;continue}d&1||(a=[],o=0);let _=H+f;for(let n=0;n<f;n+=1){let r=e[H+n];if(a.length<64){let t=Math.min(r,64-a.length);for(let n=0;n<t;n+=1)a.push(e[_+n])}o+=r,_+=r,r<255&&(t({head:Uint8Array.from(a),length:o}),a=[],o=0)}let v=s.getInt32(10,!0),y=s.getUint32(6,!0);(v!==-1||y!==4294967295)&&(i=v*2**32+y),c+=m,l+=1}if(l===0)throw k(`ogg`,`missing first page`);return{lastGranule:i}}var Le=13;function Re(e){return e[0]===127&&e[1]===70&&e[2]===76&&e[3]===65&&e[4]===67}async function ze(e){let t,n=0,r=0,i=0,a=0,{lastGranule:o}=await U(e,({head:e})=>{if(n+=1,n===1){if(!Re(e)||e[5]!==1)throw k(`ogg`,`not a FLAC mapping version 1`);let n=String.fromCharCode(...e.subarray(9,13)),i=(e[Le]??127)&127;if(n!==`fLaC`||i!==0)throw k(`ogg`,`missing FLAC STREAMINFO`);t=z(e.subarray(17,51),`ogg`),r=(e[7]??0)<<8|(e[8]??0);return}if(!t)return;let o=e[0]===255&&((e[1]??0)&254)==248;if(n-1<=r||r===0&&!o&&a===0)return;a+=1;let s=B(e,0);if(!s)i+=t.maxBlockSize;else{if(s.channels!==t.channels)throw k(`ogg`,`a frame's channel count differs from STREAMINFO`);if(s.sampleRate!==void 0&&s.sampleRate!==t.sampleRate)throw k(`ogg`,`a frame's sample rate differs from STREAMINFO`);i+=s.blockSize}});if(!t)throw k(`ogg`,`missing FLAC header`);if(a===0)throw k(`ogg`,`no audio frames`);if(t.totalFrames>i||o>i)throw k(`ogg`,`a declared length exceeds the frames`);let s=o>0?o:t.totalFrames;return{container:`ogg`,codec:`flac`,sampleRate:t.sampleRate,channels:t.channels,frameBound:i,expectedFrames:s>0?s:i,frameTolerance:t.maxBlockSize}}var Be=48e3,W=5760;function Ve(e){return String.fromCharCode(...e.subarray(0,8))===`OpusHead`}function He(e){return e<12?[480,960,1920,2880][e%4]:e<16?[480,960][e%2]:[120,240,480,960][e%4]}function Ue(e,t){let n=e[0];if(t===0||n===void 0)return W;let r=He(n>>3),i=n&3,a;if(i===0)a=1;else if(i===1||i===2)a=2;else{let t=e[1];if(t===void 0)return W;a=t&63}let o=a*r;return o===0||o>5760?W:o}function We(e,t){if(!Ve(e)||e.length<19)throw k(t,`malformed OpusHead`);if(e[8]&240)throw k(t,`unsupported OpusHead version`);let n=e[9],r=e[10]|e[11]<<8,i=e[18];if(n===0)throw k(t,`no channels`);if(i===0?n>2:i!==1||n>8)throw new O(`UNSUPPORTED_FORMAT`,`Unsupported Opus channel mapping.`,{details:{container:t,codec:`opus`,reason:`channel mapping family ${i}`}});return{channels:n,preSkip:r}}async function Ge(e){let t=0,n=0,r=0,i=0,{lastGranule:a}=await U(e,({head:e,length:a})=>{if(r+=1,r===1)({channels:t,preSkip:n}=We(e,`ogg`));else if(r===2){if(String.fromCharCode(...e.subarray(0,8))!==`OpusTags`)throw k(`ogg`,`missing OpusTags`)}else i+=Ue(e,a)});if(r<2)throw k(`ogg`,`missing Opus headers`);if(i===0)throw k(`ogg`,`no audio packets`);if(a>i)throw k(`ogg`,`a declared length exceeds the packets`);let o=a>0?Math.max(0,a-n):i;return{container:`ogg`,codec:`opus`,sampleRate:Be,channels:t,frameBound:i,expectedFrames:o,frameTolerance:Math.max(W,n)}}var Ke=`vorbis`;function qe(e){return e[0]===1&&String.fromCharCode(...e.subarray(1,7))===Ke}function Je(e,t){if(!qe(e)||e.length<30)throw k(t,`malformed Vorbis header`);let n=new DataView(e.buffer,e.byteOffset,e.byteLength);if(n.getUint32(7,!0)!==0)throw k(t,`unsupported Vorbis version`);let r=n.getUint8(11),i=n.getUint32(12,!0),a=n.getUint8(28),o=a&15,s=a>>4;if(o<6||s>13||o>s||(n.getUint8(29)&1)!=1)throw k(t,`Vorbis block sizes out of range`);if(r===0||i===0)throw k(t,`Vorbis format out of range`);return{channels:r,sampleRate:i,largeBlock:2**s}}async function Ye(e){let t=0,n=0,r=0,i=0,a=0,{lastGranule:o}=await U(e,({head:e})=>{if(i+=1,i===1)({channels:t,sampleRate:n,largeBlock:r}=Je(e,`ogg`));else if(i===2||i===3){let t=i===2?3:5;if(e[0]!==t||String.fromCharCode(...e.subarray(1,7))!==Ke)throw k(`ogg`,`missing Vorbis comment or setup header`)}else a+=1});if(i<3)throw k(`ogg`,`missing Vorbis headers`);if(a===0)throw k(`ogg`,`no audio packets`);let s=r/2*a;if(o>s)throw k(`ogg`,`a declared length exceeds the packets`);return{container:`ogg`,codec:`vorbis`,sampleRate:n,channels:t,frameBound:s,expectedFrames:o>0?o:s,frameTolerance:r}}var G=440786851,Xe=17026,Ze=408125543,Qe=357149030,$e=2807729,et=17545,tt=374648427,nt=174,rt=215,it=131,at=134,ot=25506,st=524531317,ct=163,lt=160,ut=161,dt=/* @__PURE__ */ new Set([st,475249515,307544935,272869232,423732329,290298740,Qe,tt,G]);function K(e,t){return new O(`UNSUPPORTED_FORMAT`,`Unsupported Matroska: ${e}.`,{details:t===void 0?{container:`webm`,reason:e}:{container:`webm`,codec:t,reason:e}})}function ft(e,t){let n=e[t];if(n===void 0||n===0)return;let r=Math.clz32(n)-23;if(r>4||t+r>e.length)return;let i=0;for(let n=0;n<r;n+=1)i=i*256+e[t+n];return{id:i,length:r}}function q(e,t){let n=e[t];if(n===void 0||n===0)return;let r=Math.clz32(n)-23;if(t+r>e.length)return;let i=n&255>>r,a=i===255>>r;for(let n=1;n<r;n+=1){let r=e[t+n];i=i*256+r,r!==255&&(a=!1)}return{value:i,length:r,unknown:a}}async function J(e,t){let n=await e.bytes(t,12),r=ft(n,0);if(!r)return;let i=q(n,r.length);if(i){if(!i.unknown&&!Number.isSafeInteger(i.value))throw k(`webm`,`element size out of range`);return{id:r.id,body:t+r.length+i.length,size:i.unknown?void 0:i.value}}}function Y(e){let t=[],n=0;for(;n<e.length;){let r=ft(e,n),i=r&&q(e,n+r.length);if(!r||!i||i.unknown)throw k(`webm`,`malformed element`);let a=n+r.length+i.length;if(a+i.value>e.length)throw k(`webm`,`element exceeds its parent`);t.push({id:r.id,data:e.subarray(a,a+i.value)}),n=a+i.value}return t}var X=e=>e.reduce((e,t)=>e*256+t,0);function pt(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength);return e.length===4?t.getFloat32(0):e.length===8?t.getFloat64(0):NaN}function mt(e,t){if(e===`A_OPUS`){if(!t)throw k(`webm`,`missing OpusHead`);let e=We(t,`webm`);return{codec:`opus`,sampleRate:Be,channels:e.channels,tolerance:Math.max(W,e.preSkip),frameSamples:Ue}}if(e===`A_VORBIS`){if(t?.[0]!==2)throw k(`webm`,`malformed Vorbis headers`);let e=1;for(let n=0;n<2;n+=1){for(;t[e]===255;)e+=1;if(t[e]===void 0)throw k(`webm`,`malformed Vorbis headers`);e+=1}let n=Je(t.subarray(e),`webm`),r=n.largeBlock/2;return{codec:`vorbis`,sampleRate:n.sampleRate,channels:n.channels,tolerance:n.largeBlock,frameSamples:()=>r}}if(e===`A_FLAC`){let e=t&&String.fromCharCode(...t.subarray(0,4));if(!t||e!==`fLaC`||(t[4]??127)&127)throw k(`webm`,`missing FLAC STREAMINFO`);let n=z(t.subarray(8,42),`webm`);return{codec:`flac`,sampleRate:n.sampleRate,channels:n.channels,tolerance:n.maxBlockSize,frameSamples(e){let t=B(e,0);if(!t)return n.maxBlockSize;if(t.channels!==n.channels)throw k(`webm`,`a frame's channel count differs from STREAMINFO`);if(t.sampleRate!==void 0&&t.sampleRate!==n.sampleRate)throw k(`webm`,`a frame's sample rate differs from STREAMINFO`);return t.blockSize}}}if(e.startsWith(`A_AAC`)){if(!t)throw K(`AAC without an AudioSpecificConfig`,`aac`);let e=Te(t,`webm`),n=I(e.coreRate),r=1024*Math.ceil(n/e.coreRate);return{codec:`aac`,sampleRate:n,channels:F(e.channelConfiguration),tolerance:2*r,frameSamples:()=>r}}throw K(`codec ${e}`,e)}function ht(e){let t=[];for(let n of Y(e)){if(n.id!==nt)continue;let e=Y(n.data),r=t=>e.find(e=>e.id===t)?.data,i=r(it);if(!i||X(i)!==2)continue;let a=r(rt),o=r(at);if(!a||!o)throw k(`webm`,`incomplete audio track`);let s=String.fromCharCode(...o).replace(/\0+$/,``);t.push({number:X(a),codec:mt(s,r(ot))})}if(t.length===0)throw k(`webm`,`no audio track`);if(t.length>1)throw K(`several audio tracks`);return t[0]}function gt(e,t){let n=q(e,0);if(!n||n.unknown)throw k(`webm`,`malformed block`);if(n.value!==t.number)return 0;let r=n.length+2,i=e[r];if(i===void 0)throw k(`webm`,`truncated block`);r+=1;let a=i>>1&3,o=[];if(a===0)o.push(e.length-r);else{let t=e[r];if(t===void 0)throw k(`webm`,`truncated block`);let n=t+1;if(r+=1,a===1)for(let t=0;t<n-1;t+=1){let t=0;for(;;){let n=e[r];if(n===void 0)throw k(`webm`,`truncated lacing`);if(r+=1,t+=n,n<255)break}o.push(t)}else if(a===3){let t=q(e,r);if(!t||t.unknown)throw k(`webm`,`malformed lacing`);r+=t.length,o.push(t.value);for(let t=1;t<n-1;t+=1){let n=q(e,r);if(!n||n.unknown)throw k(`webm`,`malformed lacing`);r+=n.length;let i=2**(7*n.length-1)-1;o.push(o[t-1]+n.value-i)}}let i=e.length-r;if(a===2){if(i%n!==0)throw k(`webm`,`fixed lacing doesn't divide the block`);for(let e=0;e<n;e+=1)o.push(i/n)}else{let e=o.reduce((e,t)=>e+t,0);o.push(i-e)}}if(o.some(e=>e<0)||r+o.reduce((e,t)=>e+t,0)>e.length)throw k(`webm`,`lacing exceeds the block`);let s=0;for(let n of o)s+=t.codec.frameSamples(e.subarray(r,r+Math.min(n,16)),n),r+=n;return s}async function _t(e){let t=s(e),n=await J(t,0);if(!n||n.id!==G||n.size===void 0)throw k(`webm`,`missing EBML header`);let r=Y(await t.bytes(n.body,n.size)).find(e=>e.id===Xe),i=r?String.fromCharCode(...r.data).replace(/\0+$/,``):``;if(i!==`webm`&&i!==`matroska`)throw K(`document type ${i||`missing`}`);let a=await J(t,n.body+n.size);if(!a||a.id!==Ze)throw k(`webm`,`missing Segment`);let o=Math.min(t.size,a.body+(a.size??t.size)),c,l=1e6,u,d=0,f=0,p,m=a.body;for(;m<o;){let e=await J(t,m);if(!e)throw k(`webm`,`malformed element`);if(p!==void 0&&(m>=p||dt.has(e.id))&&(p=void 0),p===void 0){if(e.id===G)throw K(`several segments`);if(e.id===st){if(!c)throw k(`webm`,`clusters before the tracks`);if(p=e.size===void 0?1/0:e.body+e.size,p!==1/0&&p>o)throw k(`webm`,`element exceeds its parent`);m=e.body;continue}if(e.size===void 0)throw k(`webm`,`unknown size outside a Segment or Cluster`);let n=e.body+e.size;if(n>o)throw k(`webm`,`element exceeds its parent`);if(e.id===Qe){let n=Y(await t.bytes(e.body,e.size)),r=n.find(e=>e.id===$e),i=n.find(e=>e.id===et);r&&(l=X(r.data)),i&&(u=pt(i.data))}else if(e.id===tt){if(c)throw K(`several Tracks elements`);c=ht(await t.bytes(e.body,e.size))}m=n;continue}if(e.size===void 0)throw k(`webm`,`unknown size inside a Cluster`);let n=e.body+e.size;if(n>Math.min(p,o))throw k(`webm`,`element exceeds its parent`);let r=c;if(e.id===ct)d+=gt(await t.bytes(e.body,e.size),r),f+=1;else if(e.id===lt)for(let n of Y(await t.bytes(e.body,e.size)))n.id===ut&&(d+=gt(n.data,r),f+=1);m=n}if(!c)throw k(`webm`,`missing Tracks`);if(f===0||d===0)throw k(`webm`,`no audio blocks`);let{codec:h}=c,g=d;if(u!==void 0){if(!Number.isFinite(u)||u<0)throw k(`webm`,`Duration out of range`);if(g=Math.round(u*l/1e9*h.sampleRate),g>d)throw k(`webm`,`the Duration claims more than the blocks carry`)}return{container:`webm`,codec:h.codec,sampleRate:h.sampleRate,channels:h.channels,frameBound:d,expectedFrames:g,frameTolerance:h.tolerance}}var vt=1e5;function Z(e,t){return new O(`UNSUPPORTED_FORMAT`,`Unsupported MP4: ${e}.`,{details:t===void 0?{container:`mp4`,reason:e}:{container:`mp4`,codec:t,reason:e}})}var yt=class{boxes=0;reader;constructor(e){this.reader=e}async u8(e){let t=await this.reader.bytes(e,1);if(t.length<1)throw k(`mp4`,`truncated box`);return t[0]}async view(e,t){let n=await this.reader.bytes(e,t);if(n.length<t)throw k(`mp4`,`truncated box`);return new DataView(n.buffer,n.byteOffset,n.byteLength)}async children(e,t,n=!1){let r=[],i=e;for(;i+8<=t;){if(this.boxes+=1,this.boxes>vt)throw k(`mp4`,`too many boxes`);let e=await this.view(i,8),a=e.getUint32(0),o=String.fromCharCode(e.getUint8(4),e.getUint8(5),e.getUint8(6),e.getUint8(7)),s=8;if(a===1){let e=await this.view(i+8,8),t=e.getUint32(0);if(t>=2**21)throw k(`mp4`,`box size out of range`);a=t*2**32+e.getUint32(4),s=16}else if(a===0){if(!n)throw k(`mp4`,`open-ended box inside another`);a=t-i}if(a<s||i+a>t)throw k(`mp4`,`box exceeds its parent`);r.push({type:o,body:i+s,end:i+a}),i+=a}return r}async child(e,t){return(await this.children(e.body,e.end)).find(e=>e.type===t)}async need(e,t){let n=await this.child(e,t);if(!n)throw k(`mp4`,`missing ${t} box`);return n}};async function bt(e,t){let n=0;for(let r=0;r<4;r+=1){let i=await e.u8(t+r);if(n=n*128+(i&127),!(i&128))return[n,r+1]}throw k(`mp4`,`malformed descriptor length`)}async function xt(e,t){let n=t.body+4,r=async r=>{if(await e.u8(n)!==r)throw Z(`unexpected ES descriptor`);let[i,a]=await bt(e,n+1);if(n+=1+a,n+i>t.end)throw k(`mp4`,`descriptor exceeds its box`)};await r(3);let i=await e.u8(n+2);n+=3,i&128&&(n+=2),i&64&&(n+=1+await e.u8(n)),i&32&&(n+=2),await r(4);let a=await e.u8(n);if(![64,102,103,104].includes(a))throw Z(`object type ${a}`,a===107||a===105?`mp3`:`unknown`);return n+=13,await r(5),Te(await e.reader.bytes(n,Math.min(5,t.end-n)),`mp4`)}async function St(e){let t=s(e),n=new yt(t),r=await n.children(0,t.size,!0);if(r.some(e=>e.type===`moof`))throw Z(`fragmented MP4`);let i=r.find(e=>e.type===`moov`);if(!i)throw k(`mp4`,`missing moov box`);let a=[];for(let e of(await n.children(i.body,i.end)).filter(e=>e.type===`trak`)){let t=await n.child(e,`mdia`),r=t&&await n.child(t,`hdlr`);t&&r&&(await n.view(r.body+8,4)).getUint32(0)===1936684398&&a.push({trak:e,mdia:t})}if(a.length===0)throw k(`mp4`,`no audio track`);if(a.length>1)throw Z(`several audio tracks`);let{trak:o,mdia:c}=a[0],l=await n.need(c,`mdhd`),u=await n.u8(l.body),d=await n.view(l.body+4,u===1?28:16),f=d.getUint32(u===1?16:8),p=u===1?d.getUint32(20)*2**32+d.getUint32(24):d.getUint32(12);if(f===0)throw k(`mp4`,`timescale is zero`);let m=await n.need(await n.need(c,`minf`),`stbl`),h=await n.need(m,`stsd`),[g]=await n.children(h.body+8,h.end);if(!g)throw k(`mp4`,`empty sample description`);let _=await n.view(g.body,28),v=_.getUint16(8);if(v>1)throw Z(`sound sample entry version 2`);let y=_.getUint16(16),b=_.getUint32(24)>>>16,x=g.body+28+(v===1?16:0),S,C,w,T;if(g.type===`mp4a`){let e=(await n.children(x,g.end)).find(e=>e.type===`esds`);if(!e)throw k(`mp4`,`missing esds box`);let{coreRate:t,channelConfiguration:r}=await xt(n,e);S=`aac`,C=I(t),w=F(r||y),T=1024*Math.ceil(C/t)}else if(g.type===`alac`){let e=(await n.children(x,g.end)).find(e=>e.type===`alac`);if(!e)throw k(`mp4`,`missing ALAC configuration`);let t=await n.view(e.body+4,24);if(S=`alac`,T=t.getUint32(0),w=t.getUint8(9),C=t.getUint32(20)||b,T===0||T>1<<20)throw k(`mp4`,`ALAC frame length out of range`);if(w===0||C===0)throw k(`mp4`,`ALAC format out of range`)}else throw Z(`codec ${g.type}`,g.type);await Ct(n,i,o,{timescale:f,duration:p,decodeRate:C,tolerance:2*T});let E=await wt(n,m,t.size);if(E===0)throw k(`mp4`,`no audio samples`);let D=E*T,ee=p===0?D:Math.round(p*C/f);if(ee>D)throw k(`mp4`,`the duration claims more than the samples carry`);return{container:`mp4`,codec:S,sampleRate:C,channels:w,frameBound:D,expectedFrames:ee,frameTolerance:2*T}}async function Ct(e,t,n,r){let i=(await e.children(n.body,n.end)).filter(e=>e.type===`edts`);if(i.length===0)return;if(i.length>1)throw Z(`several edit lists`);let a=(await e.children(i[0].body,i[0].end)).filter(e=>e.type===`elst`);if(a.length===0)return;if(a.length>1)throw Z(`several edit lists`);let o=a[0],s=await e.need(t,`mvhd`),c=await e.u8(s.body),l=(await e.view(s.body+(c===1?20:12),4)).getUint32(0);if(l===0)throw k(`mp4`,`movie timescale is zero`);let u=await e.u8(o.body),d=(await e.view(o.body+4,4)).getUint32(0),f=u===1?20:12;if(o.body+8+d*f>o.end)throw k(`mp4`,`edit list exceeds its box`);if(d>2)throw Z(`edit list with more than two edits`);let p=!1;for(let t=0;t<d;t+=1){let n=await e.view(o.body+8+t*f,f),i=u===1?n.getUint32(0)*2**32+n.getUint32(4):n.getUint32(0),a=u===1?n.getInt32(8)*2**32+n.getUint32(12):n.getInt32(4),s=n.getInt16(u===1?16:8),c=n.getUint16(u===1?18:10);if(a===-1){if(p||t>0)throw Z(`edit list with an empty edit after another edit`);if(i*r.decodeRate/l>r.tolerance)throw Z(`edit list with a long empty edit`)}else{if(p)throw Z(`edit list with several media edits`);if(p=!0,s!==1||c!==0)throw Z(`edit list with a media rate other than 1`);if(a<0||r.duration===0||a+Math.ceil(i*r.timescale/l)>r.duration)throw Z(`edit list beyond the media`)}}}async function wt(e,t,n){let r=await e.child(t,`stsz`);if(!r)throw await e.child(t,`stz2`)?Z(`compact sample sizes`):k(`mp4`,`missing stsz box`);let i=await e.view(r.body+4,8),a=i.getUint32(0),o=i.getUint32(4);if(a===0&&r.body+12+o*4>r.end)throw k(`mp4`,`stsz table exceeds its box`);let s=await e.need(t,`stsc`),c=(await e.view(s.body+4,4)).getUint32(0);if(s.body+8+c*12>s.end)throw k(`mp4`,`stsc table exceeds its box`);let l=await e.child(t,`stco`)??await e.child(t,`co64`);if(!l)throw k(`mp4`,`missing chunk offsets`);let u=l.type===`co64`,d=(await e.view(l.body+4,4)).getUint32(0);if(l.body+8+d*(u?8:4)>l.end)throw k(`mp4`,`chunk offsets exceed their box`);let f=0,p=0,m=0;for(let t=1;t<=d;t+=1){for(;p<c;){let n=await e.view(s.body+8+p*12,12);if(n.getUint32(0)>t)break;m=n.getUint32(4),p+=1}let i=await e.view(l.body+8+(t-1)*(u?8:4),u?8:4),d=u?i.getUint32(0)*2**32+i.getUint32(4):i.getUint32(0);for(let t=0;t<m;t+=1){if(f>=o)throw k(`mp4`,`the sample tables disagree`);let t=a===0?(await e.view(r.body+12+f*4,4)).getUint32(0):a;if(d+t>n)throw k(`mp4`,`a sample lies outside the file`);d+=t,f+=1}}if(f!==o)throw k(`mp4`,`the sample tables disagree`);return o}var Tt={1:[44100,48e3,32e3],2:[22050,24e3,16e3],2.5:[11025,12e3,8e3]},Et={"1-1":[0,32,64,96,128,160,192,224,256,288,320,352,384,416,448],"1-2":[0,32,48,56,64,80,96,112,128,160,192,224,256,320,384],"1-3":[0,32,40,48,56,64,80,96,112,128,160,192,224,256,320],"2-1":[0,32,48,56,64,80,96,112,128,144,160,176,192,224,256],"2-2":[0,8,16,24,32,40,48,56,64,80,96,112,128,144,160],"2-3":[0,8,16,24,32,40,48,56,64,80,96,112,128,144,160]};function Q(e,t){let n=e[t],r=e[t+1],i=e[t+2],a=e[t+3];if(n!==255||r===void 0||i===void 0||a===void 0||(r&224)!=224)return;let o=r>>3&3,s=r>>1&3,c=i>>4,l=i>>2&3;if(o===1||s===0||c===0||c===15||l===3||(a&3)==2)return;let u=o===3?1:o===2?2:2.5,d=4-s,f=Tt[u]?.[l],p=Et[`${u===1?1:2}-${d}`]?.[c]*1e3,m=i>>1&1,h=a>>6==3?1:2,g,_;if(d===1?(g=384,_=(Math.floor(12*p/f)+m)*4):d===2||u===1?(g=1152,_=Math.floor(144*p/f)+m):(g=576,_=Math.floor(72*p/f)+m),!(_<4))return{version:u,layer:d,sampleRate:f,channels:h,samplesPerFrame:g,frameLength:_,protected:!(r&1)}}function Dt(e,t){let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=t=>t+4<=e.length?String.fromCharCode(...e.subarray(t,t+4)):``,i=t.version===1?t.channels===1?17:32:t.channels===1?9:17,a=4+(t.protected?2:0)+i,o=r(a);if(o===`Xing`||o===`Info`){if(a+8>e.length)return;let t=n.getUint32(a+4);a+=8;let i;if(t&1){if(a+4>e.length)return;i=n.getUint32(a),a+=4}if(t&2&&(a+=4),t&4&&(a+=100),t&8&&(a+=4),a+24<=e.length&&r(a)===`LAME`){let t=e[a+21]*65536+e[a+22]*256+e[a+23];return{...i===void 0?{}:{frames:i},delay:t>>12,padding:t&4095}}return i===void 0?{}:{frames:i}}if(r(36)===`VBRI`&&54<=e.length)return{delay:n.getUint16(42),frames:n.getUint32(50)}}var Ot=65536,kt=(e,t)=>e.version===t.version&&e.layer===t.layer&&e.sampleRate===t.sampleRate;async function At(e,t){return P(s(e),t,Ot,Q,e=>e.frameLength,kt)}function jt(e,t,n){let r=e.sampleRate<t?Math.ceil(t/e.sampleRate):1;return e.samplesPerFrame*r*Math.ceil(e.channels/n)}async function Mt(e,t){let n=s(e),r=await P(n,t,Ot,Q,e=>e.frameLength,kt);if(!r)throw k(`mp3`,`no frames`);let{sampleRate:i,channels:a,samplesPerFrame:o}=r.first,c=Dt(await n.bytes(r.offset,r.first.frameLength),r.first),l=0,u=0;if(await Ce(n,r.offset,4,Q,e=>e.frameLength,(e,t)=>{l+=jt(e,i,a),t&&(u+=1)}),l===0)throw k(`mp3`,`no frames`);let d=c?.delay??0,f=c?.padding??0,p=u*o;if(c?.frames!==void 0){let e=c.frames*o;if(e>l)throw k(`mp3`,`the tag claims more than the frames carry`);p=Math.max(0,e-d-f)}return{container:`mp3`,codec:`mp3`,sampleRate:i,channels:a,frameBound:l,expectedFrames:Math.min(p,l),frameTolerance:2*o+d+f}}function Nt(e,t){return new O(`UNSUPPORTED_FORMAT`,`This container or codec isn't supported.`,{details:t===void 0?{container:e}:{container:e,codec:t}})}async function Pt(e){let t=s(e),n=await t.bytes(0,64),r=(e,t)=>String.fromCharCode(...n.subarray(e,t)),i=r(0,4),a=r(8,12);if((i===`RIFF`||i===`RF64`)&&a===`WAVE`||i===`FORM`&&(a===`AIFF`||a===`AIFC`))return be(e);if(i===`fLaC`)return{probe:await Pe(e)};if(n[0]===26&&n[1]===69&&n[2]===223&&n[3]===163)return{probe:await _t(e)};if([`ftyp`,`moov`,`mdat`,`free`,`skip`,`wide`].includes(r(4,8)))return{probe:await St(e)};let o=Me(n);if(o>0){let n=await t.bytes(o,4);if(String.fromCharCode(...n)===`fLaC`)return{probe:await Pe(e)}}if(i===`OggS`){let r=n[26]??0,i=await t.bytes(27+r,8);if(Re(i))return{probe:await ze(e)};if(Ve(i))return{probe:await Ge(e)};if(qe(i))return{probe:await Ye(e)};throw Nt(`ogg`)}let[c,l]=await Promise.all([At(e,o),De(e,o)]);if(c&&(!l||c.offset<=l.offset))return{probe:await Mt(e,o)};if(l)return{probe:await Oe(e,o)};throw Nt(`unknown`)}var Ft=16,It=512,Lt=9,Rt=.945;function zt(e){let t=1,n=1,r=e*e/4;for(let e=1;n>t*1e-17;e+=1)n*=r/(e*e),t+=n;return t}var Bt;function Vt(){if(Bt)return Bt;let e=Ft*It,t=/* @__PURE__ */ new Float64Array(8194),n=zt(Lt);for(let r=0;r<=e;r+=1){let e=r/It,i=r===0?1:Math.sin(Math.PI*e)/(Math.PI*e),a=e/Ft,o=zt(Lt*Math.sqrt(Math.max(0,1-a*a)))/n;t[r]=i*o}return t[e]=0,Bt=t,t}var $=(e,t)=>Number.isSafeInteger(e)&&e>=t;function Ht(e,t){return Math.ceil(Ft/(Math.min(1,t/e)*Rt))}function Ut(e){let{input:t,inputStart:n,fromRate:r,toRate:i,start:a,frames:o}=e;if(!$(r,1)||!$(i,1)||!$(a,0)||!$(o,0)||!$(n,0)||!Number.isSafeInteger((a+o)*r))throw RangeError(`Rates and positions must be whole numbers, and rates positive.`);let s=Vt(),c=Math.min(1,i/r)*Rt,l=Ht(r,i),u=c*It,d=2*l,f=new Float32Array(o),p=t.length,m=i/Wt(r,i),h=i/m,g=[],_=e=>{let t=new Float64Array(d),n=e/m,r=0;for(let e=0;e<d;e+=1){let i=Math.abs(n-(e-l+1))*u,a=Math.floor(i);if(a>=s.length-1)continue;let o=s[a],c=o+(s[a+1]-o)*(i-a);t[e]=c,r+=c}for(let e=0;e<d;e+=1)t[e]=t[e]/r;return t};for(let e=0;e<o;e+=1){let o=(a+e)*r,s=Math.floor(o/i),c=(o-s*i)/h,u=g[c];u||(u=_(c),g[c]=u);let m=s-l+1-n,v=Math.max(0,-m),y=Math.min(d,p-m),b=0;for(let e=v;e<y;e+=1)b+=t[m+e]*u[e];f[e]=b}return f}function Wt(e,t){let[n,r]=[e,t];for(;r!==0;)[n,r]=[r,n%r];return n}function Gt(e){return 10**(e/20)}function Kt(e,t){return e===`equalPower`?Math.sin(t*Math.PI/2):t}function qt(e,t){let n=e.end-e.start,r=Gt(e.gainDb),{fadeIn:i,fadeOut:a}=e;i.frames>0&&t<i.frames&&(r*=Kt(i.curve,t/i.frames));let o=n-1-t;return a.frames>0&&o<a.frames&&(r*=Kt(a.curve,Math.max(0,o)/a.frames)),r}function Jt(e,t){let n=Array.from({length:t},()=>Array(e).fill(0)),r=(e,t,r)=>{n[e][t]=r},i=Math.SQRT1_2;switch(`${e}>${t}`){case`1>2`:r(0,0,1),r(1,0,1);break;case`1>4`:r(0,0,1),r(1,0,1);break;case`1>6`:r(2,0,1);break;case`2>4`:case`2>6`:r(0,0,1),r(1,1,1);break;case`4>6`:r(0,0,1),r(1,1,1),r(4,2,1),r(5,3,1);break;case`2>1`:r(0,0,.5),r(0,1,.5);break;case`4>1`:for(let e=0;e<4;e+=1)r(0,e,.25);break;case`6>1`:r(0,0,i),r(0,1,i),r(0,2,1),r(0,4,.5),r(0,5,.5);break;case`4>2`:r(0,0,.5),r(0,2,.5),r(1,1,.5),r(1,3,.5);break;case`6>2`:r(0,0,1),r(0,2,i),r(0,4,i),r(1,1,1),r(1,2,i),r(1,5,i);break;case`6>4`:r(0,0,1),r(0,2,i),r(1,1,1),r(1,2,i),r(2,4,1),r(3,5,1);break;default:for(let n=0;n<Math.min(e,t);n+=1)r(n,n,1)}return n}function Yt(e){let{channels:t,frames:n,segments:r}=e,i=Array.from({length:t},()=>new Float64Array(n));for(let e of r){let n=Jt(e.samples.length,t);for(let r=0;r<e.samples.length;r+=1){let a=e.samples[r];for(let o=0;o<t;o+=1){let t=n[o][r];if(t===0)continue;let s=i[o];for(let n=0;n<e.frames;n+=1){let r=qt(e.clip,e.within+n)*e.gain;s[e.at+n]=s[e.at+n]+t*a[n]*r}}}}let a=Array.from({length:t},()=>new Float32Array(n)),o=0,s=0;for(let r=0;r<n;r+=1){let n=!1;for(let s=0;s<t;s+=1){let t=i[s][r]*e.gain,c=Math.abs(t);c>o&&(o=c),c>1&&(n=!0),a[s][r]=Math.max(-1,Math.min(1,t))}n&&(s+=1)}return{channels:a,peak:o,clipped:s}}var Xt={pcm16:2,pcm24:3,float32:4};function Zt(e){return Xt[e]}function Qt(e){return e.frames*e.channels*Xt[e.sampleFormat]}var $t=4294967295,en=[[`title`,`INAM`],[`artist`,`IART`],[`album`,`IPRD`],[`year`,`ICRD`],[`comment`,`ICMT`]];function tn(e){let t=new TextEncoder;return en.flatMap(([n,r])=>{let i=e[n];return i===``?[]:[{id:r,value:t.encode(`${i}\0`)}]})}function nn(e){return e.length===0?0:4+e.reduce((e,{value:t})=>e+8+t.length+t.length%2,0)}function rn(e,t=tn(e.metadata)){let{channels:n,frames:r,sampleFormat:i}=e,a=Zt(i),o=i===`float32`,s=n>2||a>2,c=s?40:o?18:16,l=nn(t),u=Qt(e),d=u%2,f=8+c+(o?12:0)+(l?8+l:0)+8+u+d,p=4+f,m=p>$t||u>$t||r>$t,h=12+(m?36:0)+f;return{bytes:a,float:o,extensible:s,fmtSize:c,infoSize:l,data:u,pad:d,riffSize:p,rf64:m,headerSize:h-u-d,fileSize:h}}var an=1,on=3,sn=65534,cn=[0,0,0,0,16,0,128,0,0,170,0,56,155,113],ln={1:4,2:3,4:51,6:63},un=4294967295,dn=class{bytes;view;at=0;constructor(e){this.bytes=new Uint8Array(e),this.view=new DataView(this.bytes.buffer)}text(e){for(let t=0;t<e.length;t+=1)this.bytes[this.at+t]=e.charCodeAt(t);this.at+=e.length}u16(e){this.view.setUint16(this.at,e,!0),this.at+=2}u32(e){this.view.setUint32(this.at,e,!0),this.at+=4}u64(e){this.u32(e%2**32),this.u32(Math.floor(e/2**32))}raw(e){this.bytes.set(e,this.at),this.at+=e.length}};function fn(e){if(e.length===0)return;let t=new dn(nn(e));t.text(`INFO`);for(let{id:n,value:r}of e)t.text(n),t.u32(r.length),t.raw(r),r.length%2==1&&(t.at+=1);return t.bytes}function pn(e){let{sampleRate:t,channels:n,frames:r,metadata:i}=e,a=tn(i),{bytes:o,float:s,extensible:c,fmtSize:l,data:u,riffSize:d,rf64:f,headerSize:p}=rn(e,a),m=fn(a),h=new dn(p);h.text(f?`RF64`:`RIFF`),h.u32(f?un:d),h.text(`WAVE`),f&&(h.text(`ds64`),h.u32(28),h.u64(d+36),h.u64(u),h.u64(r),h.u32(0)),h.text(`fmt `),h.u32(l);let g=s?on:an;if(h.u16(c?sn:g),h.u16(n),h.u32(t),h.u32(t*n*o),h.u16(n*o),h.u16(o*8),c){h.u16(22),h.u16(o*8),h.u32(ln[n]??0),h.u16(g);for(let e of cn)h.bytes[h.at++]=e}else s&&h.u16(0);return s&&(h.text(`fact`),h.u32(4),h.u32(f?un:r)),m&&(h.text(`LIST`),h.u32(m.length),h.raw(m)),h.text(`data`),h.u32(f?un:u),h.bytes}function mn(e){return e<0?-Math.round(-e):Math.round(e)}function hn(e,t){let n=Number.isFinite(e)?Math.max(-1,Math.min(1,e)):0;if(t===`float32`)return Math.fround(n);let r=t===`pcm16`?32768:8388608;return Math.min(r-1,mn(n*r))+0}function gn(e,t,n){let r=Zt(n),i=new Uint8Array(t*e.length*r),a=new DataView(i.buffer),o=0;for(let i=0;i<t;i+=1)for(let t of e){let e=hn(t[i]??0,n);n===`float32`?a.setFloat32(o,e,!0):n===`pcm16`?a.setInt16(o,e,!0):(a.setUint8(o,e&255),a.setUint8(o+1,e>>8&255),a.setUint8(o+2,e>>16&255)),o+=r}return i}function _n(e){let{channels:t,peak:n,clipped:r}=Yt(e);return{bytes:gn(t,e.frames,e.sampleFormat),peak:n,clipped:r}}function vn(e){te({probe:({file:e})=>Pt(e),async readBlock({file:e,layout:t,start:n,frames:r,bucket:i}){let a=await xe(e,t,n,r);return{channels:a,...Se(a,i)}},peaks:({channels:e,bucket:t})=>Se(e,t),resample:({channels:e,...t})=>e.map(e=>Ut({input:e,...t})),wavHeader:e=>pn(e),renderBlock:e=>_n(e)},{...e,packageVersion:`0.7.0`})}vn();