@dawcore/spectrogram 0.0.2 → 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 +130 -27
- package/dist/index.d.ts +130 -27
- package/dist/index.js +359 -71
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +359 -70
- package/dist/index.mjs.map +1 -1
- package/dist/orchestrator/index.d.mts +85 -27
- package/dist/orchestrator/index.mjs +328 -70
- 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,12 +3458,19 @@ function createSpectrogramWorker(worker) {
|
|
|
3426
3458
|
}
|
|
3427
3459
|
};
|
|
3428
3460
|
worker.onerror = (e) => {
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3461
|
+
console.error(
|
|
3462
|
+
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
3463
|
+
);
|
|
3464
|
+
for (const [id, entry] of pending) {
|
|
3465
|
+
entry.reject(new Error("Worker crashed (id=" + id + "): " + (e.message || "unknown error")));
|
|
3432
3466
|
}
|
|
3433
3467
|
pending.clear();
|
|
3434
3468
|
};
|
|
3469
|
+
worker.onmessageerror = (e) => {
|
|
3470
|
+
console.warn(
|
|
3471
|
+
"[dawcore-spectrogram] worker postMessage clone failure: " + (e.data ? JSON.stringify(e.data) : "no data")
|
|
3472
|
+
);
|
|
3473
|
+
};
|
|
3435
3474
|
return {
|
|
3436
3475
|
computeFFT(params, generation = 0) {
|
|
3437
3476
|
if (terminated) return Promise.reject(new Error("Worker terminated"));
|
|
@@ -3522,67 +3561,155 @@ function createSpectrogramWorker(worker) {
|
|
|
3522
3561
|
|
|
3523
3562
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
3524
3563
|
function parseChannelFromCanvasId(canvasId) {
|
|
3525
|
-
const match = canvasId.match(/-ch(\d+)
|
|
3526
|
-
|
|
3564
|
+
const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
|
|
3565
|
+
if (!match) {
|
|
3566
|
+
console.warn(
|
|
3567
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
|
|
3568
|
+
);
|
|
3569
|
+
return 0;
|
|
3570
|
+
}
|
|
3571
|
+
return parseInt(match[1], 10);
|
|
3527
3572
|
}
|
|
3528
3573
|
function defaultPoolSize() {
|
|
3529
3574
|
return 2;
|
|
3530
3575
|
}
|
|
3576
|
+
var MAX_POOL_CHANNELS = 32;
|
|
3531
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
|
+
}
|
|
3532
3584
|
const workers = [];
|
|
3585
|
+
let terminated = false;
|
|
3586
|
+
const registeredAudio = /* @__PURE__ */ new Map();
|
|
3587
|
+
let failedAt = -1;
|
|
3533
3588
|
try {
|
|
3534
3589
|
for (let i = 0; i < poolSize; i++) {
|
|
3590
|
+
failedAt = i;
|
|
3535
3591
|
workers.push(createSpectrogramWorker(createWorker()));
|
|
3536
3592
|
}
|
|
3593
|
+
failedAt = -1;
|
|
3537
3594
|
} catch (err) {
|
|
3538
3595
|
for (const w of workers) {
|
|
3539
|
-
|
|
3596
|
+
try {
|
|
3597
|
+
w.terminate();
|
|
3598
|
+
} catch (terminateErr) {
|
|
3599
|
+
console.warn(
|
|
3600
|
+
"[dawcore-spectrogram] pool constructor cleanup: terminate failed for worker \u2014 " + String(terminateErr)
|
|
3601
|
+
);
|
|
3602
|
+
}
|
|
3540
3603
|
}
|
|
3541
|
-
throw
|
|
3604
|
+
throw new Error(
|
|
3605
|
+
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
3606
|
+
);
|
|
3542
3607
|
}
|
|
3543
|
-
function
|
|
3544
|
-
|
|
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];
|
|
3545
3631
|
}
|
|
3546
3632
|
return {
|
|
3547
|
-
computeFFT(params, generation = 0) {
|
|
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
|
+
}
|
|
3548
3643
|
if (params.mono) {
|
|
3549
3644
|
return workers[0].computeFFT(params, generation);
|
|
3550
3645
|
}
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
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)
|
|
3555
3654
|
);
|
|
3556
|
-
|
|
3655
|
+
const settled = await Promise.allSettled(promises);
|
|
3656
|
+
const failures = settled.filter((s) => s.status === "rejected");
|
|
3657
|
+
if (failures.length > 0) {
|
|
3658
|
+
const realErrors = failures.filter((f) => !(f.reason instanceof SpectrogramAbortError));
|
|
3659
|
+
for (let i = 1; i < realErrors.length; i++) {
|
|
3660
|
+
console.warn(
|
|
3661
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (realErrors[i].reason instanceof Error ? realErrors[i].reason.message : String(realErrors[i].reason))
|
|
3662
|
+
);
|
|
3663
|
+
}
|
|
3664
|
+
throw (realErrors[0] ?? failures[0]).reason;
|
|
3665
|
+
}
|
|
3666
|
+
return settled[0].value;
|
|
3557
3667
|
},
|
|
3558
|
-
|
|
3559
|
-
|
|
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);
|
|
3560
3672
|
return worker.renderChunks({ ...params, channelIndex: 0 }, generation);
|
|
3561
3673
|
},
|
|
3562
3674
|
abortGeneration(generation) {
|
|
3675
|
+
if (terminated) return;
|
|
3563
3676
|
for (const w of workers) {
|
|
3564
3677
|
w.abortGeneration(generation);
|
|
3565
3678
|
}
|
|
3566
3679
|
},
|
|
3567
3680
|
registerCanvas(canvasId, canvas) {
|
|
3568
|
-
|
|
3569
|
-
|
|
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
|
+
}
|
|
3570
3688
|
},
|
|
3571
3689
|
unregisterCanvas(canvasId) {
|
|
3572
|
-
|
|
3573
|
-
|
|
3690
|
+
if (terminated) return;
|
|
3691
|
+
const worker = workers[parseChannelFromCanvasId(canvasId)];
|
|
3692
|
+
if (!worker) return;
|
|
3693
|
+
worker.unregisterCanvas(canvasId);
|
|
3574
3694
|
},
|
|
3575
3695
|
registerAudioData(clipId, channelDataArrays, sampleRate) {
|
|
3576
|
-
|
|
3577
|
-
|
|
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);
|
|
3578
3701
|
}
|
|
3579
3702
|
},
|
|
3580
3703
|
unregisterAudioData(clipId) {
|
|
3704
|
+
if (terminated) return;
|
|
3705
|
+
registeredAudio.delete(clipId);
|
|
3581
3706
|
for (const w of workers) {
|
|
3582
3707
|
w.unregisterAudioData(clipId);
|
|
3583
3708
|
}
|
|
3584
3709
|
},
|
|
3585
3710
|
terminate() {
|
|
3711
|
+
terminated = true;
|
|
3712
|
+
registeredAudio.clear();
|
|
3586
3713
|
for (const w of workers) {
|
|
3587
3714
|
w.terminate();
|
|
3588
3715
|
}
|
|
@@ -3590,6 +3717,13 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3590
3717
|
};
|
|
3591
3718
|
}
|
|
3592
3719
|
|
|
3720
|
+
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3721
|
+
import {
|
|
3722
|
+
SPECTROGRAM_DEFAULTS,
|
|
3723
|
+
DEFAULT_SPECTROGRAM_COLOR_MAP,
|
|
3724
|
+
MAX_CANVAS_WIDTH
|
|
3725
|
+
} from "@waveform-playlist/core";
|
|
3726
|
+
|
|
3593
3727
|
// src/orchestrator/color-lut-cache.ts
|
|
3594
3728
|
var ColorLUTCache = class {
|
|
3595
3729
|
constructor() {
|
|
@@ -3653,6 +3787,20 @@ function groupContiguousChunks(chunks) {
|
|
|
3653
3787
|
groups.push(current);
|
|
3654
3788
|
return groups;
|
|
3655
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
|
+
}
|
|
3656
3804
|
|
|
3657
3805
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3658
3806
|
var SpectrogramOrchestrator = class extends EventTarget {
|
|
@@ -3665,37 +3813,62 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3665
3813
|
this.colorLUT = new ColorLUTCache();
|
|
3666
3814
|
this.disposed = false;
|
|
3667
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();
|
|
3668
3822
|
// Tracks which trackIds have already emitted `viewport-ready` for the
|
|
3669
|
-
// current generation. Cleared on every generation bump (setViewport
|
|
3670
|
-
// setConfig
|
|
3671
|
-
//
|
|
3672
|
-
// loading — would re-fire the
|
|
3823
|
+
// current generation. Cleared on every generation bump (setViewport with a
|
|
3824
|
+
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
3825
|
+
// Without this, every render — including those triggered by late
|
|
3826
|
+
// `registerCanvas` calls during track-by-track loading — would re-fire the
|
|
3827
|
+
// event for every track in the canvas map (N×N-ish ascending fan-in:
|
|
3828
|
+
// 3 tracks → 4+3+2+1 = 10 events; the dedup reduces this to 3).
|
|
3673
3829
|
this.readyDispatched = /* @__PURE__ */ new Set();
|
|
3674
3830
|
const poolSize = opts.workerPoolSize ?? 2;
|
|
3675
3831
|
this.pool = createSpectrogramWorkerPool(opts.workerFactory, poolSize);
|
|
3676
3832
|
this.config = opts.config;
|
|
3677
|
-
this.colorMap = opts.colorMap ??
|
|
3833
|
+
this.colorMap = opts.colorMap ?? DEFAULT_SPECTROGRAM_COLOR_MAP;
|
|
3678
3834
|
this.devicePixelRatio = opts.devicePixelRatio ?? (typeof window !== "undefined" ? window.devicePixelRatio : 1);
|
|
3679
3835
|
}
|
|
3680
3836
|
registerClip(reg) {
|
|
3681
3837
|
if (this.disposed) return;
|
|
3838
|
+
const channelData = [...reg.channelData];
|
|
3682
3839
|
this.clips.set(reg.clipId, {
|
|
3683
3840
|
trackId: reg.trackId,
|
|
3684
|
-
channelData
|
|
3841
|
+
channelData,
|
|
3685
3842
|
sampleRate: reg.sampleRate,
|
|
3686
3843
|
durationSamples: reg.durationSamples,
|
|
3687
3844
|
offsetSamples: reg.offsetSamples
|
|
3688
3845
|
});
|
|
3689
|
-
this.pool.registerAudioData(reg.clipId,
|
|
3846
|
+
this.pool.registerAudioData(reg.clipId, channelData, reg.sampleRate);
|
|
3847
|
+
if (this.viewport) {
|
|
3848
|
+
for (const canvas of this.canvases.values()) {
|
|
3849
|
+
if (canvas.clipId === reg.clipId) {
|
|
3850
|
+
this.scheduleRender();
|
|
3851
|
+
break;
|
|
3852
|
+
}
|
|
3853
|
+
}
|
|
3854
|
+
}
|
|
3690
3855
|
}
|
|
3691
3856
|
unregisterClip(clipId) {
|
|
3692
3857
|
if (this.disposed) return;
|
|
3693
|
-
if (!this.clips.has(clipId))
|
|
3858
|
+
if (!this.clips.has(clipId)) {
|
|
3859
|
+
console.warn("[dawcore-spectrogram] unregisterClip: unknown clip " + clipId);
|
|
3860
|
+
return;
|
|
3861
|
+
}
|
|
3694
3862
|
this.clips.delete(clipId);
|
|
3695
3863
|
this.pool.unregisterAudioData(clipId);
|
|
3696
3864
|
}
|
|
3697
3865
|
registerCanvas(reg) {
|
|
3698
|
-
if (this.disposed)
|
|
3866
|
+
if (this.disposed) {
|
|
3867
|
+
console.warn(
|
|
3868
|
+
"[dawcore-spectrogram] registerCanvas after dispose \u2014 canvas " + reg.canvasId + " will not render (OffscreenCanvas is now dead)"
|
|
3869
|
+
);
|
|
3870
|
+
return;
|
|
3871
|
+
}
|
|
3699
3872
|
this.canvases.set(reg.canvasId, {
|
|
3700
3873
|
canvasId: reg.canvasId,
|
|
3701
3874
|
globalPixelOffset: reg.globalPixelOffset,
|
|
@@ -3706,66 +3879,130 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3706
3879
|
channelIndex: reg.channelIndex,
|
|
3707
3880
|
chunkIndex: reg.chunkIndex
|
|
3708
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
|
+
}
|
|
3709
3894
|
this.pool.registerCanvas(reg.canvasId, reg.canvas);
|
|
3710
3895
|
if (this.viewport) this.scheduleRender();
|
|
3711
3896
|
}
|
|
3712
3897
|
unregisterCanvas(canvasId) {
|
|
3713
3898
|
if (this.disposed) return;
|
|
3714
|
-
|
|
3899
|
+
const entry = this.canvases.get(canvasId);
|
|
3900
|
+
if (!entry) {
|
|
3901
|
+
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3902
|
+
return;
|
|
3903
|
+
}
|
|
3715
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
|
+
}
|
|
3716
3910
|
this.pool.unregisterCanvas(canvasId);
|
|
3717
3911
|
}
|
|
3718
3912
|
setViewport(state) {
|
|
3719
3913
|
if (this.disposed) return;
|
|
3914
|
+
if (!Number.isFinite(state.visibleStartPx) || !Number.isFinite(state.visibleEndPx) || !Number.isFinite(state.bufferStartPx) || !Number.isFinite(state.bufferEndPx) || !Number.isFinite(state.samplesPerPixel) || state.samplesPerPixel <= 0 || state.visibleStartPx > state.visibleEndPx || state.bufferStartPx > state.bufferEndPx) {
|
|
3915
|
+
console.warn(
|
|
3916
|
+
"[dawcore-spectrogram] setViewport: invalid state \u2014 ignored (" + JSON.stringify(state) + ")"
|
|
3917
|
+
);
|
|
3918
|
+
return;
|
|
3919
|
+
}
|
|
3720
3920
|
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3721
|
-
|
|
3722
|
-
this.generation += 1;
|
|
3723
|
-
this.readyDispatched.clear();
|
|
3724
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3921
|
+
this.bumpGenerationAndAbortStale();
|
|
3725
3922
|
this.viewport = state;
|
|
3726
3923
|
this.scheduleRender();
|
|
3727
3924
|
}
|
|
3728
3925
|
setConfig(config) {
|
|
3729
3926
|
if (this.disposed) return;
|
|
3730
3927
|
this.config = config;
|
|
3731
|
-
|
|
3732
|
-
this.generation += 1;
|
|
3733
|
-
this.readyDispatched.clear();
|
|
3734
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3928
|
+
this.bumpGenerationAndAbortStale();
|
|
3735
3929
|
this.colorLUT.clear();
|
|
3736
3930
|
this.scheduleRender();
|
|
3737
3931
|
}
|
|
3738
3932
|
setColorMap(colorMap) {
|
|
3739
3933
|
if (this.disposed) return;
|
|
3740
3934
|
this.colorMap = colorMap;
|
|
3741
|
-
|
|
3742
|
-
this.generation += 1;
|
|
3743
|
-
this.readyDispatched.clear();
|
|
3744
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3935
|
+
this.bumpGenerationAndAbortStale();
|
|
3745
3936
|
this.scheduleRender();
|
|
3746
3937
|
}
|
|
3747
3938
|
setDevicePixelRatio(dpr) {
|
|
3939
|
+
if (this.disposed) return;
|
|
3940
|
+
if (!Number.isFinite(dpr) || dpr <= 0) {
|
|
3941
|
+
console.warn(
|
|
3942
|
+
"[dawcore-spectrogram] setDevicePixelRatio: invalid value " + dpr + " \u2014 ignored"
|
|
3943
|
+
);
|
|
3944
|
+
return;
|
|
3945
|
+
}
|
|
3946
|
+
if (this.devicePixelRatio === dpr) return;
|
|
3748
3947
|
this.devicePixelRatio = dpr;
|
|
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() {
|
|
3959
|
+
this.generation += 1;
|
|
3960
|
+
this.readyDispatched.clear();
|
|
3961
|
+
this.pool.abortGeneration(this.generation);
|
|
3749
3962
|
}
|
|
3750
3963
|
dispose() {
|
|
3751
3964
|
if (this.disposed) return;
|
|
3752
3965
|
this.disposed = true;
|
|
3753
3966
|
this.clips.clear();
|
|
3754
3967
|
this.canvases.clear();
|
|
3968
|
+
this.clipOrigins.clear();
|
|
3755
3969
|
this.viewport = null;
|
|
3756
3970
|
this.colorLUT.clear();
|
|
3757
3971
|
this.readyDispatched.clear();
|
|
3758
3972
|
this.pool.terminate();
|
|
3759
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
|
+
*/
|
|
3760
3980
|
scheduleRender() {
|
|
3761
|
-
if (this.renderInFlight) return;
|
|
3762
3981
|
if (!this.viewport) return;
|
|
3982
|
+
if (this.renderInFlight) {
|
|
3983
|
+
this.renderQueued = true;
|
|
3984
|
+
return;
|
|
3985
|
+
}
|
|
3763
3986
|
this.renderInFlight = true;
|
|
3764
3987
|
queueMicrotask(() => {
|
|
3765
|
-
this.
|
|
3766
|
-
void this.runRender(this.generation);
|
|
3988
|
+
void this.runRenderLoop();
|
|
3767
3989
|
});
|
|
3768
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
|
+
}
|
|
3769
4006
|
async runRender(generation) {
|
|
3770
4007
|
if (this.disposed) return;
|
|
3771
4008
|
const viewport = this.viewport;
|
|
@@ -3777,21 +4014,39 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3777
4014
|
canvasesByTrack.set(c.trackId, list);
|
|
3778
4015
|
}
|
|
3779
4016
|
for (const [trackId, trackCanvases] of canvasesByTrack) {
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
this.readyDispatched.
|
|
3785
|
-
|
|
4017
|
+
try {
|
|
4018
|
+
const tiers = classifyViewport(trackCanvases, viewport);
|
|
4019
|
+
await this.renderTier(tiers.viewport, generation, viewport);
|
|
4020
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4021
|
+
if (!this.readyDispatched.has(trackId)) {
|
|
4022
|
+
this.readyDispatched.add(trackId);
|
|
4023
|
+
this.dispatchEvent(
|
|
4024
|
+
new CustomEvent("viewport-ready", { detail: { trackId, generation } })
|
|
4025
|
+
);
|
|
4026
|
+
}
|
|
4027
|
+
await this.renderTier(tiers.buffer, generation, viewport);
|
|
4028
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4029
|
+
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
4030
|
+
} catch (err) {
|
|
4031
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4032
|
+
if (err instanceof SpectrogramAbortError) {
|
|
4033
|
+
return;
|
|
4034
|
+
}
|
|
4035
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
4036
|
+
console.warn(
|
|
4037
|
+
"[dawcore-spectrogram] render failed for track " + trackId + " (generation " + generation + "): " + error.message
|
|
4038
|
+
);
|
|
4039
|
+
this.dispatchEvent(
|
|
4040
|
+
new CustomEvent("viewport-error", {
|
|
4041
|
+
detail: { trackId, generation, error }
|
|
4042
|
+
})
|
|
4043
|
+
);
|
|
3786
4044
|
}
|
|
3787
|
-
await this.renderTier(tiers.buffer, generation, viewport);
|
|
3788
|
-
if (this.generation !== generation || this.disposed) return;
|
|
3789
|
-
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
3790
4045
|
}
|
|
3791
4046
|
}
|
|
3792
4047
|
async renderTier(canvases, generation, viewport) {
|
|
3793
4048
|
if (canvases.length === 0) return;
|
|
3794
|
-
const groups =
|
|
4049
|
+
const groups = groupRenderableChunks(canvases);
|
|
3795
4050
|
for (const group of groups) {
|
|
3796
4051
|
if (this.generation !== generation || this.disposed) return;
|
|
3797
4052
|
await this.renderGroup(group, generation, viewport);
|
|
@@ -3801,10 +4056,32 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3801
4056
|
if (group.length === 0) return;
|
|
3802
4057
|
const first = group[0];
|
|
3803
4058
|
const clip = this.clips.get(first.clipId);
|
|
3804
|
-
if (!clip)
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
4059
|
+
if (!clip) {
|
|
4060
|
+
console.warn(
|
|
4061
|
+
"[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
|
|
4062
|
+
);
|
|
4063
|
+
return;
|
|
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];
|
|
4081
|
+
const fftSize = this.config.fftSize ?? SPECTROGRAM_DEFAULTS.fftSize;
|
|
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));
|
|
3808
4085
|
const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
|
|
3809
4086
|
const endSample = Math.min(
|
|
3810
4087
|
clip.offsetSamples + clip.durationSamples,
|
|
@@ -3832,16 +4109,16 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3832
4109
|
cacheKey: cacheKey2,
|
|
3833
4110
|
canvasIds: group.map((c) => c.canvasId),
|
|
3834
4111
|
canvasWidths: group.map((c) => c.widthPx),
|
|
3835
|
-
globalPixelOffsets:
|
|
4112
|
+
globalPixelOffsets: clipRelativeOffsets,
|
|
3836
4113
|
canvasHeight: first.heightPx,
|
|
3837
4114
|
devicePixelRatio: this.devicePixelRatio,
|
|
3838
4115
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
3839
4116
|
colorLUT,
|
|
3840
|
-
frequencyScale: String(this.config.frequencyScale ??
|
|
3841
|
-
minFrequency: this.config.minFrequency ??
|
|
4117
|
+
frequencyScale: String(this.config.frequencyScale ?? SPECTROGRAM_DEFAULTS.frequencyScale),
|
|
4118
|
+
minFrequency: this.config.minFrequency ?? SPECTROGRAM_DEFAULTS.minFrequency,
|
|
3842
4119
|
maxFrequency: this.config.maxFrequency ?? clip.sampleRate / 2,
|
|
3843
|
-
gainDb: this.config.gainDb ??
|
|
3844
|
-
rangeDb: this.config.rangeDb ??
|
|
4120
|
+
gainDb: this.config.gainDb ?? SPECTROGRAM_DEFAULTS.gainDb,
|
|
4121
|
+
rangeDb: this.config.rangeDb ?? SPECTROGRAM_DEFAULTS.rangeDb,
|
|
3845
4122
|
channelIndex: first.channelIndex
|
|
3846
4123
|
},
|
|
3847
4124
|
generation
|
|
@@ -3849,12 +4126,21 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3849
4126
|
}
|
|
3850
4127
|
async renderRemainingViaIdle(canvases, generation, viewport) {
|
|
3851
4128
|
if (canvases.length === 0) return;
|
|
3852
|
-
const groups =
|
|
4129
|
+
const groups = groupRenderableChunks(canvases);
|
|
3853
4130
|
for (const group of groups) {
|
|
3854
4131
|
if (this.generation !== generation || this.disposed) return;
|
|
3855
4132
|
await this.yieldUntilIdle();
|
|
3856
4133
|
if (this.generation !== generation || this.disposed) return;
|
|
3857
|
-
|
|
4134
|
+
try {
|
|
4135
|
+
await this.renderGroup(group, generation, viewport);
|
|
4136
|
+
} catch (err) {
|
|
4137
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4138
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
4139
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
4140
|
+
console.warn(
|
|
4141
|
+
"[dawcore-spectrogram] remaining-tier render failed for canvas group: " + error.message
|
|
4142
|
+
);
|
|
4143
|
+
}
|
|
3858
4144
|
}
|
|
3859
4145
|
}
|
|
3860
4146
|
yieldUntilIdle() {
|
|
@@ -3881,6 +4167,9 @@ export {
|
|
|
3881
4167
|
createSpectrogramWorkerPool,
|
|
3882
4168
|
getColorMap,
|
|
3883
4169
|
getFrequencyScale,
|
|
3884
|
-
groupContiguousChunks
|
|
4170
|
+
groupContiguousChunks,
|
|
4171
|
+
groupRenderableChunks,
|
|
4172
|
+
pixelColumnToFrame,
|
|
4173
|
+
pixelRowToBin
|
|
3885
4174
|
};
|
|
3886
4175
|
//# sourceMappingURL=index.mjs.map
|