@hussmartinez/audio-sdk 0.2.1 → 0.3.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/index.d.ts +85 -9
- package/dist/index.js +250 -52
- package/dist/index.js.map +1 -1
- package/dist/lazy/inline-worker-DVsxT7FQ.js +30 -0
- package/dist/lazy/{inline-worker-4rk87LEt.js.map → inline-worker-DVsxT7FQ.js.map} +1 -1
- package/package.json +1 -1
- package/dist/lazy/inline-worker-4rk87LEt.js +0 -30
package/dist/index.js
CHANGED
|
@@ -11,6 +11,18 @@ var { adapt: asAudioError, abortError, throwIfAborted } = /* @__PURE__ */ create
|
|
|
11
11
|
fallbackCode: "UNKNOWN",
|
|
12
12
|
fallbackMessage: "Audio processing failed."
|
|
13
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-DVsxT7FQ.js")).createInlineWorker,
|
|
22
|
+
version: "0.3.0",
|
|
23
|
+
...selfHosted ? { asset: selfHosted } : {}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
14
26
|
var LEVEL_FACTOR = 4;
|
|
15
27
|
function buildPyramid(finest, bucket = 256) {
|
|
16
28
|
const levels = [];
|
|
@@ -120,14 +132,8 @@ async function decodeOnPage(file, probe, decoder, createBuffer, signal) {
|
|
|
120
132
|
// src/adapters/audio-engine.ts
|
|
121
133
|
var BLOCK_FRAMES = 65536;
|
|
122
134
|
function createAudioEngine(options = {}) {
|
|
123
|
-
const selfHosted = options.assetsUrl === void 0 ? void 0 : new URL("audio/audio.worker.js", resolveAssetsBase(options.assetsUrl)).href;
|
|
124
135
|
return createEngine({
|
|
125
|
-
|
|
126
|
-
type: "module",
|
|
127
|
-
name: "hm-audio"
|
|
128
|
-
}) : async () => (await import("./lazy/inline-worker-4rk87LEt.js")).createInlineWorker,
|
|
129
|
-
version: "0.2.1",
|
|
130
|
-
...selfHosted ? { asset: selfHosted } : {},
|
|
136
|
+
...audioWorkerSource(options.assetsUrl),
|
|
131
137
|
createBuffer: (bufferOptions) => new AudioBuffer(bufferOptions),
|
|
132
138
|
decodePlatform: webAudioDecoder,
|
|
133
139
|
...options.idleTimeout === void 0 ? {} : { idleTimeout: options.idleTimeout }
|
|
@@ -285,6 +291,180 @@ function createEngine(environment) {
|
|
|
285
291
|
}
|
|
286
292
|
};
|
|
287
293
|
}
|
|
294
|
+
// src/core/model/plan.ts
|
|
295
|
+
var EMPTY_METADATA = Object.freeze({
|
|
296
|
+
title: "",
|
|
297
|
+
artist: "",
|
|
298
|
+
album: "",
|
|
299
|
+
year: "",
|
|
300
|
+
comment: ""
|
|
301
|
+
});
|
|
302
|
+
var NO_FADE = Object.freeze({
|
|
303
|
+
frames: 0,
|
|
304
|
+
curve: "linear"
|
|
305
|
+
});
|
|
306
|
+
function clipLength(clip) {
|
|
307
|
+
return clip.end - clip.start;
|
|
308
|
+
}
|
|
309
|
+
function planLength(plan) {
|
|
310
|
+
let total = 0;
|
|
311
|
+
for (const clip of plan.clips) total += clipLength(clip);
|
|
312
|
+
return total;
|
|
313
|
+
}
|
|
314
|
+
function clipOffsets(plan) {
|
|
315
|
+
return clipBoundaries(plan).slice(0, -1);
|
|
316
|
+
}
|
|
317
|
+
function clipBoundaries(plan) {
|
|
318
|
+
return sequenceOffsets(plan.clips.map(clipLength));
|
|
319
|
+
}
|
|
320
|
+
var MIN_GAIN_DB = -120;
|
|
321
|
+
var MAX_GAIN_DB = 120;
|
|
322
|
+
var isFrames = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
323
|
+
function clipProblem(clip, sourceFrames) {
|
|
324
|
+
if (![
|
|
325
|
+
clip.start,
|
|
326
|
+
clip.end,
|
|
327
|
+
clip.fadeIn.frames,
|
|
328
|
+
clip.fadeOut.frames
|
|
329
|
+
].every(isFrames)) return "invalid-frames";
|
|
330
|
+
if (clip.end <= clip.start) return "clip-too-short";
|
|
331
|
+
if (clip.end > sourceFrames) return "clip-outside-source";
|
|
332
|
+
if (typeof clip.gainDb !== "number" || !(clip.gainDb >= -120 && clip.gainDb <= 120)) return "gain-out-of-range";
|
|
333
|
+
if (clip.fadeIn.frames + clip.fadeOut.frames > clipLength(clip)) return "fades-too-long";
|
|
334
|
+
}
|
|
335
|
+
function assertClip(clip, sourceFrames) {
|
|
336
|
+
const problem = clipProblem(clip, sourceFrames);
|
|
337
|
+
if (problem) throw new RangeError(`Clip ${clip.key} isn't valid: ${problem}.`);
|
|
338
|
+
}
|
|
339
|
+
// src/adapters/wav/wav-writer.ts
|
|
340
|
+
var BYTES = {
|
|
341
|
+
pcm16: 2,
|
|
342
|
+
pcm24: 3,
|
|
343
|
+
float32: 4
|
|
344
|
+
};
|
|
345
|
+
function dataSize(spec) {
|
|
346
|
+
return spec.frames * spec.channels * BYTES[spec.sampleFormat];
|
|
347
|
+
}
|
|
348
|
+
// src/adapters/audio-processor.ts
|
|
349
|
+
var EXPORT_BLOCK_FRAMES = 65536;
|
|
350
|
+
var SAMPLE_FORMATS = /* @__PURE__ */ new Set([
|
|
351
|
+
"pcm16",
|
|
352
|
+
"pcm24",
|
|
353
|
+
"float32"
|
|
354
|
+
]);
|
|
355
|
+
function createAudioProcessor(options = {}) {
|
|
356
|
+
return createProcessor({
|
|
357
|
+
...audioWorkerSource(options.assetsUrl),
|
|
358
|
+
...options.idleTimeout === void 0 ? {} : { idleTimeout: options.idleTimeout }
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
function check(job) {
|
|
362
|
+
const { plan, sources, format } = job;
|
|
363
|
+
if (format.container !== "wav") throw new TypeError(`Can't export to ${String(format.container)}.`);
|
|
364
|
+
const sampleFormat = format.sampleFormat ?? "pcm16";
|
|
365
|
+
if (!SAMPLE_FORMATS.has(sampleFormat)) throw new TypeError(`Unknown sample format ${String(sampleFormat)}.`);
|
|
366
|
+
if (!Number.isSafeInteger(plan.sampleRate) || plan.sampleRate < 1) throw new RangeError("The plan's sample rate must be a whole number above 0.");
|
|
367
|
+
if (!Number.isSafeInteger(plan.channels) || plan.channels < 1) throw new RangeError("The plan's channel count must be a whole number above 0.");
|
|
368
|
+
for (const clip of plan.clips) {
|
|
369
|
+
const source = sources.get(clip.source);
|
|
370
|
+
if (!source) throw new TypeError(`Clip ${clip.key}'s source ${clip.source} isn't in the job.`);
|
|
371
|
+
assertClip(clip, source.length);
|
|
372
|
+
}
|
|
373
|
+
return sampleFormat;
|
|
374
|
+
}
|
|
375
|
+
function createProcessor(environment) {
|
|
376
|
+
let startWorker;
|
|
377
|
+
let client;
|
|
378
|
+
let destroyed = false;
|
|
379
|
+
async function ready() {
|
|
380
|
+
if (destroyed) throw new Error("The audio processor has been destroyed.");
|
|
381
|
+
startWorker ??= await environment.loadWorker();
|
|
382
|
+
client ??= createWorkerClient({
|
|
383
|
+
createWorker: () => {
|
|
384
|
+
if (!startWorker) throw new AudioError("WORKER_START_FAILED", "The audio worker hasn't loaded.");
|
|
385
|
+
return startWorker();
|
|
386
|
+
},
|
|
387
|
+
errorClasses: { AudioError },
|
|
388
|
+
packageVersion: environment.version,
|
|
389
|
+
...environment.asset === void 0 ? {} : { workerAsset: environment.asset },
|
|
390
|
+
...environment.idleTimeout === void 0 ? {} : { idleTimeout: environment.idleTimeout }
|
|
391
|
+
});
|
|
392
|
+
return client;
|
|
393
|
+
}
|
|
394
|
+
async function render(job, signal, onProgress) {
|
|
395
|
+
const { plan, sources } = job;
|
|
396
|
+
const sampleFormat = check(job);
|
|
397
|
+
throwIfAborted(signal);
|
|
398
|
+
const worker = await ready();
|
|
399
|
+
const call = signal ? { signal } : {};
|
|
400
|
+
const frames = planLength(plan);
|
|
401
|
+
const spec = {
|
|
402
|
+
sampleRate: plan.sampleRate,
|
|
403
|
+
channels: plan.channels,
|
|
404
|
+
frames,
|
|
405
|
+
sampleFormat
|
|
406
|
+
};
|
|
407
|
+
const parts = [await worker.call("wavHeader", {
|
|
408
|
+
...spec,
|
|
409
|
+
metadata: { ...plan.metadata }
|
|
410
|
+
}, call)];
|
|
411
|
+
const offsets = clipOffsets(plan);
|
|
412
|
+
let first = 0;
|
|
413
|
+
for (let start = 0; start < frames; start += EXPORT_BLOCK_FRAMES) {
|
|
414
|
+
throwIfAborted(signal);
|
|
415
|
+
const length = Math.min(EXPORT_BLOCK_FRAMES, frames - start);
|
|
416
|
+
const end = start + length;
|
|
417
|
+
const segments = [];
|
|
418
|
+
while (first < plan.clips.length) {
|
|
419
|
+
const clip = plan.clips[first];
|
|
420
|
+
if (!clip || offsets[first] + clipLength(clip) > start) break;
|
|
421
|
+
first += 1;
|
|
422
|
+
}
|
|
423
|
+
for (let index = first; index < plan.clips.length; index += 1) {
|
|
424
|
+
const clip = plan.clips[index];
|
|
425
|
+
const offset = offsets[index];
|
|
426
|
+
if (!clip || offset >= end) break;
|
|
427
|
+
const from = Math.max(start, offset);
|
|
428
|
+
const to = Math.min(end, offset + clipLength(clip));
|
|
429
|
+
const within = from - offset;
|
|
430
|
+
const source = sources.get(clip.source);
|
|
431
|
+
if (!source) throw new TypeError(`Clip ${clip.key}'s source isn't in the job.`);
|
|
432
|
+
const samples = Array.from({ length: source.numberOfChannels }, (_, channel) => source.getChannelData(channel).slice(clip.start + within, clip.start + within + (to - from)));
|
|
433
|
+
segments.push({
|
|
434
|
+
clip,
|
|
435
|
+
within,
|
|
436
|
+
at: from - start,
|
|
437
|
+
samples,
|
|
438
|
+
frames: to - from
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
parts.push(await worker.call("renderBlock", {
|
|
442
|
+
channels: plan.channels,
|
|
443
|
+
frames: length,
|
|
444
|
+
sampleFormat,
|
|
445
|
+
segments
|
|
446
|
+
}, call));
|
|
447
|
+
onProgress?.(end / frames);
|
|
448
|
+
}
|
|
449
|
+
if (dataSize(spec) % 2 === 1) parts.push(/* @__PURE__ */ new Uint8Array(1));
|
|
450
|
+
if (frames === 0) onProgress?.(1);
|
|
451
|
+
return new Blob(parts, { type: "audio/wav" });
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
async render(job, options = {}) {
|
|
455
|
+
try {
|
|
456
|
+
return await render(job, options.signal, options.onProgress);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
if (error instanceof TypeError || error instanceof RangeError) throw error;
|
|
459
|
+
throw asAudioError(error, "The recording couldn't be exported.");
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
async destroy() {
|
|
463
|
+
destroyed = true;
|
|
464
|
+
client?.destroy();
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
}
|
|
288
468
|
// src/core/errors/audio-limits.ts
|
|
289
469
|
var audioLimitDefinitions = {
|
|
290
470
|
...coreLimits,
|
|
@@ -327,49 +507,17 @@ var audioLimitDefinitions = {
|
|
|
327
507
|
}
|
|
328
508
|
};
|
|
329
509
|
var audioLimits = /* @__PURE__ */ defineLimits(audioLimitDefinitions, { errorClass: AudioError });
|
|
330
|
-
// src/core/model/plan.ts
|
|
331
|
-
var EMPTY_METADATA = Object.freeze({
|
|
332
|
-
title: "",
|
|
333
|
-
artist: "",
|
|
334
|
-
album: "",
|
|
335
|
-
year: "",
|
|
336
|
-
comment: ""
|
|
337
|
-
});
|
|
338
|
-
var NO_FADE = Object.freeze({
|
|
339
|
-
frames: 0,
|
|
340
|
-
curve: "linear"
|
|
341
|
-
});
|
|
342
|
-
function clipLength(clip) {
|
|
343
|
-
return clip.end - clip.start;
|
|
344
|
-
}
|
|
345
|
-
function planLength(plan) {
|
|
346
|
-
let total = 0;
|
|
347
|
-
for (const clip of plan.clips) total += clipLength(clip);
|
|
348
|
-
return total;
|
|
349
|
-
}
|
|
350
|
-
function clipOffsets(plan) {
|
|
351
|
-
return clipBoundaries(plan).slice(0, -1);
|
|
352
|
-
}
|
|
353
|
-
function clipBoundaries(plan) {
|
|
354
|
-
return sequenceOffsets(plan.clips.map(clipLength));
|
|
355
|
-
}
|
|
356
|
-
var MIN_GAIN_DB = -120;
|
|
357
|
-
var MAX_GAIN_DB = 120;
|
|
358
|
-
var isFrames = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
359
|
-
function clipProblem(clip, sourceFrames) {
|
|
360
|
-
if (![
|
|
361
|
-
clip.start,
|
|
362
|
-
clip.end,
|
|
363
|
-
clip.fadeIn.frames,
|
|
364
|
-
clip.fadeOut.frames
|
|
365
|
-
].every(isFrames)) return "invalid-frames";
|
|
366
|
-
if (clip.end <= clip.start) return "clip-too-short";
|
|
367
|
-
if (clip.end > sourceFrames) return "clip-outside-source";
|
|
368
|
-
if (typeof clip.gainDb !== "number" || !(clip.gainDb >= -120 && clip.gainDb <= 120)) return "gain-out-of-range";
|
|
369
|
-
if (clip.fadeIn.frames + clip.fadeOut.frames > clipLength(clip)) return "fades-too-long";
|
|
370
|
-
}
|
|
371
510
|
// src/core/operations/commands.ts
|
|
372
511
|
var MAX_METADATA_LENGTH = 1e3;
|
|
512
|
+
function longerThan(text, max) {
|
|
513
|
+
if (text.length <= max) return false;
|
|
514
|
+
let count = 0;
|
|
515
|
+
for (const _ of text) {
|
|
516
|
+
count += 1;
|
|
517
|
+
if (count > max) return true;
|
|
518
|
+
}
|
|
519
|
+
return false;
|
|
520
|
+
}
|
|
373
521
|
var CURVES = /* @__PURE__ */ new Set(["linear", "equalPower"]);
|
|
374
522
|
var Invalid = class extends Error {
|
|
375
523
|
reason;
|
|
@@ -804,7 +952,7 @@ function apply(plan, command, sourceFrames) {
|
|
|
804
952
|
const { field, value } = command;
|
|
805
953
|
if (typeof field !== "string" || !Object.hasOwn(EMPTY_METADATA, field)) invalid("unknown-metadata-field", "there's no such metadata field");
|
|
806
954
|
if (typeof value !== "string") invalid("malformed-command", "a metadata value must be text");
|
|
807
|
-
if (value
|
|
955
|
+
if (longerThan(value, 1e3)) invalid("metadata-too-long", `a metadata value can have at most ${MAX_METADATA_LENGTH} code points`);
|
|
808
956
|
return {
|
|
809
957
|
state: {
|
|
810
958
|
...plan,
|
|
@@ -901,7 +1049,7 @@ function readMetadata(value) {
|
|
|
901
1049
|
const fields = METADATA_FIELDS.map((field) => {
|
|
902
1050
|
const text = value[field];
|
|
903
1051
|
if (typeof text !== "string") malformed(`metadata.${field} is text`);
|
|
904
|
-
if (text
|
|
1052
|
+
if (longerThan(text, 1e3)) invalid("metadata-too-long", `metadata.${field} is over ${MAX_METADATA_LENGTH} code points`);
|
|
905
1053
|
return [field, text];
|
|
906
1054
|
});
|
|
907
1055
|
return Object.fromEntries(fields);
|
|
@@ -1016,6 +1164,9 @@ function clipsOf(command) {
|
|
|
1016
1164
|
default: return [];
|
|
1017
1165
|
}
|
|
1018
1166
|
}
|
|
1167
|
+
function coarseSeconds(seconds) {
|
|
1168
|
+
return seconds > 0 ? Number(seconds.toPrecision(2)) : 0;
|
|
1169
|
+
}
|
|
1019
1170
|
function keysOf(command) {
|
|
1020
1171
|
const keys = clipsOf(command).map((clip) => clip.key);
|
|
1021
1172
|
if ("newKey" in command && typeof command.newKey === "string") keys.push(command.newKey);
|
|
@@ -1116,7 +1267,7 @@ function createAudioSession(options) {
|
|
|
1116
1267
|
function facts(source) {
|
|
1117
1268
|
const { info } = source;
|
|
1118
1269
|
return {
|
|
1119
|
-
durationSeconds: framesToSeconds(info.frames, info.sampleRate),
|
|
1270
|
+
durationSeconds: coarseSeconds(framesToSeconds(info.frames, info.sampleRate)),
|
|
1120
1271
|
sampleRate: info.sampleRate,
|
|
1121
1272
|
channels: info.channels,
|
|
1122
1273
|
container: info.container,
|
|
@@ -1407,6 +1558,53 @@ function createAudioSession(options) {
|
|
|
1407
1558
|
changed();
|
|
1408
1559
|
},
|
|
1409
1560
|
getPeaks: (id) => sources.get(id)?.peaks,
|
|
1561
|
+
async export(exportOptions = {}) {
|
|
1562
|
+
const current = plan;
|
|
1563
|
+
const format = exportOptions.format ?? { container: "wav" };
|
|
1564
|
+
try {
|
|
1565
|
+
if (!current) throw new AudioError("INVALID_COMMAND", "Open a recording before exporting it.", { details: { reason: "no-recording" } });
|
|
1566
|
+
if (current.clips.length === 0) throw new AudioError("INVALID_COMMAND", "The recording has no clips to export.", { details: { reason: "empty-plan" } });
|
|
1567
|
+
const { processor } = options;
|
|
1568
|
+
if (!processor) throw new TypeError("Exporting needs a processor in createAudioSession.");
|
|
1569
|
+
const used = /* @__PURE__ */ new Map();
|
|
1570
|
+
for (const { source } of current.clips) {
|
|
1571
|
+
const held = sources.get(source);
|
|
1572
|
+
if (held) used.set(source, held.audio);
|
|
1573
|
+
}
|
|
1574
|
+
busy += 1;
|
|
1575
|
+
changed();
|
|
1576
|
+
try {
|
|
1577
|
+
const output = await withProcessingTime(policy, (signal) => processor.render({
|
|
1578
|
+
plan: current,
|
|
1579
|
+
sources: used,
|
|
1580
|
+
format
|
|
1581
|
+
}, {
|
|
1582
|
+
signal,
|
|
1583
|
+
...exportOptions.onProgress ? { onProgress: exportOptions.onProgress } : {}
|
|
1584
|
+
}), exportOptions.signal ? { signal: exportOptions.signal } : {});
|
|
1585
|
+
emit({
|
|
1586
|
+
type: "recording_exported",
|
|
1587
|
+
durationSeconds: coarseSeconds(framesToSeconds(planLength(current), current.sampleRate)),
|
|
1588
|
+
sampleRate: current.sampleRate,
|
|
1589
|
+
channels: current.channels,
|
|
1590
|
+
sampleFormat: format.sampleFormat ?? "pcm16"
|
|
1591
|
+
});
|
|
1592
|
+
return output;
|
|
1593
|
+
} finally {
|
|
1594
|
+
busy -= 1;
|
|
1595
|
+
changed();
|
|
1596
|
+
}
|
|
1597
|
+
} catch (reason) {
|
|
1598
|
+
if (reason instanceof TypeError || reason instanceof RangeError) throw reason;
|
|
1599
|
+
const failure = exportOptions.signal?.aborted ? abortError(exportOptions.signal) : asAudioError(reason, "The recording couldn't be exported.");
|
|
1600
|
+
emit({
|
|
1601
|
+
type: "failed",
|
|
1602
|
+
operation: "export",
|
|
1603
|
+
code: failure.code
|
|
1604
|
+
});
|
|
1605
|
+
throw failure;
|
|
1606
|
+
}
|
|
1607
|
+
},
|
|
1410
1608
|
async close() {
|
|
1411
1609
|
generation += 1;
|
|
1412
1610
|
cancelLoads("The recording was closed.");
|
|
@@ -1448,6 +1646,6 @@ var audioMessages = /* @__PURE__ */ defineMessages({
|
|
|
1448
1646
|
}),
|
|
1449
1647
|
"warnings.truncated": "This recording looks incomplete, perhaps from an interrupted recording or download. It opened with the audio it holds."
|
|
1450
1648
|
});
|
|
1451
|
-
export { AUDIO_PLAN_FORMAT, AUDIO_PLAN_VERSION, AudioError, EMPTY_METADATA, MAX_GAIN_DB, MAX_METADATA_LENGTH, MIN_GAIN_DB, NO_FADE, audioLimitDefinitions, audioLimits, audioMessages, clipLength, clipOffsets, createAudioEngine, createAudioSession, dbToGain, framesToSeconds, gainAt, isAudioError, planLength, readAudioPlanDocument, secondsToFrames, toAudioPlanDocument };
|
|
1649
|
+
export { AUDIO_PLAN_FORMAT, AUDIO_PLAN_VERSION, AudioError, EMPTY_METADATA, MAX_GAIN_DB, MAX_METADATA_LENGTH, MIN_GAIN_DB, NO_FADE, audioLimitDefinitions, audioLimits, audioMessages, clipLength, clipOffsets, createAudioEngine, createAudioProcessor, createAudioSession, dbToGain, framesToSeconds, gainAt, isAudioError, planLength, readAudioPlanDocument, secondsToFrames, toAudioPlanDocument };
|
|
1452
1650
|
|
|
1453
1651
|
//# sourceMappingURL=index.js.map
|