@waveform-playlist/spectrogram 14.0.1 → 15.0.0-next.0

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.mjs CHANGED
@@ -91,9 +91,9 @@ function SpectrogramMenuItems({
91
91
  }
92
92
 
93
93
  // src/components/SpectrogramSettingsModal.tsx
94
- import { useRef, useEffect, useState } from "react";
94
+ import { useRef, useEffect, useId, useLayoutEffect, useState } from "react";
95
95
  import styled2 from "styled-components";
96
- import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
96
+ import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
97
97
  var StyledDialog = styled2.dialog`
98
98
  border: 1px solid rgba(128, 128, 128, 0.4);
99
99
  border-radius: 8px;
@@ -102,6 +102,7 @@ var StyledDialog = styled2.dialog`
102
102
  color: ${(p) => p.theme.textColor ?? "inherit"};
103
103
  min-width: 380px;
104
104
  max-width: 500px;
105
+ overscroll-behavior: contain;
105
106
 
106
107
  &::backdrop {
107
108
  background: rgba(0, 0, 0, 0.5);
@@ -184,14 +185,8 @@ var WINDOW_FUNCTIONS = [
184
185
  ];
185
186
  var FREQ_SCALES = ["linear", "logarithmic", "mel", "bark", "erb"];
186
187
  var COLOR_MAPS = ["viridis", "magma", "inferno", "grayscale", "igray", "roseus"];
187
- var SpectrogramSettingsModal = ({
188
- open,
189
- onClose,
190
- config,
191
- colorMap,
192
- onApply
193
- }) => {
194
- const dialogRef = useRef(null);
188
+ var SettingsForm = ({ onClose, config, colorMap, onApply }) => {
189
+ const id = useId();
195
190
  const [fftSize, setFftSize] = useState(config.fftSize ?? 2048);
196
191
  const [windowFn, setWindowFn] = useState(config.windowFunction ?? "hann");
197
192
  const [freqScale, setFreqScale] = useState(config.frequencyScale ?? "mel");
@@ -207,35 +202,6 @@ var SpectrogramSettingsModal = ({
207
202
  config.hopSize ?? Math.floor((config.fftSize ?? 2048) / 4)
208
203
  );
209
204
  const [showLabels, setShowLabels] = useState(config.labels ?? false);
210
- useEffect(() => {
211
- setFftSize(config.fftSize ?? 2048);
212
- setWindowFn(config.windowFunction ?? "hann");
213
- setFreqScale(config.frequencyScale ?? "mel");
214
- setLocalColorMap(typeof colorMap === "string" ? colorMap : "viridis");
215
- setMinFreq(config.minFrequency ?? 0);
216
- setMaxFreq(config.maxFrequency ?? 2e4);
217
- setGainDb(config.gainDb ?? 20);
218
- setRangeDb(config.rangeDb ?? 80);
219
- setZeroPadding(config.zeroPaddingFactor ?? 2);
220
- setHopSize(config.hopSize ?? Math.floor((config.fftSize ?? 2048) / 4));
221
- setShowLabels(config.labels ?? false);
222
- }, [config, colorMap]);
223
- useEffect(() => {
224
- const dialog = dialogRef.current;
225
- if (!dialog) return;
226
- if (open && !dialog.open) {
227
- dialog.showModal();
228
- } else if (!open && dialog.open) {
229
- dialog.close();
230
- }
231
- const handleClose = () => {
232
- if (open) {
233
- onClose();
234
- }
235
- };
236
- dialog.addEventListener("close", handleClose);
237
- return () => dialog.removeEventListener("close", handleClose);
238
- }, [open, onClose]);
239
205
  const handleApply = () => {
240
206
  onApply(
241
207
  {
@@ -254,14 +220,14 @@ var SpectrogramSettingsModal = ({
254
220
  );
255
221
  onClose();
256
222
  };
257
- return /* @__PURE__ */ jsxs2(StyledDialog, { ref: dialogRef, children: [
258
- /* @__PURE__ */ jsx2(Title, { children: "Spectrogram Settings" }),
223
+ return /* @__PURE__ */ jsxs2(Fragment, { children: [
259
224
  /* @__PURE__ */ jsxs2(FormGrid, { children: [
260
225
  /* @__PURE__ */ jsxs2(Field, { children: [
261
- /* @__PURE__ */ jsx2(Label, { children: "FFT Size" }),
226
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-fft-size`, children: "FFT Size" }),
262
227
  /* @__PURE__ */ jsx2(
263
228
  Select,
264
229
  {
230
+ id: `${id}-fft-size`,
265
231
  value: fftSize,
266
232
  onChange: (e) => {
267
233
  const v = Number(e.target.value);
@@ -273,27 +239,52 @@ var SpectrogramSettingsModal = ({
273
239
  )
274
240
  ] }),
275
241
  /* @__PURE__ */ jsxs2(Field, { children: [
276
- /* @__PURE__ */ jsx2(Label, { children: "Hop Size" }),
277
- /* @__PURE__ */ jsx2(Select, { value: hopSize, onChange: (e) => setHopSize(Number(e.target.value)), children: [
278
- { label: `${fftSize} (no overlap)`, value: fftSize },
279
- { label: `${Math.floor(fftSize / 2)} (50%)`, value: Math.floor(fftSize / 2) },
280
- { label: `${Math.floor(fftSize / 4)} (75%)`, value: Math.floor(fftSize / 4) },
281
- { label: `${Math.floor(fftSize / 8)} (87.5%)`, value: Math.floor(fftSize / 8) }
282
- ].map((o) => /* @__PURE__ */ jsx2("option", { value: o.value, children: o.label }, o.value)) })
242
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-hop-size`, children: "Hop Size" }),
243
+ /* @__PURE__ */ jsx2(
244
+ Select,
245
+ {
246
+ id: `${id}-hop-size`,
247
+ value: hopSize,
248
+ onChange: (e) => setHopSize(Number(e.target.value)),
249
+ children: [
250
+ { label: `${fftSize} (no overlap)`, value: fftSize },
251
+ { label: `${Math.floor(fftSize / 2)} (50%)`, value: Math.floor(fftSize / 2) },
252
+ { label: `${Math.floor(fftSize / 4)} (75%)`, value: Math.floor(fftSize / 4) },
253
+ { label: `${Math.floor(fftSize / 8)} (87.5%)`, value: Math.floor(fftSize / 8) }
254
+ ].map((o) => /* @__PURE__ */ jsx2("option", { value: o.value, children: o.label }, o.value))
255
+ }
256
+ )
283
257
  ] }),
284
258
  /* @__PURE__ */ jsxs2(Field, { children: [
285
- /* @__PURE__ */ jsx2(Label, { children: "Zero Padding" }),
286
- /* @__PURE__ */ jsx2(Select, { value: zeroPadding, onChange: (e) => setZeroPadding(Number(e.target.value)), children: [1, 2, 4, 8, 16].map((v) => /* @__PURE__ */ jsx2("option", { value: v, children: v }, v)) })
259
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-zero-padding`, children: "Zero Padding" }),
260
+ /* @__PURE__ */ jsx2(
261
+ Select,
262
+ {
263
+ id: `${id}-zero-padding`,
264
+ value: zeroPadding,
265
+ onChange: (e) => setZeroPadding(Number(e.target.value)),
266
+ children: [1, 2, 4, 8, 16].map((v) => /* @__PURE__ */ jsx2("option", { value: v, children: v }, v))
267
+ }
268
+ )
287
269
  ] }),
288
270
  /* @__PURE__ */ jsxs2(Field, { children: [
289
- /* @__PURE__ */ jsx2(Label, { children: "Window Function" }),
290
- /* @__PURE__ */ jsx2(Select, { value: windowFn, onChange: (e) => setWindowFn(e.target.value), children: WINDOW_FUNCTIONS.map((w) => /* @__PURE__ */ jsx2("option", { value: w, children: w }, w)) })
271
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-window-function`, children: "Window Function" }),
272
+ /* @__PURE__ */ jsx2(
273
+ Select,
274
+ {
275
+ id: `${id}-window-function`,
276
+ value: windowFn,
277
+ onChange: (e) => setWindowFn(e.target.value),
278
+ children: WINDOW_FUNCTIONS.map((w) => /* @__PURE__ */ jsx2("option", { value: w, children: w }, w))
279
+ }
280
+ )
291
281
  ] }),
292
282
  /* @__PURE__ */ jsxs2(Field, { children: [
293
- /* @__PURE__ */ jsx2(Label, { children: "Frequency Scale" }),
283
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-frequency-scale`, children: "Frequency Scale" }),
294
284
  /* @__PURE__ */ jsx2(
295
285
  Select,
296
286
  {
287
+ id: `${id}-frequency-scale`,
297
288
  value: freqScale,
298
289
  onChange: (e) => setFreqScale(e.target.value),
299
290
  children: FREQ_SCALES.map((s) => /* @__PURE__ */ jsx2("option", { value: s, children: s }, s))
@@ -301,10 +292,11 @@ var SpectrogramSettingsModal = ({
301
292
  )
302
293
  ] }),
303
294
  /* @__PURE__ */ jsxs2(Field, { children: [
304
- /* @__PURE__ */ jsx2(Label, { children: "Color Map" }),
295
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-color-map`, children: "Color Map" }),
305
296
  /* @__PURE__ */ jsx2(
306
297
  Select,
307
298
  {
299
+ id: `${id}-color-map`,
308
300
  value: localColorMap,
309
301
  onChange: (e) => setLocalColorMap(e.target.value),
310
302
  children: COLOR_MAPS.map((c) => /* @__PURE__ */ jsx2("option", { value: c, children: c }, c))
@@ -312,10 +304,11 @@ var SpectrogramSettingsModal = ({
312
304
  )
313
305
  ] }),
314
306
  /* @__PURE__ */ jsxs2(Field, { children: [
315
- /* @__PURE__ */ jsx2(Label, { children: "Min Frequency (Hz)" }),
307
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-min-frequency`, children: "Min Frequency (Hz)" }),
316
308
  /* @__PURE__ */ jsx2(
317
309
  NumberInput,
318
310
  {
311
+ id: `${id}-min-frequency`,
319
312
  type: "number",
320
313
  min: 0,
321
314
  max: 5e3,
@@ -326,10 +319,11 @@ var SpectrogramSettingsModal = ({
326
319
  )
327
320
  ] }),
328
321
  /* @__PURE__ */ jsxs2(Field, { children: [
329
- /* @__PURE__ */ jsx2(Label, { children: "Max Frequency (Hz)" }),
322
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-max-frequency`, children: "Max Frequency (Hz)" }),
330
323
  /* @__PURE__ */ jsx2(
331
324
  NumberInput,
332
325
  {
326
+ id: `${id}-max-frequency`,
333
327
  type: "number",
334
328
  min: 1e3,
335
329
  max: 22050,
@@ -340,10 +334,11 @@ var SpectrogramSettingsModal = ({
340
334
  )
341
335
  ] }),
342
336
  /* @__PURE__ */ jsxs2(Field, { children: [
343
- /* @__PURE__ */ jsx2(Label, { children: "Range (dB)" }),
337
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-range-db`, children: "Range (dB)" }),
344
338
  /* @__PURE__ */ jsx2(
345
339
  NumberInput,
346
340
  {
341
+ id: `${id}-range-db`,
347
342
  type: "number",
348
343
  min: 1,
349
344
  max: 120,
@@ -354,10 +349,11 @@ var SpectrogramSettingsModal = ({
354
349
  )
355
350
  ] }),
356
351
  /* @__PURE__ */ jsxs2(Field, { children: [
357
- /* @__PURE__ */ jsx2(Label, { children: "Gain (dB)" }),
352
+ /* @__PURE__ */ jsx2(Label, { htmlFor: `${id}-gain-db`, children: "Gain (dB)" }),
358
353
  /* @__PURE__ */ jsx2(
359
354
  NumberInput,
360
355
  {
356
+ id: `${id}-gain-db`,
361
357
  type: "number",
362
358
  min: -20,
363
359
  max: 60,
@@ -380,11 +376,49 @@ var SpectrogramSettingsModal = ({
380
376
  ] }) })
381
377
  ] }),
382
378
  /* @__PURE__ */ jsxs2(ButtonRow, { children: [
383
- /* @__PURE__ */ jsx2(ModalButton, { onClick: onClose, children: "Cancel" }),
384
- /* @__PURE__ */ jsx2(ModalButton, { $primary: true, onClick: handleApply, children: "Apply" })
379
+ /* @__PURE__ */ jsx2(ModalButton, { type: "button", onClick: onClose, children: "Cancel" }),
380
+ /* @__PURE__ */ jsx2(ModalButton, { type: "button", $primary: true, onClick: handleApply, children: "Apply" })
385
381
  ] })
386
382
  ] });
387
383
  };
384
+ var SpectrogramSettingsModal = ({
385
+ open,
386
+ onClose,
387
+ config,
388
+ colorMap,
389
+ onApply
390
+ }) => {
391
+ const dialogRef = useRef(null);
392
+ const titleId = useId();
393
+ const latestRef = useRef({ open, onClose });
394
+ useLayoutEffect(() => {
395
+ latestRef.current = { open, onClose };
396
+ });
397
+ useEffect(() => {
398
+ const dialog = dialogRef.current;
399
+ if (!dialog) return;
400
+ const handleClose = () => {
401
+ if (latestRef.current.open) {
402
+ latestRef.current.onClose();
403
+ }
404
+ };
405
+ dialog.addEventListener("close", handleClose);
406
+ return () => dialog.removeEventListener("close", handleClose);
407
+ }, []);
408
+ useLayoutEffect(() => {
409
+ const dialog = dialogRef.current;
410
+ if (!dialog) return;
411
+ if (open && !dialog.open) {
412
+ dialog.showModal();
413
+ } else if (!open && dialog.open) {
414
+ dialog.close();
415
+ }
416
+ }, [open]);
417
+ return /* @__PURE__ */ jsxs2(StyledDialog, { ref: dialogRef, "aria-labelledby": titleId, children: [
418
+ /* @__PURE__ */ jsx2(Title, { id: titleId, children: "Spectrogram Settings" }),
419
+ open && /* @__PURE__ */ jsx2(SettingsForm, { onClose, config, colorMap, onApply })
420
+ ] });
421
+ };
388
422
 
389
423
  // src/SpectrogramProvider.tsx
390
424
  import { useState as useState2, useEffect as useEffect2, useRef as useRef2, useCallback, useMemo } from "react";
@@ -512,12 +546,33 @@ function mapsDiffer(prev, current) {
512
546
  }
513
547
  return false;
514
548
  }
549
+ function registeredClipRange(perClip, rangeByCanvasId) {
550
+ if (!perClip) return void 0;
551
+ for (const channelInfo of perClip.values()) {
552
+ for (const canvasId of channelInfo.canvasIds) {
553
+ const range = rangeByCanvasId.get(canvasId);
554
+ if (range) return range;
555
+ }
556
+ }
557
+ return void 0;
558
+ }
559
+ function resolveClipRange(clip, registered) {
560
+ return {
561
+ offsetSamples: registered?.offsetSamples ?? clip.offsetSamples,
562
+ durationSamples: registered?.durationSamples ?? clip.durationSamples
563
+ };
564
+ }
515
565
 
516
566
  // src/SpectrogramProvider.tsx
517
567
  import {
518
568
  SpectrogramIntegrationProvider
519
569
  } from "@waveform-playlist/browser";
520
- import { usePlaylistData, usePlaylistControls } from "@waveform-playlist/browser";
570
+ import {
571
+ usePlaybackControls,
572
+ usePlaylistView,
573
+ usePlaylistZoom,
574
+ usePlaylistTimeline
575
+ } from "@waveform-playlist/browser";
521
576
  import { jsx as jsx3 } from "react/jsx-runtime";
522
577
  var SpectrogramProvider = ({
523
578
  config: spectrogramConfig,
@@ -525,10 +580,14 @@ var SpectrogramProvider = ({
525
580
  workerPoolSize,
526
581
  children
527
582
  }) => {
528
- const { tracks, waveHeight, samplesPerPixel, isReady, mono } = usePlaylistData();
529
- const { scrollContainerRef } = usePlaylistControls();
530
- const [trackSpectrogramOverrides, setTrackSpectrogramOverrides] = useState2(/* @__PURE__ */ new Map());
583
+ const { mono } = usePlaylistView();
584
+ const { samplesPerPixel } = usePlaylistZoom();
585
+ const { tracks, isReady } = usePlaylistTimeline();
586
+ const { scrollContainerRef } = usePlaybackControls();
587
+ const [trackSpectrogramOverrides, setTrackSpectrogramOverrides] = useState2(() => /* @__PURE__ */ new Map());
531
588
  const spectrogramCanvasRegistryRef = useRef2(/* @__PURE__ */ new Map());
589
+ const canvasHeightsRef = useRef2(/* @__PURE__ */ new Map());
590
+ const canvasRangesRef = useRef2(/* @__PURE__ */ new Map());
532
591
  const [spectrogramCanvasVersion, setSpectrogramCanvasVersion] = useState2(0);
533
592
  const prevSpectrogramConfigRef = useRef2(/* @__PURE__ */ new Map());
534
593
  const prevSpectrogramFFTKeyRef = useRef2(/* @__PURE__ */ new Map());
@@ -651,6 +710,13 @@ var SpectrogramProvider = ({
651
710
  for (const clip of track.clips) {
652
711
  if (!clip.audioBuffer) continue;
653
712
  const monoFlag = mono || clip.audioBuffer.numberOfChannels === 1;
713
+ const { offsetSamples, durationSamples } = resolveClipRange(
714
+ clip,
715
+ registeredClipRange(
716
+ spectrogramCanvasRegistryRef.current.get(clip.id),
717
+ canvasRangesRef.current
718
+ )
719
+ );
654
720
  if (!trackFFTChanged && !hasRegisteredCanvases && renderedClipIdsRef.current.has(clip.id)) {
655
721
  const channelDataArrays2 = [];
656
722
  for (let ch = 0; ch < clip.audioBuffer.numberOfChannels; ch++) {
@@ -662,8 +728,8 @@ var SpectrogramProvider = ({
662
728
  channelDataArrays: channelDataArrays2,
663
729
  config: cfg,
664
730
  sampleRate: clip.audioBuffer.sampleRate,
665
- offsetSamples: clip.offsetSamples,
666
- durationSamples: clip.durationSamples,
731
+ offsetSamples,
732
+ durationSamples,
667
733
  clipStartSample: clip.startSample,
668
734
  monoFlag,
669
735
  colorMap: cm,
@@ -681,8 +747,8 @@ var SpectrogramProvider = ({
681
747
  channelDataArrays,
682
748
  config: cfg,
683
749
  sampleRate: clip.audioBuffer.sampleRate,
684
- offsetSamples: clip.offsetSamples,
685
- durationSamples: clip.durationSamples,
750
+ offsetSamples,
751
+ durationSamples,
686
752
  clipStartSample: clip.startSample,
687
753
  monoFlag,
688
754
  colorMap: cm
@@ -703,6 +769,8 @@ var SpectrogramProvider = ({
703
769
  if (indices.length === 0) return;
704
770
  const canvasIds = indices.map((i) => channelInfo.canvasIds[i]);
705
771
  const canvasWidths = indices.map((i) => channelInfo.canvasWidths[i]);
772
+ const canvasHeight = canvasHeightsRef.current.get(canvasIds[0]);
773
+ if (canvasHeight === void 0) return;
706
774
  const globalPixelOffsets = [];
707
775
  for (const idx of indices) {
708
776
  const chunkNumber = extractChunkNumber(channelInfo.canvasIds[idx]);
@@ -715,7 +783,7 @@ var SpectrogramProvider = ({
715
783
  canvasIds,
716
784
  canvasWidths,
717
785
  globalPixelOffsets,
718
- canvasHeight: waveHeight,
786
+ canvasHeight,
719
787
  devicePixelRatio: typeof window !== "undefined" ? window.devicePixelRatio : 1,
720
788
  samplesPerPixel,
721
789
  colorLUT,
@@ -955,7 +1023,6 @@ var SpectrogramProvider = ({
955
1023
  spectrogramConfig,
956
1024
  spectrogramColorMap,
957
1025
  trackSpectrogramOverrides,
958
- waveHeight,
959
1026
  samplesPerPixel,
960
1027
  spectrogramCanvasVersion,
961
1028
  scrollContainerRef
@@ -1013,6 +1080,15 @@ var SpectrogramProvider = ({
1013
1080
  );
1014
1081
  return;
1015
1082
  }
1083
+ canvasHeightsRef.current.set(reg.canvasId, reg.heightPx);
1084
+ if (reg.offsetSamples !== void 0 || reg.durationSamples !== void 0) {
1085
+ canvasRangesRef.current.set(reg.canvasId, {
1086
+ offsetSamples: reg.offsetSamples,
1087
+ durationSamples: reg.durationSamples
1088
+ });
1089
+ } else {
1090
+ canvasRangesRef.current.delete(reg.canvasId);
1091
+ }
1016
1092
  const registry = spectrogramCanvasRegistryRef.current;
1017
1093
  if (!registry.has(reg.clipId)) {
1018
1094
  registry.set(reg.clipId, /* @__PURE__ */ new Map());
@@ -1032,6 +1108,8 @@ var SpectrogramProvider = ({
1032
1108
  [ensureWorkerPool]
1033
1109
  );
1034
1110
  const unregisterSpectrogramCanvas = useCallback((canvasId) => {
1111
+ canvasHeightsRef.current.delete(canvasId);
1112
+ canvasRangesRef.current.delete(canvasId);
1035
1113
  const pool = spectrogramWorkerRef.current;
1036
1114
  if (pool) {
1037
1115
  try {