@waveform-playlist/spectrogram 15.0.0-next.1 → 15.0.0-next.10
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 +2 -2
- package/dist/index.d.mts +13 -3
- package/dist/index.d.ts +13 -3
- package/dist/index.js +213 -329
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +195 -307
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -6
package/dist/index.mjs
CHANGED
|
@@ -43,7 +43,7 @@ var RENDER_MODES = [
|
|
|
43
43
|
{ value: "spectrogram", label: "Spectrogram" },
|
|
44
44
|
{ value: "both", label: "Both" }
|
|
45
45
|
];
|
|
46
|
-
function
|
|
46
|
+
function spectrogramMenuItems({
|
|
47
47
|
renderMode,
|
|
48
48
|
onRenderModeChange,
|
|
49
49
|
onOpenSettings,
|
|
@@ -52,7 +52,6 @@ function SpectrogramMenuItems({
|
|
|
52
52
|
return [
|
|
53
53
|
{
|
|
54
54
|
id: "spectrogram-display",
|
|
55
|
-
label: "Display",
|
|
56
55
|
content: /* @__PURE__ */ jsxs(DropdownSection, { children: [
|
|
57
56
|
/* @__PURE__ */ jsx(SectionLabel, { children: "Display" }),
|
|
58
57
|
RENDER_MODES.map(({ value, label }) => /* @__PURE__ */ jsxs(RadioLabel, { children: [
|
|
@@ -93,6 +92,9 @@ function SpectrogramMenuItems({
|
|
|
93
92
|
// src/components/SpectrogramSettingsModal.tsx
|
|
94
93
|
import { useRef, useEffect, useId, useLayoutEffect, useState } from "react";
|
|
95
94
|
import styled2 from "styled-components";
|
|
95
|
+
import {
|
|
96
|
+
SPECTROGRAM_DEFAULTS
|
|
97
|
+
} from "@waveform-playlist/core";
|
|
96
98
|
import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
97
99
|
var StyledDialog = styled2.dialog`
|
|
98
100
|
border: 1px solid rgba(128, 128, 128, 0.4);
|
|
@@ -167,11 +169,12 @@ var ModalButton = styled2.button`
|
|
|
167
169
|
border-radius: 4px;
|
|
168
170
|
cursor: pointer;
|
|
169
171
|
font-size: 0.85rem;
|
|
170
|
-
background: ${(p) => p.$primary ? "
|
|
171
|
-
color: ${(p) => p.$primary ? "#fff" : "inherit"};
|
|
172
|
+
background: ${(p) => p.$primary ? p.theme.buttonBackground ?? "#0091ff" : "transparent"};
|
|
173
|
+
color: ${(p) => p.$primary ? p.theme.buttonText ?? "#fff" : "inherit"};
|
|
174
|
+
${(p) => p.$primary && p.theme.buttonBorder ? `border-color: ${p.theme.buttonBorder};` : ""}
|
|
172
175
|
|
|
173
176
|
&:hover {
|
|
174
|
-
opacity: 0.85;
|
|
177
|
+
${(p) => p.$primary && p.theme.buttonHoverBackground ? `background: ${p.theme.buttonHoverBackground};` : "opacity: 0.85;"}
|
|
175
178
|
}
|
|
176
179
|
`;
|
|
177
180
|
var FFT_SIZES = [256, 512, 1024, 2048, 4096, 8192];
|
|
@@ -185,39 +188,40 @@ var WINDOW_FUNCTIONS = [
|
|
|
185
188
|
];
|
|
186
189
|
var FREQ_SCALES = ["linear", "logarithmic", "mel", "bark", "erb"];
|
|
187
190
|
var COLOR_MAPS = ["viridis", "magma", "inferno", "grayscale", "igray", "roseus"];
|
|
188
|
-
var
|
|
191
|
+
var CUSTOM_COLOR_MAP = "custom";
|
|
192
|
+
var SettingsForm = ({
|
|
193
|
+
onClose,
|
|
194
|
+
config,
|
|
195
|
+
colorMap,
|
|
196
|
+
onApply,
|
|
197
|
+
sampleRate
|
|
198
|
+
}) => {
|
|
189
199
|
const id = useId();
|
|
190
|
-
const [
|
|
191
|
-
const [
|
|
192
|
-
const
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
);
|
|
196
|
-
const
|
|
197
|
-
const
|
|
198
|
-
const
|
|
199
|
-
const
|
|
200
|
-
const
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
);
|
|
204
|
-
const
|
|
200
|
+
const [edits, setEdits] = useState({});
|
|
201
|
+
const [editedColorMap, setEditedColorMap] = useState(void 0);
|
|
202
|
+
const shown = (key) => key in edits ? edits[key] : config[key];
|
|
203
|
+
const edit = (changes) => setEdits((prev) => ({ ...prev, ...changes }));
|
|
204
|
+
const fftSize = shown("fftSize") ?? SPECTROGRAM_DEFAULTS.fftSize;
|
|
205
|
+
const hopSize = shown("hopSize") ?? Math.floor(fftSize / 4);
|
|
206
|
+
const zeroPadding = shown("zeroPaddingFactor") ?? SPECTROGRAM_DEFAULTS.zeroPaddingFactor;
|
|
207
|
+
const windowFn = shown("windowFunction") ?? SPECTROGRAM_DEFAULTS.windowFunction;
|
|
208
|
+
const freqScale = shown("frequencyScale") ?? SPECTROGRAM_DEFAULTS.frequencyScale;
|
|
209
|
+
const minFreq = shown("minFrequency") ?? SPECTROGRAM_DEFAULTS.minFrequency;
|
|
210
|
+
const nyquist = sampleRate !== void 0 ? sampleRate / 2 : void 0;
|
|
211
|
+
const maxFreq = "maxFrequency" in edits ? edits.maxFrequency : config.maxFrequency ?? nyquist;
|
|
212
|
+
const gainDb = shown("gainDb") ?? SPECTROGRAM_DEFAULTS.gainDb;
|
|
213
|
+
const rangeDb = shown("rangeDb") ?? SPECTROGRAM_DEFAULTS.rangeDb;
|
|
214
|
+
const showLabels = shown("labels") ?? SPECTROGRAM_DEFAULTS.labels;
|
|
215
|
+
const isCustomColorMap = typeof colorMap !== "string";
|
|
216
|
+
const colorMapValue = editedColorMap ?? (isCustomColorMap ? CUSTOM_COLOR_MAP : colorMap);
|
|
205
217
|
const handleApply = () => {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
fftSize,
|
|
209
|
-
windowFunction: windowFn,
|
|
210
|
-
frequencyScale: freqScale,
|
|
211
|
-
minFrequency: minFreq,
|
|
212
|
-
maxFrequency: maxFreq,
|
|
213
|
-
gainDb,
|
|
214
|
-
rangeDb,
|
|
215
|
-
zeroPaddingFactor: zeroPadding,
|
|
216
|
-
hopSize,
|
|
217
|
-
labels: showLabels
|
|
218
|
-
},
|
|
219
|
-
localColorMap
|
|
218
|
+
const configChanged = Object.keys(edits).some(
|
|
219
|
+
(key) => edits[key] !== config[key]
|
|
220
220
|
);
|
|
221
|
+
const colorMapChanged = editedColorMap !== void 0 && editedColorMap !== colorMap;
|
|
222
|
+
if (configChanged || colorMapChanged) {
|
|
223
|
+
onApply({ ...config, ...edits }, editedColorMap ?? colorMap);
|
|
224
|
+
}
|
|
221
225
|
onClose();
|
|
222
226
|
};
|
|
223
227
|
return /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
@@ -231,8 +235,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
231
235
|
value: fftSize,
|
|
232
236
|
onChange: (e) => {
|
|
233
237
|
const v = Number(e.target.value);
|
|
234
|
-
|
|
235
|
-
setHopSize(Math.floor(v / 4));
|
|
238
|
+
edit({ fftSize: v, hopSize: Math.floor(v / 4) });
|
|
236
239
|
},
|
|
237
240
|
children: FFT_SIZES.map((s) => /* @__PURE__ */ jsx2("option", { value: s, children: s }, s))
|
|
238
241
|
}
|
|
@@ -245,7 +248,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
245
248
|
{
|
|
246
249
|
id: `${id}-hop-size`,
|
|
247
250
|
value: hopSize,
|
|
248
|
-
onChange: (e) =>
|
|
251
|
+
onChange: (e) => edit({ hopSize: Number(e.target.value) }),
|
|
249
252
|
children: [
|
|
250
253
|
{ label: `${fftSize} (no overlap)`, value: fftSize },
|
|
251
254
|
{ label: `${Math.floor(fftSize / 2)} (50%)`, value: Math.floor(fftSize / 2) },
|
|
@@ -262,7 +265,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
262
265
|
{
|
|
263
266
|
id: `${id}-zero-padding`,
|
|
264
267
|
value: zeroPadding,
|
|
265
|
-
onChange: (e) =>
|
|
268
|
+
onChange: (e) => edit({ zeroPaddingFactor: Number(e.target.value) }),
|
|
266
269
|
children: [1, 2, 4, 8, 16].map((v) => /* @__PURE__ */ jsx2("option", { value: v, children: v }, v))
|
|
267
270
|
}
|
|
268
271
|
)
|
|
@@ -274,7 +277,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
274
277
|
{
|
|
275
278
|
id: `${id}-window-function`,
|
|
276
279
|
value: windowFn,
|
|
277
|
-
onChange: (e) =>
|
|
280
|
+
onChange: (e) => edit({ windowFunction: e.target.value }),
|
|
278
281
|
children: WINDOW_FUNCTIONS.map((w) => /* @__PURE__ */ jsx2("option", { value: w, children: w }, w))
|
|
279
282
|
}
|
|
280
283
|
)
|
|
@@ -286,20 +289,28 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
286
289
|
{
|
|
287
290
|
id: `${id}-frequency-scale`,
|
|
288
291
|
value: freqScale,
|
|
289
|
-
onChange: (e) =>
|
|
292
|
+
onChange: (e) => edit({ frequencyScale: e.target.value }),
|
|
290
293
|
children: FREQ_SCALES.map((s) => /* @__PURE__ */ jsx2("option", { value: s, children: s }, s))
|
|
291
294
|
}
|
|
292
295
|
)
|
|
293
296
|
] }),
|
|
294
297
|
/* @__PURE__ */ jsxs2(Field, { children: [
|
|
295
298
|
/* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-color-map`, children: "Color Map" }),
|
|
296
|
-
/* @__PURE__ */
|
|
299
|
+
/* @__PURE__ */ jsxs2(
|
|
297
300
|
Select,
|
|
298
301
|
{
|
|
299
302
|
id: `${id}-color-map`,
|
|
300
|
-
value:
|
|
301
|
-
onChange: (e) =>
|
|
302
|
-
|
|
303
|
+
value: colorMapValue,
|
|
304
|
+
onChange: (e) => (
|
|
305
|
+
// "custom" is the colour map given (an array): no edit.
|
|
306
|
+
setEditedColorMap(
|
|
307
|
+
e.target.value === CUSTOM_COLOR_MAP ? void 0 : e.target.value
|
|
308
|
+
)
|
|
309
|
+
),
|
|
310
|
+
children: [
|
|
311
|
+
isCustomColorMap && /* @__PURE__ */ jsx2("option", { value: CUSTOM_COLOR_MAP, children: CUSTOM_COLOR_MAP }),
|
|
312
|
+
COLOR_MAPS.map((c) => /* @__PURE__ */ jsx2("option", { value: c, children: c }, c))
|
|
313
|
+
]
|
|
303
314
|
}
|
|
304
315
|
)
|
|
305
316
|
] }),
|
|
@@ -314,7 +325,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
314
325
|
max: 5e3,
|
|
315
326
|
step: 50,
|
|
316
327
|
value: minFreq,
|
|
317
|
-
onChange: (e) =>
|
|
328
|
+
onChange: (e) => edit({ minFrequency: Number(e.target.value) })
|
|
318
329
|
}
|
|
319
330
|
)
|
|
320
331
|
] }),
|
|
@@ -326,10 +337,13 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
326
337
|
id: `${id}-max-frequency`,
|
|
327
338
|
type: "number",
|
|
328
339
|
min: 1e3,
|
|
329
|
-
max:
|
|
340
|
+
max: nyquist,
|
|
330
341
|
step: 50,
|
|
331
|
-
value: maxFreq,
|
|
332
|
-
onChange: (e) =>
|
|
342
|
+
value: maxFreq ?? "",
|
|
343
|
+
onChange: (e) => (
|
|
344
|
+
// Emptied: back to the default, half the sample rate.
|
|
345
|
+
edit({ maxFrequency: e.target.value === "" ? void 0 : Number(e.target.value) })
|
|
346
|
+
)
|
|
333
347
|
}
|
|
334
348
|
)
|
|
335
349
|
] }),
|
|
@@ -344,7 +358,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
344
358
|
max: 120,
|
|
345
359
|
step: 1,
|
|
346
360
|
value: rangeDb,
|
|
347
|
-
onChange: (e) =>
|
|
361
|
+
onChange: (e) => edit({ rangeDb: Number(e.target.value) })
|
|
348
362
|
}
|
|
349
363
|
)
|
|
350
364
|
] }),
|
|
@@ -359,7 +373,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
359
373
|
max: 60,
|
|
360
374
|
step: 1,
|
|
361
375
|
value: gainDb,
|
|
362
|
-
onChange: (e) =>
|
|
376
|
+
onChange: (e) => edit({ gainDb: Number(e.target.value) })
|
|
363
377
|
}
|
|
364
378
|
)
|
|
365
379
|
] }),
|
|
@@ -369,7 +383,7 @@ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
|
|
|
369
383
|
{
|
|
370
384
|
type: "checkbox",
|
|
371
385
|
checked: showLabels,
|
|
372
|
-
onChange: (e) =>
|
|
386
|
+
onChange: (e) => edit({ labels: e.target.checked })
|
|
373
387
|
}
|
|
374
388
|
),
|
|
375
389
|
"Show Frequency Labels"
|
|
@@ -386,7 +400,8 @@ var SpectrogramSettingsModal = ({
|
|
|
386
400
|
onClose,
|
|
387
401
|
config,
|
|
388
402
|
colorMap,
|
|
389
|
-
onApply
|
|
403
|
+
onApply,
|
|
404
|
+
sampleRate
|
|
390
405
|
}) => {
|
|
391
406
|
const dialogRef = useRef(null);
|
|
392
407
|
const titleId = useId();
|
|
@@ -416,14 +431,24 @@ var SpectrogramSettingsModal = ({
|
|
|
416
431
|
}, [open]);
|
|
417
432
|
return /* @__PURE__ */ jsxs2(StyledDialog, { ref: dialogRef, "aria-labelledby": titleId, children: [
|
|
418
433
|
/* @__PURE__ */ jsx2(Title, { id: titleId, children: "Spectrogram Settings" }),
|
|
419
|
-
open && /* @__PURE__ */ jsx2(
|
|
434
|
+
open && /* @__PURE__ */ jsx2(
|
|
435
|
+
SettingsForm,
|
|
436
|
+
{
|
|
437
|
+
onClose,
|
|
438
|
+
config,
|
|
439
|
+
colorMap,
|
|
440
|
+
onApply,
|
|
441
|
+
sampleRate
|
|
442
|
+
}
|
|
443
|
+
)
|
|
420
444
|
] });
|
|
421
445
|
};
|
|
422
446
|
|
|
423
447
|
// src/SpectrogramProvider.tsx
|
|
424
|
-
import { useState as useState2, useEffect as useEffect2, useRef as useRef2, useCallback, useMemo } from "react";
|
|
448
|
+
import { useState as useState2, useEffect as useEffect2, useRef as useRef2, useCallback, useMemo as useMemo2 } from "react";
|
|
425
449
|
import {
|
|
426
|
-
MAX_CANVAS_WIDTH as MAX_CANVAS_WIDTH2
|
|
450
|
+
MAX_CANVAS_WIDTH as MAX_CANVAS_WIDTH2,
|
|
451
|
+
trackKind
|
|
427
452
|
} from "@waveform-playlist/core";
|
|
428
453
|
import {
|
|
429
454
|
getColorMap,
|
|
@@ -519,26 +544,13 @@ function computeChunkSampleRange({
|
|
|
519
544
|
samplesPerPixel
|
|
520
545
|
});
|
|
521
546
|
}
|
|
522
|
-
function resolveRenderMode(
|
|
523
|
-
return
|
|
524
|
-
}
|
|
525
|
-
function toComputeConfig(cfg) {
|
|
526
|
-
return {
|
|
527
|
-
fftSize: cfg?.fftSize,
|
|
528
|
-
hopSize: cfg?.hopSize,
|
|
529
|
-
windowFunction: cfg?.windowFunction,
|
|
530
|
-
alpha: cfg?.alpha,
|
|
531
|
-
zeroPaddingFactor: cfg?.zeroPaddingFactor
|
|
532
|
-
};
|
|
547
|
+
function resolveRenderMode(trackRenderMode) {
|
|
548
|
+
return trackRenderMode ?? "waveform";
|
|
533
549
|
}
|
|
534
550
|
function buildConfigKey(params) {
|
|
535
551
|
const { mode, cfg, cm, mono } = params;
|
|
536
552
|
return JSON.stringify({ mode, cfg, cm, mono });
|
|
537
553
|
}
|
|
538
|
-
function buildFFTKey(params) {
|
|
539
|
-
const { mode, mono, computeConfig } = params;
|
|
540
|
-
return JSON.stringify({ mode, mono, ...computeConfig });
|
|
541
|
-
}
|
|
542
554
|
function mapsDiffer(prev, current) {
|
|
543
555
|
if (current.size !== prev.size) return true;
|
|
544
556
|
for (const [key, value] of current) {
|
|
@@ -563,11 +575,46 @@ function resolveClipRange(clip, registered) {
|
|
|
563
575
|
};
|
|
564
576
|
}
|
|
565
577
|
|
|
578
|
+
// src/audioRegistration.ts
|
|
579
|
+
function createAudioKeyOf() {
|
|
580
|
+
const keys = /* @__PURE__ */ new WeakMap();
|
|
581
|
+
let next = 0;
|
|
582
|
+
return (buffer) => {
|
|
583
|
+
const existing = keys.get(buffer);
|
|
584
|
+
if (existing !== void 0) return existing;
|
|
585
|
+
next += 1;
|
|
586
|
+
const key = `audio-${next}`;
|
|
587
|
+
keys.set(buffer, key);
|
|
588
|
+
return key;
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
function spectrogramAudio(tracks, audioKeyOf) {
|
|
592
|
+
const wanted = /* @__PURE__ */ new Map();
|
|
593
|
+
for (const track of tracks) {
|
|
594
|
+
if (resolveRenderMode(track.renderMode) === "waveform") continue;
|
|
595
|
+
for (const clip of track.clips) {
|
|
596
|
+
if (clip.audioBuffer) wanted.set(audioKeyOf(clip.audioBuffer), clip.audioBuffer);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return wanted;
|
|
600
|
+
}
|
|
601
|
+
function channelData(buffer) {
|
|
602
|
+
return Array.from({ length: buffer.numberOfChannels }, (_, ch) => buffer.getChannelData(ch));
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// src/useStableByContent.ts
|
|
606
|
+
import { useMemo } from "react";
|
|
607
|
+
function useStableByContent(value) {
|
|
608
|
+
const content = JSON.stringify(value);
|
|
609
|
+
return useMemo(() => value, [content]);
|
|
610
|
+
}
|
|
611
|
+
|
|
566
612
|
// src/SpectrogramProvider.tsx
|
|
567
613
|
import {
|
|
568
|
-
|
|
614
|
+
SpectrogramIntegrationContext
|
|
569
615
|
} from "@waveform-playlist/browser";
|
|
570
616
|
import {
|
|
617
|
+
useArrangement,
|
|
571
618
|
usePlaybackControls,
|
|
572
619
|
usePlaylistView,
|
|
573
620
|
usePlaylistZoom,
|
|
@@ -575,28 +622,46 @@ import {
|
|
|
575
622
|
} from "@waveform-playlist/browser";
|
|
576
623
|
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
577
624
|
var SpectrogramProvider = ({
|
|
578
|
-
config
|
|
579
|
-
colorMap
|
|
625
|
+
config,
|
|
626
|
+
colorMap,
|
|
580
627
|
workerPoolSize,
|
|
581
628
|
children
|
|
582
629
|
}) => {
|
|
630
|
+
const spectrogramConfig = useStableByContent(config);
|
|
631
|
+
const spectrogramColorMap = useStableByContent(colorMap);
|
|
583
632
|
const { mono } = usePlaylistView();
|
|
584
633
|
const { samplesPerPixel } = usePlaylistZoom();
|
|
585
|
-
const { tracks
|
|
634
|
+
const { tracks } = usePlaylistTimeline();
|
|
586
635
|
const { scrollContainerRef } = usePlaybackControls();
|
|
587
|
-
const
|
|
636
|
+
const arrangement = useArrangement();
|
|
588
637
|
const spectrogramCanvasRegistryRef = useRef2(/* @__PURE__ */ new Map());
|
|
589
638
|
const canvasHeightsRef = useRef2(/* @__PURE__ */ new Map());
|
|
590
639
|
const canvasRangesRef = useRef2(/* @__PURE__ */ new Map());
|
|
591
640
|
const [spectrogramCanvasVersion, setSpectrogramCanvasVersion] = useState2(0);
|
|
592
641
|
const prevSpectrogramConfigRef = useRef2(/* @__PURE__ */ new Map());
|
|
593
|
-
const prevSpectrogramFFTKeyRef = useRef2(/* @__PURE__ */ new Map());
|
|
594
642
|
const spectrogramWorkerRef = useRef2(null);
|
|
595
643
|
const spectrogramGenerationRef = useRef2(0);
|
|
596
644
|
const prevCanvasVersionRef = useRef2(0);
|
|
597
|
-
const renderedClipIdsRef = useRef2(/* @__PURE__ */ new Set());
|
|
598
645
|
const backgroundRenderAbortRef = useRef2(null);
|
|
599
|
-
const
|
|
646
|
+
const [audioKeyOf] = useState2(createAudioKeyOf);
|
|
647
|
+
const ensureWorkerPool = useCallback(() => {
|
|
648
|
+
if (spectrogramWorkerRef.current) return spectrogramWorkerRef.current;
|
|
649
|
+
try {
|
|
650
|
+
const pool = createSpectrogramWorkerPool(
|
|
651
|
+
() => new Worker(new URL("@dawcore/spectrogram/worker/spectrogram.worker", import.meta.url), {
|
|
652
|
+
type: "module"
|
|
653
|
+
}),
|
|
654
|
+
workerPoolSize
|
|
655
|
+
);
|
|
656
|
+
spectrogramWorkerRef.current = pool;
|
|
657
|
+
return pool;
|
|
658
|
+
} catch (err) {
|
|
659
|
+
console.warn(
|
|
660
|
+
`[waveform-playlist] Spectrogram Web Worker unavailable: ${err instanceof Error ? err.message : String(err)}`
|
|
661
|
+
);
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
}, [workerPoolSize]);
|
|
600
665
|
useEffect2(() => {
|
|
601
666
|
return () => {
|
|
602
667
|
spectrogramWorkerRef.current?.terminate();
|
|
@@ -604,109 +669,51 @@ var SpectrogramProvider = ({
|
|
|
604
669
|
};
|
|
605
670
|
}, []);
|
|
606
671
|
useEffect2(() => {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
if (!
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
type: "module"
|
|
614
|
-
}),
|
|
615
|
-
workerPoolSize
|
|
616
|
-
);
|
|
617
|
-
spectrogramWorkerRef.current = workerApi;
|
|
618
|
-
} catch (err) {
|
|
619
|
-
console.warn(
|
|
620
|
-
`[waveform-playlist] Spectrogram Web Worker unavailable for pre-transfer: ${err instanceof Error ? err.message : String(err)}`
|
|
621
|
-
);
|
|
622
|
-
return;
|
|
623
|
-
}
|
|
672
|
+
const wanted = spectrogramAudio(tracks, audioKeyOf);
|
|
673
|
+
const pool = wanted.size > 0 ? ensureWorkerPool() : spectrogramWorkerRef.current;
|
|
674
|
+
if (!pool) return;
|
|
675
|
+
const held = new Set(pool.registeredAudioClipIds());
|
|
676
|
+
for (const [key, buffer] of wanted) {
|
|
677
|
+
if (!held.has(key)) pool.registerAudioData(key, channelData(buffer), buffer.sampleRate);
|
|
624
678
|
}
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
for (const clip of track.clips) {
|
|
628
|
-
if (!clip.audioBuffer) continue;
|
|
629
|
-
currentClipIds.add(clip.id);
|
|
630
|
-
if (!registeredAudioClipIdsRef.current.has(clip.id)) {
|
|
631
|
-
const channelDataArrays = [];
|
|
632
|
-
for (let ch = 0; ch < clip.audioBuffer.numberOfChannels; ch++) {
|
|
633
|
-
channelDataArrays.push(clip.audioBuffer.getChannelData(ch));
|
|
634
|
-
}
|
|
635
|
-
workerApi.registerAudioData(clip.id, channelDataArrays, clip.audioBuffer.sampleRate);
|
|
636
|
-
registeredAudioClipIdsRef.current.add(clip.id);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
for (const clipId of registeredAudioClipIdsRef.current) {
|
|
641
|
-
if (!currentClipIds.has(clipId)) {
|
|
642
|
-
workerApi.unregisterAudioData(clipId);
|
|
643
|
-
registeredAudioClipIdsRef.current.delete(clipId);
|
|
644
|
-
}
|
|
679
|
+
for (const key of held) {
|
|
680
|
+
if (!wanted.has(key)) pool.unregisterAudioData(key);
|
|
645
681
|
}
|
|
646
|
-
}, [
|
|
682
|
+
}, [tracks, audioKeyOf, ensureWorkerPool]);
|
|
647
683
|
useEffect2(() => {
|
|
648
684
|
if (tracks.length === 0) return;
|
|
649
685
|
const currentKeys = /* @__PURE__ */ new Map();
|
|
650
|
-
const currentFFTKeys = /* @__PURE__ */ new Map();
|
|
651
686
|
tracks.forEach((track) => {
|
|
652
|
-
const
|
|
653
|
-
const mode = resolveRenderMode(override, track.renderMode);
|
|
687
|
+
const mode = resolveRenderMode(track.renderMode);
|
|
654
688
|
if (mode === "waveform") return;
|
|
655
|
-
const cfg =
|
|
656
|
-
const cm =
|
|
689
|
+
const cfg = track.spectrogramConfig ?? spectrogramConfig;
|
|
690
|
+
const cm = track.spectrogramColorMap ?? spectrogramColorMap;
|
|
657
691
|
currentKeys.set(track.id, buildConfigKey({ mode, cfg, cm, mono }));
|
|
658
|
-
currentFFTKeys.set(
|
|
659
|
-
track.id,
|
|
660
|
-
buildFFTKey({ mode, mono, computeConfig: toComputeConfig(cfg) })
|
|
661
|
-
);
|
|
662
692
|
});
|
|
663
693
|
const prevKeys = prevSpectrogramConfigRef.current;
|
|
664
|
-
const prevFFTKeys = prevSpectrogramFFTKeyRef.current;
|
|
665
694
|
const configChanged = mapsDiffer(prevKeys, currentKeys);
|
|
666
|
-
const fftKeyChanged = mapsDiffer(prevFFTKeys, currentFFTKeys);
|
|
667
695
|
const canvasVersionChanged = spectrogramCanvasVersion !== prevCanvasVersionRef.current;
|
|
668
696
|
prevCanvasVersionRef.current = spectrogramCanvasVersion;
|
|
669
697
|
if (!configChanged && !canvasVersionChanged) return;
|
|
670
698
|
if (configChanged) {
|
|
671
699
|
prevSpectrogramConfigRef.current = currentKeys;
|
|
672
|
-
prevSpectrogramFFTKeyRef.current = currentFFTKeys;
|
|
673
700
|
}
|
|
674
701
|
if (backgroundRenderAbortRef.current) {
|
|
675
702
|
backgroundRenderAbortRef.current.aborted = true;
|
|
676
703
|
}
|
|
677
704
|
const generation = ++spectrogramGenerationRef.current;
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
workerApi = createSpectrogramWorkerPool(
|
|
685
|
-
() => new Worker(new URL("@dawcore/spectrogram/worker/spectrogram.worker", import.meta.url), {
|
|
686
|
-
type: "module"
|
|
687
|
-
}),
|
|
688
|
-
workerPoolSize
|
|
689
|
-
);
|
|
690
|
-
spectrogramWorkerRef.current = workerApi;
|
|
691
|
-
} catch (err) {
|
|
692
|
-
console.error(
|
|
693
|
-
`[waveform-playlist] Spectrogram Web Worker required but unavailable: ${err instanceof Error ? err.message : String(err)}`
|
|
694
|
-
);
|
|
695
|
-
return;
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
const clipsNeedingFFT = [];
|
|
699
|
-
const clipsNeedingDisplayOnly = [];
|
|
700
|
-
tracks.forEach((track, i) => {
|
|
701
|
-
const override = trackSpectrogramOverrides.get(track.id);
|
|
702
|
-
const mode = resolveRenderMode(override, track.renderMode);
|
|
705
|
+
const workerApi = ensureWorkerPool();
|
|
706
|
+
if (!workerApi) return;
|
|
707
|
+
workerApi.abortGeneration(generation);
|
|
708
|
+
const clipsToRender = [];
|
|
709
|
+
tracks.forEach((track) => {
|
|
710
|
+
const mode = resolveRenderMode(track.renderMode);
|
|
703
711
|
if (mode === "waveform") return;
|
|
704
712
|
const trackConfigChanged = configChanged && currentKeys.get(track.id) !== prevKeys.get(track.id);
|
|
705
|
-
const trackFFTChanged = fftKeyChanged && currentFFTKeys.get(track.id) !== prevFFTKeys.get(track.id);
|
|
706
713
|
const hasRegisteredCanvases = canvasVersionChanged && track.clips.some((clip) => spectrogramCanvasRegistryRef.current.has(clip.id));
|
|
707
714
|
if (!trackConfigChanged && !hasRegisteredCanvases) return;
|
|
708
|
-
const cfg =
|
|
709
|
-
const cm =
|
|
715
|
+
const cfg = track.spectrogramConfig ?? spectrogramConfig ?? {};
|
|
716
|
+
const cm = track.spectrogramColorMap ?? spectrogramColorMap ?? "viridis";
|
|
710
717
|
for (const clip of track.clips) {
|
|
711
718
|
if (!clip.audioBuffer) continue;
|
|
712
719
|
const monoFlag = mono || clip.audioBuffer.numberOfChannels === 1;
|
|
@@ -717,34 +724,10 @@ var SpectrogramProvider = ({
|
|
|
717
724
|
canvasRangesRef.current
|
|
718
725
|
)
|
|
719
726
|
);
|
|
720
|
-
|
|
721
|
-
const channelDataArrays2 = [];
|
|
722
|
-
for (let ch = 0; ch < clip.audioBuffer.numberOfChannels; ch++) {
|
|
723
|
-
channelDataArrays2.push(clip.audioBuffer.getChannelData(ch));
|
|
724
|
-
}
|
|
725
|
-
clipsNeedingDisplayOnly.push({
|
|
726
|
-
clipId: clip.id,
|
|
727
|
-
trackIndex: i,
|
|
728
|
-
channelDataArrays: channelDataArrays2,
|
|
729
|
-
config: cfg,
|
|
730
|
-
sampleRate: clip.audioBuffer.sampleRate,
|
|
731
|
-
offsetSamples,
|
|
732
|
-
durationSamples,
|
|
733
|
-
clipStartSample: clip.startSample,
|
|
734
|
-
monoFlag,
|
|
735
|
-
colorMap: cm,
|
|
736
|
-
numChannels: monoFlag ? 1 : clip.audioBuffer.numberOfChannels
|
|
737
|
-
});
|
|
738
|
-
continue;
|
|
739
|
-
}
|
|
740
|
-
const channelDataArrays = [];
|
|
741
|
-
for (let ch = 0; ch < clip.audioBuffer.numberOfChannels; ch++) {
|
|
742
|
-
channelDataArrays.push(clip.audioBuffer.getChannelData(ch));
|
|
743
|
-
}
|
|
744
|
-
clipsNeedingFFT.push({
|
|
727
|
+
clipsToRender.push({
|
|
745
728
|
clipId: clip.id,
|
|
746
|
-
|
|
747
|
-
channelDataArrays,
|
|
729
|
+
audioKey: audioKeyOf(clip.audioBuffer),
|
|
730
|
+
channelDataArrays: channelData(clip.audioBuffer),
|
|
748
731
|
config: cfg,
|
|
749
732
|
sampleRate: clip.audioBuffer.sampleRate,
|
|
750
733
|
offsetSamples,
|
|
@@ -755,7 +738,7 @@ var SpectrogramProvider = ({
|
|
|
755
738
|
});
|
|
756
739
|
}
|
|
757
740
|
});
|
|
758
|
-
if (
|
|
741
|
+
if (clipsToRender.length === 0) return;
|
|
759
742
|
const getVisibleChunkRange = (channelInfo, clipPixelOffset = 0) => {
|
|
760
743
|
const container = scrollContainerRef.current;
|
|
761
744
|
return classifyChunkTiers(
|
|
@@ -808,7 +791,8 @@ var SpectrogramProvider = ({
|
|
|
808
791
|
});
|
|
809
792
|
const { cacheKey } = await api.computeFFT(
|
|
810
793
|
{
|
|
811
|
-
|
|
794
|
+
// The pool's "clip id" names the registered audio: the buffer's key.
|
|
795
|
+
clipId: item.audioKey,
|
|
812
796
|
channelDataArrays: item.channelDataArrays,
|
|
813
797
|
config: item.config,
|
|
814
798
|
sampleRate: item.sampleRate,
|
|
@@ -864,7 +848,7 @@ var SpectrogramProvider = ({
|
|
|
864
848
|
}
|
|
865
849
|
return false;
|
|
866
850
|
};
|
|
867
|
-
for (const item of
|
|
851
|
+
for (const item of clipsToRender) {
|
|
868
852
|
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
869
853
|
try {
|
|
870
854
|
const clipCanvasInfo = spectrogramCanvasRegistryRef.current.get(item.clipId);
|
|
@@ -928,7 +912,6 @@ var SpectrogramProvider = ({
|
|
|
928
912
|
}
|
|
929
913
|
}
|
|
930
914
|
}
|
|
931
|
-
renderedClipIdsRef.current.add(item.clipId);
|
|
932
915
|
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
933
916
|
if (await renderBackgroundBatches(channelRanges, item)) return;
|
|
934
917
|
}
|
|
@@ -939,78 +922,6 @@ var SpectrogramProvider = ({
|
|
|
939
922
|
);
|
|
940
923
|
}
|
|
941
924
|
}
|
|
942
|
-
for (const item of clipsNeedingDisplayOnly) {
|
|
943
|
-
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
944
|
-
const clipCanvasInfo = spectrogramCanvasRegistryRef.current.get(item.clipId);
|
|
945
|
-
if (!clipCanvasInfo || clipCanvasInfo.size === 0) continue;
|
|
946
|
-
try {
|
|
947
|
-
const clipPixelOffset = Math.floor(item.clipStartSample / samplesPerPixel);
|
|
948
|
-
const channelRanges = [];
|
|
949
|
-
for (let ch = 0; ch < item.numChannels; ch++) {
|
|
950
|
-
const channelInfo = clipCanvasInfo.get(ch);
|
|
951
|
-
if (!channelInfo) continue;
|
|
952
|
-
const range = getVisibleChunkRange(channelInfo, clipPixelOffset);
|
|
953
|
-
channelRanges.push({ ch, channelInfo, ...range });
|
|
954
|
-
}
|
|
955
|
-
if (channelRanges.length > 0 && channelRanges[0].viewportIndices.length > 0) {
|
|
956
|
-
const cacheKey = await computeFFTForChunks(
|
|
957
|
-
workerApi,
|
|
958
|
-
channelRanges[0].channelInfo,
|
|
959
|
-
channelRanges[0].viewportIndices,
|
|
960
|
-
item,
|
|
961
|
-
generation
|
|
962
|
-
);
|
|
963
|
-
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
964
|
-
for (const { ch, channelInfo, viewportIndices } of channelRanges) {
|
|
965
|
-
await renderChunkSubset(
|
|
966
|
-
workerApi,
|
|
967
|
-
cacheKey,
|
|
968
|
-
channelInfo,
|
|
969
|
-
viewportIndices,
|
|
970
|
-
item,
|
|
971
|
-
ch,
|
|
972
|
-
generation
|
|
973
|
-
);
|
|
974
|
-
}
|
|
975
|
-
}
|
|
976
|
-
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
977
|
-
if (channelRanges.length > 0 && channelRanges[0].bufferIndices.length > 0) {
|
|
978
|
-
const bufferGroups = groupContiguousIndices(
|
|
979
|
-
channelRanges[0].channelInfo,
|
|
980
|
-
channelRanges[0].bufferIndices
|
|
981
|
-
);
|
|
982
|
-
for (const group of bufferGroups) {
|
|
983
|
-
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
984
|
-
const cacheKey = await computeFFTForChunks(
|
|
985
|
-
workerApi,
|
|
986
|
-
channelRanges[0].channelInfo,
|
|
987
|
-
group,
|
|
988
|
-
item,
|
|
989
|
-
generation
|
|
990
|
-
);
|
|
991
|
-
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
992
|
-
for (const { ch, channelInfo } of channelRanges) {
|
|
993
|
-
await renderChunkSubset(
|
|
994
|
-
workerApi,
|
|
995
|
-
cacheKey,
|
|
996
|
-
channelInfo,
|
|
997
|
-
group,
|
|
998
|
-
item,
|
|
999
|
-
ch,
|
|
1000
|
-
generation
|
|
1001
|
-
);
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
if (spectrogramGenerationRef.current !== generation || abortToken.aborted) return;
|
|
1006
|
-
if (await renderBackgroundBatches(channelRanges, item)) return;
|
|
1007
|
-
} catch (err) {
|
|
1008
|
-
if (err instanceof SpectrogramAbortError) return;
|
|
1009
|
-
console.warn(
|
|
1010
|
-
`[waveform-playlist] Spectrogram display re-render error for clip ${item.clipId}: ${err instanceof Error ? err.message : String(err)}`
|
|
1011
|
-
);
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
925
|
};
|
|
1015
926
|
computeAsync().catch((err) => {
|
|
1016
927
|
console.error(
|
|
@@ -1022,52 +933,31 @@ var SpectrogramProvider = ({
|
|
|
1022
933
|
mono,
|
|
1023
934
|
spectrogramConfig,
|
|
1024
935
|
spectrogramColorMap,
|
|
1025
|
-
trackSpectrogramOverrides,
|
|
1026
936
|
samplesPerPixel,
|
|
1027
937
|
spectrogramCanvasVersion,
|
|
1028
|
-
scrollContainerRef
|
|
938
|
+
scrollContainerRef,
|
|
939
|
+
audioKeyOf,
|
|
940
|
+
ensureWorkerPool
|
|
1029
941
|
]);
|
|
1030
|
-
const setTrackRenderMode = useCallback(
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
}
|
|
1037
|
-
|
|
942
|
+
const setTrackRenderMode = useCallback(
|
|
943
|
+
(trackId, mode) => {
|
|
944
|
+
arrangement.updateTrack(
|
|
945
|
+
trackId,
|
|
946
|
+
(track) => trackKind(track) === "midi" || resolveRenderMode(track.renderMode) === mode ? track : { ...track, renderMode: mode }
|
|
947
|
+
);
|
|
948
|
+
},
|
|
949
|
+
[arrangement]
|
|
950
|
+
);
|
|
1038
951
|
const setTrackSpectrogramConfig = useCallback(
|
|
1039
|
-
(trackId,
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
config,
|
|
1046
|
-
...colorMap !== void 0 ? { colorMap } : { colorMap: existing?.colorMap }
|
|
1047
|
-
});
|
|
1048
|
-
return next;
|
|
1049
|
-
});
|
|
952
|
+
(trackId, config2, colorMap2) => {
|
|
953
|
+
arrangement.updateTrack(trackId, (track) => ({
|
|
954
|
+
...track,
|
|
955
|
+
spectrogramConfig: config2,
|
|
956
|
+
spectrogramColorMap: colorMap2 ?? track.spectrogramColorMap
|
|
957
|
+
}));
|
|
1050
958
|
},
|
|
1051
|
-
[]
|
|
959
|
+
[arrangement]
|
|
1052
960
|
);
|
|
1053
|
-
const ensureWorkerPool = useCallback(() => {
|
|
1054
|
-
if (spectrogramWorkerRef.current) return spectrogramWorkerRef.current;
|
|
1055
|
-
try {
|
|
1056
|
-
const pool = createSpectrogramWorkerPool(
|
|
1057
|
-
() => new Worker(new URL("@dawcore/spectrogram/worker/spectrogram.worker", import.meta.url), {
|
|
1058
|
-
type: "module"
|
|
1059
|
-
}),
|
|
1060
|
-
workerPoolSize
|
|
1061
|
-
);
|
|
1062
|
-
spectrogramWorkerRef.current = pool;
|
|
1063
|
-
return pool;
|
|
1064
|
-
} catch (err) {
|
|
1065
|
-
console.warn(
|
|
1066
|
-
`[waveform-playlist] Spectrogram Web Worker unavailable: ${err instanceof Error ? err.message : String(err)}`
|
|
1067
|
-
);
|
|
1068
|
-
return null;
|
|
1069
|
-
}
|
|
1070
|
-
}, [workerPoolSize]);
|
|
1071
961
|
const registerSpectrogramCanvas = useCallback(
|
|
1072
962
|
(reg) => {
|
|
1073
963
|
const pool = ensureWorkerPool();
|
|
@@ -1143,7 +1033,7 @@ var SpectrogramProvider = ({
|
|
|
1143
1033
|
}, []);
|
|
1144
1034
|
const renderMenuItems = useCallback(
|
|
1145
1035
|
(props) => {
|
|
1146
|
-
return
|
|
1036
|
+
return spectrogramMenuItems({
|
|
1147
1037
|
renderMode: props.renderMode,
|
|
1148
1038
|
onRenderModeChange: props.onRenderModeChange,
|
|
1149
1039
|
onOpenSettings: props.onOpenSettings,
|
|
@@ -1152,9 +1042,8 @@ var SpectrogramProvider = ({
|
|
|
1152
1042
|
},
|
|
1153
1043
|
[]
|
|
1154
1044
|
);
|
|
1155
|
-
const value =
|
|
1045
|
+
const value = useMemo2(
|
|
1156
1046
|
() => ({
|
|
1157
|
-
trackSpectrogramOverrides,
|
|
1158
1047
|
spectrogramConfig,
|
|
1159
1048
|
spectrogramColorMap,
|
|
1160
1049
|
setTrackRenderMode,
|
|
@@ -1167,7 +1056,6 @@ var SpectrogramProvider = ({
|
|
|
1167
1056
|
getFrequencyScale
|
|
1168
1057
|
}),
|
|
1169
1058
|
[
|
|
1170
|
-
trackSpectrogramOverrides,
|
|
1171
1059
|
spectrogramConfig,
|
|
1172
1060
|
spectrogramColorMap,
|
|
1173
1061
|
setTrackRenderMode,
|
|
@@ -1177,11 +1065,11 @@ var SpectrogramProvider = ({
|
|
|
1177
1065
|
renderMenuItems
|
|
1178
1066
|
]
|
|
1179
1067
|
);
|
|
1180
|
-
return /* @__PURE__ */ jsx3(
|
|
1068
|
+
return /* @__PURE__ */ jsx3(SpectrogramIntegrationContext, { value, children });
|
|
1181
1069
|
};
|
|
1182
1070
|
export {
|
|
1183
|
-
SpectrogramMenuItems,
|
|
1184
1071
|
SpectrogramProvider,
|
|
1185
|
-
SpectrogramSettingsModal
|
|
1072
|
+
SpectrogramSettingsModal,
|
|
1073
|
+
spectrogramMenuItems
|
|
1186
1074
|
};
|
|
1187
1075
|
//# sourceMappingURL=index.mjs.map
|