@dawcore/spectrogram 0.0.4 → 0.0.5

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.
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  SPECTROGRAM_DEFAULTS,
4
4
  DEFAULT_SPECTROGRAM_COLOR_MAP,
5
- MAX_CANVAS_WIDTH
5
+ MAX_CANVAS_WIDTH as MAX_CANVAS_WIDTH2
6
6
  } from "@waveform-playlist/core";
7
7
 
8
8
  // src/worker/createSpectrogramWorker.ts
@@ -146,15 +146,16 @@ function createSpectrogramWorker(worker) {
146
146
  }
147
147
 
148
148
  // src/worker/createSpectrogramWorkerPool.ts
149
+ import { parseSpectrogramCanvasId } from "@waveform-playlist/core";
149
150
  function parseChannelFromCanvasId(canvasId) {
150
- const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
151
- if (!match) {
151
+ const parsed = parseSpectrogramCanvasId(canvasId);
152
+ if (!parsed) {
152
153
  console.warn(
153
154
  "[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
154
155
  );
155
156
  return 0;
156
157
  }
157
- return parseInt(match[1], 10);
158
+ return parsed.channelIndex;
158
159
  }
159
160
  function defaultPoolSize() {
160
161
  return 2;
@@ -303,6 +304,27 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
303
304
  };
304
305
  }
305
306
 
307
+ // src/computation/fftSampleRange.ts
308
+ import { MAX_CANVAS_WIDTH } from "@waveform-playlist/core";
309
+ function computePaddedFftRange({
310
+ chunks,
311
+ fftSize,
312
+ offsetSamples,
313
+ durationSamples,
314
+ samplesPerPixel
315
+ }) {
316
+ const startPx = Math.min(...chunks.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH));
317
+ const endPx = Math.max(...chunks.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH + c.widthPx));
318
+ const startSample = offsetSamples + Math.floor(startPx * samplesPerPixel);
319
+ const endSample = Math.min(
320
+ offsetSamples + durationSamples,
321
+ offsetSamples + Math.ceil(endPx * samplesPerPixel)
322
+ );
323
+ const paddedStart = Math.max(offsetSamples, startSample - fftSize);
324
+ const paddedEnd = Math.min(offsetSamples + durationSamples, endSample + fftSize);
325
+ return { paddedStart, paddedEnd };
326
+ }
327
+
306
328
  // src/computation/colorMaps.ts
307
329
  function interpolateLUT(stops) {
308
330
  if (stops.length === 0) {
@@ -3603,7 +3625,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3603
3625
  channelIndex: reg.channelIndex,
3604
3626
  chunkIndex: reg.chunkIndex
3605
3627
  });
3606
- const originPx = reg.globalPixelOffset - reg.chunkIndex * MAX_CANVAS_WIDTH;
3628
+ const originPx = reg.globalPixelOffset - reg.chunkIndex * MAX_CANVAS_WIDTH2;
3607
3629
  const origin = this.clipOrigins.get(reg.clipId);
3608
3630
  if (!origin) {
3609
3631
  this.clipOrigins.set(reg.clipId, { originPx, canvasCount: 1 });
@@ -3611,7 +3633,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3611
3633
  origin.canvasCount += 1;
3612
3634
  if (origin.originPx !== originPx) {
3613
3635
  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"
3636
+ "[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_WIDTH2 + "px and this clip will render shifted audio"
3615
3637
  );
3616
3638
  }
3617
3639
  }
@@ -3803,16 +3825,14 @@ var SpectrogramOrchestrator = class extends EventTarget {
3803
3825
  async renderGroupOnce(group, generation, viewport, clip) {
3804
3826
  const first = group[0];
3805
3827
  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));
3809
- const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
3810
- const endSample = Math.min(
3811
- clip.offsetSamples + clip.durationSamples,
3812
- clip.offsetSamples + Math.ceil(endPx * viewport.samplesPerPixel)
3813
- );
3814
- const paddedStart = Math.max(clip.offsetSamples, startSample - fftSize);
3815
- const paddedEnd = Math.min(clip.offsetSamples + clip.durationSamples, endSample + fftSize);
3828
+ const clipRelativeOffsets = group.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH2);
3829
+ const { paddedStart, paddedEnd } = computePaddedFftRange({
3830
+ chunks: group.map((c) => ({ chunkIndex: c.chunkIndex, widthPx: c.widthPx })),
3831
+ fftSize,
3832
+ offsetSamples: clip.offsetSamples,
3833
+ durationSamples: clip.durationSamples,
3834
+ samplesPerPixel: viewport.samplesPerPixel
3835
+ });
3816
3836
  const { cacheKey: cacheKey2 } = await this.pool.computeFFT(
3817
3837
  {
3818
3838
  clipId: first.clipId,