@meframe/core 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/dist/cache/index.d.ts +0 -3
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/resource/AudioSampleCache.d.ts +12 -0
- package/dist/cache/resource/AudioSampleCache.d.ts.map +1 -1
- package/dist/cache/resource/AudioSampleCache.js +40 -5
- package/dist/cache/resource/AudioSampleCache.js.map +1 -1
- package/dist/cache/resource/MP4IndexCache.d.ts.map +1 -1
- package/dist/cache/resource/MP4IndexCache.js +23 -13
- package/dist/cache/resource/MP4IndexCache.js.map +1 -1
- package/dist/cache/resource/ResourceCache.d.ts +4 -0
- package/dist/cache/resource/ResourceCache.d.ts.map +1 -1
- package/dist/cache/resource/ResourceCache.js +7 -0
- package/dist/cache/resource/ResourceCache.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/orchestrator/AudioWindowPreparer.d.ts +5 -0
- package/dist/orchestrator/AudioWindowPreparer.d.ts.map +1 -1
- package/dist/orchestrator/AudioWindowPreparer.js +411 -26
- package/dist/orchestrator/AudioWindowPreparer.js.map +1 -1
- package/dist/orchestrator/Orchestrator.d.ts.map +1 -1
- package/dist/orchestrator/Orchestrator.js +20 -9
- package/dist/orchestrator/Orchestrator.js.map +1 -1
- package/dist/orchestrator/VideoWindowDecodeSession.d.ts.map +1 -1
- package/dist/orchestrator/VideoWindowDecodeSession.js +59 -47
- package/dist/orchestrator/VideoWindowDecodeSession.js.map +1 -1
- package/dist/orchestrator/aac-window-timeline.d.ts +11 -1
- package/dist/orchestrator/aac-window-timeline.d.ts.map +1 -1
- package/dist/orchestrator/aac-window-timeline.js +146 -17
- package/dist/orchestrator/aac-window-timeline.js.map +1 -1
- package/dist/stages/decode/preview-audio-decoder.d.ts +57 -0
- package/dist/stages/decode/preview-audio-decoder.d.ts.map +1 -0
- package/dist/stages/decode/preview-audio-decoder.js +281 -0
- package/dist/stages/decode/preview-audio-decoder.js.map +1 -0
- package/dist/stages/demux/MP3FrameParser.d.ts +18 -0
- package/dist/stages/demux/MP3FrameParser.d.ts.map +1 -1
- package/dist/stages/demux/MP3FrameParser.js +144 -11
- package/dist/stages/demux/MP3FrameParser.js.map +1 -1
- package/dist/stages/demux/MP4IndexParser.d.ts +34 -23
- package/dist/stages/demux/MP4IndexParser.d.ts.map +1 -1
- package/dist/stages/demux/MP4IndexParser.js +226 -326
- package/dist/stages/demux/MP4IndexParser.js.map +1 -1
- package/dist/stages/demux/MP4IndexWorkerClient.d.ts +15 -0
- package/dist/stages/demux/MP4IndexWorkerClient.d.ts.map +1 -0
- package/dist/stages/demux/MP4IndexWorkerClient.js +257 -0
- package/dist/stages/demux/MP4IndexWorkerClient.js.map +1 -0
- package/dist/stages/demux/SampleTable.d.ts +74 -0
- package/dist/stages/demux/SampleTable.d.ts.map +1 -0
- package/dist/stages/demux/SampleTable.js +259 -0
- package/dist/stages/demux/SampleTable.js.map +1 -0
- package/dist/stages/demux/mp4-index-protocol.d.ts +141 -0
- package/dist/stages/demux/mp4-index-protocol.d.ts.map +1 -0
- package/dist/stages/demux/mp4-index-protocol.js +108 -0
- package/dist/stages/demux/mp4-index-protocol.js.map +1 -0
- package/dist/stages/demux/sample-collector.d.ts +35 -0
- package/dist/stages/demux/sample-collector.d.ts.map +1 -0
- package/dist/stages/demux/sample-collector.js +105 -0
- package/dist/stages/demux/sample-collector.js.map +1 -0
- package/dist/stages/demux/types.d.ts +3 -2
- package/dist/stages/demux/types.d.ts.map +1 -1
- package/dist/stages/load/ResourceLoader.d.ts +6 -13
- package/dist/stages/load/ResourceLoader.d.ts.map +1 -1
- package/dist/stages/load/ResourceLoader.js +126 -80
- package/dist/stages/load/ResourceLoader.js.map +1 -1
- package/dist/stages/load/types.d.ts +2 -0
- package/dist/stages/load/types.d.ts.map +1 -1
- package/dist/worker/WorkerPool.d.ts +4 -0
- package/dist/worker/WorkerPool.d.ts.map +1 -1
- package/dist/worker/WorkerPool.js +10 -2
- package/dist/worker/WorkerPool.js.map +1 -1
- package/dist/worker/types.d.ts +1 -1
- package/dist/worker/types.d.ts.map +1 -1
- package/dist/worker/types.js.map +1 -1
- package/dist/worker/worker-event-whitelist.d.ts.map +1 -1
- package/dist/workers/stages/demux/mp4-index.worker.DaGeoDUz.js +8431 -0
- package/dist/workers/stages/demux/mp4-index.worker.DaGeoDUz.js.map +1 -0
- package/dist/workers/stages/export/export.worker.BG8HA7dY.js.map +1 -1
- package/dist/workers/worker-manifest.json +1 -0
- package/package.json +23 -22
- package/dist/cache/l2/IndexedDBStore.d.ts +0 -74
- package/dist/cache/l2/IndexedDBStore.d.ts.map +0 -1
- package/dist/cache/l2/L2Cache.d.ts +0 -78
- package/dist/cache/l2/L2Cache.d.ts.map +0 -1
- package/dist/cache/l2/L2OPFSStore.d.ts +0 -37
- package/dist/cache/l2/L2OPFSStore.d.ts.map +0 -1
- package/dist/cache/l2/OPFSStore.d.ts +0 -46
- package/dist/cache/l2/OPFSStore.d.ts.map +0 -1
- package/dist/cache/storage/indexeddb/ChunkRecordStore.d.ts +0 -75
- package/dist/cache/storage/indexeddb/ChunkRecordStore.d.ts.map +0 -1
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { MeframeEvent } from "../event/events.js";
|
|
2
2
|
import { AudioChunkDecoder } from "../stages/decode/AudioChunkDecoder.js";
|
|
3
|
+
import { PersistentPreviewAudioDecoder } from "../stages/decode/preview-audio-decoder.js";
|
|
3
4
|
import { decodeWavPCM } from "../stages/demux/WAVIndexParser.js";
|
|
4
5
|
import { hasResourceId, isAudioClip, clipPlaybackRate } from "../model/types.js";
|
|
5
6
|
import { buildLoopedResourceSegments } from "../utils/loop-utils.js";
|
|
7
|
+
import { extractPlanesFromAudioData } from "../utils/audio-data.js";
|
|
6
8
|
import { isTerminalMediaResourceError, markReportedToHost } from "../utils/errors.js";
|
|
7
9
|
import { untilAborted } from "../utils/until-aborted.js";
|
|
8
|
-
import {
|
|
10
|
+
import { binarySearchFirst } from "../utils/binary-search.js";
|
|
11
|
+
import { sampleDurationAt, sampleTimestampAt, binarySearchFirstIndex, sampleIsKeyframeAt } from "../stages/demux/SampleTable.js";
|
|
12
|
+
import { prepareAACTimeline, readIndexedAACWindow, readSampleByteSlice, getAACWindow, findTimelineBounds } from "./aac-window-timeline.js";
|
|
9
13
|
class AudioWindowPreparer {
|
|
10
14
|
activeClips = /* @__PURE__ */ new Set();
|
|
11
15
|
/** 解码确定失败的 clip(坏包等):本会话不再重试,避免重试风暴;reset 清空 */
|
|
@@ -18,7 +22,24 @@ class AudioWindowPreparer {
|
|
|
18
22
|
recordIds = /* @__PURE__ */ new WeakMap();
|
|
19
23
|
nextRecordId = 0;
|
|
20
24
|
aacTimelines = /* @__PURE__ */ new WeakMap();
|
|
25
|
+
previewDecoders = /* @__PURE__ */ new Map();
|
|
26
|
+
clipDecoderKeys = /* @__PURE__ */ new Map();
|
|
21
27
|
lifecycle = new AbortController();
|
|
28
|
+
closePreviewDecoders(onlyKey) {
|
|
29
|
+
if (onlyKey !== void 0) {
|
|
30
|
+
const decoder = this.previewDecoders.get(onlyKey);
|
|
31
|
+
if (decoder) {
|
|
32
|
+
this.previewDecoders.delete(onlyKey);
|
|
33
|
+
decoder.close();
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
for (const decoder of this.previewDecoders.values()) {
|
|
38
|
+
decoder.close();
|
|
39
|
+
}
|
|
40
|
+
this.previewDecoders.clear();
|
|
41
|
+
this.clipDecoderKeys.clear();
|
|
42
|
+
}
|
|
22
43
|
get model() {
|
|
23
44
|
return this.deps.getCompositionModel();
|
|
24
45
|
}
|
|
@@ -34,6 +55,7 @@ class AudioWindowPreparer {
|
|
|
34
55
|
this.lifecycle.abort();
|
|
35
56
|
this.lifecycle = new AbortController();
|
|
36
57
|
}
|
|
58
|
+
this.closePreviewDecoders();
|
|
37
59
|
this.failedDecodeClips.clear();
|
|
38
60
|
this.activeClips.clear();
|
|
39
61
|
}
|
|
@@ -136,7 +158,8 @@ class AudioWindowPreparer {
|
|
|
136
158
|
const run = async () => {
|
|
137
159
|
signal?.throwIfAborted();
|
|
138
160
|
this.checkCurrent(source);
|
|
139
|
-
const
|
|
161
|
+
const firstSampleTimestamp = source.record.indexed ? source.record.indexed.samples.length > 0 ? sampleTimestampAt(source.record.indexed.samples, 0) : void 0 : source.record.samples[0]?.timestamp;
|
|
162
|
+
const firstUs = source.record.gapless ? 0 : firstSampleTimestamp ?? 0;
|
|
140
163
|
const readStartUs = Math.max(firstUs, startUs);
|
|
141
164
|
const readEndUs = Math.min(endUs, source.record.durationUs);
|
|
142
165
|
if (readEndUs <= readStartUs) return consume(null);
|
|
@@ -287,6 +310,20 @@ class AudioWindowPreparer {
|
|
|
287
310
|
return;
|
|
288
311
|
}
|
|
289
312
|
this.activeClips.delete(clipId);
|
|
313
|
+
const cacheKey = this.clipDecoderKeys.get(clipId);
|
|
314
|
+
if (cacheKey !== void 0) {
|
|
315
|
+
this.clipDecoderKeys.delete(clipId);
|
|
316
|
+
let stillInUse = false;
|
|
317
|
+
for (const otherKey of this.clipDecoderKeys.values()) {
|
|
318
|
+
if (otherKey === cacheKey) {
|
|
319
|
+
stillInUse = true;
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (!stillInUse) {
|
|
324
|
+
this.closePreviewDecoders(cacheKey);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
290
327
|
this.deps.cacheManager.clearClipAudioData(clipId);
|
|
291
328
|
}
|
|
292
329
|
/**
|
|
@@ -450,6 +487,147 @@ class AudioWindowPreparer {
|
|
|
450
487
|
}
|
|
451
488
|
return;
|
|
452
489
|
}
|
|
490
|
+
if (!strictMode && source) {
|
|
491
|
+
const owner = signal ? AbortSignal.any([this.lifecycle.signal, signal]) : this.lifecycle.signal;
|
|
492
|
+
await this.decodePreviewAudioWindow(clipId, clip, audioRecord, source, startUs, endUs, owner);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (audioRecord.indexed) {
|
|
496
|
+
const owner = signal ? AbortSignal.any([this.lifecycle.signal, signal]) : this.lifecycle.signal;
|
|
497
|
+
let windowChunks2;
|
|
498
|
+
let trim2;
|
|
499
|
+
try {
|
|
500
|
+
if (audioRecord.metadata.codec.startsWith("mp4a")) {
|
|
501
|
+
const { chunks, preroll } = await readIndexedAACWindow(
|
|
502
|
+
audioRecord.indexed,
|
|
503
|
+
source?.aacTimeline,
|
|
504
|
+
startUs,
|
|
505
|
+
endUs,
|
|
506
|
+
owner
|
|
507
|
+
);
|
|
508
|
+
windowChunks2 = chunks;
|
|
509
|
+
if (preroll > 0) {
|
|
510
|
+
const { sampleRate } = audioRecord.metadata;
|
|
511
|
+
const toFrames = (us) => Math.round(us * sampleRate / 1e6);
|
|
512
|
+
trim2 = {
|
|
513
|
+
skipSamples: 0,
|
|
514
|
+
sampleRate,
|
|
515
|
+
physicalStart: toFrames(chunks[0].timestamp),
|
|
516
|
+
validStart: toFrames(chunks[preroll].timestamp),
|
|
517
|
+
validEnd: Number.POSITIVE_INFINITY
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
} else if (audioRecord.gapless) {
|
|
521
|
+
const { skipSamples, trimEndSamples, samplesPerFrame } = audioRecord.gapless;
|
|
522
|
+
const idxSamples = audioRecord.indexed.samples;
|
|
523
|
+
const sampleRate = audioRecord.metadata.sampleRate;
|
|
524
|
+
const validStart = Math.max(
|
|
525
|
+
skipSamples,
|
|
526
|
+
Math.ceil(startUs * sampleRate / 1e6) + skipSamples
|
|
527
|
+
);
|
|
528
|
+
const validEnd = Math.min(
|
|
529
|
+
idxSamples.length * samplesPerFrame - trimEndSamples,
|
|
530
|
+
Math.ceil(endUs * sampleRate / 1e6) + skipSamples
|
|
531
|
+
);
|
|
532
|
+
if (validEnd <= validStart) return;
|
|
533
|
+
const first = Math.floor(validStart / samplesPerFrame);
|
|
534
|
+
const last = Math.min(idxSamples.length, Math.ceil(validEnd / samplesPerFrame));
|
|
535
|
+
const decodeStart = this.indexedMp3PrerollStart(audioRecord.indexed, first);
|
|
536
|
+
const built = new Array(last - decodeStart);
|
|
537
|
+
await readSampleByteSlice(
|
|
538
|
+
idxSamples,
|
|
539
|
+
audioRecord.indexed.readRange,
|
|
540
|
+
decodeStart,
|
|
541
|
+
last,
|
|
542
|
+
(idx, bytes) => {
|
|
543
|
+
built[idx - decodeStart] = new EncodedAudioChunk({
|
|
544
|
+
type: "key",
|
|
545
|
+
timestamp: Math.round(sampleTimestampAt(idxSamples, idx)),
|
|
546
|
+
duration: Math.max(0, Math.round(sampleDurationAt(idxSamples, idx))),
|
|
547
|
+
data: bytes
|
|
548
|
+
});
|
|
549
|
+
},
|
|
550
|
+
owner
|
|
551
|
+
);
|
|
552
|
+
windowChunks2 = built.filter(Boolean);
|
|
553
|
+
trim2 = {
|
|
554
|
+
skipSamples,
|
|
555
|
+
sampleRate,
|
|
556
|
+
validStart: Math.max(skipSamples, first * samplesPerFrame),
|
|
557
|
+
validEnd: Math.min(
|
|
558
|
+
idxSamples.length * samplesPerFrame - trimEndSamples,
|
|
559
|
+
last * samplesPerFrame
|
|
560
|
+
),
|
|
561
|
+
physicalStart: decodeStart * samplesPerFrame
|
|
562
|
+
};
|
|
563
|
+
} else {
|
|
564
|
+
const idxSamples = audioRecord.indexed.samples;
|
|
565
|
+
const first = binarySearchFirstIndex(
|
|
566
|
+
idxSamples.length,
|
|
567
|
+
(i) => sampleTimestampAt(idxSamples, i) + sampleDurationAt(idxSamples, i) > startUs
|
|
568
|
+
);
|
|
569
|
+
let last = first;
|
|
570
|
+
while (last < idxSamples.length && sampleTimestampAt(idxSamples, last) < endUs) last++;
|
|
571
|
+
if (first >= last) {
|
|
572
|
+
windowChunks2 = [];
|
|
573
|
+
} else {
|
|
574
|
+
const isMp3 = audioRecord.metadata.codec === "mp3";
|
|
575
|
+
const decodeStart = isMp3 && first > 0 ? this.indexedMp3PrerollStart(audioRecord.indexed, first) : first;
|
|
576
|
+
const built = new Array(last - decodeStart);
|
|
577
|
+
await readSampleByteSlice(
|
|
578
|
+
idxSamples,
|
|
579
|
+
audioRecord.indexed.readRange,
|
|
580
|
+
decodeStart,
|
|
581
|
+
last,
|
|
582
|
+
(idx, bytes) => {
|
|
583
|
+
built[idx - decodeStart] = new EncodedAudioChunk({
|
|
584
|
+
type: sampleIsKeyframeAt(idxSamples, idx) ? "key" : "delta",
|
|
585
|
+
timestamp: Math.round(sampleTimestampAt(idxSamples, idx)),
|
|
586
|
+
duration: Math.max(0, Math.round(sampleDurationAt(idxSamples, idx))),
|
|
587
|
+
data: bytes
|
|
588
|
+
});
|
|
589
|
+
},
|
|
590
|
+
owner
|
|
591
|
+
);
|
|
592
|
+
windowChunks2 = built.filter(Boolean);
|
|
593
|
+
if (isMp3 && decodeStart < first) {
|
|
594
|
+
const { sampleRate } = audioRecord.metadata;
|
|
595
|
+
const toFrames = (us) => Math.round(us * sampleRate / 1e6);
|
|
596
|
+
trim2 = {
|
|
597
|
+
skipSamples: 0,
|
|
598
|
+
sampleRate,
|
|
599
|
+
physicalStart: toFrames(sampleTimestampAt(idxSamples, decodeStart)),
|
|
600
|
+
validStart: toFrames(sampleTimestampAt(idxSamples, first)),
|
|
601
|
+
validEnd: Number.POSITIVE_INFINITY
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
} catch (error) {
|
|
607
|
+
if (owner.aborted) {
|
|
608
|
+
throw owner.reason ?? new DOMException("Audio decode cancelled", "AbortError");
|
|
609
|
+
}
|
|
610
|
+
this.deps.resourceLoader.reportByteFailure(
|
|
611
|
+
audioRecord.resourceId,
|
|
612
|
+
error,
|
|
613
|
+
() => !owner.aborted && this.deps.cacheManager.audioSampleCache.get(audioRecord.resourceId) === audioRecord
|
|
614
|
+
);
|
|
615
|
+
throw error;
|
|
616
|
+
}
|
|
617
|
+
if (windowChunks2.length === 0) return;
|
|
618
|
+
await this.decodeAudioSamples(
|
|
619
|
+
clipId,
|
|
620
|
+
windowChunks2,
|
|
621
|
+
audioRecord.metadata,
|
|
622
|
+
clip.durationUs,
|
|
623
|
+
clip.startUs,
|
|
624
|
+
strictMode,
|
|
625
|
+
trim2,
|
|
626
|
+
source,
|
|
627
|
+
signal
|
|
628
|
+
);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
453
631
|
if (audioRecord.gapless) {
|
|
454
632
|
const { skipSamples, trimEndSamples, samplesPerFrame } = audioRecord.gapless;
|
|
455
633
|
const sampleRate = audioRecord.metadata.sampleRate;
|
|
@@ -510,10 +688,16 @@ class AudioWindowPreparer {
|
|
|
510
688
|
};
|
|
511
689
|
}
|
|
512
690
|
} else {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
691
|
+
const first = binarySearchFirst(
|
|
692
|
+
audioRecord.samples,
|
|
693
|
+
(s) => s.timestamp + (s.duration ?? 0) > startUs
|
|
694
|
+
);
|
|
695
|
+
windowChunks = [];
|
|
696
|
+
for (let i = first; i < audioRecord.samples.length; i++) {
|
|
697
|
+
const s = audioRecord.samples[i];
|
|
698
|
+
if (s.timestamp >= endUs) break;
|
|
699
|
+
if (s.timestamp + (s.duration ?? 0) > startUs) windowChunks.push(s);
|
|
700
|
+
}
|
|
517
701
|
}
|
|
518
702
|
if (windowChunks.length === 0) {
|
|
519
703
|
return;
|
|
@@ -530,6 +714,206 @@ class AudioWindowPreparer {
|
|
|
530
714
|
signal
|
|
531
715
|
);
|
|
532
716
|
}
|
|
717
|
+
async decodePreviewAudioWindow(clipId, clip, audioRecord, source, startUs, endUs, owner) {
|
|
718
|
+
const indexed = audioRecord.indexed;
|
|
719
|
+
const totalPackets = indexed ? indexed.samples.length : audioRecord.samples.length;
|
|
720
|
+
if (totalPackets === 0) return;
|
|
721
|
+
const timestampAt = indexed ? (i) => sampleTimestampAt(indexed.samples, i) : (i) => audioRecord.samples[i].timestamp;
|
|
722
|
+
const durationAt = indexed ? (i) => sampleDurationAt(indexed.samples, i) : (i) => audioRecord.samples[i].duration ?? 0;
|
|
723
|
+
let first;
|
|
724
|
+
let last;
|
|
725
|
+
let prerollStart;
|
|
726
|
+
let trim;
|
|
727
|
+
let shiftUs = 0;
|
|
728
|
+
const isAAC = audioRecord.metadata.codec.startsWith("mp4a");
|
|
729
|
+
if (isAAC) {
|
|
730
|
+
const bounds = findTimelineBounds(
|
|
731
|
+
totalPackets,
|
|
732
|
+
timestampAt,
|
|
733
|
+
durationAt,
|
|
734
|
+
source.aacTimeline,
|
|
735
|
+
startUs,
|
|
736
|
+
endUs
|
|
737
|
+
);
|
|
738
|
+
first = bounds.first;
|
|
739
|
+
last = bounds.last;
|
|
740
|
+
shiftUs = bounds.shiftUs;
|
|
741
|
+
if (first === -1) return;
|
|
742
|
+
prerollStart = Math.max(0, first - 2);
|
|
743
|
+
const { sampleRate } = audioRecord.metadata;
|
|
744
|
+
const toFrames = (us) => Math.round(us * sampleRate / 1e6);
|
|
745
|
+
const firstTs = Math.round(timestampAt(first) - (first === 0 ? 0 : shiftUs));
|
|
746
|
+
const prerollTs = Math.round(timestampAt(prerollStart) - (prerollStart === 0 ? 0 : shiftUs));
|
|
747
|
+
trim = {
|
|
748
|
+
skipSamples: 0,
|
|
749
|
+
sampleRate,
|
|
750
|
+
physicalStartFrame: toFrames(prerollTs),
|
|
751
|
+
validStartFrame: toFrames(firstTs),
|
|
752
|
+
validEndFrame: Number.POSITIVE_INFINITY,
|
|
753
|
+
rebaseTimestamps: false
|
|
754
|
+
};
|
|
755
|
+
} else if (audioRecord.gapless) {
|
|
756
|
+
const { skipSamples, trimEndSamples, samplesPerFrame } = audioRecord.gapless;
|
|
757
|
+
const sampleRate = audioRecord.metadata.sampleRate;
|
|
758
|
+
const validStart = Math.max(
|
|
759
|
+
skipSamples,
|
|
760
|
+
Math.ceil(startUs * sampleRate / 1e6) + skipSamples
|
|
761
|
+
);
|
|
762
|
+
const validEnd = Math.min(
|
|
763
|
+
totalPackets * samplesPerFrame - trimEndSamples,
|
|
764
|
+
Math.ceil(endUs * sampleRate / 1e6) + skipSamples
|
|
765
|
+
);
|
|
766
|
+
if (validEnd <= validStart) return;
|
|
767
|
+
first = Math.floor(validStart / samplesPerFrame);
|
|
768
|
+
last = Math.min(totalPackets, Math.ceil(validEnd / samplesPerFrame));
|
|
769
|
+
prerollStart = indexed ? this.indexedMp3PrerollStart(indexed, first) : this.mp3PrerollStart(audioRecord, first);
|
|
770
|
+
trim = {
|
|
771
|
+
skipSamples,
|
|
772
|
+
sampleRate,
|
|
773
|
+
physicalStartFrame: prerollStart * samplesPerFrame,
|
|
774
|
+
validStartFrame: Math.max(skipSamples, first * samplesPerFrame),
|
|
775
|
+
// Keep lookahead packets emitted & cached cleanly; only trim true trailing encoder padding.
|
|
776
|
+
validEndFrame: Math.max(0, totalPackets * samplesPerFrame - trimEndSamples),
|
|
777
|
+
rebaseTimestamps: true
|
|
778
|
+
};
|
|
779
|
+
} else {
|
|
780
|
+
first = binarySearchFirstIndex(totalPackets, (i) => timestampAt(i) + durationAt(i) > startUs);
|
|
781
|
+
last = first;
|
|
782
|
+
while (last < totalPackets && timestampAt(last) < endUs) last++;
|
|
783
|
+
if (first >= last) return;
|
|
784
|
+
const isMp3 = audioRecord.metadata.codec === "mp3";
|
|
785
|
+
prerollStart = isMp3 && first > 0 ? indexed ? this.indexedMp3PrerollStart(indexed, first) : this.mp3PrerollStart(audioRecord, first) : first;
|
|
786
|
+
const { sampleRate } = audioRecord.metadata;
|
|
787
|
+
const toFrames = (us) => Math.round(us * sampleRate / 1e6);
|
|
788
|
+
trim = {
|
|
789
|
+
skipSamples: 0,
|
|
790
|
+
sampleRate,
|
|
791
|
+
physicalStartFrame: toFrames(timestampAt(prerollStart)),
|
|
792
|
+
validStartFrame: toFrames(timestampAt(first)),
|
|
793
|
+
validEndFrame: Number.POSITIVE_INFINITY,
|
|
794
|
+
rebaseTimestamps: isMp3 && prerollStart < first
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
let byteReadFailed = false;
|
|
798
|
+
const timeline = isAAC ? source.aacTimeline : void 0;
|
|
799
|
+
const packetSource = {
|
|
800
|
+
totalPackets,
|
|
801
|
+
loadChunks: async (from, to, signal) => {
|
|
802
|
+
if (!indexed) {
|
|
803
|
+
const out = [];
|
|
804
|
+
for (let i = from; i < to; i++) {
|
|
805
|
+
const sample = audioRecord.samples[i];
|
|
806
|
+
if (!isAAC && !audioRecord.gapless && i >= first && sample.timestamp + (sample.duration ?? 0) <= startUs) {
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
const timestamp = isAAC ? Math.round(sample.timestamp - (i === 0 ? 0 : shiftUs)) : sample.timestamp;
|
|
810
|
+
const duration = isAAC && i === 0 && timeline ? timeline.firstDurationUs : sample.duration ?? 0;
|
|
811
|
+
const data = new Uint8Array(sample.byteLength);
|
|
812
|
+
sample.copyTo(data);
|
|
813
|
+
out.push(
|
|
814
|
+
new EncodedAudioChunk({
|
|
815
|
+
type: sample.type,
|
|
816
|
+
timestamp,
|
|
817
|
+
duration,
|
|
818
|
+
data
|
|
819
|
+
})
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
return out;
|
|
823
|
+
}
|
|
824
|
+
const idxSamples = indexed.samples;
|
|
825
|
+
const built = new Array(to - from);
|
|
826
|
+
try {
|
|
827
|
+
await readSampleByteSlice(
|
|
828
|
+
idxSamples,
|
|
829
|
+
indexed.readRange,
|
|
830
|
+
from,
|
|
831
|
+
to,
|
|
832
|
+
(idx, bytes) => {
|
|
833
|
+
const rawTs = sampleTimestampAt(idxSamples, idx);
|
|
834
|
+
const timestamp = isAAC ? Math.round(rawTs - (idx === 0 ? 0 : shiftUs)) : Math.round(rawTs);
|
|
835
|
+
const duration = Math.max(
|
|
836
|
+
0,
|
|
837
|
+
Math.round(
|
|
838
|
+
isAAC && idx === 0 && timeline ? timeline.firstDurationUs : sampleDurationAt(idxSamples, idx)
|
|
839
|
+
)
|
|
840
|
+
);
|
|
841
|
+
built[idx - from] = new EncodedAudioChunk({
|
|
842
|
+
type: audioRecord.gapless ? "key" : sampleIsKeyframeAt(idxSamples, idx) ? "key" : "delta",
|
|
843
|
+
timestamp,
|
|
844
|
+
duration,
|
|
845
|
+
data: bytes
|
|
846
|
+
});
|
|
847
|
+
},
|
|
848
|
+
signal ?? owner
|
|
849
|
+
);
|
|
850
|
+
} catch (error) {
|
|
851
|
+
if (owner.aborted) {
|
|
852
|
+
throw owner.reason ?? new DOMException("Audio decode cancelled", "AbortError");
|
|
853
|
+
}
|
|
854
|
+
byteReadFailed = true;
|
|
855
|
+
this.deps.resourceLoader.reportByteFailure(
|
|
856
|
+
audioRecord.resourceId,
|
|
857
|
+
error,
|
|
858
|
+
() => !owner.aborted && this.deps.cacheManager.audioSampleCache.get(audioRecord.resourceId) === audioRecord
|
|
859
|
+
);
|
|
860
|
+
throw error;
|
|
861
|
+
}
|
|
862
|
+
return built.filter(Boolean);
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
let decoder = this.previewDecoders.get(source.cacheKey);
|
|
866
|
+
if (!decoder) {
|
|
867
|
+
decoder = new PersistentPreviewAudioDecoder(audioRecord.metadata);
|
|
868
|
+
this.previewDecoders.set(source.cacheKey, decoder);
|
|
869
|
+
}
|
|
870
|
+
this.clipDecoderKeys.set(clipId, source.cacheKey);
|
|
871
|
+
try {
|
|
872
|
+
await decoder.decodeWindow({
|
|
873
|
+
firstPacket: first,
|
|
874
|
+
lastPacket: last,
|
|
875
|
+
prerollStart,
|
|
876
|
+
startUs,
|
|
877
|
+
endUs,
|
|
878
|
+
trim,
|
|
879
|
+
packetSource,
|
|
880
|
+
isRangeCovered: (fromUs, toUs) => {
|
|
881
|
+
const threshold = Math.max(
|
|
882
|
+
0.5,
|
|
883
|
+
1 - 1e6 / audioRecord.metadata.sampleRate / Math.max(1, toUs - fromUs)
|
|
884
|
+
);
|
|
885
|
+
return this.deps.cacheManager.getAudioRangeCoverage(
|
|
886
|
+
source.cacheKey,
|
|
887
|
+
fromUs,
|
|
888
|
+
toUs,
|
|
889
|
+
threshold
|
|
890
|
+
).covered;
|
|
891
|
+
},
|
|
892
|
+
onDecodedAudioData: (trimmed) => {
|
|
893
|
+
this.checkCurrent(source);
|
|
894
|
+
this.deps.cacheManager.putClipAudioData(
|
|
895
|
+
source.cacheKey,
|
|
896
|
+
trimmed,
|
|
897
|
+
clip.durationUs,
|
|
898
|
+
clip.startUs + trimmed.timestamp
|
|
899
|
+
);
|
|
900
|
+
},
|
|
901
|
+
sliceAudioData,
|
|
902
|
+
signal: owner
|
|
903
|
+
});
|
|
904
|
+
} catch (error) {
|
|
905
|
+
decoder.close();
|
|
906
|
+
if (this.previewDecoders.get(source.cacheKey) === decoder) {
|
|
907
|
+
this.previewDecoders.delete(source.cacheKey);
|
|
908
|
+
}
|
|
909
|
+
if (owner.aborted) {
|
|
910
|
+
throw owner.reason ?? new DOMException("Audio decode cancelled", "AbortError");
|
|
911
|
+
}
|
|
912
|
+
this.checkCurrent(source);
|
|
913
|
+
if (byteReadFailed || error instanceof RangeError) throw error;
|
|
914
|
+
await this.handleDecodeFailure(clipId, error, false);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
533
917
|
async decodeWavWindow(clipId, record, startUs, endUs, clipDurationUs, clipStartUs, source, signal) {
|
|
534
918
|
const index = record.pcm;
|
|
535
919
|
const first = Math.max(0, Math.floor(startUs * index.sampleRate / 1e6));
|
|
@@ -614,6 +998,16 @@ class AudioWindowPreparer {
|
|
|
614
998
|
frame = batchEnd;
|
|
615
999
|
}
|
|
616
1000
|
}
|
|
1001
|
+
indexedMp3PrerollStart(indexed, first) {
|
|
1002
|
+
let start = Math.max(0, first - 2);
|
|
1003
|
+
const mainDataBytes = indexed.mainDataBytes;
|
|
1004
|
+
if (!mainDataBytes) return start;
|
|
1005
|
+
let reservoirBytes = 0;
|
|
1006
|
+
while (start > 0 && reservoirBytes < 511) {
|
|
1007
|
+
reservoirBytes += mainDataBytes[--start] ?? 0;
|
|
1008
|
+
}
|
|
1009
|
+
return start;
|
|
1010
|
+
}
|
|
617
1011
|
/** Restore two synthesis frames plus their maximum bit reservoir before a seek. */
|
|
618
1012
|
mp3PrerollStart(record, first) {
|
|
619
1013
|
let start = Math.max(0, first - 2);
|
|
@@ -775,31 +1169,22 @@ class AudioWindowPreparer {
|
|
|
775
1169
|
}
|
|
776
1170
|
function sliceAudioData(src, headFrames, tailFrames, timestamp) {
|
|
777
1171
|
const keep = src.numberOfFrames - headFrames - tailFrames;
|
|
778
|
-
const
|
|
779
|
-
const
|
|
780
|
-
const
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
});
|
|
787
|
-
const buffer = new ArrayBuffer(planeSize * planes);
|
|
788
|
-
for (let plane = 0; plane < planes; plane++) {
|
|
789
|
-
src.copyTo(new Uint8Array(buffer, plane * planeSize, planeSize), {
|
|
790
|
-
planeIndex: plane,
|
|
791
|
-
format,
|
|
792
|
-
frameOffset: headFrames,
|
|
793
|
-
frameLength: keep
|
|
794
|
-
});
|
|
1172
|
+
const channels = src.numberOfChannels;
|
|
1173
|
+
const planes = extractPlanesFromAudioData(src, channels, src.numberOfFrames);
|
|
1174
|
+
const packed = new Float32Array(keep * channels);
|
|
1175
|
+
for (let ch = 0; ch < channels; ch++) {
|
|
1176
|
+
const channelData = planes[ch];
|
|
1177
|
+
if (channelData) {
|
|
1178
|
+
packed.set(channelData.subarray(headFrames, headFrames + keep), ch * keep);
|
|
1179
|
+
}
|
|
795
1180
|
}
|
|
796
1181
|
return new AudioData({
|
|
797
|
-
format,
|
|
1182
|
+
format: "f32-planar",
|
|
798
1183
|
sampleRate: src.sampleRate,
|
|
799
1184
|
numberOfFrames: keep,
|
|
800
|
-
numberOfChannels:
|
|
1185
|
+
numberOfChannels: channels,
|
|
801
1186
|
timestamp,
|
|
802
|
-
data: buffer
|
|
1187
|
+
data: packed.buffer
|
|
803
1188
|
});
|
|
804
1189
|
}
|
|
805
1190
|
export {
|