@dawcore/spectrogram 0.0.3 → 0.0.5
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 +128 -1
- package/dist/index.d.ts +128 -1
- package/dist/index.js +261 -67
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +254 -60
- package/dist/index.mjs.map +1 -1
- package/dist/orchestrator/index.d.mts +51 -1
- package/dist/orchestrator/index.mjs +222 -60
- 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,56 @@ 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
|
+
|
|
3422
|
+
// src/computation/fftSampleRange.ts
|
|
3423
|
+
import { MAX_CANVAS_WIDTH } from "@waveform-playlist/core";
|
|
3424
|
+
function computePaddedFftRange({
|
|
3425
|
+
chunks,
|
|
3426
|
+
fftSize,
|
|
3427
|
+
offsetSamples,
|
|
3428
|
+
durationSamples,
|
|
3429
|
+
samplesPerPixel
|
|
3430
|
+
}) {
|
|
3431
|
+
const startPx = Math.min(...chunks.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH));
|
|
3432
|
+
const endPx = Math.max(...chunks.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH + c.widthPx));
|
|
3433
|
+
const startSample = offsetSamples + Math.floor(startPx * samplesPerPixel);
|
|
3434
|
+
const endSample = Math.min(
|
|
3435
|
+
offsetSamples + durationSamples,
|
|
3436
|
+
offsetSamples + Math.ceil(endPx * samplesPerPixel)
|
|
3437
|
+
);
|
|
3438
|
+
const paddedStart = Math.max(offsetSamples, startSample - fftSize);
|
|
3439
|
+
const paddedEnd = Math.min(offsetSamples + durationSamples, endSample + fftSize);
|
|
3440
|
+
return { paddedStart, paddedEnd };
|
|
3441
|
+
}
|
|
3442
|
+
|
|
3390
3443
|
// src/worker/createSpectrogramWorker.ts
|
|
3391
3444
|
var SpectrogramAbortError = class extends Error {
|
|
3392
3445
|
constructor() {
|
|
@@ -3426,7 +3479,6 @@ function createSpectrogramWorker(worker) {
|
|
|
3426
3479
|
}
|
|
3427
3480
|
};
|
|
3428
3481
|
worker.onerror = (e) => {
|
|
3429
|
-
terminated = true;
|
|
3430
3482
|
console.error(
|
|
3431
3483
|
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
3432
3484
|
);
|
|
@@ -3529,21 +3581,31 @@ function createSpectrogramWorker(worker) {
|
|
|
3529
3581
|
}
|
|
3530
3582
|
|
|
3531
3583
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
3584
|
+
import { parseSpectrogramCanvasId } from "@waveform-playlist/core";
|
|
3532
3585
|
function parseChannelFromCanvasId(canvasId) {
|
|
3533
|
-
const
|
|
3534
|
-
if (!
|
|
3586
|
+
const parsed = parseSpectrogramCanvasId(canvasId);
|
|
3587
|
+
if (!parsed) {
|
|
3535
3588
|
console.warn(
|
|
3536
|
-
"[dawcore-spectrogram] canvas ID missing -ch{N}-
|
|
3589
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
|
|
3537
3590
|
);
|
|
3538
3591
|
return 0;
|
|
3539
3592
|
}
|
|
3540
|
-
return
|
|
3593
|
+
return parsed.channelIndex;
|
|
3541
3594
|
}
|
|
3542
3595
|
function defaultPoolSize() {
|
|
3543
3596
|
return 2;
|
|
3544
3597
|
}
|
|
3598
|
+
var MAX_POOL_CHANNELS = 32;
|
|
3545
3599
|
function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
|
|
3600
|
+
if (poolSize > MAX_POOL_CHANNELS) {
|
|
3601
|
+
console.warn(
|
|
3602
|
+
"[dawcore-spectrogram] workerPoolSize " + poolSize + " exceeds the channel cap (" + MAX_POOL_CHANNELS + ") \u2014 clamping; channels beyond the cap are not servable"
|
|
3603
|
+
);
|
|
3604
|
+
poolSize = MAX_POOL_CHANNELS;
|
|
3605
|
+
}
|
|
3546
3606
|
const workers = [];
|
|
3607
|
+
let terminated = false;
|
|
3608
|
+
const registeredAudio = /* @__PURE__ */ new Map();
|
|
3547
3609
|
let failedAt = -1;
|
|
3548
3610
|
try {
|
|
3549
3611
|
for (let i = 0; i < poolSize; i++) {
|
|
@@ -3565,59 +3627,111 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3565
3627
|
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
3566
3628
|
);
|
|
3567
3629
|
}
|
|
3568
|
-
function
|
|
3569
|
-
|
|
3630
|
+
function assertValidChannelIndex(channelIndex) {
|
|
3631
|
+
if (!Number.isInteger(channelIndex) || channelIndex < 0 || channelIndex >= MAX_POOL_CHANNELS) {
|
|
3632
|
+
throw new Error(
|
|
3633
|
+
"[dawcore-spectrogram] invalid channel index " + channelIndex + " (expected an integer in 0.." + (MAX_POOL_CHANNELS - 1) + ")"
|
|
3634
|
+
);
|
|
3635
|
+
}
|
|
3636
|
+
}
|
|
3637
|
+
function ensureWorkerForChannel(channelIndex) {
|
|
3638
|
+
assertValidChannelIndex(channelIndex);
|
|
3639
|
+
if (terminated) {
|
|
3640
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
3641
|
+
}
|
|
3642
|
+
while (workers.length <= channelIndex) {
|
|
3643
|
+
const w = createSpectrogramWorker(createWorker());
|
|
3644
|
+
const workerIndex = workers.length;
|
|
3645
|
+
for (const [clipId, entry] of registeredAudio) {
|
|
3646
|
+
if (entry.channelDataArrays.length > workerIndex) {
|
|
3647
|
+
w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
|
|
3648
|
+
}
|
|
3649
|
+
}
|
|
3650
|
+
workers.push(w);
|
|
3651
|
+
}
|
|
3652
|
+
return workers[channelIndex];
|
|
3570
3653
|
}
|
|
3571
3654
|
return {
|
|
3572
3655
|
async computeFFT(params, generation = 0) {
|
|
3656
|
+
if (terminated) {
|
|
3657
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
3658
|
+
}
|
|
3659
|
+
const channelCount = params.channelDataArrays.length || registeredAudio.get(params.clipId)?.channelDataArrays.length || 0;
|
|
3660
|
+
if (channelCount === 0) {
|
|
3661
|
+
throw new Error(
|
|
3662
|
+
"[dawcore-spectrogram] computeFFT called with no channel data \u2014 pass channelDataArrays or registerAudioData the clip first"
|
|
3663
|
+
);
|
|
3664
|
+
}
|
|
3573
3665
|
if (params.mono) {
|
|
3574
3666
|
return workers[0].computeFFT(params, generation);
|
|
3575
3667
|
}
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3668
|
+
if (channelCount > MAX_POOL_CHANNELS) {
|
|
3669
|
+
throw new Error(
|
|
3670
|
+
"[dawcore-spectrogram] computeFFT: " + channelCount + " channels exceeds the pool channel cap (" + MAX_POOL_CHANNELS + ")"
|
|
3671
|
+
);
|
|
3672
|
+
}
|
|
3673
|
+
const promises = Array.from(
|
|
3674
|
+
{ length: channelCount },
|
|
3675
|
+
(_, i) => ensureWorkerForChannel(i).computeFFT({ ...params, channelFilter: i }, generation)
|
|
3580
3676
|
);
|
|
3581
3677
|
const settled = await Promise.allSettled(promises);
|
|
3582
3678
|
const failures = settled.filter((s) => s.status === "rejected");
|
|
3583
3679
|
if (failures.length > 0) {
|
|
3584
|
-
|
|
3680
|
+
const realErrors = failures.filter((f) => !(f.reason instanceof SpectrogramAbortError));
|
|
3681
|
+
for (let i = 1; i < realErrors.length; i++) {
|
|
3585
3682
|
console.warn(
|
|
3586
|
-
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (
|
|
3683
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (realErrors[i].reason instanceof Error ? realErrors[i].reason.message : String(realErrors[i].reason))
|
|
3587
3684
|
);
|
|
3588
3685
|
}
|
|
3589
|
-
throw failures[0].reason;
|
|
3686
|
+
throw (realErrors[0] ?? failures[0]).reason;
|
|
3590
3687
|
}
|
|
3591
3688
|
return settled[0].value;
|
|
3592
3689
|
},
|
|
3593
|
-
|
|
3594
|
-
|
|
3690
|
+
// async so ensureWorkerForChannel's policy throws surface as rejections,
|
|
3691
|
+
// matching computeFFT, instead of escaping synchronously.
|
|
3692
|
+
async renderChunks(params, generation = 0) {
|
|
3693
|
+
const worker = ensureWorkerForChannel(params.channelIndex);
|
|
3595
3694
|
return worker.renderChunks({ ...params, channelIndex: 0 }, generation);
|
|
3596
3695
|
},
|
|
3597
3696
|
abortGeneration(generation) {
|
|
3697
|
+
if (terminated) return;
|
|
3598
3698
|
for (const w of workers) {
|
|
3599
3699
|
w.abortGeneration(generation);
|
|
3600
3700
|
}
|
|
3601
3701
|
},
|
|
3602
3702
|
registerCanvas(canvasId, canvas) {
|
|
3603
|
-
|
|
3604
|
-
|
|
3703
|
+
try {
|
|
3704
|
+
ensureWorkerForChannel(parseChannelFromCanvasId(canvasId)).registerCanvas(canvasId, canvas);
|
|
3705
|
+
} catch (err) {
|
|
3706
|
+
console.warn(
|
|
3707
|
+
'[dawcore-spectrogram] registerCanvas("' + canvasId + '") rejected \u2014 canvas not registered: ' + (err instanceof Error ? err.message : String(err))
|
|
3708
|
+
);
|
|
3709
|
+
}
|
|
3605
3710
|
},
|
|
3606
3711
|
unregisterCanvas(canvasId) {
|
|
3607
|
-
|
|
3608
|
-
|
|
3712
|
+
if (terminated) return;
|
|
3713
|
+
const worker = workers[parseChannelFromCanvasId(canvasId)];
|
|
3714
|
+
if (!worker) return;
|
|
3715
|
+
worker.unregisterCanvas(canvasId);
|
|
3609
3716
|
},
|
|
3610
3717
|
registerAudioData(clipId, channelDataArrays, sampleRate) {
|
|
3611
|
-
|
|
3612
|
-
|
|
3718
|
+
if (terminated) return;
|
|
3719
|
+
registeredAudio.set(clipId, { channelDataArrays, sampleRate });
|
|
3720
|
+
const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
|
|
3721
|
+
for (let i = 0; i < relevantWorkers; i++) {
|
|
3722
|
+
workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
|
|
3613
3723
|
}
|
|
3614
3724
|
},
|
|
3615
3725
|
unregisterAudioData(clipId) {
|
|
3726
|
+
if (terminated) return;
|
|
3727
|
+
registeredAudio.delete(clipId);
|
|
3616
3728
|
for (const w of workers) {
|
|
3617
3729
|
w.unregisterAudioData(clipId);
|
|
3618
3730
|
}
|
|
3619
3731
|
},
|
|
3620
3732
|
terminate() {
|
|
3733
|
+
terminated = true;
|
|
3734
|
+
registeredAudio.clear();
|
|
3621
3735
|
for (const w of workers) {
|
|
3622
3736
|
w.terminate();
|
|
3623
3737
|
}
|
|
@@ -3626,7 +3740,11 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3626
3740
|
}
|
|
3627
3741
|
|
|
3628
3742
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3629
|
-
import {
|
|
3743
|
+
import {
|
|
3744
|
+
SPECTROGRAM_DEFAULTS,
|
|
3745
|
+
DEFAULT_SPECTROGRAM_COLOR_MAP,
|
|
3746
|
+
MAX_CANVAS_WIDTH as MAX_CANVAS_WIDTH2
|
|
3747
|
+
} from "@waveform-playlist/core";
|
|
3630
3748
|
|
|
3631
3749
|
// src/orchestrator/color-lut-cache.ts
|
|
3632
3750
|
var ColorLUTCache = class {
|
|
@@ -3691,6 +3809,20 @@ function groupContiguousChunks(chunks) {
|
|
|
3691
3809
|
groups.push(current);
|
|
3692
3810
|
return groups;
|
|
3693
3811
|
}
|
|
3812
|
+
function groupRenderableChunks(chunks) {
|
|
3813
|
+
const partitions = /* @__PURE__ */ new Map();
|
|
3814
|
+
for (const chunk of chunks) {
|
|
3815
|
+
const key = chunk.clipId + " " + chunk.channelIndex;
|
|
3816
|
+
const list = partitions.get(key) ?? [];
|
|
3817
|
+
list.push(chunk);
|
|
3818
|
+
partitions.set(key, list);
|
|
3819
|
+
}
|
|
3820
|
+
const groups = [];
|
|
3821
|
+
for (const partition of partitions.values()) {
|
|
3822
|
+
groups.push(...groupContiguousChunks(partition));
|
|
3823
|
+
}
|
|
3824
|
+
return groups;
|
|
3825
|
+
}
|
|
3694
3826
|
|
|
3695
3827
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3696
3828
|
var SpectrogramOrchestrator = class extends EventTarget {
|
|
@@ -3703,6 +3835,12 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3703
3835
|
this.colorLUT = new ColorLUTCache();
|
|
3704
3836
|
this.disposed = false;
|
|
3705
3837
|
this.renderInFlight = false;
|
|
3838
|
+
this.renderQueued = false;
|
|
3839
|
+
// Per-clip timeline origin (globalPixelOffset - chunkIndex * MAX_CANVAS_WIDTH)
|
|
3840
|
+
// with a live canvas count — used to detect layout-contract violations.
|
|
3841
|
+
// The origin resets once all of a clip's canvases unregister, so a moved
|
|
3842
|
+
// clip re-registering at a new position doesn't false-positive.
|
|
3843
|
+
this.clipOrigins = /* @__PURE__ */ new Map();
|
|
3706
3844
|
// Tracks which trackIds have already emitted `viewport-ready` for the
|
|
3707
3845
|
// current generation. Cleared on every generation bump (setViewport with a
|
|
3708
3846
|
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
@@ -3763,16 +3901,34 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3763
3901
|
channelIndex: reg.channelIndex,
|
|
3764
3902
|
chunkIndex: reg.chunkIndex
|
|
3765
3903
|
});
|
|
3904
|
+
const originPx = reg.globalPixelOffset - reg.chunkIndex * MAX_CANVAS_WIDTH2;
|
|
3905
|
+
const origin = this.clipOrigins.get(reg.clipId);
|
|
3906
|
+
if (!origin) {
|
|
3907
|
+
this.clipOrigins.set(reg.clipId, { originPx, canvasCount: 1 });
|
|
3908
|
+
} else {
|
|
3909
|
+
origin.canvasCount += 1;
|
|
3910
|
+
if (origin.originPx !== originPx) {
|
|
3911
|
+
console.warn(
|
|
3912
|
+
"[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_WIDTH2 + "px and this clip will render shifted audio"
|
|
3913
|
+
);
|
|
3914
|
+
}
|
|
3915
|
+
}
|
|
3766
3916
|
this.pool.registerCanvas(reg.canvasId, reg.canvas);
|
|
3767
3917
|
if (this.viewport) this.scheduleRender();
|
|
3768
3918
|
}
|
|
3769
3919
|
unregisterCanvas(canvasId) {
|
|
3770
3920
|
if (this.disposed) return;
|
|
3771
|
-
|
|
3921
|
+
const entry = this.canvases.get(canvasId);
|
|
3922
|
+
if (!entry) {
|
|
3772
3923
|
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3773
3924
|
return;
|
|
3774
3925
|
}
|
|
3775
3926
|
this.canvases.delete(canvasId);
|
|
3927
|
+
const origin = this.clipOrigins.get(entry.clipId);
|
|
3928
|
+
if (origin) {
|
|
3929
|
+
origin.canvasCount -= 1;
|
|
3930
|
+
if (origin.canvasCount <= 0) this.clipOrigins.delete(entry.clipId);
|
|
3931
|
+
}
|
|
3776
3932
|
this.pool.unregisterCanvas(canvasId);
|
|
3777
3933
|
}
|
|
3778
3934
|
setViewport(state) {
|
|
@@ -3784,30 +3940,21 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3784
3940
|
return;
|
|
3785
3941
|
}
|
|
3786
3942
|
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3787
|
-
|
|
3788
|
-
this.generation += 1;
|
|
3789
|
-
this.readyDispatched.clear();
|
|
3790
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3943
|
+
this.bumpGenerationAndAbortStale();
|
|
3791
3944
|
this.viewport = state;
|
|
3792
3945
|
this.scheduleRender();
|
|
3793
3946
|
}
|
|
3794
3947
|
setConfig(config) {
|
|
3795
3948
|
if (this.disposed) return;
|
|
3796
3949
|
this.config = config;
|
|
3797
|
-
|
|
3798
|
-
this.generation += 1;
|
|
3799
|
-
this.readyDispatched.clear();
|
|
3800
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3950
|
+
this.bumpGenerationAndAbortStale();
|
|
3801
3951
|
this.colorLUT.clear();
|
|
3802
3952
|
this.scheduleRender();
|
|
3803
3953
|
}
|
|
3804
3954
|
setColorMap(colorMap) {
|
|
3805
3955
|
if (this.disposed) return;
|
|
3806
3956
|
this.colorMap = colorMap;
|
|
3807
|
-
|
|
3808
|
-
this.generation += 1;
|
|
3809
|
-
this.readyDispatched.clear();
|
|
3810
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3957
|
+
this.bumpGenerationAndAbortStale();
|
|
3811
3958
|
this.scheduleRender();
|
|
3812
3959
|
}
|
|
3813
3960
|
setDevicePixelRatio(dpr) {
|
|
@@ -3820,36 +3967,64 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3820
3967
|
}
|
|
3821
3968
|
if (this.devicePixelRatio === dpr) return;
|
|
3822
3969
|
this.devicePixelRatio = dpr;
|
|
3823
|
-
|
|
3970
|
+
this.bumpGenerationAndAbortStale();
|
|
3971
|
+
this.scheduleRender();
|
|
3972
|
+
}
|
|
3973
|
+
/**
|
|
3974
|
+
* Start a new render generation and cancel in-flight work from older ones.
|
|
3975
|
+
*
|
|
3976
|
+
* The worker treats work as stale iff `generation < latestGeneration`, so
|
|
3977
|
+
* the abort message must carry the NEW generation — passing the previous
|
|
3978
|
+
* one leaves its in-flight FFTs running to completion (#555).
|
|
3979
|
+
*/
|
|
3980
|
+
bumpGenerationAndAbortStale() {
|
|
3824
3981
|
this.generation += 1;
|
|
3825
3982
|
this.readyDispatched.clear();
|
|
3826
|
-
this.pool.abortGeneration(
|
|
3827
|
-
this.scheduleRender();
|
|
3983
|
+
this.pool.abortGeneration(this.generation);
|
|
3828
3984
|
}
|
|
3829
3985
|
dispose() {
|
|
3830
3986
|
if (this.disposed) return;
|
|
3831
3987
|
this.disposed = true;
|
|
3832
3988
|
this.clips.clear();
|
|
3833
3989
|
this.canvases.clear();
|
|
3990
|
+
this.clipOrigins.clear();
|
|
3834
3991
|
this.viewport = null;
|
|
3835
3992
|
this.colorLUT.clear();
|
|
3836
3993
|
this.readyDispatched.clear();
|
|
3837
3994
|
this.pool.terminate();
|
|
3838
3995
|
}
|
|
3996
|
+
/**
|
|
3997
|
+
* Coalesce render requests: `renderInFlight` is held for the WHOLE async
|
|
3998
|
+
* render, not just until the microtask fires — a request arriving
|
|
3999
|
+
* mid-render (e.g. a late registerCanvas) queues one follow-up run instead
|
|
4000
|
+
* of starting a concurrent runRender for the same generation (#558).
|
|
4001
|
+
*/
|
|
3839
4002
|
scheduleRender() {
|
|
3840
|
-
if (this.renderInFlight) return;
|
|
3841
4003
|
if (!this.viewport) return;
|
|
4004
|
+
if (this.renderInFlight) {
|
|
4005
|
+
this.renderQueued = true;
|
|
4006
|
+
return;
|
|
4007
|
+
}
|
|
3842
4008
|
this.renderInFlight = true;
|
|
3843
4009
|
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
|
-
});
|
|
4010
|
+
void this.runRenderLoop();
|
|
3851
4011
|
});
|
|
3852
4012
|
}
|
|
4013
|
+
async runRenderLoop() {
|
|
4014
|
+
try {
|
|
4015
|
+
do {
|
|
4016
|
+
this.renderQueued = false;
|
|
4017
|
+
await this.runRender(this.generation).catch((err) => {
|
|
4018
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
4019
|
+
console.warn(
|
|
4020
|
+
"[dawcore-spectrogram] runRender unhandled rejection (generation " + this.generation + "): " + (err instanceof Error ? err.message : String(err))
|
|
4021
|
+
);
|
|
4022
|
+
});
|
|
4023
|
+
} while (this.renderQueued && !this.disposed);
|
|
4024
|
+
} finally {
|
|
4025
|
+
this.renderInFlight = false;
|
|
4026
|
+
}
|
|
4027
|
+
}
|
|
3853
4028
|
async runRender(generation) {
|
|
3854
4029
|
if (this.disposed) return;
|
|
3855
4030
|
const viewport = this.viewport;
|
|
@@ -3893,7 +4068,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3893
4068
|
}
|
|
3894
4069
|
async renderTier(canvases, generation, viewport) {
|
|
3895
4070
|
if (canvases.length === 0) return;
|
|
3896
|
-
const groups =
|
|
4071
|
+
const groups = groupRenderableChunks(canvases);
|
|
3897
4072
|
for (const group of groups) {
|
|
3898
4073
|
if (this.generation !== generation || this.disposed) return;
|
|
3899
4074
|
await this.renderGroup(group, generation, viewport);
|
|
@@ -3909,16 +4084,31 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3909
4084
|
);
|
|
3910
4085
|
return;
|
|
3911
4086
|
}
|
|
4087
|
+
try {
|
|
4088
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
4089
|
+
} catch (err) {
|
|
4090
|
+
if (this.generation !== generation || this.disposed) throw err;
|
|
4091
|
+
if (err instanceof Error && err.message.includes("cache-miss")) {
|
|
4092
|
+
console.warn(
|
|
4093
|
+
"[dawcore-spectrogram] FFT cache entry evicted between compute and render \u2014 retrying group once"
|
|
4094
|
+
);
|
|
4095
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
4096
|
+
return;
|
|
4097
|
+
}
|
|
4098
|
+
throw err;
|
|
4099
|
+
}
|
|
4100
|
+
}
|
|
4101
|
+
async renderGroupOnce(group, generation, viewport, clip) {
|
|
4102
|
+
const first = group[0];
|
|
3912
4103
|
const fftSize = this.config.fftSize ?? SPECTROGRAM_DEFAULTS.fftSize;
|
|
3913
|
-
const
|
|
3914
|
-
const
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
clip.
|
|
3919
|
-
|
|
3920
|
-
|
|
3921
|
-
const paddedEnd = Math.min(clip.offsetSamples + clip.durationSamples, endSample + fftSize);
|
|
4104
|
+
const clipRelativeOffsets = group.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH2);
|
|
4105
|
+
const { paddedStart, paddedEnd } = computePaddedFftRange({
|
|
4106
|
+
chunks: group.map((c) => ({ chunkIndex: c.chunkIndex, widthPx: c.widthPx })),
|
|
4107
|
+
fftSize,
|
|
4108
|
+
offsetSamples: clip.offsetSamples,
|
|
4109
|
+
durationSamples: clip.durationSamples,
|
|
4110
|
+
samplesPerPixel: viewport.samplesPerPixel
|
|
4111
|
+
});
|
|
3922
4112
|
const { cacheKey: cacheKey2 } = await this.pool.computeFFT(
|
|
3923
4113
|
{
|
|
3924
4114
|
clipId: first.clipId,
|
|
@@ -3939,7 +4129,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3939
4129
|
cacheKey: cacheKey2,
|
|
3940
4130
|
canvasIds: group.map((c) => c.canvasId),
|
|
3941
4131
|
canvasWidths: group.map((c) => c.widthPx),
|
|
3942
|
-
globalPixelOffsets:
|
|
4132
|
+
globalPixelOffsets: clipRelativeOffsets,
|
|
3943
4133
|
canvasHeight: first.heightPx,
|
|
3944
4134
|
devicePixelRatio: this.devicePixelRatio,
|
|
3945
4135
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
@@ -3956,7 +4146,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3956
4146
|
}
|
|
3957
4147
|
async renderRemainingViaIdle(canvases, generation, viewport) {
|
|
3958
4148
|
if (canvases.length === 0) return;
|
|
3959
|
-
const groups =
|
|
4149
|
+
const groups = groupRenderableChunks(canvases);
|
|
3960
4150
|
for (const group of groups) {
|
|
3961
4151
|
if (this.generation !== generation || this.disposed) return;
|
|
3962
4152
|
await this.yieldUntilIdle();
|
|
@@ -3991,12 +4181,16 @@ export {
|
|
|
3991
4181
|
SpectrogramAbortError,
|
|
3992
4182
|
SpectrogramOrchestrator,
|
|
3993
4183
|
classifyViewport,
|
|
4184
|
+
computePaddedFftRange,
|
|
3994
4185
|
computeSpectrogram,
|
|
3995
4186
|
computeSpectrogramMono,
|
|
3996
4187
|
createSpectrogramWorker,
|
|
3997
4188
|
createSpectrogramWorkerPool,
|
|
3998
4189
|
getColorMap,
|
|
3999
4190
|
getFrequencyScale,
|
|
4000
|
-
groupContiguousChunks
|
|
4191
|
+
groupContiguousChunks,
|
|
4192
|
+
groupRenderableChunks,
|
|
4193
|
+
pixelColumnToFrame,
|
|
4194
|
+
pixelRowToBin
|
|
4001
4195
|
};
|
|
4002
4196
|
//# sourceMappingURL=index.mjs.map
|