@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
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
2
|
+
import {
|
|
3
|
+
SPECTROGRAM_DEFAULTS,
|
|
4
|
+
DEFAULT_SPECTROGRAM_COLOR_MAP,
|
|
5
|
+
MAX_CANVAS_WIDTH
|
|
6
|
+
} from "@waveform-playlist/core";
|
|
7
|
+
|
|
1
8
|
// src/worker/createSpectrogramWorker.ts
|
|
2
9
|
var SpectrogramAbortError = class extends Error {
|
|
3
10
|
constructor() {
|
|
@@ -37,12 +44,19 @@ function createSpectrogramWorker(worker) {
|
|
|
37
44
|
}
|
|
38
45
|
};
|
|
39
46
|
worker.onerror = (e) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
console.error(
|
|
48
|
+
"[dawcore-spectrogram] worker crashed with " + pending.size + " pending operations: " + (e.message || "unknown error")
|
|
49
|
+
);
|
|
50
|
+
for (const [id, entry] of pending) {
|
|
51
|
+
entry.reject(new Error("Worker crashed (id=" + id + "): " + (e.message || "unknown error")));
|
|
43
52
|
}
|
|
44
53
|
pending.clear();
|
|
45
54
|
};
|
|
55
|
+
worker.onmessageerror = (e) => {
|
|
56
|
+
console.warn(
|
|
57
|
+
"[dawcore-spectrogram] worker postMessage clone failure: " + (e.data ? JSON.stringify(e.data) : "no data")
|
|
58
|
+
);
|
|
59
|
+
};
|
|
46
60
|
return {
|
|
47
61
|
computeFFT(params, generation = 0) {
|
|
48
62
|
if (terminated) return Promise.reject(new Error("Worker terminated"));
|
|
@@ -133,67 +147,155 @@ function createSpectrogramWorker(worker) {
|
|
|
133
147
|
|
|
134
148
|
// src/worker/createSpectrogramWorkerPool.ts
|
|
135
149
|
function parseChannelFromCanvasId(canvasId) {
|
|
136
|
-
const match = canvasId.match(/-ch(\d+)
|
|
137
|
-
|
|
150
|
+
const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
|
|
151
|
+
if (!match) {
|
|
152
|
+
console.warn(
|
|
153
|
+
"[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
|
|
154
|
+
);
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
return parseInt(match[1], 10);
|
|
138
158
|
}
|
|
139
159
|
function defaultPoolSize() {
|
|
140
160
|
return 2;
|
|
141
161
|
}
|
|
162
|
+
var MAX_POOL_CHANNELS = 32;
|
|
142
163
|
function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
|
|
164
|
+
if (poolSize > MAX_POOL_CHANNELS) {
|
|
165
|
+
console.warn(
|
|
166
|
+
"[dawcore-spectrogram] workerPoolSize " + poolSize + " exceeds the channel cap (" + MAX_POOL_CHANNELS + ") \u2014 clamping; channels beyond the cap are not servable"
|
|
167
|
+
);
|
|
168
|
+
poolSize = MAX_POOL_CHANNELS;
|
|
169
|
+
}
|
|
143
170
|
const workers = [];
|
|
171
|
+
let terminated = false;
|
|
172
|
+
const registeredAudio = /* @__PURE__ */ new Map();
|
|
173
|
+
let failedAt = -1;
|
|
144
174
|
try {
|
|
145
175
|
for (let i = 0; i < poolSize; i++) {
|
|
176
|
+
failedAt = i;
|
|
146
177
|
workers.push(createSpectrogramWorker(createWorker()));
|
|
147
178
|
}
|
|
179
|
+
failedAt = -1;
|
|
148
180
|
} catch (err) {
|
|
149
181
|
for (const w of workers) {
|
|
150
|
-
|
|
182
|
+
try {
|
|
183
|
+
w.terminate();
|
|
184
|
+
} catch (terminateErr) {
|
|
185
|
+
console.warn(
|
|
186
|
+
"[dawcore-spectrogram] pool constructor cleanup: terminate failed for worker \u2014 " + String(terminateErr)
|
|
187
|
+
);
|
|
188
|
+
}
|
|
151
189
|
}
|
|
152
|
-
throw
|
|
190
|
+
throw new Error(
|
|
191
|
+
"Failed to create spectrogram worker pool (size=" + poolSize + ") at worker " + failedAt + ": " + (err instanceof Error ? err.message : String(err))
|
|
192
|
+
);
|
|
153
193
|
}
|
|
154
|
-
function
|
|
155
|
-
|
|
194
|
+
function assertValidChannelIndex(channelIndex) {
|
|
195
|
+
if (!Number.isInteger(channelIndex) || channelIndex < 0 || channelIndex >= MAX_POOL_CHANNELS) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
"[dawcore-spectrogram] invalid channel index " + channelIndex + " (expected an integer in 0.." + (MAX_POOL_CHANNELS - 1) + ")"
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function ensureWorkerForChannel(channelIndex) {
|
|
202
|
+
assertValidChannelIndex(channelIndex);
|
|
203
|
+
if (terminated) {
|
|
204
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
205
|
+
}
|
|
206
|
+
while (workers.length <= channelIndex) {
|
|
207
|
+
const w = createSpectrogramWorker(createWorker());
|
|
208
|
+
const workerIndex = workers.length;
|
|
209
|
+
for (const [clipId, entry] of registeredAudio) {
|
|
210
|
+
if (entry.channelDataArrays.length > workerIndex) {
|
|
211
|
+
w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
workers.push(w);
|
|
215
|
+
}
|
|
216
|
+
return workers[channelIndex];
|
|
156
217
|
}
|
|
157
218
|
return {
|
|
158
|
-
computeFFT(params, generation = 0) {
|
|
219
|
+
async computeFFT(params, generation = 0) {
|
|
220
|
+
if (terminated) {
|
|
221
|
+
throw new Error("[dawcore-spectrogram] worker pool is terminated");
|
|
222
|
+
}
|
|
223
|
+
const channelCount = params.channelDataArrays.length || registeredAudio.get(params.clipId)?.channelDataArrays.length || 0;
|
|
224
|
+
if (channelCount === 0) {
|
|
225
|
+
throw new Error(
|
|
226
|
+
"[dawcore-spectrogram] computeFFT called with no channel data \u2014 pass channelDataArrays or registerAudioData the clip first"
|
|
227
|
+
);
|
|
228
|
+
}
|
|
159
229
|
if (params.mono) {
|
|
160
230
|
return workers[0].computeFFT(params, generation);
|
|
161
231
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
232
|
+
if (channelCount > MAX_POOL_CHANNELS) {
|
|
233
|
+
throw new Error(
|
|
234
|
+
"[dawcore-spectrogram] computeFFT: " + channelCount + " channels exceeds the pool channel cap (" + MAX_POOL_CHANNELS + ")"
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
const promises = Array.from(
|
|
238
|
+
{ length: channelCount },
|
|
239
|
+
(_, i) => ensureWorkerForChannel(i).computeFFT({ ...params, channelFilter: i }, generation)
|
|
166
240
|
);
|
|
167
|
-
|
|
241
|
+
const settled = await Promise.allSettled(promises);
|
|
242
|
+
const failures = settled.filter((s) => s.status === "rejected");
|
|
243
|
+
if (failures.length > 0) {
|
|
244
|
+
const realErrors = failures.filter((f) => !(f.reason instanceof SpectrogramAbortError));
|
|
245
|
+
for (let i = 1; i < realErrors.length; i++) {
|
|
246
|
+
console.warn(
|
|
247
|
+
"[dawcore-spectrogram] additional channel FFT failure (" + i + "): " + (realErrors[i].reason instanceof Error ? realErrors[i].reason.message : String(realErrors[i].reason))
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
throw (realErrors[0] ?? failures[0]).reason;
|
|
251
|
+
}
|
|
252
|
+
return settled[0].value;
|
|
168
253
|
},
|
|
169
|
-
|
|
170
|
-
|
|
254
|
+
// async so ensureWorkerForChannel's policy throws surface as rejections,
|
|
255
|
+
// matching computeFFT, instead of escaping synchronously.
|
|
256
|
+
async renderChunks(params, generation = 0) {
|
|
257
|
+
const worker = ensureWorkerForChannel(params.channelIndex);
|
|
171
258
|
return worker.renderChunks({ ...params, channelIndex: 0 }, generation);
|
|
172
259
|
},
|
|
173
260
|
abortGeneration(generation) {
|
|
261
|
+
if (terminated) return;
|
|
174
262
|
for (const w of workers) {
|
|
175
263
|
w.abortGeneration(generation);
|
|
176
264
|
}
|
|
177
265
|
},
|
|
178
266
|
registerCanvas(canvasId, canvas) {
|
|
179
|
-
|
|
180
|
-
|
|
267
|
+
try {
|
|
268
|
+
ensureWorkerForChannel(parseChannelFromCanvasId(canvasId)).registerCanvas(canvasId, canvas);
|
|
269
|
+
} catch (err) {
|
|
270
|
+
console.warn(
|
|
271
|
+
'[dawcore-spectrogram] registerCanvas("' + canvasId + '") rejected \u2014 canvas not registered: ' + (err instanceof Error ? err.message : String(err))
|
|
272
|
+
);
|
|
273
|
+
}
|
|
181
274
|
},
|
|
182
275
|
unregisterCanvas(canvasId) {
|
|
183
|
-
|
|
184
|
-
|
|
276
|
+
if (terminated) return;
|
|
277
|
+
const worker = workers[parseChannelFromCanvasId(canvasId)];
|
|
278
|
+
if (!worker) return;
|
|
279
|
+
worker.unregisterCanvas(canvasId);
|
|
185
280
|
},
|
|
186
281
|
registerAudioData(clipId, channelDataArrays, sampleRate) {
|
|
187
|
-
|
|
188
|
-
|
|
282
|
+
if (terminated) return;
|
|
283
|
+
registeredAudio.set(clipId, { channelDataArrays, sampleRate });
|
|
284
|
+
const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
|
|
285
|
+
for (let i = 0; i < relevantWorkers; i++) {
|
|
286
|
+
workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
|
|
189
287
|
}
|
|
190
288
|
},
|
|
191
289
|
unregisterAudioData(clipId) {
|
|
290
|
+
if (terminated) return;
|
|
291
|
+
registeredAudio.delete(clipId);
|
|
192
292
|
for (const w of workers) {
|
|
193
293
|
w.unregisterAudioData(clipId);
|
|
194
294
|
}
|
|
195
295
|
},
|
|
196
296
|
terminate() {
|
|
297
|
+
terminated = true;
|
|
298
|
+
registeredAudio.clear();
|
|
197
299
|
for (const w of workers) {
|
|
198
300
|
w.terminate();
|
|
199
301
|
}
|
|
@@ -203,6 +305,9 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
|
|
|
203
305
|
|
|
204
306
|
// src/computation/colorMaps.ts
|
|
205
307
|
function interpolateLUT(stops) {
|
|
308
|
+
if (stops.length === 0) {
|
|
309
|
+
throw new Error("[spectrogram] custom color map requires at least one color stop");
|
|
310
|
+
}
|
|
206
311
|
const lut = new Uint8Array(256 * 3);
|
|
207
312
|
const n = stops.length;
|
|
208
313
|
for (let i = 0; i < 256; i++) {
|
|
@@ -3406,6 +3511,20 @@ function groupContiguousChunks(chunks) {
|
|
|
3406
3511
|
groups.push(current);
|
|
3407
3512
|
return groups;
|
|
3408
3513
|
}
|
|
3514
|
+
function groupRenderableChunks(chunks) {
|
|
3515
|
+
const partitions = /* @__PURE__ */ new Map();
|
|
3516
|
+
for (const chunk of chunks) {
|
|
3517
|
+
const key = chunk.clipId + " " + chunk.channelIndex;
|
|
3518
|
+
const list = partitions.get(key) ?? [];
|
|
3519
|
+
list.push(chunk);
|
|
3520
|
+
partitions.set(key, list);
|
|
3521
|
+
}
|
|
3522
|
+
const groups = [];
|
|
3523
|
+
for (const partition of partitions.values()) {
|
|
3524
|
+
groups.push(...groupContiguousChunks(partition));
|
|
3525
|
+
}
|
|
3526
|
+
return groups;
|
|
3527
|
+
}
|
|
3409
3528
|
|
|
3410
3529
|
// src/orchestrator/SpectrogramOrchestrator.ts
|
|
3411
3530
|
var SpectrogramOrchestrator = class extends EventTarget {
|
|
@@ -3418,37 +3537,62 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3418
3537
|
this.colorLUT = new ColorLUTCache();
|
|
3419
3538
|
this.disposed = false;
|
|
3420
3539
|
this.renderInFlight = false;
|
|
3540
|
+
this.renderQueued = false;
|
|
3541
|
+
// Per-clip timeline origin (globalPixelOffset - chunkIndex * MAX_CANVAS_WIDTH)
|
|
3542
|
+
// with a live canvas count — used to detect layout-contract violations.
|
|
3543
|
+
// The origin resets once all of a clip's canvases unregister, so a moved
|
|
3544
|
+
// clip re-registering at a new position doesn't false-positive.
|
|
3545
|
+
this.clipOrigins = /* @__PURE__ */ new Map();
|
|
3421
3546
|
// Tracks which trackIds have already emitted `viewport-ready` for the
|
|
3422
|
-
// current generation. Cleared on every generation bump (setViewport
|
|
3423
|
-
// setConfig
|
|
3424
|
-
//
|
|
3425
|
-
// loading — would re-fire the
|
|
3547
|
+
// current generation. Cleared on every generation bump (setViewport with a
|
|
3548
|
+
// real change, setConfig, setColorMap, setDevicePixelRatio) AND in dispose().
|
|
3549
|
+
// Without this, every render — including those triggered by late
|
|
3550
|
+
// `registerCanvas` calls during track-by-track loading — would re-fire the
|
|
3551
|
+
// event for every track in the canvas map (N×N-ish ascending fan-in:
|
|
3552
|
+
// 3 tracks → 4+3+2+1 = 10 events; the dedup reduces this to 3).
|
|
3426
3553
|
this.readyDispatched = /* @__PURE__ */ new Set();
|
|
3427
3554
|
const poolSize = opts.workerPoolSize ?? 2;
|
|
3428
3555
|
this.pool = createSpectrogramWorkerPool(opts.workerFactory, poolSize);
|
|
3429
3556
|
this.config = opts.config;
|
|
3430
|
-
this.colorMap = opts.colorMap ??
|
|
3557
|
+
this.colorMap = opts.colorMap ?? DEFAULT_SPECTROGRAM_COLOR_MAP;
|
|
3431
3558
|
this.devicePixelRatio = opts.devicePixelRatio ?? (typeof window !== "undefined" ? window.devicePixelRatio : 1);
|
|
3432
3559
|
}
|
|
3433
3560
|
registerClip(reg) {
|
|
3434
3561
|
if (this.disposed) return;
|
|
3562
|
+
const channelData = [...reg.channelData];
|
|
3435
3563
|
this.clips.set(reg.clipId, {
|
|
3436
3564
|
trackId: reg.trackId,
|
|
3437
|
-
channelData
|
|
3565
|
+
channelData,
|
|
3438
3566
|
sampleRate: reg.sampleRate,
|
|
3439
3567
|
durationSamples: reg.durationSamples,
|
|
3440
3568
|
offsetSamples: reg.offsetSamples
|
|
3441
3569
|
});
|
|
3442
|
-
this.pool.registerAudioData(reg.clipId,
|
|
3570
|
+
this.pool.registerAudioData(reg.clipId, channelData, reg.sampleRate);
|
|
3571
|
+
if (this.viewport) {
|
|
3572
|
+
for (const canvas of this.canvases.values()) {
|
|
3573
|
+
if (canvas.clipId === reg.clipId) {
|
|
3574
|
+
this.scheduleRender();
|
|
3575
|
+
break;
|
|
3576
|
+
}
|
|
3577
|
+
}
|
|
3578
|
+
}
|
|
3443
3579
|
}
|
|
3444
3580
|
unregisterClip(clipId) {
|
|
3445
3581
|
if (this.disposed) return;
|
|
3446
|
-
if (!this.clips.has(clipId))
|
|
3582
|
+
if (!this.clips.has(clipId)) {
|
|
3583
|
+
console.warn("[dawcore-spectrogram] unregisterClip: unknown clip " + clipId);
|
|
3584
|
+
return;
|
|
3585
|
+
}
|
|
3447
3586
|
this.clips.delete(clipId);
|
|
3448
3587
|
this.pool.unregisterAudioData(clipId);
|
|
3449
3588
|
}
|
|
3450
3589
|
registerCanvas(reg) {
|
|
3451
|
-
if (this.disposed)
|
|
3590
|
+
if (this.disposed) {
|
|
3591
|
+
console.warn(
|
|
3592
|
+
"[dawcore-spectrogram] registerCanvas after dispose \u2014 canvas " + reg.canvasId + " will not render (OffscreenCanvas is now dead)"
|
|
3593
|
+
);
|
|
3594
|
+
return;
|
|
3595
|
+
}
|
|
3452
3596
|
this.canvases.set(reg.canvasId, {
|
|
3453
3597
|
canvasId: reg.canvasId,
|
|
3454
3598
|
globalPixelOffset: reg.globalPixelOffset,
|
|
@@ -3459,66 +3603,130 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3459
3603
|
channelIndex: reg.channelIndex,
|
|
3460
3604
|
chunkIndex: reg.chunkIndex
|
|
3461
3605
|
});
|
|
3606
|
+
const originPx = reg.globalPixelOffset - reg.chunkIndex * MAX_CANVAS_WIDTH;
|
|
3607
|
+
const origin = this.clipOrigins.get(reg.clipId);
|
|
3608
|
+
if (!origin) {
|
|
3609
|
+
this.clipOrigins.set(reg.clipId, { originPx, canvasCount: 1 });
|
|
3610
|
+
} else {
|
|
3611
|
+
origin.canvasCount += 1;
|
|
3612
|
+
if (origin.originPx !== originPx) {
|
|
3613
|
+
console.warn(
|
|
3614
|
+
"[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"
|
|
3615
|
+
);
|
|
3616
|
+
}
|
|
3617
|
+
}
|
|
3462
3618
|
this.pool.registerCanvas(reg.canvasId, reg.canvas);
|
|
3463
3619
|
if (this.viewport) this.scheduleRender();
|
|
3464
3620
|
}
|
|
3465
3621
|
unregisterCanvas(canvasId) {
|
|
3466
3622
|
if (this.disposed) return;
|
|
3467
|
-
|
|
3623
|
+
const entry = this.canvases.get(canvasId);
|
|
3624
|
+
if (!entry) {
|
|
3625
|
+
console.warn("[dawcore-spectrogram] unregisterCanvas: unknown canvas " + canvasId);
|
|
3626
|
+
return;
|
|
3627
|
+
}
|
|
3468
3628
|
this.canvases.delete(canvasId);
|
|
3629
|
+
const origin = this.clipOrigins.get(entry.clipId);
|
|
3630
|
+
if (origin) {
|
|
3631
|
+
origin.canvasCount -= 1;
|
|
3632
|
+
if (origin.canvasCount <= 0) this.clipOrigins.delete(entry.clipId);
|
|
3633
|
+
}
|
|
3469
3634
|
this.pool.unregisterCanvas(canvasId);
|
|
3470
3635
|
}
|
|
3471
3636
|
setViewport(state) {
|
|
3472
3637
|
if (this.disposed) return;
|
|
3638
|
+
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) {
|
|
3639
|
+
console.warn(
|
|
3640
|
+
"[dawcore-spectrogram] setViewport: invalid state \u2014 ignored (" + JSON.stringify(state) + ")"
|
|
3641
|
+
);
|
|
3642
|
+
return;
|
|
3643
|
+
}
|
|
3473
3644
|
if (this.viewport && viewportsEqual(this.viewport, state)) return;
|
|
3474
|
-
|
|
3475
|
-
this.generation += 1;
|
|
3476
|
-
this.readyDispatched.clear();
|
|
3477
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3645
|
+
this.bumpGenerationAndAbortStale();
|
|
3478
3646
|
this.viewport = state;
|
|
3479
3647
|
this.scheduleRender();
|
|
3480
3648
|
}
|
|
3481
3649
|
setConfig(config) {
|
|
3482
3650
|
if (this.disposed) return;
|
|
3483
3651
|
this.config = config;
|
|
3484
|
-
|
|
3485
|
-
this.generation += 1;
|
|
3486
|
-
this.readyDispatched.clear();
|
|
3487
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3652
|
+
this.bumpGenerationAndAbortStale();
|
|
3488
3653
|
this.colorLUT.clear();
|
|
3489
3654
|
this.scheduleRender();
|
|
3490
3655
|
}
|
|
3491
3656
|
setColorMap(colorMap) {
|
|
3492
3657
|
if (this.disposed) return;
|
|
3493
3658
|
this.colorMap = colorMap;
|
|
3494
|
-
|
|
3495
|
-
this.generation += 1;
|
|
3496
|
-
this.readyDispatched.clear();
|
|
3497
|
-
this.pool.abortGeneration(prevGeneration);
|
|
3659
|
+
this.bumpGenerationAndAbortStale();
|
|
3498
3660
|
this.scheduleRender();
|
|
3499
3661
|
}
|
|
3500
3662
|
setDevicePixelRatio(dpr) {
|
|
3663
|
+
if (this.disposed) return;
|
|
3664
|
+
if (!Number.isFinite(dpr) || dpr <= 0) {
|
|
3665
|
+
console.warn(
|
|
3666
|
+
"[dawcore-spectrogram] setDevicePixelRatio: invalid value " + dpr + " \u2014 ignored"
|
|
3667
|
+
);
|
|
3668
|
+
return;
|
|
3669
|
+
}
|
|
3670
|
+
if (this.devicePixelRatio === dpr) return;
|
|
3501
3671
|
this.devicePixelRatio = dpr;
|
|
3672
|
+
this.bumpGenerationAndAbortStale();
|
|
3673
|
+
this.scheduleRender();
|
|
3674
|
+
}
|
|
3675
|
+
/**
|
|
3676
|
+
* Start a new render generation and cancel in-flight work from older ones.
|
|
3677
|
+
*
|
|
3678
|
+
* The worker treats work as stale iff `generation < latestGeneration`, so
|
|
3679
|
+
* the abort message must carry the NEW generation — passing the previous
|
|
3680
|
+
* one leaves its in-flight FFTs running to completion (#555).
|
|
3681
|
+
*/
|
|
3682
|
+
bumpGenerationAndAbortStale() {
|
|
3683
|
+
this.generation += 1;
|
|
3684
|
+
this.readyDispatched.clear();
|
|
3685
|
+
this.pool.abortGeneration(this.generation);
|
|
3502
3686
|
}
|
|
3503
3687
|
dispose() {
|
|
3504
3688
|
if (this.disposed) return;
|
|
3505
3689
|
this.disposed = true;
|
|
3506
3690
|
this.clips.clear();
|
|
3507
3691
|
this.canvases.clear();
|
|
3692
|
+
this.clipOrigins.clear();
|
|
3508
3693
|
this.viewport = null;
|
|
3509
3694
|
this.colorLUT.clear();
|
|
3510
3695
|
this.readyDispatched.clear();
|
|
3511
3696
|
this.pool.terminate();
|
|
3512
3697
|
}
|
|
3698
|
+
/**
|
|
3699
|
+
* Coalesce render requests: `renderInFlight` is held for the WHOLE async
|
|
3700
|
+
* render, not just until the microtask fires — a request arriving
|
|
3701
|
+
* mid-render (e.g. a late registerCanvas) queues one follow-up run instead
|
|
3702
|
+
* of starting a concurrent runRender for the same generation (#558).
|
|
3703
|
+
*/
|
|
3513
3704
|
scheduleRender() {
|
|
3514
|
-
if (this.renderInFlight) return;
|
|
3515
3705
|
if (!this.viewport) return;
|
|
3706
|
+
if (this.renderInFlight) {
|
|
3707
|
+
this.renderQueued = true;
|
|
3708
|
+
return;
|
|
3709
|
+
}
|
|
3516
3710
|
this.renderInFlight = true;
|
|
3517
3711
|
queueMicrotask(() => {
|
|
3518
|
-
this.
|
|
3519
|
-
void this.runRender(this.generation);
|
|
3712
|
+
void this.runRenderLoop();
|
|
3520
3713
|
});
|
|
3521
3714
|
}
|
|
3715
|
+
async runRenderLoop() {
|
|
3716
|
+
try {
|
|
3717
|
+
do {
|
|
3718
|
+
this.renderQueued = false;
|
|
3719
|
+
await this.runRender(this.generation).catch((err) => {
|
|
3720
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
3721
|
+
console.warn(
|
|
3722
|
+
"[dawcore-spectrogram] runRender unhandled rejection (generation " + this.generation + "): " + (err instanceof Error ? err.message : String(err))
|
|
3723
|
+
);
|
|
3724
|
+
});
|
|
3725
|
+
} while (this.renderQueued && !this.disposed);
|
|
3726
|
+
} finally {
|
|
3727
|
+
this.renderInFlight = false;
|
|
3728
|
+
}
|
|
3729
|
+
}
|
|
3522
3730
|
async runRender(generation) {
|
|
3523
3731
|
if (this.disposed) return;
|
|
3524
3732
|
const viewport = this.viewport;
|
|
@@ -3530,21 +3738,39 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3530
3738
|
canvasesByTrack.set(c.trackId, list);
|
|
3531
3739
|
}
|
|
3532
3740
|
for (const [trackId, trackCanvases] of canvasesByTrack) {
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
this.readyDispatched.
|
|
3538
|
-
|
|
3741
|
+
try {
|
|
3742
|
+
const tiers = classifyViewport(trackCanvases, viewport);
|
|
3743
|
+
await this.renderTier(tiers.viewport, generation, viewport);
|
|
3744
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3745
|
+
if (!this.readyDispatched.has(trackId)) {
|
|
3746
|
+
this.readyDispatched.add(trackId);
|
|
3747
|
+
this.dispatchEvent(
|
|
3748
|
+
new CustomEvent("viewport-ready", { detail: { trackId, generation } })
|
|
3749
|
+
);
|
|
3750
|
+
}
|
|
3751
|
+
await this.renderTier(tiers.buffer, generation, viewport);
|
|
3752
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3753
|
+
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
3754
|
+
} catch (err) {
|
|
3755
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3756
|
+
if (err instanceof SpectrogramAbortError) {
|
|
3757
|
+
return;
|
|
3758
|
+
}
|
|
3759
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3760
|
+
console.warn(
|
|
3761
|
+
"[dawcore-spectrogram] render failed for track " + trackId + " (generation " + generation + "): " + error.message
|
|
3762
|
+
);
|
|
3763
|
+
this.dispatchEvent(
|
|
3764
|
+
new CustomEvent("viewport-error", {
|
|
3765
|
+
detail: { trackId, generation, error }
|
|
3766
|
+
})
|
|
3767
|
+
);
|
|
3539
3768
|
}
|
|
3540
|
-
await this.renderTier(tiers.buffer, generation, viewport);
|
|
3541
|
-
if (this.generation !== generation || this.disposed) return;
|
|
3542
|
-
await this.renderRemainingViaIdle(tiers.remaining, generation, viewport);
|
|
3543
3769
|
}
|
|
3544
3770
|
}
|
|
3545
3771
|
async renderTier(canvases, generation, viewport) {
|
|
3546
3772
|
if (canvases.length === 0) return;
|
|
3547
|
-
const groups =
|
|
3773
|
+
const groups = groupRenderableChunks(canvases);
|
|
3548
3774
|
for (const group of groups) {
|
|
3549
3775
|
if (this.generation !== generation || this.disposed) return;
|
|
3550
3776
|
await this.renderGroup(group, generation, viewport);
|
|
@@ -3554,10 +3780,32 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3554
3780
|
if (group.length === 0) return;
|
|
3555
3781
|
const first = group[0];
|
|
3556
3782
|
const clip = this.clips.get(first.clipId);
|
|
3557
|
-
if (!clip)
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3783
|
+
if (!clip) {
|
|
3784
|
+
console.warn(
|
|
3785
|
+
"[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
|
|
3786
|
+
);
|
|
3787
|
+
return;
|
|
3788
|
+
}
|
|
3789
|
+
try {
|
|
3790
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
3791
|
+
} catch (err) {
|
|
3792
|
+
if (this.generation !== generation || this.disposed) throw err;
|
|
3793
|
+
if (err instanceof Error && err.message.includes("cache-miss")) {
|
|
3794
|
+
console.warn(
|
|
3795
|
+
"[dawcore-spectrogram] FFT cache entry evicted between compute and render \u2014 retrying group once"
|
|
3796
|
+
);
|
|
3797
|
+
await this.renderGroupOnce(group, generation, viewport, clip);
|
|
3798
|
+
return;
|
|
3799
|
+
}
|
|
3800
|
+
throw err;
|
|
3801
|
+
}
|
|
3802
|
+
}
|
|
3803
|
+
async renderGroupOnce(group, generation, viewport, clip) {
|
|
3804
|
+
const first = group[0];
|
|
3805
|
+
const fftSize = this.config.fftSize ?? SPECTROGRAM_DEFAULTS.fftSize;
|
|
3806
|
+
const clipRelativeOffsets = group.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH);
|
|
3807
|
+
const startPx = Math.min(...clipRelativeOffsets);
|
|
3808
|
+
const endPx = Math.max(...group.map((c, i) => clipRelativeOffsets[i] + c.widthPx));
|
|
3561
3809
|
const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
|
|
3562
3810
|
const endSample = Math.min(
|
|
3563
3811
|
clip.offsetSamples + clip.durationSamples,
|
|
@@ -3585,16 +3833,16 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3585
3833
|
cacheKey: cacheKey2,
|
|
3586
3834
|
canvasIds: group.map((c) => c.canvasId),
|
|
3587
3835
|
canvasWidths: group.map((c) => c.widthPx),
|
|
3588
|
-
globalPixelOffsets:
|
|
3836
|
+
globalPixelOffsets: clipRelativeOffsets,
|
|
3589
3837
|
canvasHeight: first.heightPx,
|
|
3590
3838
|
devicePixelRatio: this.devicePixelRatio,
|
|
3591
3839
|
samplesPerPixel: viewport.samplesPerPixel,
|
|
3592
3840
|
colorLUT,
|
|
3593
|
-
frequencyScale: String(this.config.frequencyScale ??
|
|
3594
|
-
minFrequency: this.config.minFrequency ??
|
|
3841
|
+
frequencyScale: String(this.config.frequencyScale ?? SPECTROGRAM_DEFAULTS.frequencyScale),
|
|
3842
|
+
minFrequency: this.config.minFrequency ?? SPECTROGRAM_DEFAULTS.minFrequency,
|
|
3595
3843
|
maxFrequency: this.config.maxFrequency ?? clip.sampleRate / 2,
|
|
3596
|
-
gainDb: this.config.gainDb ??
|
|
3597
|
-
rangeDb: this.config.rangeDb ??
|
|
3844
|
+
gainDb: this.config.gainDb ?? SPECTROGRAM_DEFAULTS.gainDb,
|
|
3845
|
+
rangeDb: this.config.rangeDb ?? SPECTROGRAM_DEFAULTS.rangeDb,
|
|
3598
3846
|
channelIndex: first.channelIndex
|
|
3599
3847
|
},
|
|
3600
3848
|
generation
|
|
@@ -3602,12 +3850,21 @@ var SpectrogramOrchestrator = class extends EventTarget {
|
|
|
3602
3850
|
}
|
|
3603
3851
|
async renderRemainingViaIdle(canvases, generation, viewport) {
|
|
3604
3852
|
if (canvases.length === 0) return;
|
|
3605
|
-
const groups =
|
|
3853
|
+
const groups = groupRenderableChunks(canvases);
|
|
3606
3854
|
for (const group of groups) {
|
|
3607
3855
|
if (this.generation !== generation || this.disposed) return;
|
|
3608
3856
|
await this.yieldUntilIdle();
|
|
3609
3857
|
if (this.generation !== generation || this.disposed) return;
|
|
3610
|
-
|
|
3858
|
+
try {
|
|
3859
|
+
await this.renderGroup(group, generation, viewport);
|
|
3860
|
+
} catch (err) {
|
|
3861
|
+
if (this.generation !== generation || this.disposed) return;
|
|
3862
|
+
if (err instanceof SpectrogramAbortError) return;
|
|
3863
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3864
|
+
console.warn(
|
|
3865
|
+
"[dawcore-spectrogram] remaining-tier render failed for canvas group: " + error.message
|
|
3866
|
+
);
|
|
3867
|
+
}
|
|
3611
3868
|
}
|
|
3612
3869
|
}
|
|
3613
3870
|
yieldUntilIdle() {
|
|
@@ -3627,6 +3884,7 @@ export {
|
|
|
3627
3884
|
ColorLUTCache,
|
|
3628
3885
|
SpectrogramOrchestrator,
|
|
3629
3886
|
classifyViewport,
|
|
3630
|
-
groupContiguousChunks
|
|
3887
|
+
groupContiguousChunks,
|
|
3888
|
+
groupRenderableChunks
|
|
3631
3889
|
};
|
|
3632
3890
|
//# sourceMappingURL=index.mjs.map
|