@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.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,12 +3507,19 @@ function createSpectrogramWorker(worker) {
|
|
|
3472
3507
|
}
|
|
3473
3508
|
};
|
|
3474
3509
|
worker.onerror = (e) => {
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3510
|
+
console.error(
|
|
3511
|
+
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
3512
|
+
);
|
|
3513
|
+
for (const [id, entry] of pending) {
|
|
3514
|
+
entry.reject(new Error("Worker crashed (id=" + id + "): " + (e.message || "unknown error")));
|
|
3478
3515
|
}
|
|
3479
3516
|
pending.clear();
|
|
3480
3517
|
};
|
|
3518
|
+
worker.onmessageerror = (e) => {
|
|
3519
|
+
console.warn(
|
|
3520
|
+
"[dawcore-spectrogram] worker postMessage clone failure: " + (e.data ? JSON.stringify(e.data) : "no data")
|
|
3521
|
+
);
|
|
3522
|
+
};
|
|
3481
3523
|
return {
|
|
3482
3524
|
computeFFT(params, generation = 0) {
|
|
3483
3525
|
if (terminated) return Promise.reject(new Error("Worker terminated"));
|
|
@@ -3568,67 +3610,155 @@ function createSpectrogramWorker(worker) {
|
|
|
3568
3610
|
|
|
3569
3611
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
3570
3612
|
function parseChannelFromCanvasId(canvasId) {
|
|
3571
|
-
const match = canvasId.match(/-ch(\d+)
|
|
3572
|
-
|
|
3613
|
+
const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
|
|
3614
|
+
if (!match) {
|
|
3615
|
+
console.warn(
|
|
3616
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
|
|
3617
|
+
);
|
|
3618
|
+
return 0;
|
|
3619
|
+
}
|
|
3620
|
+
return parseInt(match[1], 10);
|
|
3573
3621
|
}
|
|
3574
3622
|
function defaultPoolSize() {
|
|
3575
3623
|
return 2;
|
|
3576
3624
|
}
|
|
3625
|
+
var MAX_POOL_CHANNELS = 32;
|
|
3577
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
|
+
}
|
|
3578
3633
|
const workers = [];
|
|
3634
|
+
let terminated = false;
|
|
3635
|
+
const registeredAudio = /* @__PURE__ */ new Map();
|
|
3636
|
+
let failedAt = -1;
|
|
3579
3637
|
try {
|
|
3580
3638
|
for (let i = 0; i < poolSize; i++) {
|
|
3639
|
+
failedAt = i;
|
|
3581
3640
|
workers.push(createSpectrogramWorker(createWorker()));
|
|
3582
3641
|
}
|
|
3642
|
+
failedAt = -1;
|
|
3583
3643
|
} catch (err) {
|
|
3584
3644
|
for (const w of workers) {
|
|
3585
|
-
|
|
3645
|
+
try {
|
|
3646
|
+
w.terminate();
|
|
3647
|
+
} catch (terminateErr) {
|
|
3648
|
+
console.warn(
|
|
3649
|
+
"[dawcore-spectrogram] pool constructor cleanup: terminate failed for worker \u2014 " + String(terminateErr)
|
|
3650
|
+
);
|
|
3651
|
+
}
|
|
3586
3652
|
}
|
|
3587
|
-
throw
|
|
3653
|
+
throw new Error(
|
|
3654
|
+
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
3655
|
+
);
|
|
3588
3656
|
}
|
|
3589
|
-
function
|
|
3590
|
-
|
|
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];
|
|
3591
3680
|
}
|
|
3592
3681
|
return {
|
|
3593
|
-
computeFFT(params, generation = 0) {
|
|
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
|
+
}
|
|
3594
3692
|
if (params.mono) {
|
|
3595
3693
|
return workers[0].computeFFT(params, generation);
|
|
3596
3694
|
}
|
|
3597
|
-
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
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)
|
|
3601
3703
|
);
|
|
3602
|
-
|
|
3704
|
+
const settled = await Promise.allSettled(promises);
|
|
3705
|
+
const failures = settled.filter((s) => s.status === "rejected");
|
|
3706
|
+
if (failures.length > 0) {
|
|
3707
|
+
const realErrors = failures.filter((f) => !(f.reason instanceof SpectrogramAbortError));
|
|
3708
|
+
for (let i = 1; i < realErrors.length; i++) {
|
|
3709
|
+
console.warn(
|
|
3710
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (realErrors[i].reason instanceof Error ? realErrors[i].reason.message : String(realErrors[i].reason))
|
|
3711
|
+
);
|
|
3712
|
+
}
|
|
3713
|
+
throw (realErrors[0] ?? failures[0]).reason;
|
|
3714
|
+
}
|
|
3715
|
+
return settled[0].value;
|
|
3603
3716
|
},
|
|
3604
|
-
|
|
3605
|
-
|
|
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);
|
|
3606
3721
|
return worker.renderChunks({ ...params, channelIndex: 0 }, generation);
|
|
3607
3722
|
},
|
|
3608
3723
|
abortGeneration(generation) {
|
|
3724
|
+
if (terminated) return;
|
|
3609
3725
|
for (const w of workers) {
|
|
3610
3726
|
w.abortGeneration(generation);
|
|
3611
3727
|
}
|
|
3612
3728
|
},
|
|
3613
3729
|
registerCanvas(canvasId, canvas) {
|
|
3614
|
-
|
|
3615
|
-
|
|
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
|
+
}
|
|
3616
3737
|
},
|
|
3617
3738
|
unregisterCanvas(canvasId) {
|
|
3618
|
-
|
|
3619
|
-
|
|
3739
|
+
if (terminated) return;
|
|
3740
|
+
const worker = workers[parseChannelFromCanvasId(canvasId)];
|
|
3741
|
+
if (!worker) return;
|
|
3742
|
+
worker.unregisterCanvas(canvasId);
|
|
3620
3743
|
},
|
|
3621
3744
|
registerAudioData(clipId, channelDataArrays, sampleRate) {
|
|
3622
|
-
|
|
3623
|
-
|
|
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);
|
|
3624
3750
|
}
|
|
3625
3751
|
},
|
|
3626
3752
|
unregisterAudioData(clipId) {
|
|
3753
|
+
if (terminated) return;
|
|
3754
|
+
registeredAudio.delete(clipId);
|
|
3627
3755
|
for (const w of workers) {
|
|
3628
3756
|
w.unregisterAudioData(clipId);
|
|
3629
3757
|
}
|
|
3630
3758
|
},
|
|
3631
3759
|
terminate() {
|
|
3760
|
+
terminated = true;
|
|
3761
|
+
registeredAudio.clear();
|
|
3632
3762
|
for (const w of workers) {
|
|
3633
3763
|
w.terminate();
|
|
3634
3764
|
}
|
|
@@ -3636,6 +3766,9 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3636
3766
|
};
|
|
3637
3767
|
}
|
|
3638
3768
|
|
|
3769
|
+
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3770
|
+
var import_core = require("@waveform-playlist/core");
|
|
3771
|
+
|
|
3639
3772
|
// src/orchestrator/color-lut-cache.ts
|
|
3640
3773
|
var ColorLUTCache = class {
|
|
3641
3774
|
constructor() {
|
|
@@ -3699,6 +3832,20 @@ function groupContiguousChunks(chunks) {
|
|
|
3699
3832
|
groups.push(current);
|
|
3700
3833
|
return groups;
|
|
3701
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
|
+
}
|
|
3702
3849
|
|
|
3703
3850
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3704
3851
|
var SpectrogramOrchestrator = class extends EventTarget {
|
|
@@ -3711,37 +3858,62 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3711
3858
|
this.colorLUT = new ColorLUTCache();
|
|
3712
3859
|
this.disposed = false;
|
|
3713
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();
|
|
3714
3867
|
// Tracks which trackIds have already emitted `viewport-ready` for the
|
|
3715
|
-
// current generation. Cleared on every generation bump (setViewport
|
|
3716
|
-
// setConfig
|
|
3717
|
-
//
|
|
3718
|
-
// loading — would re-fire the
|
|
3868
|
+
// current generation. Cleared on every generation bump (setViewport with a
|
|
3869
|
+
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
3870
|
+
// Without this, every render — including those triggered by late
|
|
3871
|
+
// `registerCanvas` calls during track-by-track loading — would re-fire the
|
|
3872
|
+
// event for every track in the canvas map (N×N-ish ascending fan-in:
|
|
3873
|
+
// 3 tracks → 4+3+2+1 = 10 events; the dedup reduces this to 3).
|
|
3719
3874
|
this.readyDispatched = /* @__PURE__ */ new Set();
|
|
3720
3875
|
const poolSize = opts.workerPoolSize ?? 2;
|
|
3721
3876
|
this.pool = createSpectrogramWorkerPool(opts.workerFactory, poolSize);
|
|
3722
3877
|
this.config = opts.config;
|
|
3723
|
-
this.colorMap = opts.colorMap ??
|
|
3878
|
+
this.colorMap = opts.colorMap ?? import_core.DEFAULT_SPECTROGRAM_COLOR_MAP;
|
|
3724
3879
|
this.devicePixelRatio = opts.devicePixelRatio ?? (typeof window !== "undefined" ? window.devicePixelRatio : 1);
|
|
3725
3880
|
}
|
|
3726
3881
|
registerClip(reg) {
|
|
3727
3882
|
if (this.disposed) return;
|
|
3883
|
+
const channelData = [...reg.channelData];
|
|
3728
3884
|
this.clips.set(reg.clipId, {
|
|
3729
3885
|
trackId: reg.trackId,
|
|
3730
|
-
channelData
|
|
3886
|
+
channelData,
|
|
3731
3887
|
sampleRate: reg.sampleRate,
|
|
3732
3888
|
durationSamples: reg.durationSamples,
|
|
3733
3889
|
offsetSamples: reg.offsetSamples
|
|
3734
3890
|
});
|
|
3735
|
-
this.pool.registerAudioData(reg.clipId,
|
|
3891
|
+
this.pool.registerAudioData(reg.clipId, channelData, reg.sampleRate);
|
|
3892
|
+
if (this.viewport) {
|
|
3893
|
+
for (const canvas of this.canvases.values()) {
|
|
3894
|
+
if (canvas.clipId === reg.clipId) {
|
|
3895
|
+
this.scheduleRender();
|
|
3896
|
+
break;
|
|
3897
|
+
}
|
|
3898
|
+
}
|
|
3899
|
+
}
|
|
3736
3900
|
}
|
|
3737
3901
|
unregisterClip(clipId) {
|
|
3738
3902
|
if (this.disposed) return;
|
|
3739
|
-
if (!this.clips.has(clipId))
|
|
3903
|
+
if (!this.clips.has(clipId)) {
|
|
3904
|
+
console.warn("[dawcore-spectrogram] unregisterClip: unknown clip " + clipId);
|
|
3905
|
+
return;
|
|
3906
|
+
}
|
|
3740
3907
|
this.clips.delete(clipId);
|
|
3741
3908
|
this.pool.unregisterAudioData(clipId);
|
|
3742
3909
|
}
|
|
3743
3910
|
registerCanvas(reg) {
|
|
3744
|
-
if (this.disposed)
|
|
3911
|
+
if (this.disposed) {
|
|
3912
|
+
console.warn(
|
|
3913
|
+
"[dawcore-spectrogram] registerCanvas after dispose \u2014 canvas " + reg.canvasId + " will not render (OffscreenCanvas is now dead)"
|
|
3914
|
+
);
|
|
3915
|
+
return;
|
|
3916
|
+
}
|
|
3745
3917
|
this.canvases.set(reg.canvasId, {
|
|
3746
3918
|
canvasId: reg.canvasId,
|
|
3747
3919
|
globalPixelOffset: reg.globalPixelOffset,
|
|
@@ -3752,66 +3924,130 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3752
3924
|
channelIndex: reg.channelIndex,
|
|
3753
3925
|
chunkIndex: reg.chunkIndex
|
|
3754
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
|
+
}
|
|
3755
3939
|
this.pool.registerCanvas(reg.canvasId, reg.canvas);
|
|
3756
3940
|
if (this.viewport) this.scheduleRender();
|
|
3757
3941
|
}
|
|
3758
3942
|
unregisterCanvas(canvasId) {
|
|
3759
3943
|
if (this.disposed) return;
|
|
3760
|
-
|
|
3944
|
+
const entry = this.canvases.get(canvasId);
|
|
3945
|
+
if (!entry) {
|
|
3946
|
+
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3947
|
+
return;
|
|
3948
|
+
}
|
|
3761
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
|
+
}
|
|
3762
3955
|
this.pool.unregisterCanvas(canvasId);
|
|
3763
3956
|
}
|
|
3764
3957
|
setViewport(state) {
|
|
3765
3958
|
if (this.disposed) return;
|
|
3959
|
+
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) {
|
|
3960
|
+
console.warn(
|
|
3961
|
+
"[dawcore-spectrogram] setViewport: invalid state \u2014 ignored (" + JSON.stringify(state) + ")"
|
|
3962
|
+
);
|
|
3963
|
+
return;
|
|
3964
|
+
}
|
|
3766
3965
|
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3767
|
-
|
|
3768
|
-
this.generation += 1;
|
|
3769
|
-
this.readyDispatched.clear();
|
|
3770
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3966
|
+
this.bumpGenerationAndAbortStale();
|
|
3771
3967
|
this.viewport = state;
|
|
3772
3968
|
this.scheduleRender();
|
|
3773
3969
|
}
|
|
3774
3970
|
setConfig(config) {
|
|
3775
3971
|
if (this.disposed) return;
|
|
3776
3972
|
this.config = config;
|
|
3777
|
-
|
|
3778
|
-
this.generation += 1;
|
|
3779
|
-
this.readyDispatched.clear();
|
|
3780
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3973
|
+
this.bumpGenerationAndAbortStale();
|
|
3781
3974
|
this.colorLUT.clear();
|
|
3782
3975
|
this.scheduleRender();
|
|
3783
3976
|
}
|
|
3784
3977
|
setColorMap(colorMap) {
|
|
3785
3978
|
if (this.disposed) return;
|
|
3786
3979
|
this.colorMap = colorMap;
|
|
3787
|
-
|
|
3788
|
-
this.generation += 1;
|
|
3789
|
-
this.readyDispatched.clear();
|
|
3790
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3980
|
+
this.bumpGenerationAndAbortStale();
|
|
3791
3981
|
this.scheduleRender();
|
|
3792
3982
|
}
|
|
3793
3983
|
setDevicePixelRatio(dpr) {
|
|
3984
|
+
if (this.disposed) return;
|
|
3985
|
+
if (!Number.isFinite(dpr) || dpr <= 0) {
|
|
3986
|
+
console.warn(
|
|
3987
|
+
"[dawcore-spectrogram] setDevicePixelRatio: invalid value " + dpr + " \u2014 ignored"
|
|
3988
|
+
);
|
|
3989
|
+
return;
|
|
3990
|
+
}
|
|
3991
|
+
if (this.devicePixelRatio === dpr) return;
|
|
3794
3992
|
this.devicePixelRatio = dpr;
|
|
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() {
|
|
4004
|
+
this.generation += 1;
|
|
4005
|
+
this.readyDispatched.clear();
|
|
4006
|
+
this.pool.abortGeneration(this.generation);
|
|
3795
4007
|
}
|
|
3796
4008
|
dispose() {
|
|
3797
4009
|
if (this.disposed) return;
|
|
3798
4010
|
this.disposed = true;
|
|
3799
4011
|
this.clips.clear();
|
|
3800
4012
|
this.canvases.clear();
|
|
4013
|
+
this.clipOrigins.clear();
|
|
3801
4014
|
this.viewport = null;
|
|
3802
4015
|
this.colorLUT.clear();
|
|
3803
4016
|
this.readyDispatched.clear();
|
|
3804
4017
|
this.pool.terminate();
|
|
3805
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
|
+
*/
|
|
3806
4025
|
scheduleRender() {
|
|
3807
|
-
if (this.renderInFlight) return;
|
|
3808
4026
|
if (!this.viewport) return;
|
|
4027
|
+
if (this.renderInFlight) {
|
|
4028
|
+
this.renderQueued = true;
|
|
4029
|
+
return;
|
|
4030
|
+
}
|
|
3809
4031
|
this.renderInFlight = true;
|
|
3810
4032
|
queueMicrotask(() => {
|
|
3811
|
-
this.
|
|
3812
|
-
void this.runRender(this.generation);
|
|
4033
|
+
void this.runRenderLoop();
|
|
3813
4034
|
});
|
|
3814
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
|
+
}
|
|
3815
4051
|
async runRender(generation) {
|
|
3816
4052
|
if (this.disposed) return;
|
|
3817
4053
|
const viewport = this.viewport;
|
|
@@ -3823,21 +4059,39 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3823
4059
|
canvasesByTrack.set(c.trackId, list);
|
|
3824
4060
|
}
|
|
3825
4061
|
for (const [trackId, trackCanvases] of canvasesByTrack) {
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
this.readyDispatched.
|
|
3831
|
-
|
|
4062
|
+
try {
|
|
4063
|
+
const tiers = classifyViewport(trackCanvases, viewport);
|
|
4064
|
+
await this.renderTier(tiers.viewport, generation, viewport);
|
|
4065
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4066
|
+
if (!this.readyDispatched.has(trackId)) {
|
|
4067
|
+
this.readyDispatched.add(trackId);
|
|
4068
|
+
this.dispatchEvent(
|
|
4069
|
+
new CustomEvent("viewport-ready", { detail: { trackId, generation } })
|
|
4070
|
+
);
|
|
4071
|
+
}
|
|
4072
|
+
await this.renderTier(tiers.buffer, generation, viewport);
|
|
4073
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4074
|
+
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
4075
|
+
} catch (err) {
|
|
4076
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4077
|
+
if (err instanceof SpectrogramAbortError) {
|
|
4078
|
+
return;
|
|
4079
|
+
}
|
|
4080
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
4081
|
+
console.warn(
|
|
4082
|
+
"[dawcore-spectrogram] render failed for track " + trackId + " (generation " + generation + "): " + error.message
|
|
4083
|
+
);
|
|
4084
|
+
this.dispatchEvent(
|
|
4085
|
+
new CustomEvent("viewport-error", {
|
|
4086
|
+
detail: { trackId, generation, error }
|
|
4087
|
+
})
|
|
4088
|
+
);
|
|
3832
4089
|
}
|
|
3833
|
-
await this.renderTier(tiers.buffer, generation, viewport);
|
|
3834
|
-
if (this.generation !== generation || this.disposed) return;
|
|
3835
|
-
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
3836
4090
|
}
|
|
3837
4091
|
}
|
|
3838
4092
|
async renderTier(canvases, generation, viewport) {
|
|
3839
4093
|
if (canvases.length === 0) return;
|
|
3840
|
-
const groups =
|
|
4094
|
+
const groups = groupRenderableChunks(canvases);
|
|
3841
4095
|
for (const group of groups) {
|
|
3842
4096
|
if (this.generation !== generation || this.disposed) return;
|
|
3843
4097
|
await this.renderGroup(group, generation, viewport);
|
|
@@ -3847,10 +4101,32 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3847
4101
|
if (group.length === 0) return;
|
|
3848
4102
|
const first = group[0];
|
|
3849
4103
|
const clip = this.clips.get(first.clipId);
|
|
3850
|
-
if (!clip)
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
4104
|
+
if (!clip) {
|
|
4105
|
+
console.warn(
|
|
4106
|
+
"[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
|
|
4107
|
+
);
|
|
4108
|
+
return;
|
|
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];
|
|
4126
|
+
const fftSize = this.config.fftSize ?? import_core.SPECTROGRAM_DEFAULTS.fftSize;
|
|
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));
|
|
3854
4130
|
const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
|
|
3855
4131
|
const endSample = Math.min(
|
|
3856
4132
|
clip.offsetSamples + clip.durationSamples,
|
|
@@ -3878,16 +4154,16 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3878
4154
|
cacheKey: cacheKey2,
|
|
3879
4155
|
canvasIds: group.map((c) => c.canvasId),
|
|
3880
4156
|
canvasWidths: group.map((c) => c.widthPx),
|
|
3881
|
-
globalPixelOffsets:
|
|
4157
|
+
globalPixelOffsets: clipRelativeOffsets,
|
|
3882
4158
|
canvasHeight: first.heightPx,
|
|
3883
4159
|
devicePixelRatio: this.devicePixelRatio,
|
|
3884
4160
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
3885
4161
|
colorLUT,
|
|
3886
|
-
frequencyScale: String(this.config.frequencyScale ??
|
|
3887
|
-
minFrequency: this.config.minFrequency ??
|
|
4162
|
+
frequencyScale: String(this.config.frequencyScale ?? import_core.SPECTROGRAM_DEFAULTS.frequencyScale),
|
|
4163
|
+
minFrequency: this.config.minFrequency ?? import_core.SPECTROGRAM_DEFAULTS.minFrequency,
|
|
3888
4164
|
maxFrequency: this.config.maxFrequency ?? clip.sampleRate / 2,
|
|
3889
|
-
gainDb: this.config.gainDb ??
|
|
3890
|
-
rangeDb: this.config.rangeDb ??
|
|
4165
|
+
gainDb: this.config.gainDb ?? import_core.SPECTROGRAM_DEFAULTS.gainDb,
|
|
4166
|
+
rangeDb: this.config.rangeDb ?? import_core.SPECTROGRAM_DEFAULTS.rangeDb,
|
|
3891
4167
|
channelIndex: first.channelIndex
|
|
3892
4168
|
},
|
|
3893
4169
|
generation
|
|
@@ -3895,12 +4171,21 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3895
4171
|
}
|
|
3896
4172
|
async renderRemainingViaIdle(canvases, generation, viewport) {
|
|
3897
4173
|
if (canvases.length === 0) return;
|
|
3898
|
-
const groups =
|
|
4174
|
+
const groups = groupRenderableChunks(canvases);
|
|
3899
4175
|
for (const group of groups) {
|
|
3900
4176
|
if (this.generation !== generation || this.disposed) return;
|
|
3901
4177
|
await this.yieldUntilIdle();
|
|
3902
4178
|
if (this.generation !== generation || this.disposed) return;
|
|
3903
|
-
|
|
4179
|
+
try {
|
|
4180
|
+
await this.renderGroup(group, generation, viewport);
|
|
4181
|
+
} catch (err) {
|
|
4182
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4183
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
4184
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
4185
|
+
console.warn(
|
|
4186
|
+
"[dawcore-spectrogram] remaining-tier render failed for canvas group: " + error.message
|
|
4187
|
+
);
|
|
4188
|
+
}
|
|
3904
4189
|
}
|
|
3905
4190
|
}
|
|
3906
4191
|
yieldUntilIdle() {
|
|
@@ -3928,6 +4213,9 @@ function viewportsEqual(a, b) {
|
|
|
3928
4213
|
createSpectrogramWorkerPool,
|
|
3929
4214
|
getColorMap,
|
|
3930
4215
|
getFrequencyScale,
|
|
3931
|
-
groupContiguousChunks
|
|
4216
|
+
groupContiguousChunks,
|
|
4217
|
+
groupRenderableChunks,
|
|
4218
|
+
pixelColumnToFrame,
|
|
4219
|
+
pixelRowToBin
|
|
3932
4220
|
});
|
|
3933
4221
|
//# sourceMappingURL=index.js.map
|