@hussmartinez/audio-sdk 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1017 @@
1
+ /**
2
+ * Headless core of the audio SDK: contracts, errors, the document session and
3
+ * operations.
4
+ *
5
+ * @remarks
6
+ * This entry is server-safe: it has no React and touches no DOM on import, so
7
+ * Server Components and Node can import it (ADR 0004, ADR 0007).
8
+ *
9
+ * @packageDocumentation
10
+ */
11
+
12
+ import { FilePolicy } from '@hussmartinez/sdk-core';
13
+ import { FilePolicyOverrides } from '@hussmartinez/sdk-core';
14
+ import { Limits } from '@hussmartinez/sdk-core';
15
+ import { SdkCoreErrorCode } from '@hussmartinez/sdk-core';
16
+ import { SdkError } from '@hussmartinez/sdk-core';
17
+
18
+ /**
19
+ * Options every port call accepts.
20
+ *
21
+ * @public
22
+ */
23
+ export declare interface AudioCallOptions {
24
+ /**
25
+ * Cancels the call. It then rejects with an `AudioError` whose code is
26
+ * `ABORTED`, `TIMEOUT`, or the code of an `SdkError` given as the abort
27
+ * reason, such as `PROCESSING_TIME_EXCEEDED`.
28
+ */
29
+ readonly signal?: AbortSignal;
30
+ }
31
+
32
+ /**
33
+ * One piece of a source, played in the plan (ADR 0003).
34
+ *
35
+ * @public
36
+ */
37
+ export declare interface AudioClip {
38
+ /** Stable across edits, so selection and waveform tiles follow a clip that moves. */
39
+ readonly key: string;
40
+ /** The id of the source the clip plays. */
41
+ readonly source: string;
42
+ /** The first source frame the clip plays, inclusive. */
43
+ readonly start: number;
44
+ /** The source frame the clip stops before, exclusive. */
45
+ readonly end: number;
46
+ /** The clip's gain in decibels. 0 leaves it unchanged. */
47
+ readonly gainDb: number;
48
+ readonly fadeIn: AudioFade;
49
+ readonly fadeOut: AudioFade;
50
+ }
51
+
52
+ /**
53
+ * The codecs the probe recognises (ADR 0001). `pcm` covers integer and
54
+ * floating-point samples in WAV and AIFF.
55
+ *
56
+ * @public
57
+ */
58
+ export declare type AudioCodec = "pcm" | "mp3" | "aac" | "alac" | "opus" | "vorbis" | "flac";
59
+
60
+ /**
61
+ * An edit to the clip plan (docs/adr/0003). Commands are plain,
62
+ * structured-cloneable data, never carry samples, and each has an exact
63
+ * inverse, which is how undo works (ADR 0010, ADR 0029).
64
+ *
65
+ * @public
66
+ */
67
+ export declare type AudioCommand = AudioInsertClipsCommand | AudioRemoveClipsCommand | AudioRestoreClipsCommand | AudioReplaceClipsCommand | AudioUpdateClipsCommand | AudioDeleteRangeCommand | AudioTrimClipCommand | AudioSplitClipCommand | AudioMoveClipsCommand | AudioReorderClipsCommand | AudioSetGainCommand | AudioSetFadeCommand | AudioSetMetadataCommand;
68
+
69
+ /**
70
+ * The containers the probe recognises (ADR 0001).
71
+ *
72
+ * @public
73
+ */
74
+ export declare type AudioContainer = "wav" | "aiff" | "flac" | "ogg" | "mp3" | "adts" | "mp4" | "webm";
75
+
76
+ /**
77
+ * Removes the timeline range from `start` to `end`, trimming the clips at
78
+ * its edges. A range inside one clip splits it: the part after the range
79
+ * takes `newKey`.
80
+ *
81
+ * @public
82
+ */
83
+ export declare interface AudioDeleteRangeCommand {
84
+ readonly type: "audio.deleteRange";
85
+ /** The first timeline frame removed, inclusive. */
86
+ readonly start: number;
87
+ /** The timeline frame the range stops before, exclusive. */
88
+ readonly end: number;
89
+ /** The key of the part after the range, when the range is inside one clip. Required only then. */
90
+ readonly newKey?: string;
91
+ }
92
+
93
+ /**
94
+ * The port for reading audio files: probe a file's headers, decode it, and
95
+ * build its waveform overview (ADR 0001, ADR 0003, ADR 0008). The SDK's own
96
+ * adapter probes and parses in its worker and decodes compressed formats
97
+ * with Web Audio; hosts can pass their own to the editor's `engine` prop, and
98
+ * every implementation must pass the engine's contract tests.
99
+ *
100
+ * The engine doesn't apply the file policy: the session checks the probe
101
+ * against it between `probe` and `decode` (ADR 0004).
102
+ *
103
+ * Failures reject with `AudioError`: `INVALID_AUDIO` for a malformed file,
104
+ * `UNSUPPORTED_FORMAT` for one the probe can't bound, `DECODE_FAILED` when
105
+ * the browser can't decode it, and sdk-core's codes for cancellation and
106
+ * worker failures.
107
+ *
108
+ * @public
109
+ */
110
+ export declare interface AudioEngine {
111
+ /**
112
+ * Reads a file's headers and walks its packets, without decoding it. It
113
+ * reads the file through the `Blob`, never all at once.
114
+ */
115
+ probe(file: Blob, options?: AudioCallOptions): Promise<AudioProbe>;
116
+ /**
117
+ * Decodes a probed file at the probe's sample rate and channel count. A
118
+ * result longer than the probe's bound plus its tolerance rejects with
119
+ * `INVALID_AUDIO`; a shorter one is returned as it is.
120
+ */
121
+ decode(file: Blob, probe: AudioProbe, options?: AudioProgressOptions): Promise<DecodedAudio>;
122
+ /**
123
+ * Builds a waveform overview of decoded audio. The audio isn't modified or
124
+ * kept.
125
+ */
126
+ peaks(audio: DecodedAudio, options?: AudioCallOptions): Promise<AudioPeaks>;
127
+ /** Releases the engine and its worker. Pending calls reject, and later calls throw. */
128
+ destroy(): Promise<void>;
129
+ }
130
+
131
+ /**
132
+ * Options for {@link createAudioEngine}.
133
+ *
134
+ * @public
135
+ */
136
+ export declare interface AudioEngineOptions {
137
+ /**
138
+ * Milliseconds without calls after which the worker stops. It starts again
139
+ * on the next call. Defaults to sdk-core's 30 seconds.
140
+ */
141
+ readonly idleTimeout?: number;
142
+ /**
143
+ * Where the application serves the package's `dist/assets/` directory, to
144
+ * load the worker from its own origin instead of a `blob:` URL. The worker
145
+ * is then `audio/audio.worker.js` there, and a Content Security Policy
146
+ * needs `worker-src 'self'` and no `blob:` (docs/adr/0002). Copy
147
+ * `dist/assets` again after every upgrade: a worker from another version
148
+ * is refused with `WORKER_START_FAILED`. By default the worker is inlined.
149
+ */
150
+ readonly assetsUrl?: string | URL;
151
+ }
152
+
153
+ /**
154
+ * The error `@hussmartinez/audio-sdk` throws and rejects with, with a stable
155
+ * `code`, structured `details` and the underlying error as `cause`. Show
156
+ * users the text for its code from {@link audioMessages}, never its
157
+ * `message`, which is for developers and may contain text from the file.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * try {
162
+ * await session.open(file);
163
+ * } catch (error) {
164
+ * if (isAudioError(error, "DECODE_FAILED")) {
165
+ * suggestConvertingToWav();
166
+ * } else {
167
+ * show(messages.error(error));
168
+ * }
169
+ * }
170
+ * ```
171
+ *
172
+ * @public
173
+ */
174
+ export declare class AudioError extends SdkError<AudioErrorCode> {
175
+ /** Always `"AudioError"`, which survives minification and worker boundaries. */
176
+ readonly name = "AudioError";
177
+ }
178
+
179
+ /**
180
+ * Error codes of `@hussmartinez/audio-sdk`: sdk-core's own codes, such as
181
+ * `ABORTED`, `WORKER_CRASHED` and `FILE_TOO_LARGE`, plus the audio SDK's
182
+ * (ADR 0004). They never change meaning once released.
183
+ *
184
+ * - `INVALID_AUDIO`: the file is malformed, its headers claim more audio than
185
+ * its packets carry, or it decodes to more than its bound allows.
186
+ * - `UNSUPPORTED_FORMAT`: the container or codec isn't recognised, the file's
187
+ * packets can't be walked to a bound, or the browser can't decode at its
188
+ * sample rate.
189
+ * - `DECODE_FAILED`: the browser couldn't decode a file the SDK probed,
190
+ * usually because it lacks the codec. `details.codec` names it.
191
+ * - `INVALID_COMMAND`: an edit command doesn't apply to the clip plan, such
192
+ * as a range outside its source or an unknown clip key.
193
+ * - `DURATION_TOO_LONG`: a file exceeds the `maxDuration` limit.
194
+ * - `TOO_MANY_CHANNELS`: a file exceeds the `maxChannels` limit.
195
+ * - `SAMPLE_RATE_TOO_HIGH`: a file exceeds the `maxSampleRate` limit.
196
+ * - `SESSION_TOO_LARGE`: the decoded audio the session holds exceeds the
197
+ * `maxSessionSize` limit.
198
+ * - `TOO_MANY_CLIPS`: the clip plan exceeds the `maxClips` limit.
199
+ *
200
+ * Limit codes carry `details` with the `limit`, its `maximum`, the `actual`
201
+ * value and the `unit`. Other codes' `details` name the container, codec or
202
+ * sample rate involved, never data from the file.
203
+ *
204
+ * @public
205
+ */
206
+ export declare type AudioErrorCode = SdkCoreErrorCode | "INVALID_AUDIO" | "UNSUPPORTED_FORMAT" | "DECODE_FAILED" | "INVALID_COMMAND" | "DURATION_TOO_LONG" | "TOO_MANY_CHANNELS" | "SAMPLE_RATE_TOO_HIGH" | "SESSION_TOO_LARGE" | "TOO_MANY_CLIPS";
207
+
208
+ /**
209
+ * The file an export produces. WAV is the only container so far (ADR 0001).
210
+ *
211
+ * @public
212
+ */
213
+ export declare interface AudioExportFormat {
214
+ readonly container: "wav";
215
+ /** Defaults to `pcm16`. */
216
+ readonly sampleFormat?: AudioSampleFormat_2;
217
+ }
218
+
219
+ /**
220
+ * A fade at one end of a clip. `frames: 0` is no fade.
221
+ *
222
+ * @public
223
+ */
224
+ export declare interface AudioFade {
225
+ readonly frames: number;
226
+ readonly curve: AudioFadeCurve;
227
+ }
228
+
229
+ /**
230
+ * The shape of a fade: `linear` changes the gain at a constant rate, and
231
+ * `equalPower` keeps the perceived loudness steadier, which suits crossfades.
232
+ *
233
+ * @public
234
+ */
235
+ export declare type AudioFadeCurve = "linear" | "equalPower";
236
+
237
+ /**
238
+ * A resolved file policy for the audio SDK. Violations throw
239
+ * {@link AudioError}.
240
+ *
241
+ * @public
242
+ */
243
+ export declare type AudioFilePolicy = FilePolicy<AudioLimitDefinitions>;
244
+
245
+ /**
246
+ * A host's `filePolicy`: a new maximum, or `{ maximum, warning }`, for any of
247
+ * the audio SDK's limits. `Infinity` disables a maximum, which removes a
248
+ * protection against hostile files, or disables a warning. Unknown limit
249
+ * names throw `TypeError` when the policy is resolved.
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * // Long recordings: raise the decoded and session sizes together.
254
+ * const filePolicy: AudioFilePolicyOverrides = {
255
+ * maxDecodedSize: 2e9,
256
+ * maxSessionSize: { maximum: 3e9, warning: 1.5e9 },
257
+ * };
258
+ * ```
259
+ *
260
+ * @public
261
+ */
262
+ export declare type AudioFilePolicyOverrides = FilePolicyOverrides<AudioLimitDefinitions>;
263
+
264
+ /**
265
+ * Inserts clips at a timeline frame. When the frame falls inside a clip,
266
+ * that clip is split there: its first part keeps its key, and its second
267
+ * part takes `splitKey`.
268
+ *
269
+ * @public
270
+ */
271
+ export declare interface AudioInsertClipsCommand {
272
+ readonly type: "audio.insertClips";
273
+ /** The timeline frame to insert at, from 0 to the plan's length. */
274
+ readonly at: number;
275
+ /** The clips, in order, with keys the plan doesn't have. */
276
+ readonly clips: readonly AudioClip[];
277
+ /** The key of the second part of a clip that `at` splits. Required only then. */
278
+ readonly splitKey?: string;
279
+ }
280
+
281
+ /**
282
+ * The type of {@link audioLimitDefinitions}.
283
+ *
284
+ * @public
285
+ */
286
+ export declare type AudioLimitDefinitions = typeof audioLimitDefinitions;
287
+
288
+ /**
289
+ * The audio SDK's limit definitions: sdk-core's core limits with audio
290
+ * defaults, plus five for audio (ADR 0004).
291
+ *
292
+ * | Limit | Code | Default maximum | Default warning |
293
+ * |---|---|---|---|
294
+ * | `maxFileSize` | `FILE_TOO_LARGE` | 1 GB | 250 MB |
295
+ * | `maxFiles` | `TOO_MANY_FILES` | 20 | none |
296
+ * | `maxDecodedSize` | `DECODED_SIZE_TOO_LARGE` | 1 GB | 400 MB |
297
+ * | `maxProcessingTime` | `PROCESSING_TIME_EXCEEDED` | 120 s | none |
298
+ * | `maxDuration` | `DURATION_TOO_LONG` | 3 hours | 1 hour |
299
+ * | `maxChannels` | `TOO_MANY_CHANNELS` | 8 | none |
300
+ * | `maxSampleRate` | `SAMPLE_RATE_TOO_HIGH` | 192 kHz | none |
301
+ * | `maxSessionSize` | `SESSION_TOO_LARGE` | 1.5 GB | 750 MB |
302
+ * | `maxClips` | `TOO_MANY_CLIPS` | 2,000 | none |
303
+ *
304
+ * Sizes are decimal: 1 MB is 1,000,000 bytes. Decoded sizes count 32-bit
305
+ * floats: frames × channels × 4 bytes. `maxDuration` is in milliseconds, and
306
+ * `maxSampleRate` counts samples per second (hertz).
307
+ *
308
+ * @public
309
+ */
310
+ export declare const audioLimitDefinitions: {
311
+ readonly maxFileSize: {
312
+ readonly default: 1000000000;
313
+ readonly warning: 250000000;
314
+ readonly code: "FILE_TOO_LARGE";
315
+ readonly unit: "bytes";
316
+ };
317
+ readonly maxDecodedSize: {
318
+ readonly default: 1000000000;
319
+ readonly warning: 400000000;
320
+ readonly code: "DECODED_SIZE_TOO_LARGE";
321
+ readonly unit: "bytes";
322
+ };
323
+ readonly maxDuration: {
324
+ readonly code: "DURATION_TOO_LONG";
325
+ readonly unit: "milliseconds";
326
+ readonly default: number;
327
+ readonly warning: number;
328
+ };
329
+ readonly maxChannels: {
330
+ readonly code: "TOO_MANY_CHANNELS";
331
+ readonly unit: "count";
332
+ readonly default: 8;
333
+ };
334
+ readonly maxSampleRate: {
335
+ readonly code: "SAMPLE_RATE_TOO_HIGH";
336
+ readonly unit: "count";
337
+ readonly default: 192000;
338
+ };
339
+ readonly maxSessionSize: {
340
+ readonly code: "SESSION_TOO_LARGE";
341
+ readonly unit: "bytes";
342
+ readonly default: 1500000000;
343
+ readonly warning: 750000000;
344
+ };
345
+ readonly maxClips: {
346
+ readonly code: "TOO_MANY_CLIPS";
347
+ readonly unit: "count";
348
+ readonly default: 2000;
349
+ };
350
+ readonly maxFiles: {
351
+ readonly code: "TOO_MANY_FILES";
352
+ readonly unit: "count";
353
+ readonly default: 20;
354
+ };
355
+ readonly maxProcessingTime: {
356
+ readonly code: "PROCESSING_TIME_EXCEEDED";
357
+ readonly unit: "milliseconds";
358
+ readonly default: 120000;
359
+ };
360
+ };
361
+
362
+ /**
363
+ * The names of the audio SDK's limits.
364
+ *
365
+ * @public
366
+ */
367
+ export declare type AudioLimitName = keyof AudioLimitDefinitions;
368
+
369
+ /**
370
+ * The audio SDK's limits, ready to resolve a host's `filePolicy`. Violations
371
+ * are thrown as {@link AudioError}, with `details` naming the limit.
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * const policy = audioLimits.resolve(props.filePolicy);
376
+ * policy.check("maxChannels", channels); // throws AudioError("TOO_MANY_CHANNELS") above 8
377
+ * ```
378
+ *
379
+ * @public
380
+ */
381
+ export declare const audioLimits: Limits<AudioLimitDefinitions>;
382
+
383
+ /**
384
+ * The type of {@link audioMessages}, for typing a host's overrides with
385
+ * `MessageOverrides<AudioMessageCatalogue>`.
386
+ *
387
+ * @public
388
+ */
389
+ export declare type AudioMessageCatalogue = typeof audioMessages;
390
+
391
+ /**
392
+ * The audio SDK's English message catalogue: sdk-core's messages, and text
393
+ * for every {@link AudioErrorCode} and every limit's warning. Pass it to
394
+ * `createMessages` from `@hussmartinez/sdk-core`, with the host's locale and
395
+ * overrides. The editor's text joins it as the editor arrives.
396
+ *
397
+ * Error text is looked up by code, with the error's `details` as
398
+ * parameters, and never taken from `error.message` (ADR 0019).
399
+ *
400
+ * @example
401
+ * ```ts
402
+ * const messages = createMessages(audioMessages, { locale: "en", overrides: props.messages });
403
+ * messages.error(error); // "This recording has 12 channels, which is over the 8-channel limit."
404
+ * messages.t("warnings.maxSessionSize", { actual: 800e6 });
405
+ * ```
406
+ *
407
+ * @public
408
+ */
409
+ export declare const audioMessages: {
410
+ readonly "errors.INVALID_AUDIO": "This file isn't valid audio, or it's too damaged to open.";
411
+ readonly "errors.UNSUPPORTED_FORMAT": "This audio format isn't supported. Try converting the file to WAV, then open it again.";
412
+ readonly "errors.DECODE_FAILED": "This browser can't play this kind of audio. Try another browser, or convert the file to WAV.";
413
+ readonly "errors.INVALID_COMMAND": "This edit couldn't be made. Please try again.";
414
+ readonly "errors.DURATION_TOO_LONG": "This recording is longer than the editor can open.";
415
+ readonly "errors.TOO_MANY_CHANNELS": "This recording has {actual, number} channels, which is over the {maximum, number}-channel limit.";
416
+ readonly "errors.SAMPLE_RATE_TOO_HIGH": "This recording's sample rate is {actual, number} Hz, which is over the {maximum, number} Hz limit.";
417
+ readonly "errors.SESSION_TOO_LARGE": "The open recordings would use {actual, fileSize} of memory, which is over the {maximum, fileSize} limit. Remove a recording, or clear the undo history, and try again.";
418
+ readonly "errors.TOO_MANY_CLIPS": {
419
+ readonly $plural: "maximum";
420
+ } & {
421
+ readonly one: "A recording can have {maximum} clip. Join or delete clips, and try again.";
422
+ readonly other: "A recording can have up to {maximum} clips. Join or delete clips, and try again.";
423
+ };
424
+ readonly "warnings.maxDuration": "This recording is long, and may be slow to open and edit.";
425
+ readonly "warnings.maxChannels": "This recording has {actual, number} channels, and may be slow to edit.";
426
+ readonly "warnings.maxSampleRate": "This recording's sample rate is {actual, number} Hz, and it may be slow to edit.";
427
+ readonly "warnings.maxSessionSize": "The open recordings use {actual, fileSize} of memory. Clearing the undo history frees memory.";
428
+ readonly "warnings.maxClips": {
429
+ readonly $plural: "actual";
430
+ } & {
431
+ readonly one: "This recording has {actual} clip. Many clips may be slow to play and export.";
432
+ readonly other: "This recording has {actual} clips. Many clips may be slow to play and export.";
433
+ };
434
+ readonly "errors.UNKNOWN": "Something went wrong. Please try again.";
435
+ readonly "errors.ABORTED": "The operation was cancelled.";
436
+ readonly "errors.TIMEOUT": "The operation took too long and was stopped.";
437
+ readonly "errors.WORKER_START_FAILED": "The file processor couldn't start. Reload the page and try again.";
438
+ readonly "errors.WORKER_PROTOCOL_MISMATCH": "The file processor is out of date. Reload the page to update it.";
439
+ readonly "errors.WORKER_CRASHED": "The file processor stopped unexpectedly. Please try again.";
440
+ readonly "errors.WORKER_TERMINATED": "The operation was stopped before it finished. Please try again.";
441
+ readonly "errors.WORKER_METHOD_NOT_FOUND": "This operation isn't available. Reload the page and try again.";
442
+ readonly "errors.WORKER_DATA_NOT_CLONEABLE": "Something went wrong while processing the file. Please try again.";
443
+ readonly "errors.UNSUPPORTED_ENVIRONMENT": "This browser doesn't support a feature this tool needs. Update your browser, or try another one.";
444
+ readonly "errors.FILE_TOO_LARGE": "This file is {actual, fileSize}, which is over the {maximum, fileSize} limit.";
445
+ readonly "errors.TOO_MANY_FILES": {
446
+ readonly $plural: "maximum";
447
+ } & {
448
+ readonly one: "You can add {maximum} file at a time.";
449
+ readonly other: "You can add up to {maximum} files at a time.";
450
+ };
451
+ readonly "errors.DECODED_SIZE_TOO_LARGE": "This file expands to more than {maximum, fileSize} when opened, which is over the limit.";
452
+ readonly "errors.PROCESSING_TIME_EXCEEDED": "Processing this file took too long and was stopped.";
453
+ readonly "warnings.maxFileSize": "This file is {actual, fileSize}. Large files may be slow to process.";
454
+ readonly "warnings.maxFiles": "Adding {actual} files at once may be slow.";
455
+ readonly "warnings.maxDecodedSize": "This file is very large once opened and may be slow to process.";
456
+ readonly "warnings.maxProcessingTime": "This is taking longer than usual.";
457
+ readonly "keys.control": "Ctrl";
458
+ readonly "keys.alt": "Alt";
459
+ readonly "keys.shift": "Shift";
460
+ readonly "keys.meta": "Meta";
461
+ readonly "keys.apple.control": "\u2303";
462
+ readonly "keys.apple.alt": "\u2325";
463
+ readonly "keys.apple.shift": "\u21E7";
464
+ readonly "keys.apple.meta": "\u2318";
465
+ readonly "keys.space": "Space";
466
+ readonly "keys.enter": "Enter";
467
+ readonly "keys.escape": "Esc";
468
+ readonly "keys.tab": "Tab";
469
+ readonly "keys.delete": "Delete";
470
+ readonly "keys.backspace": "Backspace";
471
+ readonly "keys.arrowUp": "\u2191";
472
+ readonly "keys.arrowDown": "\u2193";
473
+ readonly "keys.arrowLeft": "\u2190";
474
+ readonly "keys.arrowRight": "\u2192";
475
+ readonly "keys.home": "Home";
476
+ readonly "keys.end": "End";
477
+ readonly "keys.pageUp": "Page Up";
478
+ readonly "keys.pageDown": "Page Down";
479
+ };
480
+
481
+ /**
482
+ * Metadata that export writes into the file (ADR 0001).
483
+ *
484
+ * @public
485
+ */
486
+ export declare interface AudioMetadata {
487
+ readonly title: string;
488
+ readonly artist: string;
489
+ readonly album: string;
490
+ readonly year: string;
491
+ readonly comment: string;
492
+ }
493
+
494
+ /**
495
+ * Moves clips, keeping their relative order, so that the first of them
496
+ * ends up at index `to` of the resulting plan.
497
+ *
498
+ * @public
499
+ */
500
+ export declare interface AudioMoveClipsCommand {
501
+ readonly type: "audio.moveClips";
502
+ readonly keys: readonly string[];
503
+ readonly to: number;
504
+ }
505
+
506
+ /**
507
+ * The port for playback (ADR 0002, ADR 0003, ADR 0008). Playback is a
508
+ * function of the plan and the playhead: loading a changed plan or seeking
509
+ * while playing reschedules from the current position. The SDK's own
510
+ * adapter plays through Web Audio; tests use a fake with a manual clock.
511
+ *
512
+ * Positions are frames on the plan's timeline, from 0 to the plan's length.
513
+ *
514
+ * @public
515
+ */
516
+ export declare interface AudioOutput {
517
+ getSnapshot(): AudioOutputSnapshot;
518
+ /** Calls the listener after every change of state. Returns a function that unsubscribes. */
519
+ subscribe(listener: () => void): () => void;
520
+ /** The playhead, in frames, read from the audio clock. */
521
+ position(): number;
522
+ /**
523
+ * Sets what plays: the plan and the decoded sources its clips refer to.
524
+ * While playing, playback continues from the current position with the
525
+ * new plan. A playhead past the new plan's end moves to its end.
526
+ */
527
+ load(plan: AudioPlan, sources: ReadonlyMap<string, DecodedAudio>): void;
528
+ /**
529
+ * Plays from the playhead, or from the start when the playhead is at the
530
+ * end. The first call may need a user gesture, as browsers' autoplay rules
531
+ * require. When playback reaches the end, the state becomes `stopped` and
532
+ * the playhead stays at the end.
533
+ */
534
+ play(): Promise<void>;
535
+ /** Pauses, keeping the playhead. */
536
+ pause(): void;
537
+ /** Stops, and moves the playhead to the start. */
538
+ stop(): void;
539
+ /** Moves the playhead, clamped to the plan. While playing, playback continues from there. */
540
+ seek(frame: number): void;
541
+ /** Stops playback and releases the audio device. Later calls throw. */
542
+ destroy(): Promise<void>;
543
+ }
544
+
545
+ /**
546
+ * The output's state, for `useSyncExternalStore`. A new object on every
547
+ * change. The playhead isn't in it, because it changes on every audio frame
548
+ * (ADR 0003); read it with {@link AudioOutput.position}.
549
+ *
550
+ * @public
551
+ */
552
+ export declare interface AudioOutputSnapshot {
553
+ readonly state: AudioOutputState;
554
+ }
555
+
556
+ /**
557
+ * Whether the output is playing.
558
+ *
559
+ * @public
560
+ */
561
+ export declare type AudioOutputState = "stopped" | "playing" | "paused";
562
+
563
+ /**
564
+ * One level of a waveform overview: the lowest and highest sample of each
565
+ * bucket of frames, per channel.
566
+ *
567
+ * @public
568
+ */
569
+ export declare interface AudioPeakLevel {
570
+ /** How many frames each bucket covers. */
571
+ readonly framesPerBucket: number;
572
+ /** For each channel, the minimum and maximum sample of each bucket. */
573
+ readonly channels: readonly {
574
+ readonly min: Float32Array;
575
+ readonly max: Float32Array;
576
+ }[];
577
+ }
578
+
579
+ /**
580
+ * A source's waveform overview, a min/max pyramid at several zoom levels,
581
+ * computed once so that drawing the waveform never scans samples (ADR 0002).
582
+ * Levels are ordered from the finest to the coarsest.
583
+ *
584
+ * @public
585
+ */
586
+ export declare interface AudioPeaks {
587
+ readonly levels: readonly AudioPeakLevel[];
588
+ }
589
+
590
+ /**
591
+ * The session's document: clips played back to back on one track, over
592
+ * immutable sources, with the metadata (ADR 0003). Every position and length
593
+ * is a whole number of frames at `sampleRate`.
594
+ *
595
+ * @public
596
+ */
597
+ export declare interface AudioPlan {
598
+ /** The session's sample rate, from its first source. */
599
+ readonly sampleRate: number;
600
+ /** The session's channel count, from its first source. */
601
+ readonly channels: number;
602
+ readonly clips: readonly AudioClip[];
603
+ readonly metadata: AudioMetadata;
604
+ }
605
+
606
+ /**
607
+ * What the probe learned about a file before anything decodes it (ADR 0001,
608
+ * ADR 0004). Limits are checked against it.
609
+ *
610
+ * @public
611
+ */
612
+ export declare interface AudioProbe {
613
+ readonly container: AudioContainer;
614
+ readonly codec: AudioCodec;
615
+ /** The sample rate the decoder will output, which can differ from the header's, as for Opus. */
616
+ readonly sampleRate: number;
617
+ /** The channel count the decoder will output, which can differ from the header's. */
618
+ readonly channels: number;
619
+ /**
620
+ * The most frames the decoder can output, at {@link AudioProbe.sampleRate}
621
+ * and {@link AudioProbe.channels}: the exact length of an uncompressed
622
+ * file, or the sum over every packet of a compressed one, never a count
623
+ * declared in a header. A packet that could decode to more channels, or
624
+ * at a lower rate that's then resampled, counts as the frames that cover
625
+ * it, so `frameBound × channels × 4` bounds the decoded bytes.
626
+ */
627
+ readonly frameBound: number;
628
+ /**
629
+ * The expected length in frames: a header's count when it's within
630
+ * {@link AudioProbe.frameBound}, otherwise the bound itself.
631
+ */
632
+ readonly expectedFrames: number;
633
+ /**
634
+ * For a truncated uncompressed file, as from an interrupted recording or
635
+ * download, the frames its header declares. The file opens with the
636
+ * frames it holds, which are fewer; hosts can say that it's incomplete.
637
+ * Compressed files whose headers claim more than their packets carry fail
638
+ * instead (docs/adr/0001).
639
+ */
640
+ readonly declaredFrames?: number;
641
+ /**
642
+ * How many frames beyond {@link AudioProbe.frameBound} a decoder may
643
+ * legitimately return, such as one or two codec frames for encoder delay,
644
+ * priming or pre-skip. A longer result fails with `INVALID_AUDIO`.
645
+ */
646
+ readonly frameTolerance: number;
647
+ }
648
+
649
+ /**
650
+ * The port for exporting: render a plan's clips, with their gain and fades,
651
+ * and encode the result (ADR 0001, ADR 0003, ADR 0008). The SDK's own
652
+ * adapter renders and encodes in its worker, block by block; hosts can pass
653
+ * their own to the editor's `processor` prop, and every implementation must
654
+ * pass the processor's contract tests.
655
+ *
656
+ * Failures reject with `AudioError` for cancellation and worker failures,
657
+ * `TypeError` for a clip whose source isn't in the job, and `RangeError` for
658
+ * a clip outside its source.
659
+ *
660
+ * @public
661
+ */
662
+ export declare interface AudioProcessor {
663
+ /**
664
+ * Renders and encodes a plan. Every sample is clamped to full scale before
665
+ * it's quantised, so a gain above full scale clips rather than wrapping
666
+ * around. The sources aren't modified.
667
+ */
668
+ render(job: AudioRenderJob, options?: AudioProgressOptions): Promise<Blob>;
669
+ /** Releases the processor and its worker. Pending calls reject, and later calls throw. */
670
+ destroy(): Promise<void>;
671
+ }
672
+
673
+ /**
674
+ * Options for long calls, which report how far they've got.
675
+ *
676
+ * @public
677
+ */
678
+ export declare interface AudioProgressOptions extends AudioCallOptions {
679
+ /**
680
+ * Called with the fraction done, from 0 to 1. It only moves forward, and
681
+ * it's called with 1 before the call resolves.
682
+ */
683
+ readonly onProgress?: (fraction: number) => void;
684
+ }
685
+
686
+ /**
687
+ * Removes clips. Their sources stay, so undo can bring them back.
688
+ *
689
+ * @public
690
+ */
691
+ export declare interface AudioRemoveClipsCommand {
692
+ readonly type: "audio.removeClips";
693
+ readonly keys: readonly string[];
694
+ }
695
+
696
+ /**
697
+ * One export: a plan, the decoded sources its clips refer to, and the
698
+ * output format. The plan's metadata goes into the file.
699
+ *
700
+ * @public
701
+ */
702
+ export declare interface AudioRenderJob {
703
+ readonly plan: AudioPlan;
704
+ /** The decoded sources, by id. Every clip's source must be here. */
705
+ readonly sources: ReadonlyMap<string, DecodedAudio>;
706
+ readonly format: AudioExportFormat;
707
+ }
708
+
709
+ /**
710
+ * Puts every clip in a new order, given as every key exactly once. It's
711
+ * what undoing a move runs.
712
+ *
713
+ * @public
714
+ */
715
+ export declare interface AudioReorderClipsCommand {
716
+ readonly type: "audio.reorderClips";
717
+ readonly order: readonly string[];
718
+ }
719
+
720
+ /**
721
+ * Replaces `count` clips from `index` with `clips`. It's what undoing an
722
+ * insert, a split or a range deletion runs: those change a run of
723
+ * neighbouring clips.
724
+ *
725
+ * @public
726
+ */
727
+ export declare interface AudioReplaceClipsCommand {
728
+ readonly type: "audio.replaceClips";
729
+ readonly index: number;
730
+ readonly count: number;
731
+ readonly clips: readonly AudioClip[];
732
+ }
733
+
734
+ /**
735
+ * Puts removed clips back at their indexes, with every field as it was.
736
+ * It's what undoing a removal runs.
737
+ *
738
+ * @public
739
+ */
740
+ export declare interface AudioRestoreClipsCommand {
741
+ readonly type: "audio.restoreClips";
742
+ /** Each clip, and the index it takes in the resulting plan, in increasing order. */
743
+ readonly entries: readonly {
744
+ readonly index: number;
745
+ readonly clip: AudioClip;
746
+ }[];
747
+ }
748
+
749
+ /**
750
+ * How samples are stored in an exported WAV file (ADR 0001).
751
+ *
752
+ * @public
753
+ */
754
+ declare type AudioSampleFormat_2 = "pcm16" | "pcm24" | "float32";
755
+ export { AudioSampleFormat_2 as AudioSampleFormat }
756
+
757
+ /**
758
+ * Sets a clip's fade in or fade out. Together, the two fades must fit in
759
+ * the clip.
760
+ *
761
+ * @public
762
+ */
763
+ export declare interface AudioSetFadeCommand {
764
+ readonly type: "audio.setFade";
765
+ readonly key: string;
766
+ readonly edge: "in" | "out";
767
+ /** The fade's length in frames. 0 removes it. */
768
+ readonly frames: number;
769
+ readonly curve: AudioFadeCurve;
770
+ }
771
+
772
+ /**
773
+ * Sets the gain of clips.
774
+ *
775
+ * @public
776
+ */
777
+ export declare interface AudioSetGainCommand {
778
+ readonly type: "audio.setGain";
779
+ readonly keys: readonly string[];
780
+ /** Decibels, from {@link MIN_GAIN_DB} to {@link MAX_GAIN_DB}. */
781
+ readonly gainDb: number;
782
+ }
783
+
784
+ /**
785
+ * Sets one metadata field, which export writes into the file.
786
+ *
787
+ * @public
788
+ */
789
+ export declare interface AudioSetMetadataCommand {
790
+ readonly type: "audio.setMetadata";
791
+ readonly field: keyof AudioMetadata;
792
+ /** At most {@link MAX_METADATA_LENGTH} characters. */
793
+ readonly value: string;
794
+ }
795
+
796
+ /**
797
+ * A source the session holds, without its samples: what the snapshot shows
798
+ * (ADR 0003).
799
+ *
800
+ * @public
801
+ */
802
+ export declare interface AudioSourceInfo {
803
+ readonly id: string;
804
+ readonly sampleRate: number;
805
+ readonly channels: number;
806
+ /** The length in frames: the decoded length, capped at the probe's bound (ADR 0004). */
807
+ readonly frames: number;
808
+ readonly container: AudioContainer;
809
+ readonly codec: AudioCodec;
810
+ }
811
+
812
+ /**
813
+ * Splits a clip in two at a timeline frame inside it. The first part keeps
814
+ * the clip's key and fade in, and the second takes `newKey` and the fade out.
815
+ *
816
+ * @public
817
+ */
818
+ export declare interface AudioSplitClipCommand {
819
+ readonly type: "audio.splitClip";
820
+ readonly key: string;
821
+ /** A timeline frame strictly inside the clip. */
822
+ readonly at: number;
823
+ readonly newKey: string;
824
+ }
825
+
826
+ /**
827
+ * Changes the range of its source a clip plays. Fades that no longer fit
828
+ * are shortened in proportion.
829
+ *
830
+ * @public
831
+ */
832
+ export declare interface AudioTrimClipCommand {
833
+ readonly type: "audio.trimClip";
834
+ readonly key: string;
835
+ /** The first source frame, inclusive. */
836
+ readonly start: number;
837
+ /** The source frame the clip stops before, exclusive. */
838
+ readonly end: number;
839
+ }
840
+
841
+ /**
842
+ * Sets clips to new states, matched by key, with every field given. It's
843
+ * what undoing a trim, a gain or a fade runs.
844
+ *
845
+ * @public
846
+ */
847
+ export declare interface AudioUpdateClipsCommand {
848
+ readonly type: "audio.updateClips";
849
+ readonly clips: readonly AudioClip[];
850
+ }
851
+
852
+ /**
853
+ * A clip's length in frames.
854
+ *
855
+ * @public
856
+ */
857
+ export declare function clipLength(clip: AudioClip): number;
858
+
859
+ /**
860
+ * Where each clip starts on the timeline, in frames. It's derived from the
861
+ * clips before it, never stored, so it can't disagree with them (ADR 0003).
862
+ *
863
+ * @public
864
+ */
865
+ export declare function clipOffsets(plan: AudioPlan): readonly number[];
866
+
867
+ /**
868
+ * Creates the SDK's audio engine (docs/adr/0001, docs/adr/0002). It probes
869
+ * files in its worker: WAV and AIFF, FLAC (native or in Ogg), MP3, AAC in
870
+ * ADTS, AAC or ALAC in MP4, Opus or Vorbis in Ogg, and Opus, Vorbis, FLAC
871
+ * or AAC in WebM and Matroska, walking every frame of a compressed file to
872
+ * bound its decoded length. It parses WAV and AIFF block by block into an
873
+ * `AudioBuffer`, building the waveform overview as it goes. It decodes
874
+ * compressed files with the browser's own decoder, on the page, under
875
+ * sdk-core ADR 0031's rules: only probed files, a fresh buffer, the output
876
+ * checked against the probe's bound, errors by code, and the time limit of
877
+ * the caller's signal. Other containers fail with `UNSUPPORTED_FORMAT`.
878
+ *
879
+ * Nothing starts until the first call. By default the worker is inlined,
880
+ * which needs `worker-src blob:` in a Content Security Policy; with
881
+ * `assetsUrl` it loads from your own origin, which needs `worker-src 'self'`
882
+ * instead. Either way it makes no requests of its own.
883
+ *
884
+ * @example
885
+ * ```ts
886
+ * const engine = createAudioEngine();
887
+ * const probe = await engine.probe(file);
888
+ * policy.check("maxDuration", (probe.frameBound / probe.sampleRate) * 1000);
889
+ * const audio = await engine.decode(file, probe, { onProgress });
890
+ * ```
891
+ *
892
+ * @public
893
+ */
894
+ export declare function createAudioEngine(options?: AudioEngineOptions): AudioEngine;
895
+
896
+ /**
897
+ * Converts decibels to a linear gain factor.
898
+ *
899
+ * @public
900
+ */
901
+ export declare function dbToGain(db: number): number;
902
+
903
+ /**
904
+ * Decoded audio, as 32-bit float samples per channel. A Web Audio
905
+ * `AudioBuffer` is one, and tests can supply their own.
906
+ *
907
+ * @public
908
+ */
909
+ export declare interface DecodedAudio {
910
+ readonly sampleRate: number;
911
+ /** The length in frames. */
912
+ readonly length: number;
913
+ readonly numberOfChannels: number;
914
+ /** The samples of one channel. The caller must not modify them. */
915
+ getChannelData(channel: number): Float32Array;
916
+ }
917
+
918
+ /**
919
+ * Metadata with every field empty.
920
+ *
921
+ * @public
922
+ */
923
+ export declare const EMPTY_METADATA: AudioMetadata;
924
+
925
+ /**
926
+ * Every position and length in the model is a whole number of frames, at the
927
+ * session's sample rate (ADR 0003). A frame is one sample on every channel.
928
+ * Seconds appear only at the edges: in the UI and in events.
929
+ */
930
+ /**
931
+ * Converts frames to seconds, for display and events.
932
+ *
933
+ * @param frames - A whole number of frames.
934
+ * @param sampleRate - Frames per second.
935
+ * @public
936
+ */
937
+ export declare function framesToSeconds(frames: number, sampleRate: number): number;
938
+
939
+ /**
940
+ * The gain at one frame of a clip, from its gain and its fades: the single
941
+ * definition of the envelope, which export applies per sample and playback
942
+ * samples into the clip's `GainNode` (ADR 0001).
943
+ *
944
+ * A fade in starts at silence on the clip's first frame, and a fade out
945
+ * reaches silence on its last frame.
946
+ *
947
+ * @param clip - The clip.
948
+ * @param frame - A frame within the clip, from 0 to its length minus one.
949
+ * @public
950
+ */
951
+ export declare function gainAt(clip: AudioClip, frame: number): number;
952
+
953
+ /**
954
+ * Checks whether a value is an {@link AudioError}, optionally with a given
955
+ * code.
956
+ *
957
+ * Unlike `instanceof`, it recognises errors from another copy of this
958
+ * package or another realm, and errors rebuilt after crossing a worker
959
+ * boundary.
960
+ *
961
+ * @param value - Any thrown value.
962
+ * @param code - When given, the error must also have this code.
963
+ * @public
964
+ */
965
+ export declare function isAudioError<Code extends AudioErrorCode = AudioErrorCode>(value: unknown, code?: Code): value is AudioError & {
966
+ readonly code: Code;
967
+ };
968
+
969
+ /**
970
+ * The highest clip gain, in decibels: enough to normalize a very quiet
971
+ * recording, while every gain stays a finite factor.
972
+ *
973
+ * @public
974
+ */
975
+ export declare const MAX_GAIN_DB = 120;
976
+
977
+ /**
978
+ * The longest metadata value, in UTF-16 code units, so that history steps
979
+ * stay small.
980
+ *
981
+ * @public
982
+ */
983
+ export declare const MAX_METADATA_LENGTH = 1000;
984
+
985
+ /**
986
+ * The lowest clip gain, in decibels: far below audibility, so it serves as
987
+ * silence.
988
+ *
989
+ * @public
990
+ */
991
+ export declare const MIN_GAIN_DB = -120;
992
+
993
+ /**
994
+ * No fade.
995
+ *
996
+ * @public
997
+ */
998
+ export declare const NO_FADE: AudioFade;
999
+
1000
+ /**
1001
+ * The plan's length in frames: the sum of its clips' lengths.
1002
+ *
1003
+ * @public
1004
+ */
1005
+ export declare function planLength(plan: AudioPlan): number;
1006
+
1007
+ /**
1008
+ * Converts seconds to the nearest whole frame, for input from the UI, such
1009
+ * as a typed trim point.
1010
+ *
1011
+ * @param seconds - A time in seconds.
1012
+ * @param sampleRate - Frames per second.
1013
+ * @public
1014
+ */
1015
+ export declare function secondsToFrames(seconds: number, sampleRate: number): number;
1016
+
1017
+ export { }