@dawcore/spectrogram 0.0.2 → 0.0.3
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/dist/index.d.mts +34 -26
- package/dist/index.d.ts +34 -26
- package/dist/index.js +150 -34
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +150 -34
- package/dist/index.mjs.map +1 -1
- package/dist/orchestrator/index.d.mts +34 -26
- package/dist/orchestrator/index.mjs +150 -34
- package/dist/orchestrator/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -3427,11 +3427,19 @@ function createSpectrogramWorker(worker) {
|
|
|
3427
3427
|
};
|
|
3428
3428
|
worker.onerror = (e) => {
|
|
3429
3429
|
terminated = true;
|
|
3430
|
-
|
|
3431
|
-
|
|
3430
|
+
console.error(
|
|
3431
|
+
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
3432
|
+
);
|
|
3433
|
+
for (const [id, entry] of pending) {
|
|
3434
|
+
entry.reject(new Error("Worker crashed (id=" + id + "): " + (e.message || "unknown error")));
|
|
3432
3435
|
}
|
|
3433
3436
|
pending.clear();
|
|
3434
3437
|
};
|
|
3438
|
+
worker.onmessageerror = (e) => {
|
|
3439
|
+
console.warn(
|
|
3440
|
+
"[dawcore-spectrogram] worker postMessage clone failure: " + (e.data ? JSON.stringify(e.data) : "no data")
|
|
3441
|
+
);
|
|
3442
|
+
};
|
|
3435
3443
|
return {
|
|
3436
3444
|
computeFFT(params, generation = 0) {
|
|
3437
3445
|
if (terminated) return Promise.reject(new Error("Worker terminated"));
|
|
@@ -3523,28 +3531,45 @@ function createSpectrogramWorker(worker) {
|
|
|
3523
3531
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
3524
3532
|
function parseChannelFromCanvasId(canvasId) {
|
|
3525
3533
|
const match = canvasId.match(/-ch(\d+)-/);
|
|
3526
|
-
|
|
3534
|
+
if (!match) {
|
|
3535
|
+
console.warn(
|
|
3536
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}- segment, routing to worker 0: " + canvasId
|
|
3537
|
+
);
|
|
3538
|
+
return 0;
|
|
3539
|
+
}
|
|
3540
|
+
return parseInt(match[1], 10);
|
|
3527
3541
|
}
|
|
3528
3542
|
function defaultPoolSize() {
|
|
3529
3543
|
return 2;
|
|
3530
3544
|
}
|
|
3531
3545
|
function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
|
|
3532
3546
|
const workers = [];
|
|
3547
|
+
let failedAt = -1;
|
|
3533
3548
|
try {
|
|
3534
3549
|
for (let i = 0; i < poolSize; i++) {
|
|
3550
|
+
failedAt = i;
|
|
3535
3551
|
workers.push(createSpectrogramWorker(createWorker()));
|
|
3536
3552
|
}
|
|
3553
|
+
failedAt = -1;
|
|
3537
3554
|
} catch (err) {
|
|
3538
3555
|
for (const w of workers) {
|
|
3539
|
-
|
|
3556
|
+
try {
|
|
3557
|
+
w.terminate();
|
|
3558
|
+
} catch (terminateErr) {
|
|
3559
|
+
console.warn(
|
|
3560
|
+
"[dawcore-spectrogram] pool constructor cleanup: terminate failed for worker \u2014 " + String(terminateErr)
|
|
3561
|
+
);
|
|
3562
|
+
}
|
|
3540
3563
|
}
|
|
3541
|
-
throw
|
|
3564
|
+
throw new Error(
|
|
3565
|
+
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
3566
|
+
);
|
|
3542
3567
|
}
|
|
3543
3568
|
function getWorkerForChannel(channelIndex) {
|
|
3544
3569
|
return workers[channelIndex % workers.length];
|
|
3545
3570
|
}
|
|
3546
3571
|
return {
|
|
3547
|
-
computeFFT(params, generation = 0) {
|
|
3572
|
+
async computeFFT(params, generation = 0) {
|
|
3548
3573
|
if (params.mono) {
|
|
3549
3574
|
return workers[0].computeFFT(params, generation);
|
|
3550
3575
|
}
|
|
@@ -3553,7 +3578,17 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3553
3578
|
const promises = activeWorkers.map(
|
|
3554
3579
|
(w, i) => w.computeFFT({ ...params, channelFilter: i }, generation)
|
|
3555
3580
|
);
|
|
3556
|
-
|
|
3581
|
+
const settled = await Promise.allSettled(promises);
|
|
3582
|
+
const failures = settled.filter((s) => s.status === "rejected");
|
|
3583
|
+
if (failures.length > 0) {
|
|
3584
|
+
for (let i = 1; i < failures.length; i++) {
|
|
3585
|
+
console.warn(
|
|
3586
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (failures[i].reason instanceof Error ? failures[i].reason.message : String(failures[i].reason))
|
|
3587
|
+
);
|
|
3588
|
+
}
|
|
3589
|
+
throw failures[0].reason;
|
|
3590
|
+
}
|
|
3591
|
+
return settled[0].value;
|
|
3557
3592
|
},
|
|
3558
3593
|
renderChunks(params, generation = 0) {
|
|
3559
3594
|
const worker = getWorkerForChannel(params.channelIndex);
|
|
@@ -3590,6 +3625,9 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3590
3625
|
};
|
|
3591
3626
|
}
|
|
3592
3627
|
|
|
3628
|
+
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3629
|
+
import { SPECTROGRAM_DEFAULTS, DEFAULT_SPECTROGRAM_COLOR_MAP } from "@waveform-playlist/core";
|
|
3630
|
+
|
|
3593
3631
|
// src/orchestrator/color-lut-cache.ts
|
|
3594
3632
|
var ColorLUTCache = class {
|
|
3595
3633
|
constructor() {
|
|
@@ -3666,36 +3704,55 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3666
3704
|
this.disposed = false;
|
|
3667
3705
|
this.renderInFlight = false;
|
|
3668
3706
|
// 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
|
|
3707
|
+
// current generation. Cleared on every generation bump (setViewport with a
|
|
3708
|
+
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
3709
|
+
// Without this, every render — including those triggered by late
|
|
3710
|
+
// `registerCanvas` calls during track-by-track loading — would re-fire the
|
|
3711
|
+
// event for every track in the canvas map (N×N-ish ascending fan-in:
|
|
3712
|
+
// 3 tracks → 4+3+2+1 = 10 events; the dedup reduces this to 3).
|
|
3673
3713
|
this.readyDispatched = /* @__PURE__ */ new Set();
|
|
3674
3714
|
const poolSize = opts.workerPoolSize ?? 2;
|
|
3675
3715
|
this.pool = createSpectrogramWorkerPool(opts.workerFactory, poolSize);
|
|
3676
3716
|
this.config = opts.config;
|
|
3677
|
-
this.colorMap = opts.colorMap ??
|
|
3717
|
+
this.colorMap = opts.colorMap ?? DEFAULT_SPECTROGRAM_COLOR_MAP;
|
|
3678
3718
|
this.devicePixelRatio = opts.devicePixelRatio ?? (typeof window !== "undefined" ? window.devicePixelRatio : 1);
|
|
3679
3719
|
}
|
|
3680
3720
|
registerClip(reg) {
|
|
3681
3721
|
if (this.disposed) return;
|
|
3722
|
+
const channelData = [...reg.channelData];
|
|
3682
3723
|
this.clips.set(reg.clipId, {
|
|
3683
3724
|
trackId: reg.trackId,
|
|
3684
|
-
channelData
|
|
3725
|
+
channelData,
|
|
3685
3726
|
sampleRate: reg.sampleRate,
|
|
3686
3727
|
durationSamples: reg.durationSamples,
|
|
3687
3728
|
offsetSamples: reg.offsetSamples
|
|
3688
3729
|
});
|
|
3689
|
-
this.pool.registerAudioData(reg.clipId,
|
|
3730
|
+
this.pool.registerAudioData(reg.clipId, channelData, reg.sampleRate);
|
|
3731
|
+
if (this.viewport) {
|
|
3732
|
+
for (const canvas of this.canvases.values()) {
|
|
3733
|
+
if (canvas.clipId === reg.clipId) {
|
|
3734
|
+
this.scheduleRender();
|
|
3735
|
+
break;
|
|
3736
|
+
}
|
|
3737
|
+
}
|
|
3738
|
+
}
|
|
3690
3739
|
}
|
|
3691
3740
|
unregisterClip(clipId) {
|
|
3692
3741
|
if (this.disposed) return;
|
|
3693
|
-
if (!this.clips.has(clipId))
|
|
3742
|
+
if (!this.clips.has(clipId)) {
|
|
3743
|
+
console.warn("[dawcore-spectrogram] unregisterClip: unknown clip " + clipId);
|
|
3744
|
+
return;
|
|
3745
|
+
}
|
|
3694
3746
|
this.clips.delete(clipId);
|
|
3695
3747
|
this.pool.unregisterAudioData(clipId);
|
|
3696
3748
|
}
|
|
3697
3749
|
registerCanvas(reg) {
|
|
3698
|
-
if (this.disposed)
|
|
3750
|
+
if (this.disposed) {
|
|
3751
|
+
console.warn(
|
|
3752
|
+
"[dawcore-spectrogram] registerCanvas after dispose \u2014 canvas " + reg.canvasId + " will not render (OffscreenCanvas is now dead)"
|
|
3753
|
+
);
|
|
3754
|
+
return;
|
|
3755
|
+
}
|
|
3699
3756
|
this.canvases.set(reg.canvasId, {
|
|
3700
3757
|
canvasId: reg.canvasId,
|
|
3701
3758
|
globalPixelOffset: reg.globalPixelOffset,
|
|
@@ -3711,12 +3768,21 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3711
3768
|
}
|
|
3712
3769
|
unregisterCanvas(canvasId) {
|
|
3713
3770
|
if (this.disposed) return;
|
|
3714
|
-
if (!this.canvases.has(canvasId))
|
|
3771
|
+
if (!this.canvases.has(canvasId)) {
|
|
3772
|
+
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3773
|
+
return;
|
|
3774
|
+
}
|
|
3715
3775
|
this.canvases.delete(canvasId);
|
|
3716
3776
|
this.pool.unregisterCanvas(canvasId);
|
|
3717
3777
|
}
|
|
3718
3778
|
setViewport(state) {
|
|
3719
3779
|
if (this.disposed) return;
|
|
3780
|
+
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) {
|
|
3781
|
+
console.warn(
|
|
3782
|
+
"[dawcore-spectrogram] setViewport: invalid state \u2014 ignored (" + JSON.stringify(state) + ")"
|
|
3783
|
+
);
|
|
3784
|
+
return;
|
|
3785
|
+
}
|
|
3720
3786
|
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3721
3787
|
const prevGeneration = this.generation;
|
|
3722
3788
|
this.generation += 1;
|
|
@@ -3745,7 +3811,20 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3745
3811
|
this.scheduleRender();
|
|
3746
3812
|
}
|
|
3747
3813
|
setDevicePixelRatio(dpr) {
|
|
3814
|
+
if (this.disposed) return;
|
|
3815
|
+
if (!Number.isFinite(dpr) || dpr <= 0) {
|
|
3816
|
+
console.warn(
|
|
3817
|
+
"[dawcore-spectrogram] setDevicePixelRatio: invalid value " + dpr + " \u2014 ignored"
|
|
3818
|
+
);
|
|
3819
|
+
return;
|
|
3820
|
+
}
|
|
3821
|
+
if (this.devicePixelRatio === dpr) return;
|
|
3748
3822
|
this.devicePixelRatio = dpr;
|
|
3823
|
+
const prevGeneration = this.generation;
|
|
3824
|
+
this.generation += 1;
|
|
3825
|
+
this.readyDispatched.clear();
|
|
3826
|
+
this.pool.abortGeneration(prevGeneration);
|
|
3827
|
+
this.scheduleRender();
|
|
3749
3828
|
}
|
|
3750
3829
|
dispose() {
|
|
3751
3830
|
if (this.disposed) return;
|
|
@@ -3763,7 +3842,12 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3763
3842
|
this.renderInFlight = true;
|
|
3764
3843
|
queueMicrotask(() => {
|
|
3765
3844
|
this.renderInFlight = false;
|
|
3766
|
-
|
|
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
|
+
});
|
|
3767
3851
|
});
|
|
3768
3852
|
}
|
|
3769
3853
|
async runRender(generation) {
|
|
@@ -3777,16 +3861,34 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3777
3861
|
canvasesByTrack.set(c.trackId, list);
|
|
3778
3862
|
}
|
|
3779
3863
|
for (const [trackId, trackCanvases] of canvasesByTrack) {
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
this.readyDispatched.
|
|
3785
|
-
|
|
3864
|
+
try {
|
|
3865
|
+
const tiers = classifyViewport(trackCanvases, viewport);
|
|
3866
|
+
await this.renderTier(tiers.viewport, generation, viewport);
|
|
3867
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3868
|
+
if (!this.readyDispatched.has(trackId)) {
|
|
3869
|
+
this.readyDispatched.add(trackId);
|
|
3870
|
+
this.dispatchEvent(
|
|
3871
|
+
new CustomEvent("viewport-ready", { detail: { trackId, generation } })
|
|
3872
|
+
);
|
|
3873
|
+
}
|
|
3874
|
+
await this.renderTier(tiers.buffer, generation, viewport);
|
|
3875
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3876
|
+
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
3877
|
+
} catch (err) {
|
|
3878
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3879
|
+
if (err instanceof SpectrogramAbortError) {
|
|
3880
|
+
return;
|
|
3881
|
+
}
|
|
3882
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3883
|
+
console.warn(
|
|
3884
|
+
"[dawcore-spectrogram] render failed for track " + trackId + " (generation " + generation + "): " + error.message
|
|
3885
|
+
);
|
|
3886
|
+
this.dispatchEvent(
|
|
3887
|
+
new CustomEvent("viewport-error", {
|
|
3888
|
+
detail: { trackId, generation, error }
|
|
3889
|
+
})
|
|
3890
|
+
);
|
|
3786
3891
|
}
|
|
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
3892
|
}
|
|
3791
3893
|
}
|
|
3792
3894
|
async renderTier(canvases, generation, viewport) {
|
|
@@ -3801,8 +3903,13 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3801
3903
|
if (group.length === 0) return;
|
|
3802
3904
|
const first = group[0];
|
|
3803
3905
|
const clip = this.clips.get(first.clipId);
|
|
3804
|
-
if (!clip)
|
|
3805
|
-
|
|
3906
|
+
if (!clip) {
|
|
3907
|
+
console.warn(
|
|
3908
|
+
"[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
|
|
3909
|
+
);
|
|
3910
|
+
return;
|
|
3911
|
+
}
|
|
3912
|
+
const fftSize = this.config.fftSize ?? SPECTROGRAM_DEFAULTS.fftSize;
|
|
3806
3913
|
const startPx = Math.min(...group.map((c) => c.globalPixelOffset));
|
|
3807
3914
|
const endPx = Math.max(...group.map((c) => c.globalPixelOffset + c.widthPx));
|
|
3808
3915
|
const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
|
|
@@ -3837,11 +3944,11 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3837
3944
|
devicePixelRatio: this.devicePixelRatio,
|
|
3838
3945
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
3839
3946
|
colorLUT,
|
|
3840
|
-
frequencyScale: String(this.config.frequencyScale ??
|
|
3841
|
-
minFrequency: this.config.minFrequency ??
|
|
3947
|
+
frequencyScale: String(this.config.frequencyScale ?? SPECTROGRAM_DEFAULTS.frequencyScale),
|
|
3948
|
+
minFrequency: this.config.minFrequency ?? SPECTROGRAM_DEFAULTS.minFrequency,
|
|
3842
3949
|
maxFrequency: this.config.maxFrequency ?? clip.sampleRate / 2,
|
|
3843
|
-
gainDb: this.config.gainDb ??
|
|
3844
|
-
rangeDb: this.config.rangeDb ??
|
|
3950
|
+
gainDb: this.config.gainDb ?? SPECTROGRAM_DEFAULTS.gainDb,
|
|
3951
|
+
rangeDb: this.config.rangeDb ?? SPECTROGRAM_DEFAULTS.rangeDb,
|
|
3845
3952
|
channelIndex: first.channelIndex
|
|
3846
3953
|
},
|
|
3847
3954
|
generation
|
|
@@ -3854,7 +3961,16 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3854
3961
|
if (this.generation !== generation || this.disposed) return;
|
|
3855
3962
|
await this.yieldUntilIdle();
|
|
3856
3963
|
if (this.generation !== generation || this.disposed) return;
|
|
3857
|
-
|
|
3964
|
+
try {
|
|
3965
|
+
await this.renderGroup(group, generation, viewport);
|
|
3966
|
+
} catch (err) {
|
|
3967
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3968
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
3969
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3970
|
+
console.warn(
|
|
3971
|
+
"[dawcore-spectrogram] remaining-tier render failed for canvas group: " + error.message
|
|
3972
|
+
);
|
|
3973
|
+
}
|
|
3858
3974
|
}
|
|
3859
3975
|
}
|
|
3860
3976
|
yieldUntilIdle() {
|