@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.mjs
CHANGED
|
@@ -191,6 +191,9 @@ function computeSpectrogramMono(audioBuffer, config = {}, offsetSamples = 0, dur
|
|
|
191
191
|
|
|
192
192
|
// src/computation/colorMaps.ts
|
|
193
193
|
function interpolateLUT(stops) {
|
|
194
|
+
if (stops.length === 0) {
|
|
195
|
+
throw new Error("[spectrogram] custom color map requires at least one color stop");
|
|
196
|
+
}
|
|
194
197
|
const lut = new Uint8Array(256 * 3);
|
|
195
198
|
const n = stops.length;
|
|
196
199
|
for (let i = 0; i < 256; i++) {
|
|
@@ -3387,6 +3390,35 @@ function getFrequencyScale(name) {
|
|
|
3387
3390
|
}
|
|
3388
3391
|
}
|
|
3389
3392
|
|
|
3393
|
+
// src/computation/renderGeometry.ts
|
|
3394
|
+
function pixelColumnToFrame(params) {
|
|
3395
|
+
const fileSample = params.clipOffsetSamples + params.pixelX * params.samplesPerPixel;
|
|
3396
|
+
return Math.floor((fileSample - params.fftStartSample) / params.hopSize);
|
|
3397
|
+
}
|
|
3398
|
+
function pixelRowToBin(params) {
|
|
3399
|
+
const { normalizedY, frequencyBinCount, sampleRate, minFrequency, maxFrequency } = params;
|
|
3400
|
+
const nyquist = sampleRate / 2;
|
|
3401
|
+
let bin;
|
|
3402
|
+
if (params.isLinear) {
|
|
3403
|
+
const freq = minFrequency + normalizedY * (maxFrequency - minFrequency);
|
|
3404
|
+
bin = Math.floor(freq / nyquist * frequencyBinCount);
|
|
3405
|
+
} else {
|
|
3406
|
+
let lo = 0;
|
|
3407
|
+
let hi = frequencyBinCount - 1;
|
|
3408
|
+
while (lo < hi) {
|
|
3409
|
+
const mid = lo + hi >> 1;
|
|
3410
|
+
const freq = mid / frequencyBinCount * nyquist;
|
|
3411
|
+
if (params.scaleFn(freq, minFrequency, maxFrequency) < normalizedY) {
|
|
3412
|
+
lo = mid + 1;
|
|
3413
|
+
} else {
|
|
3414
|
+
hi = mid;
|
|
3415
|
+
}
|
|
3416
|
+
}
|
|
3417
|
+
bin = lo;
|
|
3418
|
+
}
|
|
3419
|
+
return Math.min(bin, frequencyBinCount - 1);
|
|
3420
|
+
}
|
|
3421
|
+
|
|
3390
3422
|
// src/worker/createSpectrogramWorker.ts
|
|
3391
3423
|
var SpectrogramAbortError = class extends Error {
|
|
3392
3424
|
constructor() {
|
|
@@ -3426,7 +3458,6 @@ function createSpectrogramWorker(worker) {
|
|
|
3426
3458
|
}
|
|
3427
3459
|
};
|
|
3428
3460
|
worker.onerror = (e) => {
|
|
3429
|
-
terminated = true;
|
|
3430
3461
|
console.error(
|
|
3431
3462
|
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
3432
3463
|
);
|
|
@@ -3530,10 +3561,10 @@ function createSpectrogramWorker(worker) {
|
|
|
3530
3561
|
|
|
3531
3562
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
3532
3563
|
function parseChannelFromCanvasId(canvasId) {
|
|
3533
|
-
const match = canvasId.match(/-ch(\d+)
|
|
3564
|
+
const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
|
|
3534
3565
|
if (!match) {
|
|
3535
3566
|
console.warn(
|
|
3536
|
-
"[dawcore-spectrogram] canvas ID missing -ch{N}-
|
|
3567
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
|
|
3537
3568
|
);
|
|
3538
3569
|
return 0;
|
|
3539
3570
|
}
|
|
@@ -3542,8 +3573,17 @@ function parseChannelFromCanvasId(canvasId) {
|
|
|
3542
3573
|
function defaultPoolSize() {
|
|
3543
3574
|
return 2;
|
|
3544
3575
|
}
|
|
3576
|
+
var MAX_POOL_CHANNELS = 32;
|
|
3545
3577
|
function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
|
|
3578
|
+
if (poolSize > MAX_POOL_CHANNELS) {
|
|
3579
|
+
console.warn(
|
|
3580
|
+
"[dawcore-spectrogram] workerPoolSize " + poolSize + " exceeds the channel cap (" + MAX_POOL_CHANNELS + ") \u2014 clamping; channels beyond the cap are not servable"
|
|
3581
|
+
);
|
|
3582
|
+
poolSize = MAX_POOL_CHANNELS;
|
|
3583
|
+
}
|
|
3546
3584
|
const workers = [];
|
|
3585
|
+
let terminated = false;
|
|
3586
|
+
const registeredAudio = /* @__PURE__ */ new Map();
|
|
3547
3587
|
let failedAt = -1;
|
|
3548
3588
|
try {
|
|
3549
3589
|
for (let i = 0; i < poolSize; i++) {
|
|
@@ -3565,59 +3605,111 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3565
3605
|
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
3566
3606
|
);
|
|
3567
3607
|
}
|
|
3568
|
-
function
|
|
3569
|
-
|
|
3608
|
+
function assertValidChannelIndex(channelIndex) {
|
|
3609
|
+
if (!Number.isInteger(channelIndex) || channelIndex < 0 || channelIndex >= MAX_POOL_CHANNELS) {
|
|
3610
|
+
throw new Error(
|
|
3611
|
+
"[dawcore-spectrogram] invalid channel index " + channelIndex + " (expected an integer in 0.." + (MAX_POOL_CHANNELS - 1) + ")"
|
|
3612
|
+
);
|
|
3613
|
+
}
|
|
3614
|
+
}
|
|
3615
|
+
function ensureWorkerForChannel(channelIndex) {
|
|
3616
|
+
assertValidChannelIndex(channelIndex);
|
|
3617
|
+
if (terminated) {
|
|
3618
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
3619
|
+
}
|
|
3620
|
+
while (workers.length <= channelIndex) {
|
|
3621
|
+
const w = createSpectrogramWorker(createWorker());
|
|
3622
|
+
const workerIndex = workers.length;
|
|
3623
|
+
for (const [clipId, entry] of registeredAudio) {
|
|
3624
|
+
if (entry.channelDataArrays.length > workerIndex) {
|
|
3625
|
+
w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
|
|
3626
|
+
}
|
|
3627
|
+
}
|
|
3628
|
+
workers.push(w);
|
|
3629
|
+
}
|
|
3630
|
+
return workers[channelIndex];
|
|
3570
3631
|
}
|
|
3571
3632
|
return {
|
|
3572
3633
|
async computeFFT(params, generation = 0) {
|
|
3634
|
+
if (terminated) {
|
|
3635
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
3636
|
+
}
|
|
3637
|
+
const channelCount = params.channelDataArrays.length || registeredAudio.get(params.clipId)?.channelDataArrays.length || 0;
|
|
3638
|
+
if (channelCount === 0) {
|
|
3639
|
+
throw new Error(
|
|
3640
|
+
"[dawcore-spectrogram] computeFFT called with no channel data \u2014 pass channelDataArrays or registerAudioData the clip first"
|
|
3641
|
+
);
|
|
3642
|
+
}
|
|
3573
3643
|
if (params.mono) {
|
|
3574
3644
|
return workers[0].computeFFT(params, generation);
|
|
3575
3645
|
}
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3646
|
+
if (channelCount > MAX_POOL_CHANNELS) {
|
|
3647
|
+
throw new Error(
|
|
3648
|
+
"[dawcore-spectrogram] computeFFT: " + channelCount + " channels exceeds the pool channel cap (" + MAX_POOL_CHANNELS + ")"
|
|
3649
|
+
);
|
|
3650
|
+
}
|
|
3651
|
+
const promises = Array.from(
|
|
3652
|
+
{ length: channelCount },
|
|
3653
|
+
(_, i) => ensureWorkerForChannel(i).computeFFT({ ...params, channelFilter: i }, generation)
|
|
3580
3654
|
);
|
|
3581
3655
|
const settled = await Promise.allSettled(promises);
|
|
3582
3656
|
const failures = settled.filter((s) => s.status === "rejected");
|
|
3583
3657
|
if (failures.length > 0) {
|
|
3584
|
-
|
|
3658
|
+
const realErrors = failures.filter((f) => !(f.reason instanceof SpectrogramAbortError));
|
|
3659
|
+
for (let i = 1; i < realErrors.length; i++) {
|
|
3585
3660
|
console.warn(
|
|
3586
|
-
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (
|
|
3661
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (realErrors[i].reason instanceof Error ? realErrors[i].reason.message : String(realErrors[i].reason))
|
|
3587
3662
|
);
|
|
3588
3663
|
}
|
|
3589
|
-
throw failures[0].reason;
|
|
3664
|
+
throw (realErrors[0] ?? failures[0]).reason;
|
|
3590
3665
|
}
|
|
3591
3666
|
return settled[0].value;
|
|
3592
3667
|
},
|
|
3593
|
-
|
|
3594
|
-
|
|
3668
|
+
// async so ensureWorkerForChannel's policy throws surface as rejections,
|
|
3669
|
+
// matching computeFFT, instead of escaping synchronously.
|
|
3670
|
+
async renderChunks(params, generation = 0) {
|
|
3671
|
+
const worker = ensureWorkerForChannel(params.channelIndex);
|
|
3595
3672
|
return worker.renderChunks({ ...params, channelIndex: 0 }, generation);
|
|
3596
3673
|
},
|
|
3597
3674
|
abortGeneration(generation) {
|
|
3675
|
+
if (terminated) return;
|
|
3598
3676
|
for (const w of workers) {
|
|
3599
3677
|
w.abortGeneration(generation);
|
|
3600
3678
|
}
|
|
3601
3679
|
},
|
|
3602
3680
|
registerCanvas(canvasId, canvas) {
|
|
3603
|
-
|
|
3604
|
-
|
|
3681
|
+
try {
|
|
3682
|
+
ensureWorkerForChannel(parseChannelFromCanvasId(canvasId)).registerCanvas(canvasId, canvas);
|
|
3683
|
+
} catch (err) {
|
|
3684
|
+
console.warn(
|
|
3685
|
+
'[dawcore-spectrogram] registerCanvas("' + canvasId + '") rejected \u2014 canvas not registered: ' + (err instanceof Error ? err.message : String(err))
|
|
3686
|
+
);
|
|
3687
|
+
}
|
|
3605
3688
|
},
|
|
3606
3689
|
unregisterCanvas(canvasId) {
|
|
3607
|
-
|
|
3608
|
-
|
|
3690
|
+
if (terminated) return;
|
|
3691
|
+
const worker = workers[parseChannelFromCanvasId(canvasId)];
|
|
3692
|
+
if (!worker) return;
|
|
3693
|
+
worker.unregisterCanvas(canvasId);
|
|
3609
3694
|
},
|
|
3610
3695
|
registerAudioData(clipId, channelDataArrays, sampleRate) {
|
|
3611
|
-
|
|
3612
|
-
|
|
3696
|
+
if (terminated) return;
|
|
3697
|
+
registeredAudio.set(clipId, { channelDataArrays, sampleRate });
|
|
3698
|
+
const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
|
|
3699
|
+
for (let i = 0; i < relevantWorkers; i++) {
|
|
3700
|
+
workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
|
|
3613
3701
|
}
|
|
3614
3702
|
},
|
|
3615
3703
|
unregisterAudioData(clipId) {
|
|
3704
|
+
if (terminated) return;
|
|
3705
|
+
registeredAudio.delete(clipId);
|
|
3616
3706
|
for (const w of workers) {
|
|
3617
3707
|
w.unregisterAudioData(clipId);
|
|
3618
3708
|
}
|
|
3619
3709
|
},
|
|
3620
3710
|
terminate() {
|
|
3711
|
+
terminated = true;
|
|
3712
|
+
registeredAudio.clear();
|
|
3621
3713
|
for (const w of workers) {
|
|
3622
3714
|
w.terminate();
|
|
3623
3715
|
}
|
|
@@ -3626,7 +3718,11 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3626
3718
|
}
|
|
3627
3719
|
|
|
3628
3720
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3629
|
-
import {
|
|
3721
|
+
import {
|
|
3722
|
+
SPECTROGRAM_DEFAULTS,
|
|
3723
|
+
DEFAULT_SPECTROGRAM_COLOR_MAP,
|
|
3724
|
+
MAX_CANVAS_WIDTH
|
|
3725
|
+
} from "@waveform-playlist/core";
|
|
3630
3726
|
|
|
3631
3727
|
// src/orchestrator/color-lut-cache.ts
|
|
3632
3728
|
var ColorLUTCache = class {
|
|
@@ -3691,6 +3787,20 @@ function groupContiguousChunks(chunks) {
|
|
|
3691
3787
|
groups.push(current);
|
|
3692
3788
|
return groups;
|
|
3693
3789
|
}
|
|
3790
|
+
function groupRenderableChunks(chunks) {
|
|
3791
|
+
const partitions = /* @__PURE__ */ new Map();
|
|
3792
|
+
for (const chunk of chunks) {
|
|
3793
|
+
const key = chunk.clipId + " " + chunk.channelIndex;
|
|
3794
|
+
const list = partitions.get(key) ?? [];
|
|
3795
|
+
list.push(chunk);
|
|
3796
|
+
partitions.set(key, list);
|
|
3797
|
+
}
|
|
3798
|
+
const groups = [];
|
|
3799
|
+
for (const partition of partitions.values()) {
|
|
3800
|
+
groups.push(...groupContiguousChunks(partition));
|
|
3801
|
+
}
|
|
3802
|
+
return groups;
|
|
3803
|
+
}
|
|
3694
3804
|
|
|
3695
3805
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3696
3806
|
var SpectrogramOrchestrator = class extends EventTarget {
|
|
@@ -3703,6 +3813,12 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3703
3813
|
this.colorLUT = new ColorLUTCache();
|
|
3704
3814
|
this.disposed = false;
|
|
3705
3815
|
this.renderInFlight = false;
|
|
3816
|
+
this.renderQueued = false;
|
|
3817
|
+
// Per-clip timeline origin (globalPixelOffset - chunkIndex * MAX_CANVAS_WIDTH)
|
|
3818
|
+
// with a live canvas count — used to detect layout-contract violations.
|
|
3819
|
+
// The origin resets once all of a clip's canvases unregister, so a moved
|
|
3820
|
+
// clip re-registering at a new position doesn't false-positive.
|
|
3821
|
+
this.clipOrigins = /* @__PURE__ */ new Map();
|
|
3706
3822
|
// Tracks which trackIds have already emitted `viewport-ready` for the
|
|
3707
3823
|
// current generation. Cleared on every generation bump (setViewport with a
|
|
3708
3824
|
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
@@ -3763,16 +3879,34 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3763
3879
|
channelIndex: reg.channelIndex,
|
|
3764
3880
|
chunkIndex: reg.chunkIndex
|
|
3765
3881
|
});
|
|
3882
|
+
const originPx = reg.globalPixelOffset - reg.chunkIndex * MAX_CANVAS_WIDTH;
|
|
3883
|
+
const origin = this.clipOrigins.get(reg.clipId);
|
|
3884
|
+
if (!origin) {
|
|
3885
|
+
this.clipOrigins.set(reg.clipId, { originPx, canvasCount: 1 });
|
|
3886
|
+
} else {
|
|
3887
|
+
origin.canvasCount += 1;
|
|
3888
|
+
if (origin.originPx !== originPx) {
|
|
3889
|
+
console.warn(
|
|
3890
|
+
"[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 * " + MAX_CANVAS_WIDTH + "px and this clip will render shifted audio"
|
|
3891
|
+
);
|
|
3892
|
+
}
|
|
3893
|
+
}
|
|
3766
3894
|
this.pool.registerCanvas(reg.canvasId, reg.canvas);
|
|
3767
3895
|
if (this.viewport) this.scheduleRender();
|
|
3768
3896
|
}
|
|
3769
3897
|
unregisterCanvas(canvasId) {
|
|
3770
3898
|
if (this.disposed) return;
|
|
3771
|
-
|
|
3899
|
+
const entry = this.canvases.get(canvasId);
|
|
3900
|
+
if (!entry) {
|
|
3772
3901
|
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3773
3902
|
return;
|
|
3774
3903
|
}
|
|
3775
3904
|
this.canvases.delete(canvasId);
|
|
3905
|
+
const origin = this.clipOrigins.get(entry.clipId);
|
|
3906
|
+
if (origin) {
|
|
3907
|
+
origin.canvasCount -= 1;
|
|
3908
|
+
if (origin.canvasCount <= 0) this.clipOrigins.delete(entry.clipId);
|
|
3909
|
+
}
|
|
3776
3910
|
this.pool.unregisterCanvas(canvasId);
|
|
3777
3911
|
}
|
|
3778
3912
|
setViewport(state) {
|
|
@@ -3784,30 +3918,21 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3784
3918
|
return;
|
|
3785
3919
|
}
|
|
3786
3920
|
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3787
|
-
|
|
3788
|
-
this.generation += 1;
|
|
3789
|
-
this.readyDispatched.clear();
|
|
3790
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3921
|
+
this.bumpGenerationAndAbortStale();
|
|
3791
3922
|
this.viewport = state;
|
|
3792
3923
|
this.scheduleRender();
|
|
3793
3924
|
}
|
|
3794
3925
|
setConfig(config) {
|
|
3795
3926
|
if (this.disposed) return;
|
|
3796
3927
|
this.config = config;
|
|
3797
|
-
|
|
3798
|
-
this.generation += 1;
|
|
3799
|
-
this.readyDispatched.clear();
|
|
3800
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3928
|
+
this.bumpGenerationAndAbortStale();
|
|
3801
3929
|
this.colorLUT.clear();
|
|
3802
3930
|
this.scheduleRender();
|
|
3803
3931
|
}
|
|
3804
3932
|
setColorMap(colorMap) {
|
|
3805
3933
|
if (this.disposed) return;
|
|
3806
3934
|
this.colorMap = colorMap;
|
|
3807
|
-
|
|
3808
|
-
this.generation += 1;
|
|
3809
|
-
this.readyDispatched.clear();
|
|
3810
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3935
|
+
this.bumpGenerationAndAbortStale();
|
|
3811
3936
|
this.scheduleRender();
|
|
3812
3937
|
}
|
|
3813
3938
|
setDevicePixelRatio(dpr) {
|
|
@@ -3820,36 +3945,64 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3820
3945
|
}
|
|
3821
3946
|
if (this.devicePixelRatio === dpr) return;
|
|
3822
3947
|
this.devicePixelRatio = dpr;
|
|
3823
|
-
|
|
3948
|
+
this.bumpGenerationAndAbortStale();
|
|
3949
|
+
this.scheduleRender();
|
|
3950
|
+
}
|
|
3951
|
+
/**
|
|
3952
|
+
* Start a new render generation and cancel in-flight work from older ones.
|
|
3953
|
+
*
|
|
3954
|
+
* The worker treats work as stale iff `generation < latestGeneration`, so
|
|
3955
|
+
* the abort message must carry the NEW generation — passing the previous
|
|
3956
|
+
* one leaves its in-flight FFTs running to completion (#555).
|
|
3957
|
+
*/
|
|
3958
|
+
bumpGenerationAndAbortStale() {
|
|
3824
3959
|
this.generation += 1;
|
|
3825
3960
|
this.readyDispatched.clear();
|
|
3826
|
-
this.pool.abortGeneration(
|
|
3827
|
-
this.scheduleRender();
|
|
3961
|
+
this.pool.abortGeneration(this.generation);
|
|
3828
3962
|
}
|
|
3829
3963
|
dispose() {
|
|
3830
3964
|
if (this.disposed) return;
|
|
3831
3965
|
this.disposed = true;
|
|
3832
3966
|
this.clips.clear();
|
|
3833
3967
|
this.canvases.clear();
|
|
3968
|
+
this.clipOrigins.clear();
|
|
3834
3969
|
this.viewport = null;
|
|
3835
3970
|
this.colorLUT.clear();
|
|
3836
3971
|
this.readyDispatched.clear();
|
|
3837
3972
|
this.pool.terminate();
|
|
3838
3973
|
}
|
|
3974
|
+
/**
|
|
3975
|
+
* Coalesce render requests: `renderInFlight` is held for the WHOLE async
|
|
3976
|
+
* render, not just until the microtask fires — a request arriving
|
|
3977
|
+
* mid-render (e.g. a late registerCanvas) queues one follow-up run instead
|
|
3978
|
+
* of starting a concurrent runRender for the same generation (#558).
|
|
3979
|
+
*/
|
|
3839
3980
|
scheduleRender() {
|
|
3840
|
-
if (this.renderInFlight) return;
|
|
3841
3981
|
if (!this.viewport) return;
|
|
3982
|
+
if (this.renderInFlight) {
|
|
3983
|
+
this.renderQueued = true;
|
|
3984
|
+
return;
|
|
3985
|
+
}
|
|
3842
3986
|
this.renderInFlight = true;
|
|
3843
3987
|
queueMicrotask(() => {
|
|
3844
|
-
this.
|
|
3845
|
-
this.runRender(this.generation).catch((err) => {
|
|
3846
|
-
if (err instanceof SpectrogramAbortError) return;
|
|
3847
|
-
console.warn(
|
|
3848
|
-
"[dawcore-spectrogram] runRender unhandled rejection (generation " + this.generation + "): " + (err instanceof Error ? err.message : String(err))
|
|
3849
|
-
);
|
|
3850
|
-
});
|
|
3988
|
+
void this.runRenderLoop();
|
|
3851
3989
|
});
|
|
3852
3990
|
}
|
|
3991
|
+
async runRenderLoop() {
|
|
3992
|
+
try {
|
|
3993
|
+
do {
|
|
3994
|
+
this.renderQueued = false;
|
|
3995
|
+
await this.runRender(this.generation).catch((err) => {
|
|
3996
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
3997
|
+
console.warn(
|
|
3998
|
+
"[dawcore-spectrogram] runRender unhandled rejection (generation " + this.generation + "): " + (err instanceof Error ? err.message : String(err))
|
|
3999
|
+
);
|
|
4000
|
+
});
|
|
4001
|
+
} while (this.renderQueued && !this.disposed);
|
|
4002
|
+
} finally {
|
|
4003
|
+
this.renderInFlight = false;
|
|
4004
|
+
}
|
|
4005
|
+
}
|
|
3853
4006
|
async runRender(generation) {
|
|
3854
4007
|
if (this.disposed) return;
|
|
3855
4008
|
const viewport = this.viewport;
|
|
@@ -3893,7 +4046,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3893
4046
|
}
|
|
3894
4047
|
async renderTier(canvases, generation, viewport) {
|
|
3895
4048
|
if (canvases.length === 0) return;
|
|
3896
|
-
const groups =
|
|
4049
|
+
const groups = groupRenderableChunks(canvases);
|
|
3897
4050
|
for (const group of groups) {
|
|
3898
4051
|
if (this.generation !== generation || this.disposed) return;
|
|
3899
4052
|
await this.renderGroup(group, generation, viewport);
|
|
@@ -3909,9 +4062,26 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3909
4062
|
);
|
|
3910
4063
|
return;
|
|
3911
4064
|
}
|
|
4065
|
+
try {
|
|
4066
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
4067
|
+
} catch (err) {
|
|
4068
|
+
if (this.generation !== generation || this.disposed) throw err;
|
|
4069
|
+
if (err instanceof Error && err.message.includes("cache-miss")) {
|
|
4070
|
+
console.warn(
|
|
4071
|
+
"[dawcore-spectrogram] FFT cache entry evicted between compute and render \u2014 retrying group once"
|
|
4072
|
+
);
|
|
4073
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
4074
|
+
return;
|
|
4075
|
+
}
|
|
4076
|
+
throw err;
|
|
4077
|
+
}
|
|
4078
|
+
}
|
|
4079
|
+
async renderGroupOnce(group, generation, viewport, clip) {
|
|
4080
|
+
const first = group[0];
|
|
3912
4081
|
const fftSize = this.config.fftSize ?? SPECTROGRAM_DEFAULTS.fftSize;
|
|
3913
|
-
const
|
|
3914
|
-
const
|
|
4082
|
+
const clipRelativeOffsets = group.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH);
|
|
4083
|
+
const startPx = Math.min(...clipRelativeOffsets);
|
|
4084
|
+
const endPx = Math.max(...group.map((c, i) => clipRelativeOffsets[i] + c.widthPx));
|
|
3915
4085
|
const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
|
|
3916
4086
|
const endSample = Math.min(
|
|
3917
4087
|
clip.offsetSamples + clip.durationSamples,
|
|
@@ -3939,7 +4109,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3939
4109
|
cacheKey: cacheKey2,
|
|
3940
4110
|
canvasIds: group.map((c) => c.canvasId),
|
|
3941
4111
|
canvasWidths: group.map((c) => c.widthPx),
|
|
3942
|
-
globalPixelOffsets:
|
|
4112
|
+
globalPixelOffsets: clipRelativeOffsets,
|
|
3943
4113
|
canvasHeight: first.heightPx,
|
|
3944
4114
|
devicePixelRatio: this.devicePixelRatio,
|
|
3945
4115
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
@@ -3956,7 +4126,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3956
4126
|
}
|
|
3957
4127
|
async renderRemainingViaIdle(canvases, generation, viewport) {
|
|
3958
4128
|
if (canvases.length === 0) return;
|
|
3959
|
-
const groups =
|
|
4129
|
+
const groups = groupRenderableChunks(canvases);
|
|
3960
4130
|
for (const group of groups) {
|
|
3961
4131
|
if (this.generation !== generation || this.disposed) return;
|
|
3962
4132
|
await this.yieldUntilIdle();
|
|
@@ -3997,6 +4167,9 @@ export {
|
|
|
3997
4167
|
createSpectrogramWorkerPool,
|
|
3998
4168
|
getColorMap,
|
|
3999
4169
|
getFrequencyScale,
|
|
4000
|
-
groupContiguousChunks
|
|
4170
|
+
groupContiguousChunks,
|
|
4171
|
+
groupRenderableChunks,
|
|
4172
|
+
pixelColumnToFrame,
|
|
4173
|
+
pixelRowToBin
|
|
4001
4174
|
};
|
|
4002
4175
|
//# sourceMappingURL=index.mjs.map
|