@hussmartinez/audio-sdk 0.6.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/dist/assets/audio/audio.worker.js +1 -1
- package/dist/element.d.ts +1010 -1
- package/dist/element.js +121 -0
- package/dist/element.js.map +1 -0
- package/dist/index.js +1 -2657
- package/dist/index.js.map +1 -1
- package/dist/lazy/inline-worker-CVcXmYih.js +30 -0
- package/dist/lazy/{inline-worker-D8bxbE-Q.js.map → inline-worker-CVcXmYih.js.map} +1 -1
- package/dist/react.d.ts +1957 -3
- package/dist/react.js +1 -0
- package/dist/shared/AudioEditor-C9yohR_Z.js +2801 -0
- package/dist/shared/AudioEditor-C9yohR_Z.js.map +1 -0
- package/dist/shared/text-CgDrmmTa.js +2663 -0
- package/dist/shared/text-CgDrmmTa.js.map +1 -0
- package/dist/styles.css +329 -0
- package/package.json +9 -5
- package/dist/lazy/inline-worker-D8bxbE-Q.js +0 -30
package/dist/index.js
CHANGED
|
@@ -1,889 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
// src/core/errors/audio-error.ts
|
|
3
|
-
var AudioError = class extends SdkError {
|
|
4
|
-
name = "AudioError";
|
|
5
|
-
};
|
|
6
|
-
function isAudioError(value, code) {
|
|
7
|
-
return isSdkError(value, code) && value.name === "AudioError";
|
|
8
|
-
}
|
|
9
|
-
// src/core/errors/abort.ts
|
|
10
|
-
var { adapt: asAudioError, abortError, throwIfAborted } = /* @__PURE__ */ createErrorHelpers(AudioError, {
|
|
11
|
-
fallbackCode: "UNKNOWN",
|
|
12
|
-
fallbackMessage: "Audio processing failed."
|
|
13
|
-
});
|
|
14
|
-
// src/adapters/audio-worker/loader.ts
|
|
15
|
-
function audioWorkerSource(assetsUrl) {
|
|
16
|
-
const selfHosted = assetsUrl === void 0 ? void 0 : new URL("audio/audio.worker.js", resolveAssetsBase(assetsUrl)).href;
|
|
17
|
-
return {
|
|
18
|
-
loadWorker: selfHosted ? async () => () => new Worker(selfHosted, {
|
|
19
|
-
type: "module",
|
|
20
|
-
name: "hm-audio"
|
|
21
|
-
}) : async () => (await import("./lazy/inline-worker-D8bxbE-Q.js")).createInlineWorker,
|
|
22
|
-
version: "0.6.0",
|
|
23
|
-
...selfHosted ? { asset: selfHosted } : {}
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
var LEVEL_FACTOR = 4;
|
|
27
|
-
function buildPyramid(finest, bucket = 256) {
|
|
28
|
-
const levels = [];
|
|
29
|
-
let current = finest;
|
|
30
|
-
let framesPerBucket = bucket;
|
|
31
|
-
for (;;) {
|
|
32
|
-
levels.push({
|
|
33
|
-
framesPerBucket,
|
|
34
|
-
channels: current.min.map((min, c) => ({
|
|
35
|
-
min,
|
|
36
|
-
max: current.max[c]
|
|
37
|
-
}))
|
|
38
|
-
});
|
|
39
|
-
if ((current.min[0]?.length ?? 0) <= 1) break;
|
|
40
|
-
current = {
|
|
41
|
-
min: current.min.map((values) => combine(values, Math.min)),
|
|
42
|
-
max: current.max.map((values) => combine(values, Math.max))
|
|
43
|
-
};
|
|
44
|
-
framesPerBucket *= LEVEL_FACTOR;
|
|
45
|
-
}
|
|
46
|
-
return { levels };
|
|
47
|
-
}
|
|
48
|
-
function combine(values, pick) {
|
|
49
|
-
const out = new Float32Array(Math.ceil(values.length / LEVEL_FACTOR));
|
|
50
|
-
for (let i = 0; i < out.length; i += 1) {
|
|
51
|
-
let value = values[i * LEVEL_FACTOR];
|
|
52
|
-
const end = Math.min(values.length, (i + 1) * LEVEL_FACTOR);
|
|
53
|
-
for (let j = i * LEVEL_FACTOR + 1; j < end; j += 1) value = pick(value, values[j]);
|
|
54
|
-
out[i] = value;
|
|
55
|
-
}
|
|
56
|
-
return out;
|
|
57
|
-
}
|
|
58
|
-
// src/adapters/resample/resample.ts
|
|
59
|
-
var ZERO_CROSSINGS = 16;
|
|
60
|
-
var PASS_BAND = .945;
|
|
61
|
-
var isWhole$1 = (value, least) => Number.isSafeInteger(value) && value >= least;
|
|
62
|
-
function resampledLength(frames, fromRate, toRate) {
|
|
63
|
-
if (!isWhole$1(frames, 0) || !isWhole$1(fromRate, 1) || !isWhole$1(toRate, 1)) throw new RangeError("Frames and rates must be whole numbers, and rates positive.");
|
|
64
|
-
const length = Math.ceil(frames * toRate / fromRate);
|
|
65
|
-
if (!Number.isSafeInteger(length)) throw new RangeError("The resampled length is too large.");
|
|
66
|
-
return length;
|
|
67
|
-
}
|
|
68
|
-
function resampleReach(fromRate, toRate) {
|
|
69
|
-
return Math.ceil(ZERO_CROSSINGS / (Math.min(1, toRate / fromRate) * PASS_BAND));
|
|
70
|
-
}
|
|
71
|
-
function resampleWindow(start, frames, fromRate, toRate, inputFrames) {
|
|
72
|
-
const reach = resampleReach(fromRate, toRate);
|
|
73
|
-
const clip = (frame) => Math.min(inputFrames, Math.max(0, frame));
|
|
74
|
-
return {
|
|
75
|
-
first: clip(Math.floor(start * fromRate / toRate) - reach),
|
|
76
|
-
end: clip(Math.floor((start + frames - 1) * fromRate / toRate) + reach + 1)
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
// src/adapters/web-audio/platform-decode.ts
|
|
80
|
-
var webAudioDecoder = (bytes, { sampleRate, channels }) => {
|
|
81
|
-
const Offline = globalThis.OfflineAudioContext;
|
|
82
|
-
if (typeof Offline !== "function") throw new AudioError("UNSUPPORTED_ENVIRONMENT", "Web Audio isn't available.", { details: { feature: "OfflineAudioContext" } });
|
|
83
|
-
let context;
|
|
84
|
-
try {
|
|
85
|
-
context = new Offline({
|
|
86
|
-
numberOfChannels: channels,
|
|
87
|
-
length: 1,
|
|
88
|
-
sampleRate
|
|
89
|
-
});
|
|
90
|
-
} catch (error) {
|
|
91
|
-
throw new AudioError("UNSUPPORTED_FORMAT", "This browser can't decode at this rate.", {
|
|
92
|
-
details: { sampleRate },
|
|
93
|
-
cause: error
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
return context.decodeAudioData(bytes);
|
|
97
|
-
};
|
|
98
|
-
function untilAborted(work, signal) {
|
|
99
|
-
work.catch(() => {});
|
|
100
|
-
if (!signal) return work;
|
|
101
|
-
return new Promise((resolve, reject) => {
|
|
102
|
-
const onAbort = () => reject(abortError(signal));
|
|
103
|
-
if (signal.aborted) {
|
|
104
|
-
onAbort();
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
108
|
-
work.then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort));
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
async function decodeOnPage(file, probe, decoder, createBuffer, signal) {
|
|
112
|
-
const { container, codec, sampleRate, channels, frameBound, frameTolerance } = probe;
|
|
113
|
-
throwIfAborted(signal);
|
|
114
|
-
const bytes = await untilAborted(file.arrayBuffer(), signal);
|
|
115
|
-
let decoded;
|
|
116
|
-
try {
|
|
117
|
-
decoded = await untilAborted(Promise.resolve().then(() => decoder(bytes, {
|
|
118
|
-
sampleRate,
|
|
119
|
-
channels
|
|
120
|
-
})), signal);
|
|
121
|
-
} catch (error) {
|
|
122
|
-
if (isAudioError(error)) throw error;
|
|
123
|
-
throw new AudioError("DECODE_FAILED", "The browser couldn't decode this file.", {
|
|
124
|
-
details: {
|
|
125
|
-
container,
|
|
126
|
-
codec
|
|
127
|
-
},
|
|
128
|
-
cause: error
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
if (decoded.length === 0) throw new AudioError("DECODE_FAILED", "The browser decoded no audio from this file.", { details: {
|
|
132
|
-
container,
|
|
133
|
-
codec,
|
|
134
|
-
reason: "no audio decoded"
|
|
135
|
-
} });
|
|
136
|
-
const invalid = (reason) => new AudioError("INVALID_AUDIO", `The decoded audio ${reason}.`, { details: {
|
|
137
|
-
container,
|
|
138
|
-
codec,
|
|
139
|
-
reason: `decoded ${reason}`
|
|
140
|
-
} });
|
|
141
|
-
if (decoded.sampleRate !== sampleRate) throw invalid("rate differs from the probe");
|
|
142
|
-
if (decoded.numberOfChannels > channels) throw invalid("channel count exceeds the probe");
|
|
143
|
-
if (decoded.length > frameBound + frameTolerance) throw invalid("length exceeds the bound");
|
|
144
|
-
if (decoded.length <= frameBound) return decoded;
|
|
145
|
-
const capped = createBuffer({
|
|
146
|
-
numberOfChannels: decoded.numberOfChannels,
|
|
147
|
-
length: frameBound,
|
|
148
|
-
sampleRate
|
|
149
|
-
});
|
|
150
|
-
for (let c = 0; c < decoded.numberOfChannels; c += 1) capped.copyToChannel(decoded.getChannelData(c).subarray(0, frameBound), c, 0);
|
|
151
|
-
return capped;
|
|
152
|
-
}
|
|
153
|
-
// src/adapters/audio-engine.ts
|
|
154
|
-
var BLOCK_FRAMES = 65536;
|
|
155
|
-
function createAudioEngine(options = {}) {
|
|
156
|
-
return createEngine({
|
|
157
|
-
...audioWorkerSource(options.assetsUrl),
|
|
158
|
-
createBuffer: (bufferOptions) => new AudioBuffer(bufferOptions),
|
|
159
|
-
decodePlatform: webAudioDecoder,
|
|
160
|
-
...options.idleTimeout === void 0 ? {} : { idleTimeout: options.idleTimeout }
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
function createEngine(environment) {
|
|
164
|
-
let startWorker;
|
|
165
|
-
let destroyed = false;
|
|
166
|
-
const layouts = /* @__PURE__ */ new WeakMap();
|
|
167
|
-
const compressedProbes = /* @__PURE__ */ new WeakSet();
|
|
168
|
-
const finestPeaks = /* @__PURE__ */ new WeakMap();
|
|
169
|
-
const newClient = () => createWorkerClient({
|
|
170
|
-
createWorker: () => {
|
|
171
|
-
if (!startWorker) throw new AudioError("WORKER_START_FAILED", "The audio worker hasn't loaded.");
|
|
172
|
-
return startWorker();
|
|
173
|
-
},
|
|
174
|
-
errorClasses: { AudioError },
|
|
175
|
-
packageVersion: environment.version,
|
|
176
|
-
...environment.asset === void 0 ? {} : { workerAsset: environment.asset },
|
|
177
|
-
...environment.idleTimeout === void 0 ? {} : { idleTimeout: environment.idleTimeout }
|
|
178
|
-
});
|
|
179
|
-
let main;
|
|
180
|
-
let analysis;
|
|
181
|
-
async function ready() {
|
|
182
|
-
if (destroyed) throw new Error("The audio engine has been destroyed.");
|
|
183
|
-
startWorker ??= await environment.loadWorker();
|
|
184
|
-
}
|
|
185
|
-
async function run(work) {
|
|
186
|
-
try {
|
|
187
|
-
return await work();
|
|
188
|
-
} catch (error) {
|
|
189
|
-
throw error instanceof RangeError || error instanceof TypeError ? error : asAudioError(error);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
async function reprobe(file, probe, signal) {
|
|
193
|
-
main ??= newClient();
|
|
194
|
-
const fresh = await main.call("probe", { file }, signal ? { signal } : {});
|
|
195
|
-
if (!(fresh.probe.codec === probe.codec && fresh.probe.sampleRate === probe.sampleRate && fresh.probe.channels === probe.channels && fresh.probe.frameBound === probe.frameBound)) throw new AudioError("INVALID_AUDIO", "The probe doesn't describe this file.", { details: { container: probe.container } });
|
|
196
|
-
return fresh.probe;
|
|
197
|
-
}
|
|
198
|
-
async function layoutOf(file, probe, signal) {
|
|
199
|
-
const known = layouts.get(probe);
|
|
200
|
-
if (known) return known;
|
|
201
|
-
main ??= newClient();
|
|
202
|
-
const fresh = await main.call("probe", { file }, signal ? { signal } : {});
|
|
203
|
-
if (!(fresh.probe.sampleRate === probe.sampleRate && fresh.probe.channels === probe.channels && fresh.probe.frameBound === probe.frameBound)) throw new AudioError("INVALID_AUDIO", "The probe doesn't describe this file.", { details: { container: probe.container } });
|
|
204
|
-
if (!fresh.layout) throw new AudioError("UNSUPPORTED_FORMAT", "Compressed formats aren't supported yet.", { details: {
|
|
205
|
-
container: probe.container,
|
|
206
|
-
codec: probe.codec
|
|
207
|
-
} });
|
|
208
|
-
return fresh.layout;
|
|
209
|
-
}
|
|
210
|
-
function bufferFor(numberOfChannels, length, sampleRate, container) {
|
|
211
|
-
try {
|
|
212
|
-
return environment.createBuffer({
|
|
213
|
-
numberOfChannels,
|
|
214
|
-
length,
|
|
215
|
-
sampleRate
|
|
216
|
-
});
|
|
217
|
-
} catch (error) {
|
|
218
|
-
throw new AudioError("UNSUPPORTED_FORMAT", "This browser can't hold audio at this rate.", {
|
|
219
|
-
details: {
|
|
220
|
-
container,
|
|
221
|
-
sampleRate
|
|
222
|
-
},
|
|
223
|
-
cause: error
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
async function resample(audio, toRate, container, signal, onProgress) {
|
|
228
|
-
const fromRate = audio.sampleRate;
|
|
229
|
-
const length = resampledLength(audio.length, fromRate, toRate);
|
|
230
|
-
const buffer = bufferFor(audio.numberOfChannels, length, toRate, container);
|
|
231
|
-
const room = BLOCK_FRAMES - 2 * resampleReach(fromRate, toRate);
|
|
232
|
-
const blockFrames = Math.max(1, Math.min(BLOCK_FRAMES, Math.floor(room * toRate / fromRate)));
|
|
233
|
-
main ??= newClient();
|
|
234
|
-
for (let start = 0; start < length; start += blockFrames) {
|
|
235
|
-
const frames = Math.min(blockFrames, length - start);
|
|
236
|
-
const { first, end } = resampleWindow(start, frames, fromRate, toRate, audio.length);
|
|
237
|
-
const channels = Array.from({ length: audio.numberOfChannels }, (_, c) => audio.getChannelData(c).slice(first, end));
|
|
238
|
-
const block = await main.call("resample", {
|
|
239
|
-
channels,
|
|
240
|
-
inputStart: first,
|
|
241
|
-
fromRate,
|
|
242
|
-
toRate,
|
|
243
|
-
start,
|
|
244
|
-
frames
|
|
245
|
-
}, {
|
|
246
|
-
transfer: true,
|
|
247
|
-
...signal ? { signal } : {}
|
|
248
|
-
});
|
|
249
|
-
for (let c = 0; c < audio.numberOfChannels; c += 1) buffer.copyToChannel(block[c], c, start);
|
|
250
|
-
onProgress?.((start + frames) / length);
|
|
251
|
-
}
|
|
252
|
-
if (length === 0) onProgress?.(1);
|
|
253
|
-
return buffer;
|
|
254
|
-
}
|
|
255
|
-
async function decodeAtOwnRate(file, probe, signal, onProgress) {
|
|
256
|
-
if (probe.codec !== "pcm") {
|
|
257
|
-
const audio = await decodeOnPage(file, compressedProbes.has(probe) ? probe : await reprobe(file, probe, signal), environment.decodePlatform, environment.createBuffer, signal);
|
|
258
|
-
onProgress?.(1);
|
|
259
|
-
return audio;
|
|
260
|
-
}
|
|
261
|
-
const layout = await layoutOf(file, probe, signal);
|
|
262
|
-
const { frames, channels, sampleRate } = layout;
|
|
263
|
-
const buffer = bufferFor(channels, frames, sampleRate, layout.container);
|
|
264
|
-
const buckets = Math.ceil(frames / 256);
|
|
265
|
-
const peaks = {
|
|
266
|
-
min: Array.from({ length: channels }, () => new Float32Array(buckets)),
|
|
267
|
-
max: Array.from({ length: channels }, () => new Float32Array(buckets))
|
|
268
|
-
};
|
|
269
|
-
main ??= newClient();
|
|
270
|
-
for (let start = 0; start < frames; start += BLOCK_FRAMES) {
|
|
271
|
-
const block = await main.call("readBlock", {
|
|
272
|
-
file,
|
|
273
|
-
layout,
|
|
274
|
-
start,
|
|
275
|
-
frames: Math.min(BLOCK_FRAMES, frames - start),
|
|
276
|
-
bucket: 256
|
|
277
|
-
}, signal ? { signal } : {});
|
|
278
|
-
const at = start / 256;
|
|
279
|
-
for (let c = 0; c < channels; c += 1) {
|
|
280
|
-
buffer.copyToChannel(block.channels[c], c, start);
|
|
281
|
-
peaks.min[c]?.set(block.min[c], at);
|
|
282
|
-
peaks.max[c]?.set(block.max[c], at);
|
|
283
|
-
}
|
|
284
|
-
onProgress?.(Math.min(start + BLOCK_FRAMES, frames) / frames);
|
|
285
|
-
}
|
|
286
|
-
if (frames === 0) onProgress?.(1);
|
|
287
|
-
finestPeaks.set(buffer, peaks);
|
|
288
|
-
return buffer;
|
|
289
|
-
}
|
|
290
|
-
return {
|
|
291
|
-
probe(file, options = {}) {
|
|
292
|
-
return run(async () => {
|
|
293
|
-
throwIfAborted(options.signal);
|
|
294
|
-
await ready();
|
|
295
|
-
main ??= newClient();
|
|
296
|
-
const result = await main.call("probe", { file }, options.signal ? { signal: options.signal } : {});
|
|
297
|
-
if (result.layout) layouts.set(result.probe, result.layout);
|
|
298
|
-
else compressedProbes.add(result.probe);
|
|
299
|
-
return result.probe;
|
|
300
|
-
});
|
|
301
|
-
},
|
|
302
|
-
decode(file, probe, options = {}) {
|
|
303
|
-
return run(async () => {
|
|
304
|
-
throwIfAborted(options.signal);
|
|
305
|
-
const { signal, onProgress, sampleRate = probe.sampleRate } = options;
|
|
306
|
-
if (!Number.isSafeInteger(sampleRate) || sampleRate < 1) throw new RangeError("The sample rate must be a positive whole number of hertz.");
|
|
307
|
-
await ready();
|
|
308
|
-
if (sampleRate === probe.sampleRate) return decodeAtOwnRate(file, probe, signal, onProgress);
|
|
309
|
-
const half = (from) => onProgress && ((fraction) => onProgress(from + fraction / 2));
|
|
310
|
-
return resample(await decodeAtOwnRate(file, probe, signal, half(0)), sampleRate, probe.container, signal, half(.5));
|
|
311
|
-
});
|
|
312
|
-
},
|
|
313
|
-
peaks(audio, options = {}) {
|
|
314
|
-
return run(async () => {
|
|
315
|
-
throwIfAborted(options.signal);
|
|
316
|
-
const known = finestPeaks.get(audio);
|
|
317
|
-
if (known) return buildPyramid(known, 256);
|
|
318
|
-
await ready();
|
|
319
|
-
analysis ??= newClient();
|
|
320
|
-
const buckets = Math.ceil(audio.length / 256);
|
|
321
|
-
const peaks = {
|
|
322
|
-
min: Array.from({ length: audio.numberOfChannels }, () => new Float32Array(buckets)),
|
|
323
|
-
max: Array.from({ length: audio.numberOfChannels }, () => new Float32Array(buckets))
|
|
324
|
-
};
|
|
325
|
-
for (let start = 0; start < audio.length; start += BLOCK_FRAMES) {
|
|
326
|
-
const end = Math.min(audio.length, start + BLOCK_FRAMES);
|
|
327
|
-
const channels = Array.from({ length: audio.numberOfChannels }, (_, c) => audio.getChannelData(c).slice(start, end));
|
|
328
|
-
const block = await analysis.call("peaks", {
|
|
329
|
-
channels,
|
|
330
|
-
bucket: 256
|
|
331
|
-
}, {
|
|
332
|
-
transfer: true,
|
|
333
|
-
...options.signal ? { signal: options.signal } : {}
|
|
334
|
-
});
|
|
335
|
-
const at = start / 256;
|
|
336
|
-
for (let c = 0; c < audio.numberOfChannels; c += 1) {
|
|
337
|
-
peaks.min[c]?.set(block.min[c], at);
|
|
338
|
-
peaks.max[c]?.set(block.max[c], at);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
finestPeaks.set(audio, peaks);
|
|
342
|
-
return buildPyramid(peaks, 256);
|
|
343
|
-
});
|
|
344
|
-
},
|
|
345
|
-
async destroy() {
|
|
346
|
-
destroyed = true;
|
|
347
|
-
main?.destroy();
|
|
348
|
-
analysis?.destroy();
|
|
349
|
-
}
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
// src/core/model/plan.ts
|
|
353
|
-
var EMPTY_METADATA = Object.freeze({
|
|
354
|
-
title: "",
|
|
355
|
-
artist: "",
|
|
356
|
-
album: "",
|
|
357
|
-
year: "",
|
|
358
|
-
comment: ""
|
|
359
|
-
});
|
|
360
|
-
var NO_FADE = Object.freeze({
|
|
361
|
-
frames: 0,
|
|
362
|
-
curve: "linear"
|
|
363
|
-
});
|
|
364
|
-
var MAX_TRACK_NAME_LENGTH = 100;
|
|
365
|
-
function clipLength(clip) {
|
|
366
|
-
return clip.end - clip.start;
|
|
367
|
-
}
|
|
368
|
-
function clipEnd(clip) {
|
|
369
|
-
return clip.at + clipLength(clip);
|
|
370
|
-
}
|
|
371
|
-
function trackLength(track) {
|
|
372
|
-
const last = track.clips.at(-1);
|
|
373
|
-
return last ? clipEnd(last) : 0;
|
|
374
|
-
}
|
|
375
|
-
function planLength(plan) {
|
|
376
|
-
let length = 0;
|
|
377
|
-
for (const track of plan.tracks) length = Math.max(length, trackLength(track));
|
|
378
|
-
return length;
|
|
379
|
-
}
|
|
380
|
-
function audibleTracks(plan) {
|
|
381
|
-
const solo = plan.tracks.some((track) => track.soloed);
|
|
382
|
-
return plan.tracks.filter((track) => !track.muted && (!solo || track.soloed));
|
|
383
|
-
}
|
|
384
|
-
function orderProblem(clips) {
|
|
385
|
-
for (let i = 1; i < clips.length; i += 1) {
|
|
386
|
-
const before = clips[i - 1];
|
|
387
|
-
const clip = clips[i];
|
|
388
|
-
if (clip.at < before.at) return "clips-out-of-order";
|
|
389
|
-
if (clip.at < clipEnd(before)) return "clips-overlap";
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
function clipIndexAt(clips, frame) {
|
|
393
|
-
let low = 0;
|
|
394
|
-
let high = clips.length;
|
|
395
|
-
while (low < high) {
|
|
396
|
-
const middle = low + high >>> 1;
|
|
397
|
-
if (clipEnd(clips[middle]) > frame) high = middle;
|
|
398
|
-
else low = middle + 1;
|
|
399
|
-
}
|
|
400
|
-
return low;
|
|
401
|
-
}
|
|
402
|
-
function clipAt(track, frame) {
|
|
403
|
-
const clip = track.clips[clipIndexAt(track.clips, frame)];
|
|
404
|
-
return clip && clip.at <= frame ? clip : void 0;
|
|
405
|
-
}
|
|
406
|
-
var MIN_GAIN_DB = -120;
|
|
407
|
-
var MAX_GAIN_DB = 120;
|
|
408
|
-
var isFrames$1 = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
409
|
-
function clipProblem(clip, sourceFrames) {
|
|
410
|
-
if (![
|
|
411
|
-
clip.start,
|
|
412
|
-
clip.end,
|
|
413
|
-
clip.fadeIn.frames,
|
|
414
|
-
clip.fadeOut.frames
|
|
415
|
-
].every(isFrames$1)) return "invalid-frames";
|
|
416
|
-
if (clip.end <= clip.start) return "clip-too-short";
|
|
417
|
-
if (clip.end > sourceFrames) return "clip-outside-source";
|
|
418
|
-
if (typeof clip.gainDb !== "number" || !(clip.gainDb >= -120 && clip.gainDb <= 120)) return "gain-out-of-range";
|
|
419
|
-
if (clip.fadeIn.frames + clip.fadeOut.frames > clipLength(clip)) return "fades-too-long";
|
|
420
|
-
}
|
|
421
|
-
function assertClip(clip, sourceFrames) {
|
|
422
|
-
const problem = clipProblem(clip, sourceFrames);
|
|
423
|
-
if (problem) throw new RangeError(`Clip ${clip.key} isn't valid: ${problem}.`);
|
|
424
|
-
}
|
|
425
|
-
// src/core/model/envelope.ts
|
|
426
|
-
function dbToGain(db) {
|
|
427
|
-
return 10 ** (db / 20);
|
|
428
|
-
}
|
|
429
|
-
function fadeShape(curve, position) {
|
|
430
|
-
return curve === "equalPower" ? Math.sin(position * Math.PI / 2) : position;
|
|
431
|
-
}
|
|
432
|
-
function gainAt(clip, frame) {
|
|
433
|
-
const length = clip.end - clip.start;
|
|
434
|
-
let gain = dbToGain(clip.gainDb);
|
|
435
|
-
const { fadeIn, fadeOut } = clip;
|
|
436
|
-
if (fadeIn.frames > 0 && frame < fadeIn.frames) gain *= fadeShape(fadeIn.curve, frame / fadeIn.frames);
|
|
437
|
-
const fromEnd = length - 1 - frame;
|
|
438
|
-
if (fadeOut.frames > 0 && fromEnd < fadeOut.frames) gain *= fadeShape(fadeOut.curve, Math.max(0, fromEnd) / fadeOut.frames);
|
|
439
|
-
return gain;
|
|
440
|
-
}
|
|
441
|
-
// src/adapters/web-audio/gain-automation.ts
|
|
442
|
-
var MAX_CURVE_POINTS = 1024;
|
|
443
|
-
function curve(clip, from, to) {
|
|
444
|
-
const points = Math.min(MAX_CURVE_POINTS, to - from + 1);
|
|
445
|
-
const values = new Float32Array(points);
|
|
446
|
-
for (let i = 0; i < points; i += 1) {
|
|
447
|
-
const frame = points === 1 ? from : from + (to - from) * i / (points - 1);
|
|
448
|
-
values[i] = gainAt(clip, Math.round(frame));
|
|
449
|
-
}
|
|
450
|
-
return values;
|
|
451
|
-
}
|
|
452
|
-
function gainEvents(clip, from, sampleRate) {
|
|
453
|
-
const length = clipLength(clip);
|
|
454
|
-
const fadeInEnd = clip.fadeIn.frames;
|
|
455
|
-
const fadeOutStart = length - clip.fadeOut.frames;
|
|
456
|
-
const at = (frame) => (frame - from) / sampleRate;
|
|
457
|
-
const events = [];
|
|
458
|
-
let cursor = from;
|
|
459
|
-
if (from < fadeInEnd) {
|
|
460
|
-
events.push({
|
|
461
|
-
kind: "curve",
|
|
462
|
-
time: 0,
|
|
463
|
-
duration: at(fadeInEnd),
|
|
464
|
-
values: curve(clip, from, fadeInEnd)
|
|
465
|
-
});
|
|
466
|
-
cursor = fadeInEnd;
|
|
467
|
-
}
|
|
468
|
-
const outFrom = Math.max(cursor, fadeOutStart);
|
|
469
|
-
const fadingOut = clip.fadeOut.frames > 0 && outFrom < length - 1;
|
|
470
|
-
if (events.length === 0 && !(fadingOut && outFrom === from)) events.push({
|
|
471
|
-
kind: "set",
|
|
472
|
-
time: 0,
|
|
473
|
-
value: gainAt(clip, from)
|
|
474
|
-
});
|
|
475
|
-
if (fadingOut) events.push({
|
|
476
|
-
kind: "curve",
|
|
477
|
-
time: at(outFrom),
|
|
478
|
-
duration: at(length - 1) - at(outFrom),
|
|
479
|
-
values: curve(clip, outFrom, length - 1)
|
|
480
|
-
});
|
|
481
|
-
else if (clip.fadeOut.frames > 0 && outFrom > cursor) events.push({
|
|
482
|
-
kind: "set",
|
|
483
|
-
time: at(outFrom),
|
|
484
|
-
value: gainAt(clip, outFrom)
|
|
485
|
-
});
|
|
486
|
-
return events;
|
|
487
|
-
}
|
|
488
|
-
// src/adapters/web-audio/play-graph.ts
|
|
489
|
-
var GAIN_RAMP = .005;
|
|
490
|
-
function tracksWithClips(plan) {
|
|
491
|
-
const found = /* @__PURE__ */ new Map();
|
|
492
|
-
for (const track of plan.tracks) if (track.clips.length > 0) found.set(track.key, track.clips);
|
|
493
|
-
return found;
|
|
494
|
-
}
|
|
495
|
-
function sameClips(a, b) {
|
|
496
|
-
if (a.sampleRate !== b.sampleRate || a.channels !== b.channels) return false;
|
|
497
|
-
const before = tracksWithClips(a);
|
|
498
|
-
const after = tracksWithClips(b);
|
|
499
|
-
if (before.size !== after.size) return false;
|
|
500
|
-
for (const [key, clips] of before) if (after.get(key) !== clips) return false;
|
|
501
|
-
return true;
|
|
502
|
-
}
|
|
503
|
-
function trackLevels(plan) {
|
|
504
|
-
const audible = new Set(audibleTracks(plan));
|
|
505
|
-
return new Map(plan.tracks.map((track) => [track.key, audible.has(track) ? dbToGain(track.gainDb) : 0]));
|
|
506
|
-
}
|
|
507
|
-
function schedulePlan(context, destination, plan, buffer, from, at) {
|
|
508
|
-
const rate = plan.sampleRate;
|
|
509
|
-
const bus = context.createGain();
|
|
510
|
-
bus.gain.value = dbToGain(plan.gainDb);
|
|
511
|
-
bus.channelCount = plan.channels;
|
|
512
|
-
bus.channelCountMode = "explicit";
|
|
513
|
-
bus.channelInterpretation = "speakers";
|
|
514
|
-
bus.connect(destination);
|
|
515
|
-
const nodes = [];
|
|
516
|
-
const tracks = /* @__PURE__ */ new Map();
|
|
517
|
-
const levels = trackLevels(plan);
|
|
518
|
-
let master = dbToGain(plan.gainDb);
|
|
519
|
-
for (const track of plan.tracks) {
|
|
520
|
-
if (track.clips.length === 0) continue;
|
|
521
|
-
const output = context.createGain();
|
|
522
|
-
output.gain.value = levels.get(track.key) ?? 0;
|
|
523
|
-
output.channelCount = plan.channels;
|
|
524
|
-
output.channelCountMode = "explicit";
|
|
525
|
-
output.channelInterpretation = "speakers";
|
|
526
|
-
output.connect(bus);
|
|
527
|
-
tracks.set(track.key, output);
|
|
528
|
-
for (const clip of track.clips) {
|
|
529
|
-
const length = clipLength(clip);
|
|
530
|
-
if (clip.at + length <= from) continue;
|
|
531
|
-
const within = Math.max(0, from - clip.at);
|
|
532
|
-
const when = at + Math.max(0, clip.at - from) / rate;
|
|
533
|
-
const source = context.createBufferSource();
|
|
534
|
-
source.buffer = buffer(clip.source);
|
|
535
|
-
const gain = context.createGain();
|
|
536
|
-
for (const event of gainEvents(clip, within, rate)) if (event.kind === "set") gain.gain.setValueAtTime(event.value, when + event.time);
|
|
537
|
-
else gain.gain.setValueCurveAtTime(event.values, when + event.time, event.duration);
|
|
538
|
-
source.connect(gain).connect(output);
|
|
539
|
-
source.start(when, (clip.start + within) / rate, (length - within) / rate);
|
|
540
|
-
nodes.push({
|
|
541
|
-
source,
|
|
542
|
-
gain
|
|
543
|
-
});
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
const ramp = (param, from, to, time) => {
|
|
547
|
-
if (from === to) return;
|
|
548
|
-
param.setValueAtTime(from, time);
|
|
549
|
-
param.linearRampToValueAtTime(to, time + GAIN_RAMP);
|
|
550
|
-
};
|
|
551
|
-
return {
|
|
552
|
-
setLevels(next, time) {
|
|
553
|
-
const targets = trackLevels(next);
|
|
554
|
-
for (const [key, output] of tracks) {
|
|
555
|
-
const target = targets.get(key) ?? 0;
|
|
556
|
-
ramp(output.gain, levels.get(key) ?? 0, target, time);
|
|
557
|
-
levels.set(key, target);
|
|
558
|
-
}
|
|
559
|
-
const target = dbToGain(next.gainDb);
|
|
560
|
-
ramp(bus.gain, master, target, time);
|
|
561
|
-
master = target;
|
|
562
|
-
},
|
|
563
|
-
stop() {
|
|
564
|
-
for (const { source, gain } of nodes) {
|
|
565
|
-
try {
|
|
566
|
-
source.stop();
|
|
567
|
-
} catch {}
|
|
568
|
-
source.disconnect();
|
|
569
|
-
gain.disconnect();
|
|
570
|
-
}
|
|
571
|
-
for (const output of tracks.values()) output.disconnect();
|
|
572
|
-
bus.disconnect();
|
|
573
|
-
}
|
|
574
|
-
};
|
|
575
|
-
}
|
|
576
|
-
// src/adapters/audio-output.ts
|
|
577
|
-
var START_LEAD = .03;
|
|
578
|
-
var RESUME_TIMEOUT = 2e3;
|
|
579
|
-
function createAudioOutput() {
|
|
580
|
-
return createOutput({ createContext: () => new AudioContext() });
|
|
581
|
-
}
|
|
582
|
-
function createOutput(environment) {
|
|
583
|
-
const store = createStore(Object.freeze({ state: "stopped" }));
|
|
584
|
-
const copies = /* @__PURE__ */ new WeakMap();
|
|
585
|
-
let context;
|
|
586
|
-
let plan;
|
|
587
|
-
let sources = /* @__PURE__ */ new Map();
|
|
588
|
-
let playhead = 0;
|
|
589
|
-
let playing;
|
|
590
|
-
let endTimer;
|
|
591
|
-
let destroyed = false;
|
|
592
|
-
const length = () => plan ? planLength(plan) : 0;
|
|
593
|
-
const assertAlive = () => {
|
|
594
|
-
if (destroyed) throw new Error("The audio output has been destroyed.");
|
|
595
|
-
};
|
|
596
|
-
const setState = (state) => {
|
|
597
|
-
if (store.getSnapshot().state !== state) store.set(Object.freeze({ state }));
|
|
598
|
-
};
|
|
599
|
-
const jumped = () => store.set(Object.freeze({ state: store.getSnapshot().state }));
|
|
600
|
-
function buffer(id) {
|
|
601
|
-
const audio = sources.get(id);
|
|
602
|
-
if (!audio) throw new TypeError(`The plan's source ${id} isn't loaded.`);
|
|
603
|
-
if (audio instanceof AudioBuffer) return audio;
|
|
604
|
-
let copy = copies.get(audio);
|
|
605
|
-
if (!copy) {
|
|
606
|
-
copy = new AudioBuffer({
|
|
607
|
-
length: audio.length,
|
|
608
|
-
numberOfChannels: audio.numberOfChannels,
|
|
609
|
-
sampleRate: audio.sampleRate
|
|
610
|
-
});
|
|
611
|
-
for (let channel = 0; channel < audio.numberOfChannels; channel += 1) copy.copyToChannel(Float32Array.from(audio.getChannelData(channel)), channel);
|
|
612
|
-
copies.set(audio, copy);
|
|
613
|
-
}
|
|
614
|
-
return copy;
|
|
615
|
-
}
|
|
616
|
-
function position() {
|
|
617
|
-
if (!playing || !context || !plan) return playhead;
|
|
618
|
-
const elapsed = Math.max(0, context.currentTime - playing.time);
|
|
619
|
-
return Math.min(length(), Math.floor(playing.frame + elapsed * plan.sampleRate));
|
|
620
|
-
}
|
|
621
|
-
function unschedule() {
|
|
622
|
-
if (!playing) return;
|
|
623
|
-
playhead = position();
|
|
624
|
-
playing.scheduled.stop();
|
|
625
|
-
playing = void 0;
|
|
626
|
-
clearTimeout(endTimer);
|
|
627
|
-
endTimer = void 0;
|
|
628
|
-
}
|
|
629
|
-
function watchEnd() {
|
|
630
|
-
clearTimeout(endTimer);
|
|
631
|
-
if (!playing || !context || !plan) return;
|
|
632
|
-
const remaining = playing.time - context.currentTime + (length() - playing.frame) / plan.sampleRate;
|
|
633
|
-
endTimer = setTimeout(() => {
|
|
634
|
-
if (position() >= length()) {
|
|
635
|
-
unschedule();
|
|
636
|
-
playhead = length();
|
|
637
|
-
setState("stopped");
|
|
638
|
-
} else watchEnd();
|
|
639
|
-
}, Math.max(0, remaining * 1e3) + 5);
|
|
640
|
-
}
|
|
641
|
-
function schedule() {
|
|
642
|
-
if (!context || !plan) return;
|
|
643
|
-
const time = context.currentTime + START_LEAD;
|
|
644
|
-
playing = {
|
|
645
|
-
scheduled: schedulePlan(context, context.destination, plan, buffer, playhead, time),
|
|
646
|
-
frame: playhead,
|
|
647
|
-
time
|
|
648
|
-
};
|
|
649
|
-
watchEnd();
|
|
650
|
-
}
|
|
651
|
-
async function running(audio) {
|
|
652
|
-
if (audio.state === "running") return true;
|
|
653
|
-
let timer;
|
|
654
|
-
const timedOut = new Promise((resolve) => {
|
|
655
|
-
timer = setTimeout(() => resolve(false), RESUME_TIMEOUT);
|
|
656
|
-
});
|
|
657
|
-
try {
|
|
658
|
-
await Promise.race([audio.resume().then(() => true), timedOut]);
|
|
659
|
-
} finally {
|
|
660
|
-
clearTimeout(timer);
|
|
661
|
-
}
|
|
662
|
-
return audio.state === "running";
|
|
663
|
-
}
|
|
664
|
-
return {
|
|
665
|
-
getSnapshot: store.getSnapshot,
|
|
666
|
-
subscribe: store.subscribe,
|
|
667
|
-
position,
|
|
668
|
-
load(next, nextSources) {
|
|
669
|
-
assertAlive();
|
|
670
|
-
if (playing && context && plan && sameClips(plan, next)) {
|
|
671
|
-
plan = next;
|
|
672
|
-
sources = nextSources;
|
|
673
|
-
playing.scheduled.setLevels(next, context.currentTime);
|
|
674
|
-
return;
|
|
675
|
-
}
|
|
676
|
-
const wasPlaying = playing !== void 0;
|
|
677
|
-
unschedule();
|
|
678
|
-
plan = next;
|
|
679
|
-
sources = nextSources;
|
|
680
|
-
const before = playhead;
|
|
681
|
-
playhead = Math.min(playhead, length());
|
|
682
|
-
if (!wasPlaying) {
|
|
683
|
-
if (playhead !== before) jumped();
|
|
684
|
-
return;
|
|
685
|
-
}
|
|
686
|
-
if (playhead >= length()) {
|
|
687
|
-
playhead = length();
|
|
688
|
-
setState("stopped");
|
|
689
|
-
} else schedule();
|
|
690
|
-
},
|
|
691
|
-
async play() {
|
|
692
|
-
assertAlive();
|
|
693
|
-
if (length() === 0 || playing) return;
|
|
694
|
-
context ??= environment.createContext();
|
|
695
|
-
if (!await running(context)) throw new AudioError("UNSUPPORTED_ENVIRONMENT", "The browser didn't start audio output. Play from a click or a key press.", { details: {
|
|
696
|
-
feature: "audio-output",
|
|
697
|
-
reason: "autoplay-blocked"
|
|
698
|
-
} });
|
|
699
|
-
if (destroyed || playing || length() === 0) return;
|
|
700
|
-
if (playhead >= length()) playhead = 0;
|
|
701
|
-
schedule();
|
|
702
|
-
setState("playing");
|
|
703
|
-
},
|
|
704
|
-
pause() {
|
|
705
|
-
assertAlive();
|
|
706
|
-
if (!playing) return;
|
|
707
|
-
unschedule();
|
|
708
|
-
setState("paused");
|
|
709
|
-
},
|
|
710
|
-
stop() {
|
|
711
|
-
assertAlive();
|
|
712
|
-
unschedule();
|
|
713
|
-
const before = playhead;
|
|
714
|
-
playhead = 0;
|
|
715
|
-
if (store.getSnapshot().state !== "stopped") setState("stopped");
|
|
716
|
-
else if (before !== 0) jumped();
|
|
717
|
-
},
|
|
718
|
-
seek(frame) {
|
|
719
|
-
assertAlive();
|
|
720
|
-
const wasPlaying = playing !== void 0;
|
|
721
|
-
const before = position();
|
|
722
|
-
unschedule();
|
|
723
|
-
playhead = Math.max(0, Math.min(Math.round(frame), length()));
|
|
724
|
-
if (wasPlaying && playhead < length()) schedule();
|
|
725
|
-
if (wasPlaying && playhead >= length()) setState("stopped");
|
|
726
|
-
else if (playhead !== before) jumped();
|
|
727
|
-
},
|
|
728
|
-
async destroy() {
|
|
729
|
-
if (destroyed) return;
|
|
730
|
-
destroyed = true;
|
|
731
|
-
unschedule();
|
|
732
|
-
setState("stopped");
|
|
733
|
-
await context?.close().catch(() => {});
|
|
734
|
-
context = void 0;
|
|
735
|
-
}
|
|
736
|
-
};
|
|
737
|
-
}
|
|
738
|
-
// src/core/model/mix.ts
|
|
739
|
-
function mixLength(plan) {
|
|
740
|
-
let length = 0;
|
|
741
|
-
for (const track of audibleTracks(plan)) length = Math.max(length, trackLength(track));
|
|
742
|
-
return length;
|
|
743
|
-
}
|
|
744
|
-
function gatherSegments(plan, sources, start, frames) {
|
|
745
|
-
const end = start + frames;
|
|
746
|
-
const segments = [];
|
|
747
|
-
for (const track of audibleTracks(plan)) {
|
|
748
|
-
const gain = dbToGain(track.gainDb);
|
|
749
|
-
for (let index = clipIndexAt(track.clips, start); index < track.clips.length; index += 1) {
|
|
750
|
-
const clip = track.clips[index];
|
|
751
|
-
if (!clip || clip.at >= end) break;
|
|
752
|
-
const from = Math.max(start, clip.at);
|
|
753
|
-
const to = Math.min(end, clipEnd(clip));
|
|
754
|
-
const within = from - clip.at;
|
|
755
|
-
const source = sources.get(clip.source);
|
|
756
|
-
if (!source) throw new TypeError(`Clip ${clip.key}'s source ${clip.source} isn't given.`);
|
|
757
|
-
const samples = Array.from({ length: source.numberOfChannels }, (_, channel) => source.getChannelData(channel).slice(clip.start + within, clip.start + within + to - from));
|
|
758
|
-
segments.push({
|
|
759
|
-
clip,
|
|
760
|
-
gain,
|
|
761
|
-
within,
|
|
762
|
-
at: from - start,
|
|
763
|
-
samples,
|
|
764
|
-
frames: to - from
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
return segments;
|
|
769
|
-
}
|
|
770
|
-
// src/adapters/wav/wav-writer.ts
|
|
771
|
-
var BYTES = {
|
|
772
|
-
pcm16: 2,
|
|
773
|
-
pcm24: 3,
|
|
774
|
-
float32: 4
|
|
775
|
-
};
|
|
776
|
-
function dataSize(spec) {
|
|
777
|
-
return spec.frames * spec.channels * BYTES[spec.sampleFormat];
|
|
778
|
-
}
|
|
779
|
-
// src/adapters/audio-processor.ts
|
|
780
|
-
var EXPORT_BLOCK_FRAMES = 65536;
|
|
781
|
-
var SAMPLE_FORMATS = /* @__PURE__ */ new Set([
|
|
782
|
-
"pcm16",
|
|
783
|
-
"pcm24",
|
|
784
|
-
"float32"
|
|
785
|
-
]);
|
|
786
|
-
function createAudioProcessor(options = {}) {
|
|
787
|
-
return createProcessor({
|
|
788
|
-
...audioWorkerSource(options.assetsUrl),
|
|
789
|
-
...options.idleTimeout === void 0 ? {} : { idleTimeout: options.idleTimeout }
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
function check(job) {
|
|
793
|
-
const { plan, sources, format } = job;
|
|
794
|
-
if (format.container !== "wav") throw new TypeError(`Can't export to ${String(format.container)}.`);
|
|
795
|
-
const sampleFormat = format.sampleFormat ?? "pcm16";
|
|
796
|
-
if (!SAMPLE_FORMATS.has(sampleFormat)) throw new TypeError(`Unknown sample format ${String(sampleFormat)}.`);
|
|
797
|
-
if (!Number.isSafeInteger(plan.sampleRate) || plan.sampleRate < 1) throw new RangeError("The plan's sample rate must be a whole number above 0.");
|
|
798
|
-
if (!Number.isSafeInteger(plan.channels) || plan.channels < 1) throw new RangeError("The plan's channel count must be a whole number above 0.");
|
|
799
|
-
for (const track of plan.tracks) for (const clip of track.clips) {
|
|
800
|
-
const source = sources.get(clip.source);
|
|
801
|
-
if (!source) throw new TypeError(`Clip ${clip.key}'s source ${clip.source} isn't in the job.`);
|
|
802
|
-
assertClip(clip, source.length);
|
|
803
|
-
}
|
|
804
|
-
return sampleFormat;
|
|
805
|
-
}
|
|
806
|
-
function createProcessor(environment) {
|
|
807
|
-
let startWorker;
|
|
808
|
-
let client;
|
|
809
|
-
let destroyed = false;
|
|
810
|
-
async function ready() {
|
|
811
|
-
if (destroyed) throw new Error("The audio processor has been destroyed.");
|
|
812
|
-
startWorker ??= await environment.loadWorker();
|
|
813
|
-
client ??= createWorkerClient({
|
|
814
|
-
createWorker: () => {
|
|
815
|
-
if (!startWorker) throw new AudioError("WORKER_START_FAILED", "The audio worker hasn't loaded.");
|
|
816
|
-
return startWorker();
|
|
817
|
-
},
|
|
818
|
-
errorClasses: { AudioError },
|
|
819
|
-
packageVersion: environment.version,
|
|
820
|
-
...environment.asset === void 0 ? {} : { workerAsset: environment.asset },
|
|
821
|
-
...environment.idleTimeout === void 0 ? {} : { idleTimeout: environment.idleTimeout }
|
|
822
|
-
});
|
|
823
|
-
return client;
|
|
824
|
-
}
|
|
825
|
-
async function render(job, signal, onProgress) {
|
|
826
|
-
const { plan, sources } = job;
|
|
827
|
-
const sampleFormat = check(job);
|
|
828
|
-
throwIfAborted(signal);
|
|
829
|
-
const worker = await ready();
|
|
830
|
-
const call = signal ? { signal } : {};
|
|
831
|
-
const frames = mixLength(plan);
|
|
832
|
-
const gain = dbToGain(plan.gainDb);
|
|
833
|
-
const spec = {
|
|
834
|
-
sampleRate: plan.sampleRate,
|
|
835
|
-
channels: plan.channels,
|
|
836
|
-
frames,
|
|
837
|
-
sampleFormat
|
|
838
|
-
};
|
|
839
|
-
const parts = [await worker.call("wavHeader", {
|
|
840
|
-
...spec,
|
|
841
|
-
metadata: { ...plan.metadata }
|
|
842
|
-
}, call)];
|
|
843
|
-
let peak = 0;
|
|
844
|
-
let clippedFrames = 0;
|
|
845
|
-
for (let start = 0; start < frames; start += EXPORT_BLOCK_FRAMES) {
|
|
846
|
-
throwIfAborted(signal);
|
|
847
|
-
const length = Math.min(EXPORT_BLOCK_FRAMES, frames - start);
|
|
848
|
-
const segments = gatherSegments(plan, sources, start, length);
|
|
849
|
-
const block = await worker.call("renderBlock", {
|
|
850
|
-
channels: plan.channels,
|
|
851
|
-
frames: length,
|
|
852
|
-
gain,
|
|
853
|
-
sampleFormat,
|
|
854
|
-
segments
|
|
855
|
-
}, {
|
|
856
|
-
...call,
|
|
857
|
-
transfer: true
|
|
858
|
-
});
|
|
859
|
-
parts.push(block.bytes);
|
|
860
|
-
peak = Math.max(peak, block.peak);
|
|
861
|
-
clippedFrames += block.clipped;
|
|
862
|
-
onProgress?.((start + length) / frames);
|
|
863
|
-
}
|
|
864
|
-
if (dataSize(spec) % 2 === 1) parts.push(/* @__PURE__ */ new Uint8Array(1));
|
|
865
|
-
if (frames === 0) onProgress?.(1);
|
|
866
|
-
return {
|
|
867
|
-
blob: new Blob(parts, { type: "audio/wav" }),
|
|
868
|
-
peakDb: 20 * Math.log10(peak),
|
|
869
|
-
clippedFrames
|
|
870
|
-
};
|
|
871
|
-
}
|
|
872
|
-
return {
|
|
873
|
-
async render(job, options = {}) {
|
|
874
|
-
try {
|
|
875
|
-
return await render(job, options.signal, options.onProgress);
|
|
876
|
-
} catch (error) {
|
|
877
|
-
if (error instanceof TypeError || error instanceof RangeError) throw error;
|
|
878
|
-
throw asAudioError(error, "The recording couldn't be exported.");
|
|
879
|
-
}
|
|
880
|
-
},
|
|
881
|
-
async destroy() {
|
|
882
|
-
destroyed = true;
|
|
883
|
-
client?.destroy();
|
|
884
|
-
}
|
|
885
|
-
};
|
|
886
|
-
}
|
|
1
|
+
import { A as createAudioOutput, B as clipEnd, C as onlyFields, E as createAudioProcessor, F as MAX_TRACK_NAME_LENGTH, G as AudioError, H as planLength, I as MIN_GAIN_DB, K as isAudioError, L as NO_FADE, M as gainAt, N as EMPTY_METADATA, P as MAX_GAIN_DB, R as audibleTracks, S as longerThan, T as audioLimits, U as trackLength, V as clipLength, W as createAudioEngine, _ as MAX_METADATA_LENGTH, a as createAudioSession, b as isKey, c as formatTimecode, d as MAX_SOURCE_NAME_LENGTH, f as peakColumns, g as Invalid, h as refuse, i as watchPlayhead, j as dbToGain, k as mixLength, l as framesToSeconds, m as checkUniqueKeys, n as audioWarningText, p as checkTrack, r as audioMessages, s as connectOutput, t as audioErrorText, u as secondsToFrames, v as checkClipBody, w as audioLimitDefinitions, x as isRecord, y as invalid, z as clipAt } from "./shared/text-CgDrmmTa.js";
|
|
887
2
|
// src/adapters/audio-adapters.ts
|
|
888
3
|
function createAudioAdapters(options = {}) {
|
|
889
4
|
const worker = {
|
|
@@ -906,140 +21,6 @@ function createAudioAdapters(options = {}) {
|
|
|
906
21
|
}
|
|
907
22
|
};
|
|
908
23
|
}
|
|
909
|
-
// src/core/errors/audio-limits.ts
|
|
910
|
-
var audioLimitDefinitions = {
|
|
911
|
-
...coreLimits,
|
|
912
|
-
maxFileSize: {
|
|
913
|
-
...coreLimits.maxFileSize,
|
|
914
|
-
default: 1e9,
|
|
915
|
-
warning: 25e7
|
|
916
|
-
},
|
|
917
|
-
maxDecodedSize: {
|
|
918
|
-
...coreLimits.maxDecodedSize,
|
|
919
|
-
default: 1e9,
|
|
920
|
-
warning: 4e8
|
|
921
|
-
},
|
|
922
|
-
maxDuration: {
|
|
923
|
-
code: "DURATION_TOO_LONG",
|
|
924
|
-
unit: "milliseconds",
|
|
925
|
-
default: 108e5,
|
|
926
|
-
warning: 36e5
|
|
927
|
-
},
|
|
928
|
-
maxChannels: {
|
|
929
|
-
code: "TOO_MANY_CHANNELS",
|
|
930
|
-
unit: "count",
|
|
931
|
-
default: 8
|
|
932
|
-
},
|
|
933
|
-
maxSampleRate: {
|
|
934
|
-
code: "SAMPLE_RATE_TOO_HIGH",
|
|
935
|
-
unit: "hertz",
|
|
936
|
-
default: 192e3
|
|
937
|
-
},
|
|
938
|
-
maxSessionSize: {
|
|
939
|
-
code: "SESSION_TOO_LARGE",
|
|
940
|
-
unit: "bytes",
|
|
941
|
-
default: 15e8,
|
|
942
|
-
warning: 75e7
|
|
943
|
-
},
|
|
944
|
-
maxClips: {
|
|
945
|
-
code: "TOO_MANY_CLIPS",
|
|
946
|
-
unit: "count",
|
|
947
|
-
default: 2e3
|
|
948
|
-
},
|
|
949
|
-
maxTracks: {
|
|
950
|
-
code: "TOO_MANY_TRACKS",
|
|
951
|
-
unit: "count",
|
|
952
|
-
default: 16
|
|
953
|
-
}
|
|
954
|
-
};
|
|
955
|
-
var audioLimits = /* @__PURE__ */ defineLimits(audioLimitDefinitions, { errorClass: AudioError });
|
|
956
|
-
// src/core/operations/checks.ts
|
|
957
|
-
var MAX_METADATA_LENGTH = 1e3;
|
|
958
|
-
function longerThan(text, max) {
|
|
959
|
-
if (text.length <= max) return false;
|
|
960
|
-
let count = 0;
|
|
961
|
-
for (const _ of text) {
|
|
962
|
-
count += 1;
|
|
963
|
-
if (count > max) return true;
|
|
964
|
-
}
|
|
965
|
-
return false;
|
|
966
|
-
}
|
|
967
|
-
var CURVES = /* @__PURE__ */ new Set(["linear", "equalPower"]);
|
|
968
|
-
var Invalid = class extends Error {
|
|
969
|
-
reason;
|
|
970
|
-
constructor(reason, message) {
|
|
971
|
-
super(message);
|
|
972
|
-
this.reason = reason;
|
|
973
|
-
}
|
|
974
|
-
};
|
|
975
|
-
function invalid(reason, message) {
|
|
976
|
-
throw new Invalid(reason, message);
|
|
977
|
-
}
|
|
978
|
-
function isRecord(value) {
|
|
979
|
-
return typeof value === "object" && value !== null;
|
|
980
|
-
}
|
|
981
|
-
function isKey(value) {
|
|
982
|
-
return typeof value === "string" && value.length > 0;
|
|
983
|
-
}
|
|
984
|
-
function isIndex(value, max) {
|
|
985
|
-
return Number.isSafeInteger(value) && value >= 0 && value <= max;
|
|
986
|
-
}
|
|
987
|
-
function fade(value, name) {
|
|
988
|
-
if (!isRecord(value)) invalid("malformed-command", `${name} isn't a fade`);
|
|
989
|
-
onlyFields(value, FADE_FIELDS, name);
|
|
990
|
-
if (typeof value.curve !== "string" || !CURVES.has(value.curve)) invalid("invalid-fade-curve", `${name}'s curve isn't linear or equalPower`);
|
|
991
|
-
return {
|
|
992
|
-
frames: value.frames,
|
|
993
|
-
curve: value.curve
|
|
994
|
-
};
|
|
995
|
-
}
|
|
996
|
-
var CLIP_FIELDS = [
|
|
997
|
-
"key",
|
|
998
|
-
"source",
|
|
999
|
-
"start",
|
|
1000
|
-
"end",
|
|
1001
|
-
"gainDb",
|
|
1002
|
-
"fadeIn",
|
|
1003
|
-
"fadeOut"
|
|
1004
|
-
];
|
|
1005
|
-
var FADE_FIELDS = ["frames", "curve"];
|
|
1006
|
-
function onlyFields(value, allowed, what) {
|
|
1007
|
-
for (const [field, content] of Object.entries(value)) if (content !== void 0 && !allowed.includes(field)) invalid("malformed-command", `${what} has no field ${field}`);
|
|
1008
|
-
}
|
|
1009
|
-
function checkClipBody(value, sourceFrames) {
|
|
1010
|
-
if (!isRecord(value) || !isKey(value.key) || !isKey(value.source)) invalid("malformed-command", "a clip needs a key and a source");
|
|
1011
|
-
onlyFields(value, CLIP_FIELDS, "a clip");
|
|
1012
|
-
const clip = {
|
|
1013
|
-
key: value.key,
|
|
1014
|
-
source: value.source,
|
|
1015
|
-
start: value.start,
|
|
1016
|
-
end: value.end,
|
|
1017
|
-
gainDb: value.gainDb,
|
|
1018
|
-
fadeIn: fade(value.fadeIn, "fadeIn"),
|
|
1019
|
-
fadeOut: fade(value.fadeOut, "fadeOut")
|
|
1020
|
-
};
|
|
1021
|
-
const frames = sourceFrames(clip.source);
|
|
1022
|
-
if (frames === void 0) invalid("unknown-source", `the session has no source ${clip.source}`);
|
|
1023
|
-
const problem = clipProblem(clip, frames);
|
|
1024
|
-
if (problem) invalid(problem, `clip ${clip.key} isn't valid within its source`);
|
|
1025
|
-
return clip;
|
|
1026
|
-
}
|
|
1027
|
-
function fitFades(clip) {
|
|
1028
|
-
const length = clipLength(clip);
|
|
1029
|
-
const total = clip.fadeIn.frames + clip.fadeOut.frames;
|
|
1030
|
-
if (total <= length || !(length > 0)) return clip;
|
|
1031
|
-
return {
|
|
1032
|
-
...clip,
|
|
1033
|
-
fadeIn: {
|
|
1034
|
-
...clip.fadeIn,
|
|
1035
|
-
frames: Math.floor(clip.fadeIn.frames * length / total)
|
|
1036
|
-
},
|
|
1037
|
-
fadeOut: {
|
|
1038
|
-
...clip.fadeOut,
|
|
1039
|
-
frames: Math.floor(clip.fadeOut.frames * length / total)
|
|
1040
|
-
}
|
|
1041
|
-
};
|
|
1042
|
-
}
|
|
1043
24
|
// src/core/format/document-parts.ts
|
|
1044
25
|
var AUDIO_PLAN_FORMAT = "hm-audio-plan";
|
|
1045
26
|
var SOURCE_FIELDS = ["frames", "asset"];
|
|
@@ -1098,863 +79,6 @@ function readMetadata(value) {
|
|
|
1098
79
|
});
|
|
1099
80
|
return Object.fromEntries(fields);
|
|
1100
81
|
}
|
|
1101
|
-
// src/core/operations/commands.ts
|
|
1102
|
-
function refuse(reason, message) {
|
|
1103
|
-
throw new Invalid(reason, message);
|
|
1104
|
-
}
|
|
1105
|
-
var COMMAND_FIELDS = {
|
|
1106
|
-
"audio.insertClips": ["track", "clips"],
|
|
1107
|
-
"audio.removeClips": ["keys"],
|
|
1108
|
-
"audio.trimClip": [
|
|
1109
|
-
"key",
|
|
1110
|
-
"start",
|
|
1111
|
-
"end"
|
|
1112
|
-
],
|
|
1113
|
-
"audio.splitClip": [
|
|
1114
|
-
"key",
|
|
1115
|
-
"at",
|
|
1116
|
-
"newKey"
|
|
1117
|
-
],
|
|
1118
|
-
"audio.moveClips": [
|
|
1119
|
-
"keys",
|
|
1120
|
-
"by",
|
|
1121
|
-
"track"
|
|
1122
|
-
],
|
|
1123
|
-
"audio.setGain": ["keys", "gainDb"],
|
|
1124
|
-
"audio.setFade": [
|
|
1125
|
-
"key",
|
|
1126
|
-
"edge",
|
|
1127
|
-
"frames",
|
|
1128
|
-
"curve"
|
|
1129
|
-
],
|
|
1130
|
-
"audio.deleteRange": [
|
|
1131
|
-
"start",
|
|
1132
|
-
"end",
|
|
1133
|
-
"tracks",
|
|
1134
|
-
"ripple",
|
|
1135
|
-
"newKeys"
|
|
1136
|
-
],
|
|
1137
|
-
"audio.keepRange": ["start", "end"],
|
|
1138
|
-
"audio.insertTime": [
|
|
1139
|
-
"at",
|
|
1140
|
-
"frames",
|
|
1141
|
-
"tracks",
|
|
1142
|
-
"newKeys"
|
|
1143
|
-
],
|
|
1144
|
-
"audio.addTrack": [
|
|
1145
|
-
"key",
|
|
1146
|
-
"index",
|
|
1147
|
-
"name"
|
|
1148
|
-
],
|
|
1149
|
-
"audio.removeTracks": ["keys"],
|
|
1150
|
-
"audio.moveTrack": ["key", "to"],
|
|
1151
|
-
"audio.setTrack": [
|
|
1152
|
-
"key",
|
|
1153
|
-
"name",
|
|
1154
|
-
"gainDb",
|
|
1155
|
-
"muted",
|
|
1156
|
-
"soloed"
|
|
1157
|
-
],
|
|
1158
|
-
"audio.setMasterGain": ["gainDb"],
|
|
1159
|
-
"audio.setChannels": ["channels"],
|
|
1160
|
-
"audio.setMetadata": ["field", "value"],
|
|
1161
|
-
"audio.restoreClips": ["entries"],
|
|
1162
|
-
"audio.updateClips": ["clips"],
|
|
1163
|
-
"audio.placeClips": ["entries"],
|
|
1164
|
-
"audio.replaceClips": ["edits"],
|
|
1165
|
-
"audio.restoreTracks": ["entries"]
|
|
1166
|
-
};
|
|
1167
|
-
var TRACK_FIELDS = [
|
|
1168
|
-
"key",
|
|
1169
|
-
"name",
|
|
1170
|
-
"gainDb",
|
|
1171
|
-
"muted",
|
|
1172
|
-
"soloed",
|
|
1173
|
-
"clips"
|
|
1174
|
-
];
|
|
1175
|
-
var isFrames = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
1176
|
-
function checkGain(value) {
|
|
1177
|
-
if (typeof value !== "number" || !(value >= -120 && value <= 120)) refuse("gain-out-of-range", `gain must be from ${MIN_GAIN_DB} to 120 dB`);
|
|
1178
|
-
return value;
|
|
1179
|
-
}
|
|
1180
|
-
function checkName(value) {
|
|
1181
|
-
if (typeof value !== "string") refuse("malformed-command", "a track's name must be text");
|
|
1182
|
-
if (longerThan(value, 100)) refuse("name-too-long", `a track's name can have at most 100 code points`);
|
|
1183
|
-
return value;
|
|
1184
|
-
}
|
|
1185
|
-
function checkFlag(value, name) {
|
|
1186
|
-
if (typeof value !== "boolean") refuse("malformed-command", `${name} must be true or false`);
|
|
1187
|
-
return value;
|
|
1188
|
-
}
|
|
1189
|
-
function checkClip(value, sourceFrames) {
|
|
1190
|
-
if (!isRecord(value)) refuse("malformed-command", "a clip is an object");
|
|
1191
|
-
const clip = checkClipBody({
|
|
1192
|
-
...value,
|
|
1193
|
-
at: void 0
|
|
1194
|
-
}, sourceFrames);
|
|
1195
|
-
const { at } = value;
|
|
1196
|
-
if (!isFrames(at) || !Number.isSafeInteger(at + (clip.end - clip.start))) refuse("invalid-frames", `clip ${clip.key}'s position isn't a whole number of frames`);
|
|
1197
|
-
return {
|
|
1198
|
-
key: clip.key,
|
|
1199
|
-
at,
|
|
1200
|
-
source: clip.source,
|
|
1201
|
-
start: clip.start,
|
|
1202
|
-
end: clip.end,
|
|
1203
|
-
gainDb: clip.gainDb,
|
|
1204
|
-
fadeIn: clip.fadeIn,
|
|
1205
|
-
fadeOut: clip.fadeOut
|
|
1206
|
-
};
|
|
1207
|
-
}
|
|
1208
|
-
function checkClips(values, sourceFrames) {
|
|
1209
|
-
if (!Array.isArray(values)) refuse("malformed-command", "clips must be a list");
|
|
1210
|
-
return values.map((value) => checkClip(value, sourceFrames));
|
|
1211
|
-
}
|
|
1212
|
-
function checkOrder(clips) {
|
|
1213
|
-
const problem = orderProblem(clips);
|
|
1214
|
-
if (problem === "clips-overlap") refuse(problem, "two clips on a track would overlap");
|
|
1215
|
-
if (problem) refuse(problem, "a track's clips must be in time order");
|
|
1216
|
-
}
|
|
1217
|
-
function sorted(clips) {
|
|
1218
|
-
return [...clips].sort((a, b) => a.at - b.at);
|
|
1219
|
-
}
|
|
1220
|
-
function checkTrack(value, sourceFrames) {
|
|
1221
|
-
if (!isRecord(value) || !isKey(value.key)) refuse("malformed-command", "a track needs a key");
|
|
1222
|
-
onlyFields(value, TRACK_FIELDS, "a track");
|
|
1223
|
-
const clips = checkClips(value.clips, sourceFrames);
|
|
1224
|
-
checkOrder(clips);
|
|
1225
|
-
return {
|
|
1226
|
-
key: value.key,
|
|
1227
|
-
name: checkName(value.name),
|
|
1228
|
-
gainDb: checkGain(value.gainDb),
|
|
1229
|
-
muted: checkFlag(value.muted, "muted"),
|
|
1230
|
-
soloed: checkFlag(value.soloed, "soloed"),
|
|
1231
|
-
clips
|
|
1232
|
-
};
|
|
1233
|
-
}
|
|
1234
|
-
function keysOf$1(plan) {
|
|
1235
|
-
const keys = /* @__PURE__ */ new Set();
|
|
1236
|
-
for (const track of plan.tracks) {
|
|
1237
|
-
keys.add(track.key);
|
|
1238
|
-
for (const clip of track.clips) keys.add(clip.key);
|
|
1239
|
-
}
|
|
1240
|
-
return keys;
|
|
1241
|
-
}
|
|
1242
|
-
function checkUniqueKeys(plan) {
|
|
1243
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1244
|
-
const once = (key) => {
|
|
1245
|
-
if (seen.has(key)) refuse("duplicate-key", `the key ${key} appears twice`);
|
|
1246
|
-
seen.add(key);
|
|
1247
|
-
};
|
|
1248
|
-
for (const track of plan.tracks) {
|
|
1249
|
-
once(track.key);
|
|
1250
|
-
for (const clip of track.clips) once(clip.key);
|
|
1251
|
-
}
|
|
1252
|
-
}
|
|
1253
|
-
function claim(keys, taken) {
|
|
1254
|
-
const before = new Set(taken);
|
|
1255
|
-
for (const key of keys) {
|
|
1256
|
-
if (!isKey(key)) refuse("malformed-command", "a key must be a non-empty string");
|
|
1257
|
-
if (before.has(key)) refuse("key-taken", `the key ${key} is taken`);
|
|
1258
|
-
if (taken.has(key)) refuse("duplicate-key", `the key ${key} appears twice`);
|
|
1259
|
-
taken.add(key);
|
|
1260
|
-
}
|
|
1261
|
-
}
|
|
1262
|
-
function keyList(value, empty) {
|
|
1263
|
-
if (!Array.isArray(value)) refuse("malformed-command", "keys must be a list");
|
|
1264
|
-
if (value.length === 0) refuse(empty, "it needs at least one");
|
|
1265
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1266
|
-
for (const key of value) {
|
|
1267
|
-
if (!isKey(key)) refuse("malformed-command", "a key must be a non-empty string");
|
|
1268
|
-
if (seen.has(key)) refuse("duplicate-key", `the key ${key} appears twice`);
|
|
1269
|
-
seen.add(key);
|
|
1270
|
-
}
|
|
1271
|
-
return value;
|
|
1272
|
-
}
|
|
1273
|
-
function trackIndex(plan, key) {
|
|
1274
|
-
if (!isKey(key)) refuse("malformed-command", "a track is named by its key");
|
|
1275
|
-
const index = plan.tracks.findIndex((track) => track.key === key);
|
|
1276
|
-
if (index < 0) refuse("unknown-track", `the plan has no track ${key}`);
|
|
1277
|
-
return index;
|
|
1278
|
-
}
|
|
1279
|
-
function trackIndexes(plan, keys) {
|
|
1280
|
-
return keyList(keys, "no-tracks").map((key) => trackIndex(plan, key));
|
|
1281
|
-
}
|
|
1282
|
-
function locateClips(plan, keys) {
|
|
1283
|
-
const wanted = new Set(keyList(keys, "no-clips"));
|
|
1284
|
-
const found = [];
|
|
1285
|
-
plan.tracks.forEach((track, index) => {
|
|
1286
|
-
for (const clip of track.clips) if (wanted.has(clip.key)) found.push({
|
|
1287
|
-
track: index,
|
|
1288
|
-
clip
|
|
1289
|
-
});
|
|
1290
|
-
});
|
|
1291
|
-
if (found.length !== wanted.size) refuse("unknown-clip", "the plan doesn't have every clip");
|
|
1292
|
-
return found;
|
|
1293
|
-
}
|
|
1294
|
-
function locateClip(plan, key) {
|
|
1295
|
-
return locateClips(plan, [key])[0];
|
|
1296
|
-
}
|
|
1297
|
-
function withClips(plan, changes) {
|
|
1298
|
-
if (changes.size === 0) return plan;
|
|
1299
|
-
return {
|
|
1300
|
-
...plan,
|
|
1301
|
-
tracks: plan.tracks.map((track, index) => {
|
|
1302
|
-
const clips = changes.get(index);
|
|
1303
|
-
return clips ? {
|
|
1304
|
-
...track,
|
|
1305
|
-
clips
|
|
1306
|
-
} : track;
|
|
1307
|
-
})
|
|
1308
|
-
};
|
|
1309
|
-
}
|
|
1310
|
-
function withUpdated(plan, clips) {
|
|
1311
|
-
const byKey = new Map(clips.map((clip) => [clip.key, clip]));
|
|
1312
|
-
const changes = /* @__PURE__ */ new Map();
|
|
1313
|
-
plan.tracks.forEach((track, index) => {
|
|
1314
|
-
if (track.clips.some((clip) => byKey.has(clip.key))) {
|
|
1315
|
-
const next = track.clips.map((clip) => byKey.get(clip.key) ?? clip);
|
|
1316
|
-
checkOrder(next);
|
|
1317
|
-
changes.set(index, next);
|
|
1318
|
-
}
|
|
1319
|
-
});
|
|
1320
|
-
return withClips(plan, changes);
|
|
1321
|
-
}
|
|
1322
|
-
function cut(clip, from, to, key, at) {
|
|
1323
|
-
return fitFades({
|
|
1324
|
-
...clip,
|
|
1325
|
-
key,
|
|
1326
|
-
at,
|
|
1327
|
-
start: clip.start + from,
|
|
1328
|
-
end: clip.start + to,
|
|
1329
|
-
fadeIn: from === 0 ? clip.fadeIn : NO_FADE,
|
|
1330
|
-
fadeOut: to === clip.end - clip.start ? clip.fadeOut : NO_FADE
|
|
1331
|
-
});
|
|
1332
|
-
}
|
|
1333
|
-
function shifted(clips, by) {
|
|
1334
|
-
if (by === 0) return [...clips];
|
|
1335
|
-
return clips.map((clip) => {
|
|
1336
|
-
const at = clip.at + by;
|
|
1337
|
-
if (at < 0) refuse("frame-outside-plan", "a clip can't start before the timeline does");
|
|
1338
|
-
if (!Number.isSafeInteger(at + (clip.end - clip.start))) refuse("invalid-frames", "a clip's position isn't a whole number of frames");
|
|
1339
|
-
return {
|
|
1340
|
-
...clip,
|
|
1341
|
-
at
|
|
1342
|
-
};
|
|
1343
|
-
});
|
|
1344
|
-
}
|
|
1345
|
-
function checkRange(plan, start, end) {
|
|
1346
|
-
const length = planLength(plan);
|
|
1347
|
-
if (!isIndex(start, length) || !isIndex(end, length)) refuse("frame-outside-plan", "the range must be inside the plan");
|
|
1348
|
-
if (end <= start) refuse("empty-range", "the range must be at least one frame long");
|
|
1349
|
-
return [start, end];
|
|
1350
|
-
}
|
|
1351
|
-
function newKeysFor(value, tracks) {
|
|
1352
|
-
const keys = /* @__PURE__ */ new Map();
|
|
1353
|
-
if (value === void 0) return keys;
|
|
1354
|
-
if (!isRecord(value) || Array.isArray(value)) refuse("malformed-command", "newKeys is an object of keys, by track");
|
|
1355
|
-
for (const [track, key] of Object.entries(value)) {
|
|
1356
|
-
if (!isKey(track) || !isKey(key)) refuse("malformed-command", "newKeys is an object of keys, by track");
|
|
1357
|
-
if (!tracks.includes(track)) refuse("unknown-track", `the command doesn't edit track ${track}`);
|
|
1358
|
-
keys.set(track, key);
|
|
1359
|
-
}
|
|
1360
|
-
return keys;
|
|
1361
|
-
}
|
|
1362
|
-
function place(plan, placements) {
|
|
1363
|
-
const arriving = /* @__PURE__ */ new Map();
|
|
1364
|
-
const changes = /* @__PURE__ */ new Map();
|
|
1365
|
-
plan.tracks.forEach((track, index) => {
|
|
1366
|
-
const staying = [];
|
|
1367
|
-
for (const clip of track.clips) {
|
|
1368
|
-
const to = placements.get(clip.key);
|
|
1369
|
-
if (!to) {
|
|
1370
|
-
staying.push(clip);
|
|
1371
|
-
continue;
|
|
1372
|
-
}
|
|
1373
|
-
const moved = shifted([clip], to.at - clip.at);
|
|
1374
|
-
arriving.set(to.track, [...arriving.get(to.track) ?? [], ...moved]);
|
|
1375
|
-
}
|
|
1376
|
-
if (staying.length !== track.clips.length) changes.set(index, staying);
|
|
1377
|
-
});
|
|
1378
|
-
for (const [index, clips] of arriving) {
|
|
1379
|
-
const base = changes.get(index) ?? plan.tracks[index].clips;
|
|
1380
|
-
changes.set(index, sorted([...base, ...clips]));
|
|
1381
|
-
}
|
|
1382
|
-
for (const clips of changes.values()) checkOrder(clips);
|
|
1383
|
-
return withClips(plan, changes);
|
|
1384
|
-
}
|
|
1385
|
-
function timeEdit(plan, edit) {
|
|
1386
|
-
return {
|
|
1387
|
-
state: withClips(plan, edit.changes),
|
|
1388
|
-
inverse: {
|
|
1389
|
-
type: "audio.replaceClips",
|
|
1390
|
-
edits: edit.undo
|
|
1391
|
-
}
|
|
1392
|
-
};
|
|
1393
|
-
}
|
|
1394
|
-
function apply(plan, command, sourceFrames) {
|
|
1395
|
-
if (!isRecord(command)) refuse("malformed-command", "a command is an object");
|
|
1396
|
-
const fields = Object.hasOwn(COMMAND_FIELDS, command.type) ? COMMAND_FIELDS[command.type] : void 0;
|
|
1397
|
-
if (!fields) refuse("unknown-command", "unknown command");
|
|
1398
|
-
onlyFields(command, ["type", ...fields], command.type);
|
|
1399
|
-
switch (command.type) {
|
|
1400
|
-
case "audio.insertClips": {
|
|
1401
|
-
const index = trackIndex(plan, command.track);
|
|
1402
|
-
const clips = checkClips(command.clips, sourceFrames);
|
|
1403
|
-
if (clips.length === 0) refuse("no-clips", "it needs at least one clip");
|
|
1404
|
-
const keys = clips.map((clip) => clip.key);
|
|
1405
|
-
claim(keys, keysOf$1(plan));
|
|
1406
|
-
const next = sorted([...plan.tracks[index].clips, ...clips]);
|
|
1407
|
-
checkOrder(next);
|
|
1408
|
-
return {
|
|
1409
|
-
state: withClips(plan, /* @__PURE__ */ new Map([[index, next]])),
|
|
1410
|
-
inverse: {
|
|
1411
|
-
type: "audio.removeClips",
|
|
1412
|
-
keys
|
|
1413
|
-
}
|
|
1414
|
-
};
|
|
1415
|
-
}
|
|
1416
|
-
case "audio.removeClips": {
|
|
1417
|
-
const found = locateClips(plan, command.keys);
|
|
1418
|
-
const removed = new Set(found.map(({ clip }) => clip.key));
|
|
1419
|
-
const changes = /* @__PURE__ */ new Map();
|
|
1420
|
-
for (const { track } of found) changes.set(track, plan.tracks[track].clips.filter((clip) => !removed.has(clip.key)));
|
|
1421
|
-
return {
|
|
1422
|
-
state: withClips(plan, changes),
|
|
1423
|
-
inverse: {
|
|
1424
|
-
type: "audio.restoreClips",
|
|
1425
|
-
entries: found.map(({ track, clip }) => ({
|
|
1426
|
-
track: plan.tracks[track].key,
|
|
1427
|
-
clip
|
|
1428
|
-
}))
|
|
1429
|
-
}
|
|
1430
|
-
};
|
|
1431
|
-
}
|
|
1432
|
-
case "audio.restoreClips": {
|
|
1433
|
-
if (!Array.isArray(command.entries)) refuse("malformed-command", "entries must be a list");
|
|
1434
|
-
if (command.entries.length === 0) refuse("no-clips", "it needs at least one clip");
|
|
1435
|
-
const arriving = /* @__PURE__ */ new Map();
|
|
1436
|
-
const keys = [];
|
|
1437
|
-
for (const entry of command.entries) {
|
|
1438
|
-
if (!isRecord(entry)) refuse("malformed-command", "an entry needs a track and a clip");
|
|
1439
|
-
onlyFields(entry, ["track", "clip"], "an entry");
|
|
1440
|
-
const index = trackIndex(plan, entry.track);
|
|
1441
|
-
const clip = checkClip(entry.clip, sourceFrames);
|
|
1442
|
-
keys.push(clip.key);
|
|
1443
|
-
arriving.set(index, [...arriving.get(index) ?? [], clip]);
|
|
1444
|
-
}
|
|
1445
|
-
claim(keys, keysOf$1(plan));
|
|
1446
|
-
const changes = /* @__PURE__ */ new Map();
|
|
1447
|
-
for (const [index, clips] of arriving) {
|
|
1448
|
-
const next = sorted([...plan.tracks[index].clips, ...clips]);
|
|
1449
|
-
checkOrder(next);
|
|
1450
|
-
changes.set(index, next);
|
|
1451
|
-
}
|
|
1452
|
-
return {
|
|
1453
|
-
state: withClips(plan, changes),
|
|
1454
|
-
inverse: {
|
|
1455
|
-
type: "audio.removeClips",
|
|
1456
|
-
keys
|
|
1457
|
-
}
|
|
1458
|
-
};
|
|
1459
|
-
}
|
|
1460
|
-
case "audio.updateClips": {
|
|
1461
|
-
const clips = checkClips(command.clips, sourceFrames);
|
|
1462
|
-
const before = locateClips(plan, clips.map((clip) => clip.key)).map(({ clip }) => clip);
|
|
1463
|
-
return {
|
|
1464
|
-
state: withUpdated(plan, clips),
|
|
1465
|
-
inverse: {
|
|
1466
|
-
type: "audio.updateClips",
|
|
1467
|
-
clips: before
|
|
1468
|
-
}
|
|
1469
|
-
};
|
|
1470
|
-
}
|
|
1471
|
-
case "audio.placeClips": {
|
|
1472
|
-
if (!Array.isArray(command.entries)) refuse("malformed-command", "entries must be a list");
|
|
1473
|
-
const placements = /* @__PURE__ */ new Map();
|
|
1474
|
-
const keys = [];
|
|
1475
|
-
for (const entry of command.entries) {
|
|
1476
|
-
if (!isRecord(entry)) refuse("malformed-command", "an entry needs a key, a track and a position");
|
|
1477
|
-
onlyFields(entry, [
|
|
1478
|
-
"key",
|
|
1479
|
-
"track",
|
|
1480
|
-
"at"
|
|
1481
|
-
], "an entry");
|
|
1482
|
-
if (!isFrames(entry.at)) refuse("invalid-frames", "a position is a whole number of frames");
|
|
1483
|
-
keys.push(entry.key);
|
|
1484
|
-
if (isKey(entry.key)) placements.set(entry.key, {
|
|
1485
|
-
track: trackIndex(plan, entry.track),
|
|
1486
|
-
at: entry.at
|
|
1487
|
-
});
|
|
1488
|
-
}
|
|
1489
|
-
const found = locateClips(plan, keys);
|
|
1490
|
-
return {
|
|
1491
|
-
state: place(plan, placements),
|
|
1492
|
-
inverse: {
|
|
1493
|
-
type: "audio.placeClips",
|
|
1494
|
-
entries: found.map(({ track, clip }) => ({
|
|
1495
|
-
key: clip.key,
|
|
1496
|
-
track: plan.tracks[track].key,
|
|
1497
|
-
at: clip.at
|
|
1498
|
-
}))
|
|
1499
|
-
}
|
|
1500
|
-
};
|
|
1501
|
-
}
|
|
1502
|
-
case "audio.moveClips": {
|
|
1503
|
-
const { by } = command;
|
|
1504
|
-
if (!Number.isSafeInteger(by)) refuse("malformed-command", "by is a whole number of frames");
|
|
1505
|
-
const found = locateClips(plan, command.keys);
|
|
1506
|
-
const to = command.track === void 0 ? void 0 : trackIndex(plan, command.track);
|
|
1507
|
-
return {
|
|
1508
|
-
state: place(plan, new Map(found.map(({ track, clip }) => [clip.key, {
|
|
1509
|
-
track: to ?? track,
|
|
1510
|
-
at: clip.at + by
|
|
1511
|
-
}]))),
|
|
1512
|
-
inverse: {
|
|
1513
|
-
type: "audio.placeClips",
|
|
1514
|
-
entries: found.map(({ track, clip }) => ({
|
|
1515
|
-
key: clip.key,
|
|
1516
|
-
track: plan.tracks[track].key,
|
|
1517
|
-
at: clip.at
|
|
1518
|
-
}))
|
|
1519
|
-
}
|
|
1520
|
-
};
|
|
1521
|
-
}
|
|
1522
|
-
case "audio.trimClip": {
|
|
1523
|
-
const { track, clip } = locateClip(plan, command.key);
|
|
1524
|
-
const { start, end } = command;
|
|
1525
|
-
const at = isFrames(start) ? clip.at + (start - clip.start) : clip.at;
|
|
1526
|
-
if (at < 0) refuse("frame-outside-plan", "a clip can't start before the timeline does");
|
|
1527
|
-
const trimmed = checkClip(fitFades({
|
|
1528
|
-
...clip,
|
|
1529
|
-
at,
|
|
1530
|
-
start,
|
|
1531
|
-
end
|
|
1532
|
-
}), sourceFrames);
|
|
1533
|
-
const clips = sorted(plan.tracks[track].clips.map((entry) => entry.key === clip.key ? trimmed : entry));
|
|
1534
|
-
checkOrder(clips);
|
|
1535
|
-
return {
|
|
1536
|
-
state: withClips(plan, /* @__PURE__ */ new Map([[track, clips]])),
|
|
1537
|
-
inverse: {
|
|
1538
|
-
type: "audio.updateClips",
|
|
1539
|
-
clips: [clip]
|
|
1540
|
-
}
|
|
1541
|
-
};
|
|
1542
|
-
}
|
|
1543
|
-
case "audio.splitClip": {
|
|
1544
|
-
const { track, clip } = locateClip(plan, command.key);
|
|
1545
|
-
const { at } = command;
|
|
1546
|
-
if (!isIndex(at, planLength(plan))) refuse("frame-outside-plan", "the frame is outside the plan");
|
|
1547
|
-
if (at <= clip.at || at >= clipEnd(clip)) refuse("not-inside-clip", "the frame must be strictly inside the clip");
|
|
1548
|
-
claim([command.newKey], keysOf$1(plan));
|
|
1549
|
-
const newKey = command.newKey;
|
|
1550
|
-
const length = clip.end - clip.start;
|
|
1551
|
-
const parts = [cut(clip, 0, at - clip.at, clip.key, clip.at), cut(clip, at - clip.at, length, newKey, at)];
|
|
1552
|
-
const clips = plan.tracks[track].clips;
|
|
1553
|
-
const index = clips.indexOf(clip);
|
|
1554
|
-
return {
|
|
1555
|
-
state: withClips(plan, /* @__PURE__ */ new Map([[track, [
|
|
1556
|
-
...clips.slice(0, index),
|
|
1557
|
-
...parts,
|
|
1558
|
-
...clips.slice(index + 1)
|
|
1559
|
-
]]])),
|
|
1560
|
-
inverse: {
|
|
1561
|
-
type: "audio.replaceClips",
|
|
1562
|
-
edits: [{
|
|
1563
|
-
track: plan.tracks[track].key,
|
|
1564
|
-
index,
|
|
1565
|
-
count: 2,
|
|
1566
|
-
clips: [clip],
|
|
1567
|
-
shift: 0
|
|
1568
|
-
}]
|
|
1569
|
-
}
|
|
1570
|
-
};
|
|
1571
|
-
}
|
|
1572
|
-
case "audio.setGain": {
|
|
1573
|
-
const gainDb = checkGain(command.gainDb);
|
|
1574
|
-
const before = locateClips(plan, command.keys).map(({ clip }) => clip);
|
|
1575
|
-
return {
|
|
1576
|
-
state: withUpdated(plan, before.map((clip) => ({
|
|
1577
|
-
...clip,
|
|
1578
|
-
gainDb
|
|
1579
|
-
}))),
|
|
1580
|
-
inverse: {
|
|
1581
|
-
type: "audio.updateClips",
|
|
1582
|
-
clips: before
|
|
1583
|
-
}
|
|
1584
|
-
};
|
|
1585
|
-
}
|
|
1586
|
-
case "audio.setFade": {
|
|
1587
|
-
const { clip } = locateClip(plan, command.key);
|
|
1588
|
-
const { edge, frames } = command;
|
|
1589
|
-
if (edge !== "in" && edge !== "out") refuse("invalid-edge", "the edge must be in or out");
|
|
1590
|
-
const next = {
|
|
1591
|
-
frames,
|
|
1592
|
-
curve: fade({
|
|
1593
|
-
frames,
|
|
1594
|
-
curve: command.curve
|
|
1595
|
-
}, "the fade").curve
|
|
1596
|
-
};
|
|
1597
|
-
return {
|
|
1598
|
-
state: withUpdated(plan, [checkClip(edge === "in" ? {
|
|
1599
|
-
...clip,
|
|
1600
|
-
fadeIn: next
|
|
1601
|
-
} : {
|
|
1602
|
-
...clip,
|
|
1603
|
-
fadeOut: next
|
|
1604
|
-
}, sourceFrames)]),
|
|
1605
|
-
inverse: {
|
|
1606
|
-
type: "audio.updateClips",
|
|
1607
|
-
clips: [clip]
|
|
1608
|
-
}
|
|
1609
|
-
};
|
|
1610
|
-
}
|
|
1611
|
-
case "audio.deleteRange": {
|
|
1612
|
-
const [start, end] = checkRange(plan, command.start, command.end);
|
|
1613
|
-
const indexes = trackIndexes(plan, command.tracks);
|
|
1614
|
-
const ripple = checkFlag(command.ripple, "ripple");
|
|
1615
|
-
const newKeys = newKeysFor(command.newKeys, command.tracks);
|
|
1616
|
-
const taken = keysOf$1(plan);
|
|
1617
|
-
const edit = {
|
|
1618
|
-
changes: /* @__PURE__ */ new Map(),
|
|
1619
|
-
undo: []
|
|
1620
|
-
};
|
|
1621
|
-
indexes.forEach((index) => {
|
|
1622
|
-
const track = plan.tracks[index];
|
|
1623
|
-
const { clips } = track;
|
|
1624
|
-
const first = clipIndexAt(clips, start);
|
|
1625
|
-
let after = first;
|
|
1626
|
-
while (after < clips.length && clips[after].at < end) after += 1;
|
|
1627
|
-
const kept = [];
|
|
1628
|
-
if (after > first) {
|
|
1629
|
-
const head = clips[first];
|
|
1630
|
-
const tail = clips[after - 1];
|
|
1631
|
-
if (head.at < start) kept.push(cut(head, 0, start - head.at, head.key, head.at));
|
|
1632
|
-
if (clipEnd(tail) > end) {
|
|
1633
|
-
let key = tail.key;
|
|
1634
|
-
if (kept.length > 0 && tail === head) {
|
|
1635
|
-
const newKey = newKeys.get(track.key);
|
|
1636
|
-
if (newKey === void 0) refuse("new-key-required", "deleting inside a clip needs a new key for its track");
|
|
1637
|
-
claim([newKey], taken);
|
|
1638
|
-
key = newKey;
|
|
1639
|
-
}
|
|
1640
|
-
kept.push(cut(tail, end - tail.at, tail.end - tail.start, key, ripple ? start : end));
|
|
1641
|
-
}
|
|
1642
|
-
}
|
|
1643
|
-
const rest = clips.slice(after);
|
|
1644
|
-
const shift = ripple && rest.length > 0 ? start - end : 0;
|
|
1645
|
-
if (after === first && shift === 0) return;
|
|
1646
|
-
edit.changes.set(index, [
|
|
1647
|
-
...clips.slice(0, first),
|
|
1648
|
-
...kept,
|
|
1649
|
-
...shifted(rest, shift)
|
|
1650
|
-
]);
|
|
1651
|
-
edit.undo.push({
|
|
1652
|
-
track: track.key,
|
|
1653
|
-
index: first,
|
|
1654
|
-
count: kept.length,
|
|
1655
|
-
clips: clips.slice(first, after),
|
|
1656
|
-
shift: shift === 0 ? 0 : -shift
|
|
1657
|
-
});
|
|
1658
|
-
});
|
|
1659
|
-
return timeEdit(plan, edit);
|
|
1660
|
-
}
|
|
1661
|
-
case "audio.keepRange": {
|
|
1662
|
-
const [start, end] = checkRange(plan, command.start, command.end);
|
|
1663
|
-
const edit = {
|
|
1664
|
-
changes: /* @__PURE__ */ new Map(),
|
|
1665
|
-
undo: []
|
|
1666
|
-
};
|
|
1667
|
-
plan.tracks.forEach((track, index) => {
|
|
1668
|
-
const { clips } = track;
|
|
1669
|
-
if (clips.length === 0) return;
|
|
1670
|
-
const kept = [];
|
|
1671
|
-
for (const clip of clips) {
|
|
1672
|
-
const from = Math.max(clip.at, start);
|
|
1673
|
-
const to = Math.min(clipEnd(clip), end);
|
|
1674
|
-
if (to > from) kept.push(cut(clip, from - clip.at, to - clip.at, clip.key, from - start));
|
|
1675
|
-
}
|
|
1676
|
-
edit.changes.set(index, kept);
|
|
1677
|
-
edit.undo.push({
|
|
1678
|
-
track: track.key,
|
|
1679
|
-
index: 0,
|
|
1680
|
-
count: kept.length,
|
|
1681
|
-
clips,
|
|
1682
|
-
shift: 0
|
|
1683
|
-
});
|
|
1684
|
-
});
|
|
1685
|
-
return timeEdit(plan, edit);
|
|
1686
|
-
}
|
|
1687
|
-
case "audio.insertTime": {
|
|
1688
|
-
const { at, frames } = command;
|
|
1689
|
-
if (!isFrames(frames) || frames < 1) refuse("invalid-frames", "the time to insert is a whole number of frames, at least one");
|
|
1690
|
-
if (!isIndex(at, planLength(plan))) refuse("frame-outside-plan", "the frame is outside the plan");
|
|
1691
|
-
const indexes = trackIndexes(plan, command.tracks);
|
|
1692
|
-
const newKeys = newKeysFor(command.newKeys, command.tracks);
|
|
1693
|
-
const taken = keysOf$1(plan);
|
|
1694
|
-
const edit = {
|
|
1695
|
-
changes: /* @__PURE__ */ new Map(),
|
|
1696
|
-
undo: []
|
|
1697
|
-
};
|
|
1698
|
-
indexes.forEach((index) => {
|
|
1699
|
-
const track = plan.tracks[index];
|
|
1700
|
-
const { clips } = track;
|
|
1701
|
-
const first = clipIndexAt(clips, at);
|
|
1702
|
-
const across = clips[first];
|
|
1703
|
-
if (across && across.at < at) {
|
|
1704
|
-
const newKey = newKeys.get(track.key);
|
|
1705
|
-
if (newKey === void 0) refuse("new-key-required", "inserting time inside a clip needs a new key for its track");
|
|
1706
|
-
claim([newKey], taken);
|
|
1707
|
-
const parts = [cut(across, 0, at - across.at, across.key, across.at), ...shifted([cut(across, at - across.at, across.end - across.start, newKey, at)], frames)];
|
|
1708
|
-
edit.changes.set(index, [
|
|
1709
|
-
...clips.slice(0, first),
|
|
1710
|
-
...parts,
|
|
1711
|
-
...shifted(clips.slice(first + 1), frames)
|
|
1712
|
-
]);
|
|
1713
|
-
edit.undo.push({
|
|
1714
|
-
track: track.key,
|
|
1715
|
-
index: first,
|
|
1716
|
-
count: 2,
|
|
1717
|
-
clips: [across],
|
|
1718
|
-
shift: -frames
|
|
1719
|
-
});
|
|
1720
|
-
return;
|
|
1721
|
-
}
|
|
1722
|
-
const rest = clips.slice(first);
|
|
1723
|
-
if (rest.length === 0) return;
|
|
1724
|
-
edit.changes.set(index, [...clips.slice(0, first), ...shifted(rest, frames)]);
|
|
1725
|
-
edit.undo.push({
|
|
1726
|
-
track: track.key,
|
|
1727
|
-
index: first,
|
|
1728
|
-
count: 0,
|
|
1729
|
-
clips: [],
|
|
1730
|
-
shift: -frames
|
|
1731
|
-
});
|
|
1732
|
-
});
|
|
1733
|
-
return timeEdit(plan, edit);
|
|
1734
|
-
}
|
|
1735
|
-
case "audio.replaceClips": {
|
|
1736
|
-
if (!Array.isArray(command.edits)) refuse("malformed-command", "edits must be a list");
|
|
1737
|
-
const edit = {
|
|
1738
|
-
changes: /* @__PURE__ */ new Map(),
|
|
1739
|
-
undo: []
|
|
1740
|
-
};
|
|
1741
|
-
for (const value of command.edits) {
|
|
1742
|
-
if (!isRecord(value)) refuse("malformed-command", "an edit is an object");
|
|
1743
|
-
onlyFields(value, [
|
|
1744
|
-
"track",
|
|
1745
|
-
"index",
|
|
1746
|
-
"count",
|
|
1747
|
-
"clips",
|
|
1748
|
-
"shift"
|
|
1749
|
-
], "an edit");
|
|
1750
|
-
const index = trackIndex(plan, value.track);
|
|
1751
|
-
if (edit.changes.has(index)) refuse("duplicate-key", "a track is edited twice");
|
|
1752
|
-
const track = plan.tracks[index];
|
|
1753
|
-
const { index: from, count, shift } = value;
|
|
1754
|
-
if (!isIndex(from, track.clips.length) || !isIndex(count, track.clips.length - from)) refuse("invalid-index", "the run of clips is outside the track");
|
|
1755
|
-
if (!Number.isSafeInteger(shift)) refuse("malformed-command", "shift is a whole number of frames");
|
|
1756
|
-
const clips = checkClips(value.clips, sourceFrames);
|
|
1757
|
-
const next = [
|
|
1758
|
-
...track.clips.slice(0, from),
|
|
1759
|
-
...clips,
|
|
1760
|
-
...shifted(track.clips.slice(from + count), shift)
|
|
1761
|
-
];
|
|
1762
|
-
checkOrder(next);
|
|
1763
|
-
edit.changes.set(index, next);
|
|
1764
|
-
edit.undo.push({
|
|
1765
|
-
track: track.key,
|
|
1766
|
-
index: from,
|
|
1767
|
-
count: clips.length,
|
|
1768
|
-
clips: track.clips.slice(from, from + count),
|
|
1769
|
-
shift: shift === 0 ? 0 : -shift
|
|
1770
|
-
});
|
|
1771
|
-
}
|
|
1772
|
-
const applied = timeEdit(plan, edit);
|
|
1773
|
-
checkUniqueKeys(applied.state);
|
|
1774
|
-
return applied;
|
|
1775
|
-
}
|
|
1776
|
-
case "audio.addTrack": {
|
|
1777
|
-
const { key, index } = command;
|
|
1778
|
-
const name = command.name === void 0 ? "" : checkName(command.name);
|
|
1779
|
-
claim([key], keysOf$1(plan));
|
|
1780
|
-
if (!isIndex(index, plan.tracks.length)) refuse("invalid-index", "the track can't go there");
|
|
1781
|
-
const track = {
|
|
1782
|
-
key,
|
|
1783
|
-
name,
|
|
1784
|
-
gainDb: 0,
|
|
1785
|
-
muted: false,
|
|
1786
|
-
soloed: false,
|
|
1787
|
-
clips: []
|
|
1788
|
-
};
|
|
1789
|
-
return {
|
|
1790
|
-
state: {
|
|
1791
|
-
...plan,
|
|
1792
|
-
tracks: [
|
|
1793
|
-
...plan.tracks.slice(0, index),
|
|
1794
|
-
track,
|
|
1795
|
-
...plan.tracks.slice(index)
|
|
1796
|
-
]
|
|
1797
|
-
},
|
|
1798
|
-
inverse: {
|
|
1799
|
-
type: "audio.removeTracks",
|
|
1800
|
-
keys: [track.key]
|
|
1801
|
-
}
|
|
1802
|
-
};
|
|
1803
|
-
}
|
|
1804
|
-
case "audio.removeTracks": {
|
|
1805
|
-
const removed = new Set(trackIndexes(plan, command.keys));
|
|
1806
|
-
if (removed.size === plan.tracks.length) refuse("last-track", "a plan keeps at least one track");
|
|
1807
|
-
return {
|
|
1808
|
-
state: {
|
|
1809
|
-
...plan,
|
|
1810
|
-
tracks: plan.tracks.filter((_, index) => !removed.has(index))
|
|
1811
|
-
},
|
|
1812
|
-
inverse: {
|
|
1813
|
-
type: "audio.restoreTracks",
|
|
1814
|
-
entries: [...removed].sort((a, b) => a - b).map((index) => ({
|
|
1815
|
-
index,
|
|
1816
|
-
track: plan.tracks[index]
|
|
1817
|
-
}))
|
|
1818
|
-
}
|
|
1819
|
-
};
|
|
1820
|
-
}
|
|
1821
|
-
case "audio.restoreTracks": {
|
|
1822
|
-
if (!Array.isArray(command.entries)) refuse("malformed-command", "entries must be a list");
|
|
1823
|
-
if (command.entries.length === 0) refuse("no-tracks", "it needs at least one track");
|
|
1824
|
-
const tracks = [...plan.tracks];
|
|
1825
|
-
const keys = [];
|
|
1826
|
-
let previous = -1;
|
|
1827
|
-
for (const entry of command.entries) {
|
|
1828
|
-
if (!isRecord(entry)) refuse("malformed-command", "an entry needs an index and a track");
|
|
1829
|
-
onlyFields(entry, ["index", "track"], "an entry");
|
|
1830
|
-
const track = checkTrack(entry.track, sourceFrames);
|
|
1831
|
-
const { index } = entry;
|
|
1832
|
-
if (!isIndex(index, tracks.length) || index <= previous) refuse("invalid-index", `track ${track.key} can't be restored at that index`);
|
|
1833
|
-
tracks.splice(index, 0, track);
|
|
1834
|
-
keys.push(track.key);
|
|
1835
|
-
previous = index;
|
|
1836
|
-
}
|
|
1837
|
-
const state = {
|
|
1838
|
-
...plan,
|
|
1839
|
-
tracks
|
|
1840
|
-
};
|
|
1841
|
-
checkUniqueKeys(state);
|
|
1842
|
-
return {
|
|
1843
|
-
state,
|
|
1844
|
-
inverse: {
|
|
1845
|
-
type: "audio.removeTracks",
|
|
1846
|
-
keys
|
|
1847
|
-
}
|
|
1848
|
-
};
|
|
1849
|
-
}
|
|
1850
|
-
case "audio.moveTrack": {
|
|
1851
|
-
const from = trackIndex(plan, command.key);
|
|
1852
|
-
const { to } = command;
|
|
1853
|
-
if (!isIndex(to, plan.tracks.length - 1)) refuse("invalid-index", "the track can't go there");
|
|
1854
|
-
const tracks = plan.tracks.filter((_, index) => index !== from);
|
|
1855
|
-
tracks.splice(to, 0, plan.tracks[from]);
|
|
1856
|
-
return {
|
|
1857
|
-
state: {
|
|
1858
|
-
...plan,
|
|
1859
|
-
tracks
|
|
1860
|
-
},
|
|
1861
|
-
inverse: {
|
|
1862
|
-
type: "audio.moveTrack",
|
|
1863
|
-
key: command.key,
|
|
1864
|
-
to: from
|
|
1865
|
-
}
|
|
1866
|
-
};
|
|
1867
|
-
}
|
|
1868
|
-
case "audio.setTrack": {
|
|
1869
|
-
const { name, gainDb, muted, soloed } = command;
|
|
1870
|
-
const next = {
|
|
1871
|
-
...name === void 0 ? {} : { name: checkName(name) },
|
|
1872
|
-
...gainDb === void 0 ? {} : { gainDb: checkGain(gainDb) },
|
|
1873
|
-
...muted === void 0 ? {} : { muted: checkFlag(muted, "muted") },
|
|
1874
|
-
...soloed === void 0 ? {} : { soloed: checkFlag(soloed, "soloed") }
|
|
1875
|
-
};
|
|
1876
|
-
const changed = Object.keys(next);
|
|
1877
|
-
if (changed.length === 0) refuse("empty-change", "it sets nothing");
|
|
1878
|
-
const index = trackIndex(plan, command.key);
|
|
1879
|
-
const track = plan.tracks[index];
|
|
1880
|
-
const before = Object.fromEntries(changed.map((field) => [field, track[field]]));
|
|
1881
|
-
return {
|
|
1882
|
-
state: {
|
|
1883
|
-
...plan,
|
|
1884
|
-
tracks: plan.tracks.map((entry, at) => at === index ? {
|
|
1885
|
-
...track,
|
|
1886
|
-
...next
|
|
1887
|
-
} : entry)
|
|
1888
|
-
},
|
|
1889
|
-
inverse: {
|
|
1890
|
-
type: "audio.setTrack",
|
|
1891
|
-
key: track.key,
|
|
1892
|
-
...before
|
|
1893
|
-
}
|
|
1894
|
-
};
|
|
1895
|
-
}
|
|
1896
|
-
case "audio.setMasterGain": return {
|
|
1897
|
-
state: {
|
|
1898
|
-
...plan,
|
|
1899
|
-
gainDb: checkGain(command.gainDb)
|
|
1900
|
-
},
|
|
1901
|
-
inverse: {
|
|
1902
|
-
type: "audio.setMasterGain",
|
|
1903
|
-
gainDb: plan.gainDb
|
|
1904
|
-
}
|
|
1905
|
-
};
|
|
1906
|
-
case "audio.setChannels": {
|
|
1907
|
-
const { channels } = command;
|
|
1908
|
-
if (!isFrames(channels) || channels < 1) refuse("malformed-command", "channels is a whole number, at least one");
|
|
1909
|
-
return {
|
|
1910
|
-
state: {
|
|
1911
|
-
...plan,
|
|
1912
|
-
channels
|
|
1913
|
-
},
|
|
1914
|
-
inverse: {
|
|
1915
|
-
type: "audio.setChannels",
|
|
1916
|
-
channels: plan.channels
|
|
1917
|
-
}
|
|
1918
|
-
};
|
|
1919
|
-
}
|
|
1920
|
-
case "audio.setMetadata": {
|
|
1921
|
-
const { field, value } = command;
|
|
1922
|
-
if (typeof field !== "string" || !Object.hasOwn(EMPTY_METADATA, field)) refuse("unknown-metadata-field", "there's no such metadata field");
|
|
1923
|
-
if (typeof value !== "string") refuse("malformed-command", "a metadata value must be text");
|
|
1924
|
-
if (longerThan(value, 1e3)) refuse("metadata-too-long", `a metadata value can have at most ${MAX_METADATA_LENGTH} code points`);
|
|
1925
|
-
return {
|
|
1926
|
-
state: {
|
|
1927
|
-
...plan,
|
|
1928
|
-
metadata: {
|
|
1929
|
-
...plan.metadata,
|
|
1930
|
-
[field]: value
|
|
1931
|
-
}
|
|
1932
|
-
},
|
|
1933
|
-
inverse: {
|
|
1934
|
-
type: "audio.setMetadata",
|
|
1935
|
-
field,
|
|
1936
|
-
value: plan.metadata[field]
|
|
1937
|
-
}
|
|
1938
|
-
};
|
|
1939
|
-
}
|
|
1940
|
-
default: return refuse("unknown-command", "unknown command");
|
|
1941
|
-
}
|
|
1942
|
-
}
|
|
1943
|
-
function createPlanReducer(sourceFrames) {
|
|
1944
|
-
return (plan, command) => {
|
|
1945
|
-
try {
|
|
1946
|
-
return apply(plan, command, sourceFrames);
|
|
1947
|
-
} catch (error) {
|
|
1948
|
-
if (!(error instanceof Invalid)) throw error;
|
|
1949
|
-
const type = isRecord(command) && typeof command.type === "string" ? command.type : "";
|
|
1950
|
-
const known = type.startsWith("audio.") && type.length <= 64;
|
|
1951
|
-
throw new AudioError("INVALID_COMMAND", `Invalid ${known ? type : "command"}: ${error.message}.`, { details: known ? {
|
|
1952
|
-
command: type,
|
|
1953
|
-
reason: error.reason
|
|
1954
|
-
} : { reason: error.reason } });
|
|
1955
|
-
}
|
|
1956
|
-
};
|
|
1957
|
-
}
|
|
1958
82
|
// src/core/format/version-1.ts
|
|
1959
83
|
var DOCUMENT_FIELDS$1 = [
|
|
1960
84
|
"format",
|
|
@@ -2319,786 +443,6 @@ function toAudioPlanDocument(plan, sources) {
|
|
|
2319
443
|
metadata: { ...plan.metadata }
|
|
2320
444
|
};
|
|
2321
445
|
}
|
|
2322
|
-
// src/core/model/peaks.ts
|
|
2323
|
-
function levelFor(peaks, framesPerColumn) {
|
|
2324
|
-
let chosen;
|
|
2325
|
-
for (const level of peaks.levels) if (level.framesPerBucket <= framesPerColumn) chosen = level;
|
|
2326
|
-
return chosen ?? peaks.levels[0];
|
|
2327
|
-
}
|
|
2328
|
-
function peakColumns(peaks, options) {
|
|
2329
|
-
const { from, to, columns } = options;
|
|
2330
|
-
if (!Number.isSafeInteger(from) || !Number.isSafeInteger(to) || from < 0 || to <= from) throw new RangeError("from and to are whole frames, with from at 0 or more and before to.");
|
|
2331
|
-
if (!Number.isSafeInteger(columns) || columns < 1) throw new RangeError("columns is a whole number from 1.");
|
|
2332
|
-
const min = new Float32Array(columns);
|
|
2333
|
-
const max = new Float32Array(columns);
|
|
2334
|
-
const framesPerColumn = (to - from) / columns;
|
|
2335
|
-
const level = levelFor(peaks, framesPerColumn);
|
|
2336
|
-
if (!level) return {
|
|
2337
|
-
min,
|
|
2338
|
-
max
|
|
2339
|
-
};
|
|
2340
|
-
const { framesPerBucket, channels } = level;
|
|
2341
|
-
const buckets = channels[0]?.min.length ?? 0;
|
|
2342
|
-
for (let column = 0; column < columns; column += 1) {
|
|
2343
|
-
const start = from + column * framesPerColumn;
|
|
2344
|
-
const end = from + (column + 1) * framesPerColumn;
|
|
2345
|
-
const first = Math.floor(start / framesPerBucket);
|
|
2346
|
-
const last = Math.min(buckets, Math.max(first + 1, Math.ceil(end / framesPerBucket)));
|
|
2347
|
-
let low = 0;
|
|
2348
|
-
let high = 0;
|
|
2349
|
-
for (const channel of channels) for (let bucket = first; bucket < last; bucket += 1) {
|
|
2350
|
-
low = Math.min(low, channel.min[bucket]);
|
|
2351
|
-
high = Math.max(high, channel.max[bucket]);
|
|
2352
|
-
}
|
|
2353
|
-
min[column] = low;
|
|
2354
|
-
max[column] = high;
|
|
2355
|
-
}
|
|
2356
|
-
return {
|
|
2357
|
-
min,
|
|
2358
|
-
max
|
|
2359
|
-
};
|
|
2360
|
-
}
|
|
2361
|
-
// src/core/model/source.ts
|
|
2362
|
-
var MAX_SOURCE_NAME_LENGTH = 128;
|
|
2363
|
-
// src/core/model/time.ts
|
|
2364
|
-
function framesToSeconds(frames, sampleRate) {
|
|
2365
|
-
return frames / sampleRate;
|
|
2366
|
-
}
|
|
2367
|
-
function secondsToFrames(seconds, sampleRate) {
|
|
2368
|
-
return Math.round(seconds * sampleRate);
|
|
2369
|
-
}
|
|
2370
|
-
function assertFrames(name, value) {
|
|
2371
|
-
if (!Number.isSafeInteger(value) || value < 0) throw new RangeError(`${name} must be a non-negative whole number of frames, not ${value}.`);
|
|
2372
|
-
}
|
|
2373
|
-
function formatTimecode(frames, sampleRate) {
|
|
2374
|
-
assertFrames("frames", frames);
|
|
2375
|
-
if (!Number.isSafeInteger(sampleRate) || sampleRate < 1) throw new RangeError(`The sample rate must be a positive whole number, not ${sampleRate}.`);
|
|
2376
|
-
const hundredths = Math.floor(frames * 100 / sampleRate);
|
|
2377
|
-
const two = (value) => String(value).padStart(2, "0");
|
|
2378
|
-
const fraction = two(hundredths % 100);
|
|
2379
|
-
const seconds = Math.floor(hundredths / 100) % 60;
|
|
2380
|
-
const minutes = Math.floor(hundredths / 6e3) % 60;
|
|
2381
|
-
const hours = Math.floor(hundredths / 36e4);
|
|
2382
|
-
return hours > 0 ? `${hours}:${two(minutes)}:${two(seconds)}.${fraction}` : `${minutes}:${two(seconds)}.${fraction}`;
|
|
2383
|
-
}
|
|
2384
|
-
// src/core/session/connect-output.ts
|
|
2385
|
-
function connectOutput(session, output) {
|
|
2386
|
-
let plan;
|
|
2387
|
-
let held = /* @__PURE__ */ new Set();
|
|
2388
|
-
function sync() {
|
|
2389
|
-
const snapshot = session.getSnapshot();
|
|
2390
|
-
const next = snapshot.plan;
|
|
2391
|
-
const ids = new Set(snapshot.sources.map((source) => source.id));
|
|
2392
|
-
if (next === plan) {
|
|
2393
|
-
held = ids;
|
|
2394
|
-
return;
|
|
2395
|
-
}
|
|
2396
|
-
const clips = next ? next.tracks.flatMap((track) => track.clips) : [];
|
|
2397
|
-
if ((next === void 0 || plan !== void 0 && !clips.some((clip) => held.has(clip.source))) && plan !== void 0) output.stop();
|
|
2398
|
-
plan = next;
|
|
2399
|
-
held = ids;
|
|
2400
|
-
if (!next) return;
|
|
2401
|
-
const sources = /* @__PURE__ */ new Map();
|
|
2402
|
-
for (const { source } of clips) {
|
|
2403
|
-
const audio = session.getAudio(source);
|
|
2404
|
-
if (audio) sources.set(source, audio);
|
|
2405
|
-
}
|
|
2406
|
-
output.load(next, sources);
|
|
2407
|
-
}
|
|
2408
|
-
sync();
|
|
2409
|
-
return session.subscribe(sync);
|
|
2410
|
-
}
|
|
2411
|
-
// src/core/session/source-name.ts
|
|
2412
|
-
var CONTROL = /\p{Cc}/gu;
|
|
2413
|
-
var BIDI = /[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/gu;
|
|
2414
|
-
var REFUSED = /[<>:"|?*]/gu;
|
|
2415
|
-
function cleanFileName(raw) {
|
|
2416
|
-
if (typeof raw !== "string") return void 0;
|
|
2417
|
-
let name = raw.replace(CONTROL, "").replace(BIDI, "");
|
|
2418
|
-
name = name.slice(Math.max(name.lastIndexOf("/"), name.lastIndexOf("\\")) + 1);
|
|
2419
|
-
name = name.replace(REFUSED, "").normalize("NFC");
|
|
2420
|
-
name = name.replace(/^[\s.]+/u, "").trimEnd();
|
|
2421
|
-
const points = Array.from(name);
|
|
2422
|
-
if (points.length > 128) name = points.slice(0, 128).join("").trimEnd();
|
|
2423
|
-
return name === "" ? void 0 : name;
|
|
2424
|
-
}
|
|
2425
|
-
// src/core/session/session.ts
|
|
2426
|
-
var BYTES_PER_SAMPLE = 4;
|
|
2427
|
-
function clipsOf(command) {
|
|
2428
|
-
switch (command.type) {
|
|
2429
|
-
case "audio.insertClips":
|
|
2430
|
-
case "audio.updateClips": return command.clips;
|
|
2431
|
-
case "audio.restoreClips": return command.entries.map((entry) => entry.clip);
|
|
2432
|
-
case "audio.replaceClips": return command.edits.flatMap((edit) => edit.clips);
|
|
2433
|
-
case "audio.restoreTracks": return command.entries.flatMap((entry) => entry.track.clips);
|
|
2434
|
-
default: return [];
|
|
2435
|
-
}
|
|
2436
|
-
}
|
|
2437
|
-
function coarseSeconds(seconds) {
|
|
2438
|
-
return seconds > 0 ? Number(seconds.toPrecision(2)) : 0;
|
|
2439
|
-
}
|
|
2440
|
-
function keysOf(command) {
|
|
2441
|
-
const keys = clipsOf(command).map((clip) => clip.key);
|
|
2442
|
-
if ("newKey" in command && typeof command.newKey === "string") keys.push(command.newKey);
|
|
2443
|
-
if ("newKeys" in command && command.newKeys) keys.push(...Object.values(command.newKeys));
|
|
2444
|
-
if (command.type === "audio.addTrack" && typeof command.key === "string") keys.push(command.key);
|
|
2445
|
-
if (command.type === "audio.restoreTracks") keys.push(...command.entries.map((entry) => entry.track.key));
|
|
2446
|
-
return keys;
|
|
2447
|
-
}
|
|
2448
|
-
var TIME_EDITS = /* @__PURE__ */ new Set([
|
|
2449
|
-
"audio.deleteRange",
|
|
2450
|
-
"audio.keepRange",
|
|
2451
|
-
"audio.insertTime"
|
|
2452
|
-
]);
|
|
2453
|
-
function equalData(a, b) {
|
|
2454
|
-
if (Object.is(a, b)) return true;
|
|
2455
|
-
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
|
2456
|
-
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
2457
|
-
const keys = Object.keys(a);
|
|
2458
|
-
if (keys.length !== Object.keys(b).length) return false;
|
|
2459
|
-
return keys.every((key) => Object.hasOwn(b, key) && equalData(a[key], b[key]));
|
|
2460
|
-
}
|
|
2461
|
-
function clipsIn(plan) {
|
|
2462
|
-
return plan ? plan.tracks.flatMap((track) => track.clips) : [];
|
|
2463
|
-
}
|
|
2464
|
-
function wholeClip(key, source, at) {
|
|
2465
|
-
return {
|
|
2466
|
-
key,
|
|
2467
|
-
at,
|
|
2468
|
-
source: source.id,
|
|
2469
|
-
start: 0,
|
|
2470
|
-
end: source.frames,
|
|
2471
|
-
gainDb: 0,
|
|
2472
|
-
fadeIn: NO_FADE,
|
|
2473
|
-
fadeOut: NO_FADE
|
|
2474
|
-
};
|
|
2475
|
-
}
|
|
2476
|
-
function createAudioSession(options) {
|
|
2477
|
-
const { engine } = options;
|
|
2478
|
-
const policy = audioLimits.resolve(options.filePolicy);
|
|
2479
|
-
const history = createHistory();
|
|
2480
|
-
const sources = /* @__PURE__ */ new Map();
|
|
2481
|
-
const reducer = createPlanReducer((id) => sources.get(id)?.info.frames);
|
|
2482
|
-
let plan;
|
|
2483
|
-
let status = "empty";
|
|
2484
|
-
let error;
|
|
2485
|
-
let busy = 0;
|
|
2486
|
-
let selection;
|
|
2487
|
-
let nextKey = 0;
|
|
2488
|
-
let nextSource = 0;
|
|
2489
|
-
const loading = /* @__PURE__ */ new Set();
|
|
2490
|
-
const cancelLoads = (message) => {
|
|
2491
|
-
for (const controller of loading) controller.abort(new AudioError("ABORTED", message));
|
|
2492
|
-
};
|
|
2493
|
-
let generation = 0;
|
|
2494
|
-
const superseded = (since) => since !== generation ? new AudioError("ABORTED", "The recording was closed or replaced.") : void 0;
|
|
2495
|
-
const heldBytes = () => {
|
|
2496
|
-
let total = 0;
|
|
2497
|
-
for (const source of sources.values()) total += source.bytes;
|
|
2498
|
-
return total;
|
|
2499
|
-
};
|
|
2500
|
-
function referenced(steps) {
|
|
2501
|
-
const ids = new Set(clipsIn(plan).map((clip) => clip.source));
|
|
2502
|
-
for (const { commands, inverses } of steps) for (const command of [...commands, ...inverses]) for (const clip of clipsOf(command)) ids.add(clip.source);
|
|
2503
|
-
return ids;
|
|
2504
|
-
}
|
|
2505
|
-
function collect() {
|
|
2506
|
-
const keep = referenced(history.steps);
|
|
2507
|
-
for (const id of sources.keys()) if (!keep.has(id)) sources.delete(id);
|
|
2508
|
-
}
|
|
2509
|
-
function stepsToDrop(extra) {
|
|
2510
|
-
const { maximum } = policy.limits.maxSessionSize;
|
|
2511
|
-
const order = history.dropOrder;
|
|
2512
|
-
for (let count = 0; count <= order.length; count += 1) {
|
|
2513
|
-
const kept = new Set(order.slice(count));
|
|
2514
|
-
const ids = referenced(history.steps.filter((entry) => kept.has(entry)));
|
|
2515
|
-
let total = extra;
|
|
2516
|
-
for (const id of ids) total += sources.get(id)?.bytes ?? 0;
|
|
2517
|
-
if (total <= maximum) return count;
|
|
2518
|
-
}
|
|
2519
|
-
}
|
|
2520
|
-
function sessionWarnings() {
|
|
2521
|
-
const found = [...sources.values()].flatMap((source) => source.warnings);
|
|
2522
|
-
const bytes = heldBytes();
|
|
2523
|
-
if (bytes > 0 && bytes >= policy.limits.maxSessionSize.warning) found.push({
|
|
2524
|
-
type: "limit",
|
|
2525
|
-
limit: "maxSessionSize",
|
|
2526
|
-
actual: bytes
|
|
2527
|
-
});
|
|
2528
|
-
const clips = clipsIn(plan).length;
|
|
2529
|
-
if (clips > 0 && clips >= policy.limits.maxClips.warning) found.push({
|
|
2530
|
-
type: "limit",
|
|
2531
|
-
limit: "maxClips",
|
|
2532
|
-
actual: clips
|
|
2533
|
-
});
|
|
2534
|
-
const tracks = plan?.tracks.length ?? 0;
|
|
2535
|
-
if (tracks > 0 && tracks >= policy.limits.maxTracks.warning) found.push({
|
|
2536
|
-
type: "limit",
|
|
2537
|
-
limit: "maxTracks",
|
|
2538
|
-
actual: tracks
|
|
2539
|
-
});
|
|
2540
|
-
return found;
|
|
2541
|
-
}
|
|
2542
|
-
function makeSnapshot() {
|
|
2543
|
-
return Object.freeze({
|
|
2544
|
-
status,
|
|
2545
|
-
plan,
|
|
2546
|
-
sources: Object.freeze([...sources.values()].map((source) => source.info)),
|
|
2547
|
-
canUndo: history.canUndo,
|
|
2548
|
-
canRedo: history.canRedo,
|
|
2549
|
-
historyTrimmed: history.trimmed,
|
|
2550
|
-
busy: busy > 0,
|
|
2551
|
-
error,
|
|
2552
|
-
warnings: Object.freeze(sessionWarnings()),
|
|
2553
|
-
sessionBytes: heldBytes(),
|
|
2554
|
-
selection
|
|
2555
|
-
});
|
|
2556
|
-
}
|
|
2557
|
-
const store = createStore(makeSnapshot());
|
|
2558
|
-
function changed() {
|
|
2559
|
-
if (selection) {
|
|
2560
|
-
const end = Math.min(selection.end, plan ? planLength(plan) : 0);
|
|
2561
|
-
const tracks = (plan?.tracks ?? []).map((track) => track.key).filter((key) => selection?.tracks.includes(key));
|
|
2562
|
-
if (selection.start >= end || tracks.length === 0) selection = void 0;
|
|
2563
|
-
else if (end !== selection.end || tracks.length !== selection.tracks.length) selection = Object.freeze({
|
|
2564
|
-
start: selection.start,
|
|
2565
|
-
end,
|
|
2566
|
-
tracks: Object.freeze(tracks)
|
|
2567
|
-
});
|
|
2568
|
-
}
|
|
2569
|
-
store.set(makeSnapshot());
|
|
2570
|
-
}
|
|
2571
|
-
function emit(event) {
|
|
2572
|
-
try {
|
|
2573
|
-
options.onEvent?.(event);
|
|
2574
|
-
} catch {}
|
|
2575
|
-
}
|
|
2576
|
-
function facts(source) {
|
|
2577
|
-
const { info } = source;
|
|
2578
|
-
return {
|
|
2579
|
-
durationSeconds: coarseSeconds(framesToSeconds(info.frames, info.sampleRate)),
|
|
2580
|
-
sampleRate: info.sampleRate,
|
|
2581
|
-
channels: info.channels,
|
|
2582
|
-
container: info.container,
|
|
2583
|
-
codec: info.codec,
|
|
2584
|
-
truncated: source.warnings.some((warning) => warning.type === "truncated")
|
|
2585
|
-
};
|
|
2586
|
-
}
|
|
2587
|
-
async function load(file, signal, onProgress, rate, givenName) {
|
|
2588
|
-
if (!(file instanceof Blob)) throw new TypeError("A recording must be a Blob or a File.");
|
|
2589
|
-
const name = cleanFileName(givenName ?? (typeof File === "function" && file instanceof File ? file.name : void 0));
|
|
2590
|
-
nextSource += 1;
|
|
2591
|
-
const id = `s${nextSource}`;
|
|
2592
|
-
const warnings = [];
|
|
2593
|
-
const warnAt = (limit, actual) => {
|
|
2594
|
-
if (policy.check(limit, actual) === "warning") warnings.push({
|
|
2595
|
-
type: "limit",
|
|
2596
|
-
limit,
|
|
2597
|
-
actual,
|
|
2598
|
-
source: id
|
|
2599
|
-
});
|
|
2600
|
-
};
|
|
2601
|
-
if (checkFile(policy, file) === "warning") warnings.push({
|
|
2602
|
-
type: "limit",
|
|
2603
|
-
limit: "maxFileSize",
|
|
2604
|
-
actual: file.size,
|
|
2605
|
-
source: id
|
|
2606
|
-
});
|
|
2607
|
-
const probe = await withProcessingTime(policy, (limited) => engine.probe(file, { signal: limited }), { signal });
|
|
2608
|
-
warnAt("maxChannels", probe.channels);
|
|
2609
|
-
warnAt("maxSampleRate", probe.sampleRate);
|
|
2610
|
-
warnAt("maxDuration", Math.ceil(probe.frameBound * 1e3 / probe.sampleRate));
|
|
2611
|
-
warnAt("maxDecodedSize", probe.frameBound * probe.channels * BYTES_PER_SAMPLE);
|
|
2612
|
-
const sampleRate = rate ?? probe.sampleRate;
|
|
2613
|
-
const converts = sampleRate !== probe.sampleRate;
|
|
2614
|
-
const frameBound = Math.ceil(probe.frameBound * sampleRate / probe.sampleRate);
|
|
2615
|
-
if (converts) warnAt("maxDecodedSize", frameBound * probe.channels * BYTES_PER_SAMPLE);
|
|
2616
|
-
const audio = await withProcessingTime(policy, (limited) => engine.decode(file, probe, {
|
|
2617
|
-
signal: limited,
|
|
2618
|
-
...onProgress ? { onProgress } : {},
|
|
2619
|
-
...converts ? { sampleRate } : {}
|
|
2620
|
-
}), { signal });
|
|
2621
|
-
const frames = Math.min(audio.length, frameBound);
|
|
2622
|
-
const channels = audio.numberOfChannels;
|
|
2623
|
-
if (audio.sampleRate !== sampleRate || channels < 1 || channels > probe.channels) throw new AudioError("INVALID_AUDIO", "The decoder's output doesn't match the probe.", { details: {
|
|
2624
|
-
container: probe.container,
|
|
2625
|
-
codec: probe.codec
|
|
2626
|
-
} });
|
|
2627
|
-
if (frames < 1) throw new AudioError("DECODE_FAILED", "The recording decoded to no audio.", { details: { codec: probe.codec } });
|
|
2628
|
-
const peaks = await withProcessingTime(policy, (limited) => engine.peaks(audio, { signal: limited }), { signal });
|
|
2629
|
-
throwIfAborted(signal);
|
|
2630
|
-
if (probe.declaredFrames !== void 0 && probe.declaredFrames > frames) warnings.push({
|
|
2631
|
-
type: "truncated",
|
|
2632
|
-
source: id,
|
|
2633
|
-
frames,
|
|
2634
|
-
declaredFrames: probe.declaredFrames
|
|
2635
|
-
});
|
|
2636
|
-
return {
|
|
2637
|
-
info: Object.freeze({
|
|
2638
|
-
id,
|
|
2639
|
-
sampleRate: audio.sampleRate,
|
|
2640
|
-
channels,
|
|
2641
|
-
frames,
|
|
2642
|
-
container: probe.container,
|
|
2643
|
-
codec: probe.codec,
|
|
2644
|
-
...converts ? { resampledFrom: probe.sampleRate } : {},
|
|
2645
|
-
...name === void 0 ? {} : { name }
|
|
2646
|
-
}),
|
|
2647
|
-
audio,
|
|
2648
|
-
peaks,
|
|
2649
|
-
bytes: frames * channels * BYTES_PER_SAMPLE,
|
|
2650
|
-
warnings: Object.freeze(warnings)
|
|
2651
|
-
};
|
|
2652
|
-
}
|
|
2653
|
-
async function track(signal, replaces, run) {
|
|
2654
|
-
if (replaces) cancelLoads("A newer recording replaced this one.");
|
|
2655
|
-
const controller = new AbortController();
|
|
2656
|
-
loading.add(controller);
|
|
2657
|
-
const combined = signal ? AbortSignal.any([signal, controller.signal]) : controller.signal;
|
|
2658
|
-
busy += 1;
|
|
2659
|
-
changed();
|
|
2660
|
-
try {
|
|
2661
|
-
return await run(combined);
|
|
2662
|
-
} finally {
|
|
2663
|
-
busy -= 1;
|
|
2664
|
-
loading.delete(controller);
|
|
2665
|
-
changed();
|
|
2666
|
-
}
|
|
2667
|
-
}
|
|
2668
|
-
function applyChecked(current, commands) {
|
|
2669
|
-
const result = applyCommands(reducer, current, commands);
|
|
2670
|
-
const next = result.state;
|
|
2671
|
-
const clips = clipsIn(next).length;
|
|
2672
|
-
if (clips > clipsIn(current).length) policy.check("maxClips", clips);
|
|
2673
|
-
if (next.tracks.length > current.tracks.length) policy.check("maxTracks", next.tracks.length);
|
|
2674
|
-
const length = planLength(next);
|
|
2675
|
-
if (length > planLength(current)) try {
|
|
2676
|
-
policy.check("maxDuration", Math.ceil(length * 1e3 / next.sampleRate));
|
|
2677
|
-
} catch (error) {
|
|
2678
|
-
if (!isAudioError(error, "DURATION_TOO_LONG")) throw error;
|
|
2679
|
-
throw new AudioError("DURATION_TOO_LONG", "The plan would be longer than maxDuration.", { details: {
|
|
2680
|
-
...error.details,
|
|
2681
|
-
reason: "plan-too-long"
|
|
2682
|
-
} });
|
|
2683
|
-
}
|
|
2684
|
-
return result;
|
|
2685
|
-
}
|
|
2686
|
-
function commit(result, commands) {
|
|
2687
|
-
plan = result.state;
|
|
2688
|
-
history.push({
|
|
2689
|
-
commands,
|
|
2690
|
-
inverses: result.inverses
|
|
2691
|
-
});
|
|
2692
|
-
collect();
|
|
2693
|
-
}
|
|
2694
|
-
function newKey(also = /* @__PURE__ */ new Set()) {
|
|
2695
|
-
const used = new Set(clipsIn(plan).map((clip) => clip.key));
|
|
2696
|
-
for (const key of also) used.add(key);
|
|
2697
|
-
for (const track of plan?.tracks ?? []) used.add(track.key);
|
|
2698
|
-
for (const { commands, inverses } of history.steps) for (const command of [...commands, ...inverses]) for (const key of keysOf(command)) used.add(key);
|
|
2699
|
-
let key;
|
|
2700
|
-
do {
|
|
2701
|
-
nextKey += 1;
|
|
2702
|
-
key = `c${nextKey}`;
|
|
2703
|
-
} while (used.has(key));
|
|
2704
|
-
return key;
|
|
2705
|
-
}
|
|
2706
|
-
function fillKeys(commands) {
|
|
2707
|
-
const taken = new Set(commands.flatMap(keysOf));
|
|
2708
|
-
const fresh = () => {
|
|
2709
|
-
const key = newKey(taken);
|
|
2710
|
-
taken.add(key);
|
|
2711
|
-
return key;
|
|
2712
|
-
};
|
|
2713
|
-
commands.forEach((command, index) => {
|
|
2714
|
-
if (command.type === "audio.splitClip" && command.newKey === void 0) commands[index] = {
|
|
2715
|
-
...command,
|
|
2716
|
-
newKey: fresh()
|
|
2717
|
-
};
|
|
2718
|
-
else if (command.type === "audio.addTrack" && command.key === void 0) commands[index] = {
|
|
2719
|
-
...command,
|
|
2720
|
-
key: fresh()
|
|
2721
|
-
};
|
|
2722
|
-
else if ((command.type === "audio.deleteRange" || command.type === "audio.insertTime") && Array.isArray(command.tracks)) {
|
|
2723
|
-
const given = command.newKeys ?? {};
|
|
2724
|
-
const missing = command.tracks.filter((track) => typeof track === "string" && !Object.hasOwn(given, track));
|
|
2725
|
-
if (missing.length === 0) return;
|
|
2726
|
-
const newKeys = { ...given };
|
|
2727
|
-
for (const track of missing) Object.defineProperty(newKeys, track, {
|
|
2728
|
-
value: fresh(),
|
|
2729
|
-
enumerable: true,
|
|
2730
|
-
writable: true,
|
|
2731
|
-
configurable: true
|
|
2732
|
-
});
|
|
2733
|
-
commands[index] = {
|
|
2734
|
-
...command,
|
|
2735
|
-
newKeys
|
|
2736
|
-
};
|
|
2737
|
-
}
|
|
2738
|
-
});
|
|
2739
|
-
}
|
|
2740
|
-
return {
|
|
2741
|
-
getSnapshot: store.getSnapshot,
|
|
2742
|
-
subscribe: store.subscribe,
|
|
2743
|
-
async open(file, callOptions = {}) {
|
|
2744
|
-
generation += 1;
|
|
2745
|
-
const started = generation;
|
|
2746
|
-
status = "opening";
|
|
2747
|
-
error = void 0;
|
|
2748
|
-
try {
|
|
2749
|
-
const source = await track(callOptions.signal, true, (signal) => load(file, signal, callOptions.onProgress, void 0, callOptions.name));
|
|
2750
|
-
const replaced = superseded(started);
|
|
2751
|
-
if (replaced) throw replaced;
|
|
2752
|
-
policy.check("maxSessionSize", source.bytes);
|
|
2753
|
-
sources.clear();
|
|
2754
|
-
history.clear();
|
|
2755
|
-
sources.set(source.info.id, source);
|
|
2756
|
-
plan = void 0;
|
|
2757
|
-
const first = {
|
|
2758
|
-
key: newKey(),
|
|
2759
|
-
name: "",
|
|
2760
|
-
gainDb: 0,
|
|
2761
|
-
muted: false,
|
|
2762
|
-
soloed: false,
|
|
2763
|
-
clips: [wholeClip(newKey(), source.info, 0)]
|
|
2764
|
-
};
|
|
2765
|
-
plan = {
|
|
2766
|
-
sampleRate: source.info.sampleRate,
|
|
2767
|
-
channels: source.info.channels,
|
|
2768
|
-
gainDb: 0,
|
|
2769
|
-
tracks: [first],
|
|
2770
|
-
metadata: EMPTY_METADATA
|
|
2771
|
-
};
|
|
2772
|
-
selection = void 0;
|
|
2773
|
-
status = "ready";
|
|
2774
|
-
changed();
|
|
2775
|
-
emit({
|
|
2776
|
-
type: "recording_opened",
|
|
2777
|
-
...facts(source)
|
|
2778
|
-
});
|
|
2779
|
-
} catch (reason) {
|
|
2780
|
-
const failure = asAudioError(reason, "The recording couldn't be opened.");
|
|
2781
|
-
if (started !== generation) throw failure;
|
|
2782
|
-
status = plan ? "ready" : "error";
|
|
2783
|
-
error = failure;
|
|
2784
|
-
changed();
|
|
2785
|
-
emit({
|
|
2786
|
-
type: "failed",
|
|
2787
|
-
operation: "open",
|
|
2788
|
-
code: failure.code
|
|
2789
|
-
});
|
|
2790
|
-
throw failure;
|
|
2791
|
-
}
|
|
2792
|
-
},
|
|
2793
|
-
async insert(file, insertOptions = {}) {
|
|
2794
|
-
if (!plan || status !== "ready") throw new AudioError("INVALID_COMMAND", "Open a recording before inserting another.", { details: { reason: "no-recording" } });
|
|
2795
|
-
const started = generation;
|
|
2796
|
-
error = void 0;
|
|
2797
|
-
try {
|
|
2798
|
-
const source = await track(insertOptions.signal, false, (signal) => {
|
|
2799
|
-
policy.check("maxFiles", sources.size + 1);
|
|
2800
|
-
return load(file, signal, insertOptions.onProgress, plan?.sampleRate, insertOptions.name);
|
|
2801
|
-
});
|
|
2802
|
-
const replaced = superseded(started);
|
|
2803
|
-
if (replaced) throw replaced;
|
|
2804
|
-
const current = plan;
|
|
2805
|
-
const drop = stepsToDrop(source.bytes);
|
|
2806
|
-
if (drop === void 0) throw policy.error("maxSessionSize", heldBytes() + source.bytes);
|
|
2807
|
-
const clip = wholeClip(newKey(), source.info, insertOptions.at ?? 0);
|
|
2808
|
-
const commands = [];
|
|
2809
|
-
if (source.info.channels > current.channels) commands.push({
|
|
2810
|
-
type: "audio.setChannels",
|
|
2811
|
-
channels: source.info.channels
|
|
2812
|
-
});
|
|
2813
|
-
let target = insertOptions.track;
|
|
2814
|
-
if (target === void 0) {
|
|
2815
|
-
target = newKey();
|
|
2816
|
-
commands.push({
|
|
2817
|
-
type: "audio.addTrack",
|
|
2818
|
-
key: target,
|
|
2819
|
-
index: current.tracks.length
|
|
2820
|
-
});
|
|
2821
|
-
}
|
|
2822
|
-
commands.push({
|
|
2823
|
-
type: "audio.insertClips",
|
|
2824
|
-
track: target,
|
|
2825
|
-
clips: [clip]
|
|
2826
|
-
});
|
|
2827
|
-
sources.set(source.info.id, source);
|
|
2828
|
-
let result;
|
|
2829
|
-
try {
|
|
2830
|
-
result = applyChecked(current, commands);
|
|
2831
|
-
} catch (reason) {
|
|
2832
|
-
sources.delete(source.info.id);
|
|
2833
|
-
throw reason;
|
|
2834
|
-
}
|
|
2835
|
-
history.drop(drop);
|
|
2836
|
-
commit(result, commands);
|
|
2837
|
-
changed();
|
|
2838
|
-
emit({
|
|
2839
|
-
type: "recording_inserted",
|
|
2840
|
-
...facts(source)
|
|
2841
|
-
});
|
|
2842
|
-
} catch (reason) {
|
|
2843
|
-
const failure = asAudioError(reason, "The recording couldn't be inserted.");
|
|
2844
|
-
if (started !== generation) throw failure;
|
|
2845
|
-
error = failure;
|
|
2846
|
-
changed();
|
|
2847
|
-
emit({
|
|
2848
|
-
type: "failed",
|
|
2849
|
-
operation: "insert",
|
|
2850
|
-
code: failure.code
|
|
2851
|
-
});
|
|
2852
|
-
throw failure;
|
|
2853
|
-
}
|
|
2854
|
-
},
|
|
2855
|
-
execute(commands) {
|
|
2856
|
-
const list = Array.isArray(commands) ? [...commands] : [commands];
|
|
2857
|
-
if (list.length === 0) return;
|
|
2858
|
-
try {
|
|
2859
|
-
if (!plan) throw new AudioError("INVALID_COMMAND", "Open a recording before editing it.", { details: { reason: "no-recording" } });
|
|
2860
|
-
let copies;
|
|
2861
|
-
try {
|
|
2862
|
-
copies = structuredClone(list);
|
|
2863
|
-
} catch (reason) {
|
|
2864
|
-
throw new AudioError("INVALID_COMMAND", "Commands must be plain data.", {
|
|
2865
|
-
cause: reason,
|
|
2866
|
-
details: { reason: "not-plain-data" }
|
|
2867
|
-
});
|
|
2868
|
-
}
|
|
2869
|
-
fillKeys(copies);
|
|
2870
|
-
const result = applyChecked(plan, copies);
|
|
2871
|
-
if (equalData(result.state, plan)) return;
|
|
2872
|
-
commit(result, copies);
|
|
2873
|
-
if (copies.some((command) => TIME_EDITS.has(command.type))) selection = void 0;
|
|
2874
|
-
} catch (reason) {
|
|
2875
|
-
const failure = asAudioError(reason);
|
|
2876
|
-
emit({
|
|
2877
|
-
type: "failed",
|
|
2878
|
-
operation: "command",
|
|
2879
|
-
code: failure.code
|
|
2880
|
-
});
|
|
2881
|
-
throw failure;
|
|
2882
|
-
}
|
|
2883
|
-
changed();
|
|
2884
|
-
emit({
|
|
2885
|
-
type: "command",
|
|
2886
|
-
commands: list.map((command) => command.type)
|
|
2887
|
-
});
|
|
2888
|
-
},
|
|
2889
|
-
group: (fn) => history.group(fn),
|
|
2890
|
-
createKey: newKey,
|
|
2891
|
-
undo() {
|
|
2892
|
-
const current = plan;
|
|
2893
|
-
const entry = current && history.undo();
|
|
2894
|
-
if (!current || !entry) return;
|
|
2895
|
-
try {
|
|
2896
|
-
plan = applyCommands(reducer, current, entry.inverses).state;
|
|
2897
|
-
} catch (reason) {
|
|
2898
|
-
history.restore("undo");
|
|
2899
|
-
throw reason;
|
|
2900
|
-
}
|
|
2901
|
-
changed();
|
|
2902
|
-
emit({ type: "undo" });
|
|
2903
|
-
},
|
|
2904
|
-
redo() {
|
|
2905
|
-
const current = plan;
|
|
2906
|
-
const entry = current && history.redo();
|
|
2907
|
-
if (!current || !entry) return;
|
|
2908
|
-
try {
|
|
2909
|
-
plan = applyCommands(reducer, current, entry.commands).state;
|
|
2910
|
-
} catch (reason) {
|
|
2911
|
-
history.restore("redo");
|
|
2912
|
-
throw reason;
|
|
2913
|
-
}
|
|
2914
|
-
changed();
|
|
2915
|
-
emit({ type: "redo" });
|
|
2916
|
-
},
|
|
2917
|
-
select(range) {
|
|
2918
|
-
let next;
|
|
2919
|
-
if (range) {
|
|
2920
|
-
const { start, end } = range;
|
|
2921
|
-
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end)) throw new RangeError("A selection is a range of whole frames.");
|
|
2922
|
-
const length = plan ? planLength(plan) : 0;
|
|
2923
|
-
const from = Math.max(0, Math.min(start, end, length));
|
|
2924
|
-
const to = Math.min(length, Math.max(start, end, 0));
|
|
2925
|
-
const tracks = (plan?.tracks ?? []).map((track) => track.key).filter((key) => range.tracks === void 0 || range.tracks.includes(key));
|
|
2926
|
-
if (from < to && tracks.length > 0) next = Object.freeze({
|
|
2927
|
-
start: from,
|
|
2928
|
-
end: to,
|
|
2929
|
-
tracks: Object.freeze(tracks)
|
|
2930
|
-
});
|
|
2931
|
-
}
|
|
2932
|
-
if (next?.start === selection?.start && next?.end === selection?.end && next?.tracks.join("\n") === selection?.tracks.join("\n")) return;
|
|
2933
|
-
selection = next;
|
|
2934
|
-
changed();
|
|
2935
|
-
},
|
|
2936
|
-
getPeaks: (id) => sources.get(id)?.peaks,
|
|
2937
|
-
getAudio: (id) => sources.get(id)?.audio,
|
|
2938
|
-
async export(exportOptions = {}) {
|
|
2939
|
-
const current = plan;
|
|
2940
|
-
const format = exportOptions.format ?? { container: "wav" };
|
|
2941
|
-
try {
|
|
2942
|
-
if (!current) throw new AudioError("INVALID_COMMAND", "Open a recording before exporting it.", { details: { reason: "no-recording" } });
|
|
2943
|
-
const clips = clipsIn(current);
|
|
2944
|
-
if (clips.length === 0) throw new AudioError("INVALID_COMMAND", "The recording has no clips to export.", { details: { reason: "empty-plan" } });
|
|
2945
|
-
if (mixLength(current) === 0) throw new AudioError("INVALID_COMMAND", "No track with clips is audible.", { details: { reason: "nothing-audible" } });
|
|
2946
|
-
const { processor } = options;
|
|
2947
|
-
if (!processor) throw new TypeError("Exporting needs a processor in createAudioSession.");
|
|
2948
|
-
const used = /* @__PURE__ */ new Map();
|
|
2949
|
-
for (const { source } of clips) {
|
|
2950
|
-
const held = sources.get(source);
|
|
2951
|
-
if (held) used.set(source, held.audio);
|
|
2952
|
-
}
|
|
2953
|
-
busy += 1;
|
|
2954
|
-
changed();
|
|
2955
|
-
try {
|
|
2956
|
-
const output = await withProcessingTime(policy, (signal) => processor.render({
|
|
2957
|
-
plan: current,
|
|
2958
|
-
sources: used,
|
|
2959
|
-
format
|
|
2960
|
-
}, {
|
|
2961
|
-
signal,
|
|
2962
|
-
...exportOptions.onProgress ? { onProgress: exportOptions.onProgress } : {}
|
|
2963
|
-
}), exportOptions.signal ? { signal: exportOptions.signal } : {});
|
|
2964
|
-
emit({
|
|
2965
|
-
type: "recording_exported",
|
|
2966
|
-
durationSeconds: coarseSeconds(framesToSeconds(mixLength(current), current.sampleRate)),
|
|
2967
|
-
sampleRate: current.sampleRate,
|
|
2968
|
-
channels: current.channels,
|
|
2969
|
-
sampleFormat: format.sampleFormat ?? "pcm16"
|
|
2970
|
-
});
|
|
2971
|
-
return output;
|
|
2972
|
-
} finally {
|
|
2973
|
-
busy -= 1;
|
|
2974
|
-
changed();
|
|
2975
|
-
}
|
|
2976
|
-
} catch (reason) {
|
|
2977
|
-
if (reason instanceof TypeError || reason instanceof RangeError) throw reason;
|
|
2978
|
-
const failure = exportOptions.signal?.aborted ? abortError(exportOptions.signal) : asAudioError(reason, "The recording couldn't be exported.");
|
|
2979
|
-
emit({
|
|
2980
|
-
type: "failed",
|
|
2981
|
-
operation: "export",
|
|
2982
|
-
code: failure.code
|
|
2983
|
-
});
|
|
2984
|
-
throw failure;
|
|
2985
|
-
}
|
|
2986
|
-
},
|
|
2987
|
-
async close() {
|
|
2988
|
-
generation += 1;
|
|
2989
|
-
cancelLoads("The recording was closed.");
|
|
2990
|
-
const hadRecording = plan !== void 0;
|
|
2991
|
-
sources.clear();
|
|
2992
|
-
history.clear();
|
|
2993
|
-
plan = void 0;
|
|
2994
|
-
selection = void 0;
|
|
2995
|
-
error = void 0;
|
|
2996
|
-
status = "empty";
|
|
2997
|
-
changed();
|
|
2998
|
-
if (hadRecording) emit({ type: "recording_closed" });
|
|
2999
|
-
}
|
|
3000
|
-
};
|
|
3001
|
-
}
|
|
3002
|
-
// src/core/session/watch-playhead.ts
|
|
3003
|
-
function pageFrames() {
|
|
3004
|
-
const page = globalThis;
|
|
3005
|
-
if (page.requestAnimationFrame && page.cancelAnimationFrame) {
|
|
3006
|
-
const { requestAnimationFrame: request, cancelAnimationFrame: cancel } = page;
|
|
3007
|
-
return {
|
|
3008
|
-
request: (callback) => request(callback),
|
|
3009
|
-
cancel: (handle) => cancel(handle)
|
|
3010
|
-
};
|
|
3011
|
-
}
|
|
3012
|
-
return {
|
|
3013
|
-
request: (callback) => setTimeout(callback, 16),
|
|
3014
|
-
cancel: (handle) => clearTimeout(handle)
|
|
3015
|
-
};
|
|
3016
|
-
}
|
|
3017
|
-
function watchPlayhead(output, listener, frames = pageFrames()) {
|
|
3018
|
-
let last;
|
|
3019
|
-
let pending;
|
|
3020
|
-
let stopped = false;
|
|
3021
|
-
const report = () => {
|
|
3022
|
-
const frame = output.position();
|
|
3023
|
-
if (frame === last) return;
|
|
3024
|
-
last = frame;
|
|
3025
|
-
listener(frame);
|
|
3026
|
-
};
|
|
3027
|
-
const tick = () => {
|
|
3028
|
-
pending = void 0;
|
|
3029
|
-
if (stopped) return;
|
|
3030
|
-
report();
|
|
3031
|
-
if (output.getSnapshot().state === "playing") pending = frames.request(tick);
|
|
3032
|
-
};
|
|
3033
|
-
const changed = () => {
|
|
3034
|
-
if (stopped) return;
|
|
3035
|
-
report();
|
|
3036
|
-
if (output.getSnapshot().state === "playing") pending ??= frames.request(tick);
|
|
3037
|
-
else if (pending !== void 0) {
|
|
3038
|
-
frames.cancel(pending);
|
|
3039
|
-
pending = void 0;
|
|
3040
|
-
}
|
|
3041
|
-
};
|
|
3042
|
-
const unsubscribe = output.subscribe(changed);
|
|
3043
|
-
changed();
|
|
3044
|
-
return () => {
|
|
3045
|
-
stopped = true;
|
|
3046
|
-
unsubscribe();
|
|
3047
|
-
if (pending !== void 0) frames.cancel(pending);
|
|
3048
|
-
pending = void 0;
|
|
3049
|
-
};
|
|
3050
|
-
}
|
|
3051
|
-
// src/messages/en.ts
|
|
3052
|
-
var audioMessages = /* @__PURE__ */ defineMessages({
|
|
3053
|
-
...sdkCoreMessages,
|
|
3054
|
-
"errors.INVALID_AUDIO": "This file isn't valid audio, or it's too damaged to open.",
|
|
3055
|
-
"errors.UNSUPPORTED_FORMAT": "This audio format isn't supported. Try converting the file to WAV, then open it again.",
|
|
3056
|
-
"errors.DECODE_FAILED": "This browser can't play this kind of audio. Try another browser, or convert the file to WAV.",
|
|
3057
|
-
"errors.INVALID_COMMAND": "This edit couldn't be made. Please try again.",
|
|
3058
|
-
"errors.INVALID_DOCUMENT": "This project is damaged, or it isn't an audio project.",
|
|
3059
|
-
"errors.DURATION_TOO_LONG": "This recording is {actual, duration} long, which is over the {maximum, duration} limit.",
|
|
3060
|
-
"errors.TOO_MANY_CHANNELS": "This recording has {actual, number} channels, which is over the {maximum, number}-channel limit.",
|
|
3061
|
-
"errors.SAMPLE_RATE_TOO_HIGH": "This recording's sample rate is {actual, number} Hz, which is over the {maximum, number} Hz limit.",
|
|
3062
|
-
"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.",
|
|
3063
|
-
"errors.TOO_MANY_CLIPS": /* @__PURE__ */ plural("maximum", {
|
|
3064
|
-
one: "A recording can have {maximum} clip. Join or delete clips, and try again.",
|
|
3065
|
-
other: "A recording can have up to {maximum} clips. Join or delete clips, and try again."
|
|
3066
|
-
}),
|
|
3067
|
-
"errors.TOO_MANY_TRACKS": /* @__PURE__ */ plural("maximum", {
|
|
3068
|
-
one: "A recording can have {maximum} track. Remove a track, and try again.",
|
|
3069
|
-
other: "A recording can have up to {maximum} tracks. Remove a track, and try again."
|
|
3070
|
-
}),
|
|
3071
|
-
"warnings.maxDuration": "This recording is {actual, duration} long, and may be slow to open and edit.",
|
|
3072
|
-
"warnings.maxChannels": "This recording has {actual, number} channels, and may be slow to edit.",
|
|
3073
|
-
"warnings.maxSampleRate": "This recording's sample rate is {actual, number} Hz, and it may be slow to edit.",
|
|
3074
|
-
"warnings.maxSessionSize": "The open recordings use {actual, fileSize} of memory. Clearing the undo history frees memory.",
|
|
3075
|
-
"warnings.maxClips": /* @__PURE__ */ plural("actual", {
|
|
3076
|
-
one: "This recording has {actual} clip. Many clips may be slow to play and export.",
|
|
3077
|
-
other: "This recording has {actual} clips. Many clips may be slow to play and export."
|
|
3078
|
-
}),
|
|
3079
|
-
"warnings.maxTracks": /* @__PURE__ */ plural("actual", {
|
|
3080
|
-
one: "This recording has {actual} track. Many tracks may be slow to play and export.",
|
|
3081
|
-
other: "This recording has {actual} tracks. Many tracks may be slow to play and export."
|
|
3082
|
-
}),
|
|
3083
|
-
"errors.UNSUPPORTED_ENVIRONMENT.autoplay-blocked": "The browser didn't let sound start. Press Play to start it.",
|
|
3084
|
-
"errors.DURATION_TOO_LONG.plan-too-long": "This would make the recording {actual, duration} long, which is over the {maximum, duration} limit.",
|
|
3085
|
-
"warnings.truncated": "This recording looks incomplete, perhaps from an interrupted recording or download. It opened with the audio it holds."
|
|
3086
|
-
});
|
|
3087
|
-
// src/messages/text.ts
|
|
3088
|
-
function audioErrorText(messages, error) {
|
|
3089
|
-
if (isSdkError(error)) {
|
|
3090
|
-
const details = error.details ?? {};
|
|
3091
|
-
if (typeof details.reason === "string") {
|
|
3092
|
-
const key = `errors.${error.code}.${details.reason}`;
|
|
3093
|
-
if (Object.hasOwn(audioMessages, key)) return messages.t(key, details);
|
|
3094
|
-
}
|
|
3095
|
-
}
|
|
3096
|
-
return messages.error(error);
|
|
3097
|
-
}
|
|
3098
|
-
function audioWarningText(messages, warning) {
|
|
3099
|
-
if (warning.type === "truncated") return messages.t("warnings.truncated");
|
|
3100
|
-
return messages.t(`warnings.${warning.limit}`, { actual: warning.actual });
|
|
3101
|
-
}
|
|
3102
446
|
export { AUDIO_PLAN_FORMAT, AUDIO_PLAN_VERSION, AudioError, EMPTY_METADATA, MAX_GAIN_DB, MAX_METADATA_LENGTH, MAX_SOURCE_NAME_LENGTH, MAX_TRACK_NAME_LENGTH, MIN_GAIN_DB, NO_FADE, audibleTracks, audioErrorText, audioLimitDefinitions, audioLimits, audioMessages, audioWarningText, clipAt, clipEnd, clipLength, connectOutput, createAudioAdapters, createAudioEngine, createAudioOutput, createAudioProcessor, createAudioSession, dbToGain, formatTimecode, framesToSeconds, gainAt, isAudioError, migrateAudioCommand, migratePlan, mixLength, peakColumns, planLength, readAudioPlanDocument, secondsToFrames, toAudioPlanDocument, trackLength, watchPlayhead };
|
|
3103
447
|
|
|
3104
448
|
//# sourceMappingURL=index.js.map
|