@dawcore/spectrogram 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -6
- package/dist/index.d.mts +96 -1
- package/dist/index.d.ts +96 -1
- package/dist/index.js +223 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +224 -51
- package/dist/index.mjs.map +1 -1
- package/dist/orchestrator/index.d.mts +51 -1
- package/dist/orchestrator/index.mjs +193 -51
- package/dist/orchestrator/index.mjs.map +1 -1
- package/dist/worker/spectrogram.worker.mjs +111 -37
- package/dist/worker/spectrogram.worker.mjs.map +1 -1
- package/package.json +8 -6
package/dist/index.js
CHANGED
|
@@ -40,7 +40,10 @@ __export(src_exports, {
|
|
|
40
40
|
createSpectrogramWorkerPool: () => createSpectrogramWorkerPool,
|
|
41
41
|
getColorMap: () => getColorMap,
|
|
42
42
|
getFrequencyScale: () => getFrequencyScale,
|
|
43
|
-
groupContiguousChunks: () => groupContiguousChunks
|
|
43
|
+
groupContiguousChunks: () => groupContiguousChunks,
|
|
44
|
+
groupRenderableChunks: () => groupRenderableChunks,
|
|
45
|
+
pixelColumnToFrame: () => pixelColumnToFrame,
|
|
46
|
+
pixelRowToBin: () => pixelRowToBin
|
|
44
47
|
});
|
|
45
48
|
module.exports = __toCommonJS(src_exports);
|
|
46
49
|
|
|
@@ -237,6 +240,9 @@ function computeSpectrogramMono(audioBuffer, config = {}, offsetSamples = 0, dur
|
|
|
237
240
|
|
|
238
241
|
// src/computation/colorMaps.ts
|
|
239
242
|
function interpolateLUT(stops) {
|
|
243
|
+
if (stops.length === 0) {
|
|
244
|
+
throw new Error("[spectrogram] custom color map requires at least one color stop");
|
|
245
|
+
}
|
|
240
246
|
const lut = new Uint8Array(256 * 3);
|
|
241
247
|
const n = stops.length;
|
|
242
248
|
for (let i = 0; i < 256; i++) {
|
|
@@ -3433,6 +3439,35 @@ function getFrequencyScale(name) {
|
|
|
3433
3439
|
}
|
|
3434
3440
|
}
|
|
3435
3441
|
|
|
3442
|
+
// src/computation/renderGeometry.ts
|
|
3443
|
+
function pixelColumnToFrame(params) {
|
|
3444
|
+
const fileSample = params.clipOffsetSamples + params.pixelX * params.samplesPerPixel;
|
|
3445
|
+
return Math.floor((fileSample - params.fftStartSample) / params.hopSize);
|
|
3446
|
+
}
|
|
3447
|
+
function pixelRowToBin(params) {
|
|
3448
|
+
const { normalizedY, frequencyBinCount, sampleRate, minFrequency, maxFrequency } = params;
|
|
3449
|
+
const nyquist = sampleRate / 2;
|
|
3450
|
+
let bin;
|
|
3451
|
+
if (params.isLinear) {
|
|
3452
|
+
const freq = minFrequency + normalizedY * (maxFrequency - minFrequency);
|
|
3453
|
+
bin = Math.floor(freq / nyquist * frequencyBinCount);
|
|
3454
|
+
} else {
|
|
3455
|
+
let lo = 0;
|
|
3456
|
+
let hi = frequencyBinCount - 1;
|
|
3457
|
+
while (lo < hi) {
|
|
3458
|
+
const mid = lo + hi >> 1;
|
|
3459
|
+
const freq = mid / frequencyBinCount * nyquist;
|
|
3460
|
+
if (params.scaleFn(freq, minFrequency, maxFrequency) < normalizedY) {
|
|
3461
|
+
lo = mid + 1;
|
|
3462
|
+
} else {
|
|
3463
|
+
hi = mid;
|
|
3464
|
+
}
|
|
3465
|
+
}
|
|
3466
|
+
bin = lo;
|
|
3467
|
+
}
|
|
3468
|
+
return Math.min(bin, frequencyBinCount - 1);
|
|
3469
|
+
}
|
|
3470
|
+
|
|
3436
3471
|
// src/worker/createSpectrogramWorker.ts
|
|
3437
3472
|
var SpectrogramAbortError = class extends Error {
|
|
3438
3473
|
constructor() {
|
|
@@ -3472,7 +3507,6 @@ function createSpectrogramWorker(worker) {
|
|
|
3472
3507
|
}
|
|
3473
3508
|
};
|
|
3474
3509
|
worker.onerror = (e) => {
|
|
3475
|
-
terminated = true;
|
|
3476
3510
|
console.error(
|
|
3477
3511
|
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
3478
3512
|
);
|
|
@@ -3576,10 +3610,10 @@ function createSpectrogramWorker(worker) {
|
|
|
3576
3610
|
|
|
3577
3611
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
3578
3612
|
function parseChannelFromCanvasId(canvasId) {
|
|
3579
|
-
const match = canvasId.match(/-ch(\d+)
|
|
3613
|
+
const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
|
|
3580
3614
|
if (!match) {
|
|
3581
3615
|
console.warn(
|
|
3582
|
-
"[dawcore-spectrogram] canvas ID missing -ch{N}-
|
|
3616
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
|
|
3583
3617
|
);
|
|
3584
3618
|
return 0;
|
|
3585
3619
|
}
|
|
@@ -3588,8 +3622,17 @@ function parseChannelFromCanvasId(canvasId) {
|
|
|
3588
3622
|
function defaultPoolSize() {
|
|
3589
3623
|
return 2;
|
|
3590
3624
|
}
|
|
3625
|
+
var MAX_POOL_CHANNELS = 32;
|
|
3591
3626
|
function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
|
|
3627
|
+
if (poolSize > MAX_POOL_CHANNELS) {
|
|
3628
|
+
console.warn(
|
|
3629
|
+
"[dawcore-spectrogram] workerPoolSize " + poolSize + " exceeds the channel cap (" + MAX_POOL_CHANNELS + ") \u2014 clamping; channels beyond the cap are not servable"
|
|
3630
|
+
);
|
|
3631
|
+
poolSize = MAX_POOL_CHANNELS;
|
|
3632
|
+
}
|
|
3592
3633
|
const workers = [];
|
|
3634
|
+
let terminated = false;
|
|
3635
|
+
const registeredAudio = /* @__PURE__ */ new Map();
|
|
3593
3636
|
let failedAt = -1;
|
|
3594
3637
|
try {
|
|
3595
3638
|
for (let i = 0; i < poolSize; i++) {
|
|
@@ -3611,59 +3654,111 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3611
3654
|
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
3612
3655
|
);
|
|
3613
3656
|
}
|
|
3614
|
-
function
|
|
3615
|
-
|
|
3657
|
+
function assertValidChannelIndex(channelIndex) {
|
|
3658
|
+
if (!Number.isInteger(channelIndex) || channelIndex < 0 || channelIndex >= MAX_POOL_CHANNELS) {
|
|
3659
|
+
throw new Error(
|
|
3660
|
+
"[dawcore-spectrogram] invalid channel index " + channelIndex + " (expected an integer in 0.." + (MAX_POOL_CHANNELS - 1) + ")"
|
|
3661
|
+
);
|
|
3662
|
+
}
|
|
3663
|
+
}
|
|
3664
|
+
function ensureWorkerForChannel(channelIndex) {
|
|
3665
|
+
assertValidChannelIndex(channelIndex);
|
|
3666
|
+
if (terminated) {
|
|
3667
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
3668
|
+
}
|
|
3669
|
+
while (workers.length <= channelIndex) {
|
|
3670
|
+
const w = createSpectrogramWorker(createWorker());
|
|
3671
|
+
const workerIndex = workers.length;
|
|
3672
|
+
for (const [clipId, entry] of registeredAudio) {
|
|
3673
|
+
if (entry.channelDataArrays.length > workerIndex) {
|
|
3674
|
+
w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
|
|
3675
|
+
}
|
|
3676
|
+
}
|
|
3677
|
+
workers.push(w);
|
|
3678
|
+
}
|
|
3679
|
+
return workers[channelIndex];
|
|
3616
3680
|
}
|
|
3617
3681
|
return {
|
|
3618
3682
|
async computeFFT(params, generation = 0) {
|
|
3683
|
+
if (terminated) {
|
|
3684
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
3685
|
+
}
|
|
3686
|
+
const channelCount = params.channelDataArrays.length || registeredAudio.get(params.clipId)?.channelDataArrays.length || 0;
|
|
3687
|
+
if (channelCount === 0) {
|
|
3688
|
+
throw new Error(
|
|
3689
|
+
"[dawcore-spectrogram] computeFFT called with no channel data \u2014 pass channelDataArrays or registerAudioData the clip first"
|
|
3690
|
+
);
|
|
3691
|
+
}
|
|
3619
3692
|
if (params.mono) {
|
|
3620
3693
|
return workers[0].computeFFT(params, generation);
|
|
3621
3694
|
}
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3695
|
+
if (channelCount > MAX_POOL_CHANNELS) {
|
|
3696
|
+
throw new Error(
|
|
3697
|
+
"[dawcore-spectrogram] computeFFT: " + channelCount + " channels exceeds the pool channel cap (" + MAX_POOL_CHANNELS + ")"
|
|
3698
|
+
);
|
|
3699
|
+
}
|
|
3700
|
+
const promises = Array.from(
|
|
3701
|
+
{ length: channelCount },
|
|
3702
|
+
(_, i) => ensureWorkerForChannel(i).computeFFT({ ...params, channelFilter: i }, generation)
|
|
3626
3703
|
);
|
|
3627
3704
|
const settled = await Promise.allSettled(promises);
|
|
3628
3705
|
const failures = settled.filter((s) => s.status === "rejected");
|
|
3629
3706
|
if (failures.length > 0) {
|
|
3630
|
-
|
|
3707
|
+
const realErrors = failures.filter((f) => !(f.reason instanceof SpectrogramAbortError));
|
|
3708
|
+
for (let i = 1; i < realErrors.length; i++) {
|
|
3631
3709
|
console.warn(
|
|
3632
|
-
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (
|
|
3710
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (realErrors[i].reason instanceof Error ? realErrors[i].reason.message : String(realErrors[i].reason))
|
|
3633
3711
|
);
|
|
3634
3712
|
}
|
|
3635
|
-
throw failures[0].reason;
|
|
3713
|
+
throw (realErrors[0] ?? failures[0]).reason;
|
|
3636
3714
|
}
|
|
3637
3715
|
return settled[0].value;
|
|
3638
3716
|
},
|
|
3639
|
-
|
|
3640
|
-
|
|
3717
|
+
// async so ensureWorkerForChannel's policy throws surface as rejections,
|
|
3718
|
+
// matching computeFFT, instead of escaping synchronously.
|
|
3719
|
+
async renderChunks(params, generation = 0) {
|
|
3720
|
+
const worker = ensureWorkerForChannel(params.channelIndex);
|
|
3641
3721
|
return worker.renderChunks({ ...params, channelIndex: 0 }, generation);
|
|
3642
3722
|
},
|
|
3643
3723
|
abortGeneration(generation) {
|
|
3724
|
+
if (terminated) return;
|
|
3644
3725
|
for (const w of workers) {
|
|
3645
3726
|
w.abortGeneration(generation);
|
|
3646
3727
|
}
|
|
3647
3728
|
},
|
|
3648
3729
|
registerCanvas(canvasId, canvas) {
|
|
3649
|
-
|
|
3650
|
-
|
|
3730
|
+
try {
|
|
3731
|
+
ensureWorkerForChannel(parseChannelFromCanvasId(canvasId)).registerCanvas(canvasId, canvas);
|
|
3732
|
+
} catch (err) {
|
|
3733
|
+
console.warn(
|
|
3734
|
+
'[dawcore-spectrogram] registerCanvas("' + canvasId + '") rejected \u2014 canvas not registered: ' + (err instanceof Error ? err.message : String(err))
|
|
3735
|
+
);
|
|
3736
|
+
}
|
|
3651
3737
|
},
|
|
3652
3738
|
unregisterCanvas(canvasId) {
|
|
3653
|
-
|
|
3654
|
-
|
|
3739
|
+
if (terminated) return;
|
|
3740
|
+
const worker = workers[parseChannelFromCanvasId(canvasId)];
|
|
3741
|
+
if (!worker) return;
|
|
3742
|
+
worker.unregisterCanvas(canvasId);
|
|
3655
3743
|
},
|
|
3656
3744
|
registerAudioData(clipId, channelDataArrays, sampleRate) {
|
|
3657
|
-
|
|
3658
|
-
|
|
3745
|
+
if (terminated) return;
|
|
3746
|
+
registeredAudio.set(clipId, { channelDataArrays, sampleRate });
|
|
3747
|
+
const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
|
|
3748
|
+
for (let i = 0; i < relevantWorkers; i++) {
|
|
3749
|
+
workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
|
|
3659
3750
|
}
|
|
3660
3751
|
},
|
|
3661
3752
|
unregisterAudioData(clipId) {
|
|
3753
|
+
if (terminated) return;
|
|
3754
|
+
registeredAudio.delete(clipId);
|
|
3662
3755
|
for (const w of workers) {
|
|
3663
3756
|
w.unregisterAudioData(clipId);
|
|
3664
3757
|
}
|
|
3665
3758
|
},
|
|
3666
3759
|
terminate() {
|
|
3760
|
+
terminated = true;
|
|
3761
|
+
registeredAudio.clear();
|
|
3667
3762
|
for (const w of workers) {
|
|
3668
3763
|
w.terminate();
|
|
3669
3764
|
}
|
|
@@ -3737,6 +3832,20 @@ function groupContiguousChunks(chunks) {
|
|
|
3737
3832
|
groups.push(current);
|
|
3738
3833
|
return groups;
|
|
3739
3834
|
}
|
|
3835
|
+
function groupRenderableChunks(chunks) {
|
|
3836
|
+
const partitions = /* @__PURE__ */ new Map();
|
|
3837
|
+
for (const chunk of chunks) {
|
|
3838
|
+
const key = chunk.clipId + " " + chunk.channelIndex;
|
|
3839
|
+
const list = partitions.get(key) ?? [];
|
|
3840
|
+
list.push(chunk);
|
|
3841
|
+
partitions.set(key, list);
|
|
3842
|
+
}
|
|
3843
|
+
const groups = [];
|
|
3844
|
+
for (const partition of partitions.values()) {
|
|
3845
|
+
groups.push(...groupContiguousChunks(partition));
|
|
3846
|
+
}
|
|
3847
|
+
return groups;
|
|
3848
|
+
}
|
|
3740
3849
|
|
|
3741
3850
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3742
3851
|
var SpectrogramOrchestrator = class extends EventTarget {
|
|
@@ -3749,6 +3858,12 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3749
3858
|
this.colorLUT = new ColorLUTCache();
|
|
3750
3859
|
this.disposed = false;
|
|
3751
3860
|
this.renderInFlight = false;
|
|
3861
|
+
this.renderQueued = false;
|
|
3862
|
+
// Per-clip timeline origin (globalPixelOffset - chunkIndex * MAX_CANVAS_WIDTH)
|
|
3863
|
+
// with a live canvas count — used to detect layout-contract violations.
|
|
3864
|
+
// The origin resets once all of a clip's canvases unregister, so a moved
|
|
3865
|
+
// clip re-registering at a new position doesn't false-positive.
|
|
3866
|
+
this.clipOrigins = /* @__PURE__ */ new Map();
|
|
3752
3867
|
// Tracks which trackIds have already emitted `viewport-ready` for the
|
|
3753
3868
|
// current generation. Cleared on every generation bump (setViewport with a
|
|
3754
3869
|
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
@@ -3809,16 +3924,34 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3809
3924
|
channelIndex: reg.channelIndex,
|
|
3810
3925
|
chunkIndex: reg.chunkIndex
|
|
3811
3926
|
});
|
|
3927
|
+
const originPx = reg.globalPixelOffset - reg.chunkIndex * import_core.MAX_CANVAS_WIDTH;
|
|
3928
|
+
const origin = this.clipOrigins.get(reg.clipId);
|
|
3929
|
+
if (!origin) {
|
|
3930
|
+
this.clipOrigins.set(reg.clipId, { originPx, canvasCount: 1 });
|
|
3931
|
+
} else {
|
|
3932
|
+
origin.canvasCount += 1;
|
|
3933
|
+
if (origin.originPx !== originPx) {
|
|
3934
|
+
console.warn(
|
|
3935
|
+
"[dawcore-spectrogram] registerCanvas: " + reg.canvasId + " violates the chunk layout contract (chunk " + reg.chunkIndex + " at " + reg.globalPixelOffset + "px implies clip origin " + originPx + "px, but other canvases imply " + origin.originPx + "px) \u2014 sample math assumes chunkIndex * " + import_core.MAX_CANVAS_WIDTH + "px and this clip will render shifted audio"
|
|
3936
|
+
);
|
|
3937
|
+
}
|
|
3938
|
+
}
|
|
3812
3939
|
this.pool.registerCanvas(reg.canvasId, reg.canvas);
|
|
3813
3940
|
if (this.viewport) this.scheduleRender();
|
|
3814
3941
|
}
|
|
3815
3942
|
unregisterCanvas(canvasId) {
|
|
3816
3943
|
if (this.disposed) return;
|
|
3817
|
-
|
|
3944
|
+
const entry = this.canvases.get(canvasId);
|
|
3945
|
+
if (!entry) {
|
|
3818
3946
|
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3819
3947
|
return;
|
|
3820
3948
|
}
|
|
3821
3949
|
this.canvases.delete(canvasId);
|
|
3950
|
+
const origin = this.clipOrigins.get(entry.clipId);
|
|
3951
|
+
if (origin) {
|
|
3952
|
+
origin.canvasCount -= 1;
|
|
3953
|
+
if (origin.canvasCount <= 0) this.clipOrigins.delete(entry.clipId);
|
|
3954
|
+
}
|
|
3822
3955
|
this.pool.unregisterCanvas(canvasId);
|
|
3823
3956
|
}
|
|
3824
3957
|
setViewport(state) {
|
|
@@ -3830,30 +3963,21 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3830
3963
|
return;
|
|
3831
3964
|
}
|
|
3832
3965
|
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3833
|
-
|
|
3834
|
-
this.generation += 1;
|
|
3835
|
-
this.readyDispatched.clear();
|
|
3836
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3966
|
+
this.bumpGenerationAndAbortStale();
|
|
3837
3967
|
this.viewport = state;
|
|
3838
3968
|
this.scheduleRender();
|
|
3839
3969
|
}
|
|
3840
3970
|
setConfig(config) {
|
|
3841
3971
|
if (this.disposed) return;
|
|
3842
3972
|
this.config = config;
|
|
3843
|
-
|
|
3844
|
-
this.generation += 1;
|
|
3845
|
-
this.readyDispatched.clear();
|
|
3846
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3973
|
+
this.bumpGenerationAndAbortStale();
|
|
3847
3974
|
this.colorLUT.clear();
|
|
3848
3975
|
this.scheduleRender();
|
|
3849
3976
|
}
|
|
3850
3977
|
setColorMap(colorMap) {
|
|
3851
3978
|
if (this.disposed) return;
|
|
3852
3979
|
this.colorMap = colorMap;
|
|
3853
|
-
|
|
3854
|
-
this.generation += 1;
|
|
3855
|
-
this.readyDispatched.clear();
|
|
3856
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3980
|
+
this.bumpGenerationAndAbortStale();
|
|
3857
3981
|
this.scheduleRender();
|
|
3858
3982
|
}
|
|
3859
3983
|
setDevicePixelRatio(dpr) {
|
|
@@ -3866,36 +3990,64 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3866
3990
|
}
|
|
3867
3991
|
if (this.devicePixelRatio === dpr) return;
|
|
3868
3992
|
this.devicePixelRatio = dpr;
|
|
3869
|
-
|
|
3993
|
+
this.bumpGenerationAndAbortStale();
|
|
3994
|
+
this.scheduleRender();
|
|
3995
|
+
}
|
|
3996
|
+
/**
|
|
3997
|
+
* Start a new render generation and cancel in-flight work from older ones.
|
|
3998
|
+
*
|
|
3999
|
+
* The worker treats work as stale iff `generation < latestGeneration`, so
|
|
4000
|
+
* the abort message must carry the NEW generation — passing the previous
|
|
4001
|
+
* one leaves its in-flight FFTs running to completion (#555).
|
|
4002
|
+
*/
|
|
4003
|
+
bumpGenerationAndAbortStale() {
|
|
3870
4004
|
this.generation += 1;
|
|
3871
4005
|
this.readyDispatched.clear();
|
|
3872
|
-
this.pool.abortGeneration(
|
|
3873
|
-
this.scheduleRender();
|
|
4006
|
+
this.pool.abortGeneration(this.generation);
|
|
3874
4007
|
}
|
|
3875
4008
|
dispose() {
|
|
3876
4009
|
if (this.disposed) return;
|
|
3877
4010
|
this.disposed = true;
|
|
3878
4011
|
this.clips.clear();
|
|
3879
4012
|
this.canvases.clear();
|
|
4013
|
+
this.clipOrigins.clear();
|
|
3880
4014
|
this.viewport = null;
|
|
3881
4015
|
this.colorLUT.clear();
|
|
3882
4016
|
this.readyDispatched.clear();
|
|
3883
4017
|
this.pool.terminate();
|
|
3884
4018
|
}
|
|
4019
|
+
/**
|
|
4020
|
+
* Coalesce render requests: `renderInFlight` is held for the WHOLE async
|
|
4021
|
+
* render, not just until the microtask fires — a request arriving
|
|
4022
|
+
* mid-render (e.g. a late registerCanvas) queues one follow-up run instead
|
|
4023
|
+
* of starting a concurrent runRender for the same generation (#558).
|
|
4024
|
+
*/
|
|
3885
4025
|
scheduleRender() {
|
|
3886
|
-
if (this.renderInFlight) return;
|
|
3887
4026
|
if (!this.viewport) return;
|
|
4027
|
+
if (this.renderInFlight) {
|
|
4028
|
+
this.renderQueued = true;
|
|
4029
|
+
return;
|
|
4030
|
+
}
|
|
3888
4031
|
this.renderInFlight = true;
|
|
3889
4032
|
queueMicrotask(() => {
|
|
3890
|
-
this.
|
|
3891
|
-
this.runRender(this.generation).catch((err) => {
|
|
3892
|
-
if (err instanceof SpectrogramAbortError) return;
|
|
3893
|
-
console.warn(
|
|
3894
|
-
"[dawcore-spectrogram] runRender unhandled rejection (generation " + this.generation + "): " + (err instanceof Error ? err.message : String(err))
|
|
3895
|
-
);
|
|
3896
|
-
});
|
|
4033
|
+
void this.runRenderLoop();
|
|
3897
4034
|
});
|
|
3898
4035
|
}
|
|
4036
|
+
async runRenderLoop() {
|
|
4037
|
+
try {
|
|
4038
|
+
do {
|
|
4039
|
+
this.renderQueued = false;
|
|
4040
|
+
await this.runRender(this.generation).catch((err) => {
|
|
4041
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
4042
|
+
console.warn(
|
|
4043
|
+
"[dawcore-spectrogram] runRender unhandled rejection (generation " + this.generation + "): " + (err instanceof Error ? err.message : String(err))
|
|
4044
|
+
);
|
|
4045
|
+
});
|
|
4046
|
+
} while (this.renderQueued && !this.disposed);
|
|
4047
|
+
} finally {
|
|
4048
|
+
this.renderInFlight = false;
|
|
4049
|
+
}
|
|
4050
|
+
}
|
|
3899
4051
|
async runRender(generation) {
|
|
3900
4052
|
if (this.disposed) return;
|
|
3901
4053
|
const viewport = this.viewport;
|
|
@@ -3939,7 +4091,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3939
4091
|
}
|
|
3940
4092
|
async renderTier(canvases, generation, viewport) {
|
|
3941
4093
|
if (canvases.length === 0) return;
|
|
3942
|
-
const groups =
|
|
4094
|
+
const groups = groupRenderableChunks(canvases);
|
|
3943
4095
|
for (const group of groups) {
|
|
3944
4096
|
if (this.generation !== generation || this.disposed) return;
|
|
3945
4097
|
await this.renderGroup(group, generation, viewport);
|
|
@@ -3955,9 +4107,26 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3955
4107
|
);
|
|
3956
4108
|
return;
|
|
3957
4109
|
}
|
|
4110
|
+
try {
|
|
4111
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
4112
|
+
} catch (err) {
|
|
4113
|
+
if (this.generation !== generation || this.disposed) throw err;
|
|
4114
|
+
if (err instanceof Error && err.message.includes("cache-miss")) {
|
|
4115
|
+
console.warn(
|
|
4116
|
+
"[dawcore-spectrogram] FFT cache entry evicted between compute and render \u2014 retrying group once"
|
|
4117
|
+
);
|
|
4118
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
4119
|
+
return;
|
|
4120
|
+
}
|
|
4121
|
+
throw err;
|
|
4122
|
+
}
|
|
4123
|
+
}
|
|
4124
|
+
async renderGroupOnce(group, generation, viewport, clip) {
|
|
4125
|
+
const first = group[0];
|
|
3958
4126
|
const fftSize = this.config.fftSize ?? import_core.SPECTROGRAM_DEFAULTS.fftSize;
|
|
3959
|
-
const
|
|
3960
|
-
const
|
|
4127
|
+
const clipRelativeOffsets = group.map((c) => c.chunkIndex * import_core.MAX_CANVAS_WIDTH);
|
|
4128
|
+
const startPx = Math.min(...clipRelativeOffsets);
|
|
4129
|
+
const endPx = Math.max(...group.map((c, i) => clipRelativeOffsets[i] + c.widthPx));
|
|
3961
4130
|
const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
|
|
3962
4131
|
const endSample = Math.min(
|
|
3963
4132
|
clip.offsetSamples + clip.durationSamples,
|
|
@@ -3985,7 +4154,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3985
4154
|
cacheKey: cacheKey2,
|
|
3986
4155
|
canvasIds: group.map((c) => c.canvasId),
|
|
3987
4156
|
canvasWidths: group.map((c) => c.widthPx),
|
|
3988
|
-
globalPixelOffsets:
|
|
4157
|
+
globalPixelOffsets: clipRelativeOffsets,
|
|
3989
4158
|
canvasHeight: first.heightPx,
|
|
3990
4159
|
devicePixelRatio: this.devicePixelRatio,
|
|
3991
4160
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
@@ -4002,7 +4171,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
4002
4171
|
}
|
|
4003
4172
|
async renderRemainingViaIdle(canvases, generation, viewport) {
|
|
4004
4173
|
if (canvases.length === 0) return;
|
|
4005
|
-
const groups =
|
|
4174
|
+
const groups = groupRenderableChunks(canvases);
|
|
4006
4175
|
for (const group of groups) {
|
|
4007
4176
|
if (this.generation !== generation || this.disposed) return;
|
|
4008
4177
|
await this.yieldUntilIdle();
|
|
@@ -4044,6 +4213,9 @@ function viewportsEqual(a, b) {
|
|
|
4044
4213
|
createSpectrogramWorkerPool,
|
|
4045
4214
|
getColorMap,
|
|
4046
4215
|
getFrequencyScale,
|
|
4047
|
-
groupContiguousChunks
|
|
4216
|
+
groupContiguousChunks,
|
|
4217
|
+
groupRenderableChunks,
|
|
4218
|
+
pixelColumnToFrame,
|
|
4219
|
+
pixelRowToBin
|
|
4048
4220
|
});
|
|
4049
4221
|
//# sourceMappingURL=index.js.map
|