@dawcore/spectrogram 0.0.1 → 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 +35 -26
- package/dist/index.d.ts +35 -26
- package/dist/index.js +161 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +161 -28
- package/dist/index.mjs.map +1 -1
- package/dist/orchestrator/index.d.mts +35 -26
- package/dist/orchestrator/index.mjs +161 -28
- package/dist/orchestrator/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -135,10 +135,10 @@ interface CanvasMeta {
|
|
|
135
135
|
widthPx: number;
|
|
136
136
|
}
|
|
137
137
|
interface ViewportBounds {
|
|
138
|
-
visibleStartPx: number;
|
|
139
|
-
visibleEndPx: number;
|
|
140
|
-
bufferStartPx: number;
|
|
141
|
-
bufferEndPx: number;
|
|
138
|
+
readonly visibleStartPx: number;
|
|
139
|
+
readonly visibleEndPx: number;
|
|
140
|
+
readonly bufferStartPx: number;
|
|
141
|
+
readonly bufferEndPx: number;
|
|
142
142
|
}
|
|
143
143
|
interface ClassifiedTiers<T extends CanvasMeta> {
|
|
144
144
|
viewport: T[];
|
|
@@ -148,33 +148,33 @@ interface ClassifiedTiers<T extends CanvasMeta> {
|
|
|
148
148
|
declare function classifyViewport<T extends CanvasMeta>(canvases: T[], bounds: ViewportBounds): ClassifiedTiers<T>;
|
|
149
149
|
|
|
150
150
|
interface SpectrogramOrchestratorOptions {
|
|
151
|
-
workerFactory: () => Worker;
|
|
152
|
-
workerPoolSize?: number;
|
|
153
|
-
config: SpectrogramConfig;
|
|
154
|
-
colorMap?: ColorMapValue;
|
|
155
|
-
devicePixelRatio?: number;
|
|
151
|
+
readonly workerFactory: () => Worker;
|
|
152
|
+
readonly workerPoolSize?: number;
|
|
153
|
+
readonly config: SpectrogramConfig;
|
|
154
|
+
readonly colorMap?: ColorMapValue;
|
|
155
|
+
readonly devicePixelRatio?: number;
|
|
156
156
|
}
|
|
157
157
|
interface ClipRegistration {
|
|
158
|
-
clipId: string;
|
|
159
|
-
trackId: string;
|
|
160
|
-
channelData: Float32Array
|
|
161
|
-
sampleRate: number;
|
|
162
|
-
durationSamples: number;
|
|
163
|
-
offsetSamples: number;
|
|
158
|
+
readonly clipId: string;
|
|
159
|
+
readonly trackId: string;
|
|
160
|
+
readonly channelData: ReadonlyArray<Float32Array>;
|
|
161
|
+
readonly sampleRate: number;
|
|
162
|
+
readonly durationSamples: number;
|
|
163
|
+
readonly offsetSamples: number;
|
|
164
164
|
}
|
|
165
165
|
interface CanvasRegistration {
|
|
166
|
-
canvasId: string;
|
|
167
|
-
canvas: OffscreenCanvas;
|
|
168
|
-
clipId: string;
|
|
169
|
-
trackId: string;
|
|
170
|
-
channelIndex: number;
|
|
171
|
-
chunkIndex: number;
|
|
172
|
-
globalPixelOffset: number;
|
|
173
|
-
widthPx: number;
|
|
174
|
-
heightPx: number;
|
|
166
|
+
readonly canvasId: string;
|
|
167
|
+
readonly canvas: OffscreenCanvas;
|
|
168
|
+
readonly clipId: string;
|
|
169
|
+
readonly trackId: string;
|
|
170
|
+
readonly channelIndex: number;
|
|
171
|
+
readonly chunkIndex: number;
|
|
172
|
+
readonly globalPixelOffset: number;
|
|
173
|
+
readonly widthPx: number;
|
|
174
|
+
readonly heightPx: number;
|
|
175
175
|
}
|
|
176
176
|
interface ViewportState extends ViewportBounds {
|
|
177
|
-
samplesPerPixel: number;
|
|
177
|
+
readonly samplesPerPixel: number;
|
|
178
178
|
}
|
|
179
179
|
interface ClipEntry {
|
|
180
180
|
trackId: string;
|
|
@@ -202,6 +202,7 @@ declare class SpectrogramOrchestrator extends EventTarget {
|
|
|
202
202
|
protected colorLUT: ColorLUTCache;
|
|
203
203
|
protected disposed: boolean;
|
|
204
204
|
protected renderInFlight: boolean;
|
|
205
|
+
protected readyDispatched: Set<string>;
|
|
205
206
|
constructor(opts: SpectrogramOrchestratorOptions);
|
|
206
207
|
registerClip(reg: ClipRegistration): void;
|
|
207
208
|
unregisterClip(clipId: string): void;
|
|
@@ -220,8 +221,16 @@ declare class SpectrogramOrchestrator extends EventTarget {
|
|
|
220
221
|
protected yieldUntilIdle(): Promise<void>;
|
|
221
222
|
}
|
|
222
223
|
|
|
224
|
+
/**
|
|
225
|
+
* `viewport-ready` fires at most once per `(generation, trackId)` pair after
|
|
226
|
+
* the viewport-tier FFT completes. Generation bumps (setViewport with a real
|
|
227
|
+
* change, setConfig, setColorMap, setDevicePixelRatio) reset the dispatched-set
|
|
228
|
+
* so the event re-fires on the next render. The `generation` field lets
|
|
229
|
+
* consumers correlate ready events to their own state.
|
|
230
|
+
*/
|
|
223
231
|
interface ViewportReadyDetail {
|
|
224
|
-
trackId: string;
|
|
232
|
+
readonly trackId: string;
|
|
233
|
+
readonly generation: number;
|
|
225
234
|
}
|
|
226
235
|
|
|
227
236
|
interface ChunkLike {
|
package/dist/index.d.ts
CHANGED
|
@@ -135,10 +135,10 @@ interface CanvasMeta {
|
|
|
135
135
|
widthPx: number;
|
|
136
136
|
}
|
|
137
137
|
interface ViewportBounds {
|
|
138
|
-
visibleStartPx: number;
|
|
139
|
-
visibleEndPx: number;
|
|
140
|
-
bufferStartPx: number;
|
|
141
|
-
bufferEndPx: number;
|
|
138
|
+
readonly visibleStartPx: number;
|
|
139
|
+
readonly visibleEndPx: number;
|
|
140
|
+
readonly bufferStartPx: number;
|
|
141
|
+
readonly bufferEndPx: number;
|
|
142
142
|
}
|
|
143
143
|
interface ClassifiedTiers<T extends CanvasMeta> {
|
|
144
144
|
viewport: T[];
|
|
@@ -148,33 +148,33 @@ interface ClassifiedTiers<T extends CanvasMeta> {
|
|
|
148
148
|
declare function classifyViewport<T extends CanvasMeta>(canvases: T[], bounds: ViewportBounds): ClassifiedTiers<T>;
|
|
149
149
|
|
|
150
150
|
interface SpectrogramOrchestratorOptions {
|
|
151
|
-
workerFactory: () => Worker;
|
|
152
|
-
workerPoolSize?: number;
|
|
153
|
-
config: SpectrogramConfig;
|
|
154
|
-
colorMap?: ColorMapValue;
|
|
155
|
-
devicePixelRatio?: number;
|
|
151
|
+
readonly workerFactory: () => Worker;
|
|
152
|
+
readonly workerPoolSize?: number;
|
|
153
|
+
readonly config: SpectrogramConfig;
|
|
154
|
+
readonly colorMap?: ColorMapValue;
|
|
155
|
+
readonly devicePixelRatio?: number;
|
|
156
156
|
}
|
|
157
157
|
interface ClipRegistration {
|
|
158
|
-
clipId: string;
|
|
159
|
-
trackId: string;
|
|
160
|
-
channelData: Float32Array
|
|
161
|
-
sampleRate: number;
|
|
162
|
-
durationSamples: number;
|
|
163
|
-
offsetSamples: number;
|
|
158
|
+
readonly clipId: string;
|
|
159
|
+
readonly trackId: string;
|
|
160
|
+
readonly channelData: ReadonlyArray<Float32Array>;
|
|
161
|
+
readonly sampleRate: number;
|
|
162
|
+
readonly durationSamples: number;
|
|
163
|
+
readonly offsetSamples: number;
|
|
164
164
|
}
|
|
165
165
|
interface CanvasRegistration {
|
|
166
|
-
canvasId: string;
|
|
167
|
-
canvas: OffscreenCanvas;
|
|
168
|
-
clipId: string;
|
|
169
|
-
trackId: string;
|
|
170
|
-
channelIndex: number;
|
|
171
|
-
chunkIndex: number;
|
|
172
|
-
globalPixelOffset: number;
|
|
173
|
-
widthPx: number;
|
|
174
|
-
heightPx: number;
|
|
166
|
+
readonly canvasId: string;
|
|
167
|
+
readonly canvas: OffscreenCanvas;
|
|
168
|
+
readonly clipId: string;
|
|
169
|
+
readonly trackId: string;
|
|
170
|
+
readonly channelIndex: number;
|
|
171
|
+
readonly chunkIndex: number;
|
|
172
|
+
readonly globalPixelOffset: number;
|
|
173
|
+
readonly widthPx: number;
|
|
174
|
+
readonly heightPx: number;
|
|
175
175
|
}
|
|
176
176
|
interface ViewportState extends ViewportBounds {
|
|
177
|
-
samplesPerPixel: number;
|
|
177
|
+
readonly samplesPerPixel: number;
|
|
178
178
|
}
|
|
179
179
|
interface ClipEntry {
|
|
180
180
|
trackId: string;
|
|
@@ -202,6 +202,7 @@ declare class SpectrogramOrchestrator extends EventTarget {
|
|
|
202
202
|
protected colorLUT: ColorLUTCache;
|
|
203
203
|
protected disposed: boolean;
|
|
204
204
|
protected renderInFlight: boolean;
|
|
205
|
+
protected readyDispatched: Set<string>;
|
|
205
206
|
constructor(opts: SpectrogramOrchestratorOptions);
|
|
206
207
|
registerClip(reg: ClipRegistration): void;
|
|
207
208
|
unregisterClip(clipId: string): void;
|
|
@@ -220,8 +221,16 @@ declare class SpectrogramOrchestrator extends EventTarget {
|
|
|
220
221
|
protected yieldUntilIdle(): Promise<void>;
|
|
221
222
|
}
|
|
222
223
|
|
|
224
|
+
/**
|
|
225
|
+
* `viewport-ready` fires at most once per `(generation, trackId)` pair after
|
|
226
|
+
* the viewport-tier FFT completes. Generation bumps (setViewport with a real
|
|
227
|
+
* change, setConfig, setColorMap, setDevicePixelRatio) reset the dispatched-set
|
|
228
|
+
* so the event re-fires on the next render. The `generation` field lets
|
|
229
|
+
* consumers correlate ready events to their own state.
|
|
230
|
+
*/
|
|
223
231
|
interface ViewportReadyDetail {
|
|
224
|
-
trackId: string;
|
|
232
|
+
readonly trackId: string;
|
|
233
|
+
readonly generation: number;
|
|
225
234
|
}
|
|
226
235
|
|
|
227
236
|
interface ChunkLike {
|
package/dist/index.js
CHANGED
|
@@ -3473,11 +3473,19 @@ function createSpectrogramWorker(worker) {
|
|
|
3473
3473
|
};
|
|
3474
3474
|
worker.onerror = (e) => {
|
|
3475
3475
|
terminated = true;
|
|
3476
|
-
|
|
3477
|
-
|
|
3476
|
+
console.error(
|
|
3477
|
+
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
3478
|
+
);
|
|
3479
|
+
for (const [id, entry] of pending) {
|
|
3480
|
+
entry.reject(new Error("Worker crashed (id=" + id + "): " + (e.message || "unknown error")));
|
|
3478
3481
|
}
|
|
3479
3482
|
pending.clear();
|
|
3480
3483
|
};
|
|
3484
|
+
worker.onmessageerror = (e) => {
|
|
3485
|
+
console.warn(
|
|
3486
|
+
"[dawcore-spectrogram] worker postMessage clone failure: " + (e.data ? JSON.stringify(e.data) : "no data")
|
|
3487
|
+
);
|
|
3488
|
+
};
|
|
3481
3489
|
return {
|
|
3482
3490
|
computeFFT(params, generation = 0) {
|
|
3483
3491
|
if (terminated) return Promise.reject(new Error("Worker terminated"));
|
|
@@ -3569,28 +3577,45 @@ function createSpectrogramWorker(worker) {
|
|
|
3569
3577
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
3570
3578
|
function parseChannelFromCanvasId(canvasId) {
|
|
3571
3579
|
const match = canvasId.match(/-ch(\d+)-/);
|
|
3572
|
-
|
|
3580
|
+
if (!match) {
|
|
3581
|
+
console.warn(
|
|
3582
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}- segment, routing to worker 0: " + canvasId
|
|
3583
|
+
);
|
|
3584
|
+
return 0;
|
|
3585
|
+
}
|
|
3586
|
+
return parseInt(match[1], 10);
|
|
3573
3587
|
}
|
|
3574
3588
|
function defaultPoolSize() {
|
|
3575
3589
|
return 2;
|
|
3576
3590
|
}
|
|
3577
3591
|
function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
|
|
3578
3592
|
const workers = [];
|
|
3593
|
+
let failedAt = -1;
|
|
3579
3594
|
try {
|
|
3580
3595
|
for (let i = 0; i < poolSize; i++) {
|
|
3596
|
+
failedAt = i;
|
|
3581
3597
|
workers.push(createSpectrogramWorker(createWorker()));
|
|
3582
3598
|
}
|
|
3599
|
+
failedAt = -1;
|
|
3583
3600
|
} catch (err) {
|
|
3584
3601
|
for (const w of workers) {
|
|
3585
|
-
|
|
3602
|
+
try {
|
|
3603
|
+
w.terminate();
|
|
3604
|
+
} catch (terminateErr) {
|
|
3605
|
+
console.warn(
|
|
3606
|
+
"[dawcore-spectrogram] pool constructor cleanup: terminate failed for worker \u2014 " + String(terminateErr)
|
|
3607
|
+
);
|
|
3608
|
+
}
|
|
3586
3609
|
}
|
|
3587
|
-
throw
|
|
3610
|
+
throw new Error(
|
|
3611
|
+
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
3612
|
+
);
|
|
3588
3613
|
}
|
|
3589
3614
|
function getWorkerForChannel(channelIndex) {
|
|
3590
3615
|
return workers[channelIndex % workers.length];
|
|
3591
3616
|
}
|
|
3592
3617
|
return {
|
|
3593
|
-
computeFFT(params, generation = 0) {
|
|
3618
|
+
async computeFFT(params, generation = 0) {
|
|
3594
3619
|
if (params.mono) {
|
|
3595
3620
|
return workers[0].computeFFT(params, generation);
|
|
3596
3621
|
}
|
|
@@ -3599,7 +3624,17 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3599
3624
|
const promises = activeWorkers.map(
|
|
3600
3625
|
(w, i) => w.computeFFT({ ...params, channelFilter: i }, generation)
|
|
3601
3626
|
);
|
|
3602
|
-
|
|
3627
|
+
const settled = await Promise.allSettled(promises);
|
|
3628
|
+
const failures = settled.filter((s) => s.status === "rejected");
|
|
3629
|
+
if (failures.length > 0) {
|
|
3630
|
+
for (let i = 1; i < failures.length; i++) {
|
|
3631
|
+
console.warn(
|
|
3632
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (failures[i].reason instanceof Error ? failures[i].reason.message : String(failures[i].reason))
|
|
3633
|
+
);
|
|
3634
|
+
}
|
|
3635
|
+
throw failures[0].reason;
|
|
3636
|
+
}
|
|
3637
|
+
return settled[0].value;
|
|
3603
3638
|
},
|
|
3604
3639
|
renderChunks(params, generation = 0) {
|
|
3605
3640
|
const worker = getWorkerForChannel(params.channelIndex);
|
|
@@ -3636,6 +3671,9 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
3636
3671
|
};
|
|
3637
3672
|
}
|
|
3638
3673
|
|
|
3674
|
+
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3675
|
+
var import_core = require("@waveform-playlist/core");
|
|
3676
|
+
|
|
3639
3677
|
// src/orchestrator/color-lut-cache.ts
|
|
3640
3678
|
var ColorLUTCache = class {
|
|
3641
3679
|
constructor() {
|
|
@@ -3711,31 +3749,56 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3711
3749
|
this.colorLUT = new ColorLUTCache();
|
|
3712
3750
|
this.disposed = false;
|
|
3713
3751
|
this.renderInFlight = false;
|
|
3752
|
+
// Tracks which trackIds have already emitted `viewport-ready` for the
|
|
3753
|
+
// current generation. Cleared on every generation bump (setViewport with a
|
|
3754
|
+
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
3755
|
+
// Without this, every render — including those triggered by late
|
|
3756
|
+
// `registerCanvas` calls during track-by-track loading — would re-fire the
|
|
3757
|
+
// event for every track in the canvas map (N×N-ish ascending fan-in:
|
|
3758
|
+
// 3 tracks → 4+3+2+1 = 10 events; the dedup reduces this to 3).
|
|
3759
|
+
this.readyDispatched = /* @__PURE__ */ new Set();
|
|
3714
3760
|
const poolSize = opts.workerPoolSize ?? 2;
|
|
3715
3761
|
this.pool = createSpectrogramWorkerPool(opts.workerFactory, poolSize);
|
|
3716
3762
|
this.config = opts.config;
|
|
3717
|
-
this.colorMap = opts.colorMap ??
|
|
3763
|
+
this.colorMap = opts.colorMap ?? import_core.DEFAULT_SPECTROGRAM_COLOR_MAP;
|
|
3718
3764
|
this.devicePixelRatio = opts.devicePixelRatio ?? (typeof window !== "undefined" ? window.devicePixelRatio : 1);
|
|
3719
3765
|
}
|
|
3720
3766
|
registerClip(reg) {
|
|
3721
3767
|
if (this.disposed) return;
|
|
3768
|
+
const channelData = [...reg.channelData];
|
|
3722
3769
|
this.clips.set(reg.clipId, {
|
|
3723
3770
|
trackId: reg.trackId,
|
|
3724
|
-
channelData
|
|
3771
|
+
channelData,
|
|
3725
3772
|
sampleRate: reg.sampleRate,
|
|
3726
3773
|
durationSamples: reg.durationSamples,
|
|
3727
3774
|
offsetSamples: reg.offsetSamples
|
|
3728
3775
|
});
|
|
3729
|
-
this.pool.registerAudioData(reg.clipId,
|
|
3776
|
+
this.pool.registerAudioData(reg.clipId, channelData, reg.sampleRate);
|
|
3777
|
+
if (this.viewport) {
|
|
3778
|
+
for (const canvas of this.canvases.values()) {
|
|
3779
|
+
if (canvas.clipId === reg.clipId) {
|
|
3780
|
+
this.scheduleRender();
|
|
3781
|
+
break;
|
|
3782
|
+
}
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3730
3785
|
}
|
|
3731
3786
|
unregisterClip(clipId) {
|
|
3732
3787
|
if (this.disposed) return;
|
|
3733
|
-
if (!this.clips.has(clipId))
|
|
3788
|
+
if (!this.clips.has(clipId)) {
|
|
3789
|
+
console.warn("[dawcore-spectrogram] unregisterClip: unknown clip " + clipId);
|
|
3790
|
+
return;
|
|
3791
|
+
}
|
|
3734
3792
|
this.clips.delete(clipId);
|
|
3735
3793
|
this.pool.unregisterAudioData(clipId);
|
|
3736
3794
|
}
|
|
3737
3795
|
registerCanvas(reg) {
|
|
3738
|
-
if (this.disposed)
|
|
3796
|
+
if (this.disposed) {
|
|
3797
|
+
console.warn(
|
|
3798
|
+
"[dawcore-spectrogram] registerCanvas after dispose \u2014 canvas " + reg.canvasId + " will not render (OffscreenCanvas is now dead)"
|
|
3799
|
+
);
|
|
3800
|
+
return;
|
|
3801
|
+
}
|
|
3739
3802
|
this.canvases.set(reg.canvasId, {
|
|
3740
3803
|
canvasId: reg.canvasId,
|
|
3741
3804
|
globalPixelOffset: reg.globalPixelOffset,
|
|
@@ -3751,14 +3814,25 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3751
3814
|
}
|
|
3752
3815
|
unregisterCanvas(canvasId) {
|
|
3753
3816
|
if (this.disposed) return;
|
|
3754
|
-
if (!this.canvases.has(canvasId))
|
|
3817
|
+
if (!this.canvases.has(canvasId)) {
|
|
3818
|
+
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3819
|
+
return;
|
|
3820
|
+
}
|
|
3755
3821
|
this.canvases.delete(canvasId);
|
|
3756
3822
|
this.pool.unregisterCanvas(canvasId);
|
|
3757
3823
|
}
|
|
3758
3824
|
setViewport(state) {
|
|
3759
3825
|
if (this.disposed) return;
|
|
3826
|
+
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) {
|
|
3827
|
+
console.warn(
|
|
3828
|
+
"[dawcore-spectrogram] setViewport: invalid state \u2014 ignored (" + JSON.stringify(state) + ")"
|
|
3829
|
+
);
|
|
3830
|
+
return;
|
|
3831
|
+
}
|
|
3832
|
+
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3760
3833
|
const prevGeneration = this.generation;
|
|
3761
3834
|
this.generation += 1;
|
|
3835
|
+
this.readyDispatched.clear();
|
|
3762
3836
|
this.pool.abortGeneration(prevGeneration);
|
|
3763
3837
|
this.viewport = state;
|
|
3764
3838
|
this.scheduleRender();
|
|
@@ -3768,6 +3842,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3768
3842
|
this.config = config;
|
|
3769
3843
|
const prevGeneration = this.generation;
|
|
3770
3844
|
this.generation += 1;
|
|
3845
|
+
this.readyDispatched.clear();
|
|
3771
3846
|
this.pool.abortGeneration(prevGeneration);
|
|
3772
3847
|
this.colorLUT.clear();
|
|
3773
3848
|
this.scheduleRender();
|
|
@@ -3777,11 +3852,25 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3777
3852
|
this.colorMap = colorMap;
|
|
3778
3853
|
const prevGeneration = this.generation;
|
|
3779
3854
|
this.generation += 1;
|
|
3855
|
+
this.readyDispatched.clear();
|
|
3780
3856
|
this.pool.abortGeneration(prevGeneration);
|
|
3781
3857
|
this.scheduleRender();
|
|
3782
3858
|
}
|
|
3783
3859
|
setDevicePixelRatio(dpr) {
|
|
3860
|
+
if (this.disposed) return;
|
|
3861
|
+
if (!Number.isFinite(dpr) || dpr <= 0) {
|
|
3862
|
+
console.warn(
|
|
3863
|
+
"[dawcore-spectrogram] setDevicePixelRatio: invalid value " + dpr + " \u2014 ignored"
|
|
3864
|
+
);
|
|
3865
|
+
return;
|
|
3866
|
+
}
|
|
3867
|
+
if (this.devicePixelRatio === dpr) return;
|
|
3784
3868
|
this.devicePixelRatio = dpr;
|
|
3869
|
+
const prevGeneration = this.generation;
|
|
3870
|
+
this.generation += 1;
|
|
3871
|
+
this.readyDispatched.clear();
|
|
3872
|
+
this.pool.abortGeneration(prevGeneration);
|
|
3873
|
+
this.scheduleRender();
|
|
3785
3874
|
}
|
|
3786
3875
|
dispose() {
|
|
3787
3876
|
if (this.disposed) return;
|
|
@@ -3790,6 +3879,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3790
3879
|
this.canvases.clear();
|
|
3791
3880
|
this.viewport = null;
|
|
3792
3881
|
this.colorLUT.clear();
|
|
3882
|
+
this.readyDispatched.clear();
|
|
3793
3883
|
this.pool.terminate();
|
|
3794
3884
|
}
|
|
3795
3885
|
scheduleRender() {
|
|
@@ -3798,7 +3888,12 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3798
3888
|
this.renderInFlight = true;
|
|
3799
3889
|
queueMicrotask(() => {
|
|
3800
3890
|
this.renderInFlight = false;
|
|
3801
|
-
|
|
3891
|
+
this.runRender(this.generation).catch((err) => {
|
|
3892
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
3893
|
+
console.warn(
|
|
3894
|
+
"[dawcore-spectrogram] runRender unhandled rejection (generation " + this.generation + "): " + (err instanceof Error ? err.message : String(err))
|
|
3895
|
+
);
|
|
3896
|
+
});
|
|
3802
3897
|
});
|
|
3803
3898
|
}
|
|
3804
3899
|
async runRender(generation) {
|
|
@@ -3812,13 +3907,34 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3812
3907
|
canvasesByTrack.set(c.trackId, list);
|
|
3813
3908
|
}
|
|
3814
3909
|
for (const [trackId, trackCanvases] of canvasesByTrack) {
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3910
|
+
try {
|
|
3911
|
+
const tiers = classifyViewport(trackCanvases, viewport);
|
|
3912
|
+
await this.renderTier(tiers.viewport, generation, viewport);
|
|
3913
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3914
|
+
if (!this.readyDispatched.has(trackId)) {
|
|
3915
|
+
this.readyDispatched.add(trackId);
|
|
3916
|
+
this.dispatchEvent(
|
|
3917
|
+
new CustomEvent("viewport-ready", { detail: { trackId, generation } })
|
|
3918
|
+
);
|
|
3919
|
+
}
|
|
3920
|
+
await this.renderTier(tiers.buffer, generation, viewport);
|
|
3921
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3922
|
+
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
3923
|
+
} catch (err) {
|
|
3924
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3925
|
+
if (err instanceof SpectrogramAbortError) {
|
|
3926
|
+
return;
|
|
3927
|
+
}
|
|
3928
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3929
|
+
console.warn(
|
|
3930
|
+
"[dawcore-spectrogram] render failed for track " + trackId + " (generation " + generation + "): " + error.message
|
|
3931
|
+
);
|
|
3932
|
+
this.dispatchEvent(
|
|
3933
|
+
new CustomEvent("viewport-error", {
|
|
3934
|
+
detail: { trackId, generation, error }
|
|
3935
|
+
})
|
|
3936
|
+
);
|
|
3937
|
+
}
|
|
3822
3938
|
}
|
|
3823
3939
|
}
|
|
3824
3940
|
async renderTier(canvases, generation, viewport) {
|
|
@@ -3833,8 +3949,13 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3833
3949
|
if (group.length === 0) return;
|
|
3834
3950
|
const first = group[0];
|
|
3835
3951
|
const clip = this.clips.get(first.clipId);
|
|
3836
|
-
if (!clip)
|
|
3837
|
-
|
|
3952
|
+
if (!clip) {
|
|
3953
|
+
console.warn(
|
|
3954
|
+
"[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
|
|
3955
|
+
);
|
|
3956
|
+
return;
|
|
3957
|
+
}
|
|
3958
|
+
const fftSize = this.config.fftSize ?? import_core.SPECTROGRAM_DEFAULTS.fftSize;
|
|
3838
3959
|
const startPx = Math.min(...group.map((c) => c.globalPixelOffset));
|
|
3839
3960
|
const endPx = Math.max(...group.map((c) => c.globalPixelOffset + c.widthPx));
|
|
3840
3961
|
const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
|
|
@@ -3869,11 +3990,11 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3869
3990
|
devicePixelRatio: this.devicePixelRatio,
|
|
3870
3991
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
3871
3992
|
colorLUT,
|
|
3872
|
-
frequencyScale: String(this.config.frequencyScale ??
|
|
3873
|
-
minFrequency: this.config.minFrequency ??
|
|
3993
|
+
frequencyScale: String(this.config.frequencyScale ?? import_core.SPECTROGRAM_DEFAULTS.frequencyScale),
|
|
3994
|
+
minFrequency: this.config.minFrequency ?? import_core.SPECTROGRAM_DEFAULTS.minFrequency,
|
|
3874
3995
|
maxFrequency: this.config.maxFrequency ?? clip.sampleRate / 2,
|
|
3875
|
-
gainDb: this.config.gainDb ??
|
|
3876
|
-
rangeDb: this.config.rangeDb ??
|
|
3996
|
+
gainDb: this.config.gainDb ?? import_core.SPECTROGRAM_DEFAULTS.gainDb,
|
|
3997
|
+
rangeDb: this.config.rangeDb ?? import_core.SPECTROGRAM_DEFAULTS.rangeDb,
|
|
3877
3998
|
channelIndex: first.channelIndex
|
|
3878
3999
|
},
|
|
3879
4000
|
generation
|
|
@@ -3886,7 +4007,16 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3886
4007
|
if (this.generation !== generation || this.disposed) return;
|
|
3887
4008
|
await this.yieldUntilIdle();
|
|
3888
4009
|
if (this.generation !== generation || this.disposed) return;
|
|
3889
|
-
|
|
4010
|
+
try {
|
|
4011
|
+
await this.renderGroup(group, generation, viewport);
|
|
4012
|
+
} catch (err) {
|
|
4013
|
+
if (this.generation !== generation || this.disposed) return;
|
|
4014
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
4015
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
4016
|
+
console.warn(
|
|
4017
|
+
"[dawcore-spectrogram] remaining-tier render failed for canvas group: " + error.message
|
|
4018
|
+
);
|
|
4019
|
+
}
|
|
3890
4020
|
}
|
|
3891
4021
|
}
|
|
3892
4022
|
yieldUntilIdle() {
|
|
@@ -3899,6 +4029,9 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3899
4029
|
});
|
|
3900
4030
|
}
|
|
3901
4031
|
};
|
|
4032
|
+
function viewportsEqual(a, b) {
|
|
4033
|
+
return a.visibleStartPx === b.visibleStartPx && a.visibleEndPx === b.visibleEndPx && a.bufferStartPx === b.bufferStartPx && a.bufferEndPx === b.bufferEndPx && a.samplesPerPixel === b.samplesPerPixel;
|
|
4034
|
+
}
|
|
3902
4035
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3903
4036
|
0 && (module.exports = {
|
|
3904
4037
|
ColorLUTCache,
|