@dawcore/spectrogram 0.0.4 → 0.0.6

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
@@ -3419,6 +3419,27 @@ function pixelRowToBin(params) {
3419
3419
  return Math.min(bin, frequencyBinCount - 1);
3420
3420
  }
3421
3421
 
3422
+ // src/computation/fftSampleRange.ts
3423
+ import { MAX_CANVAS_WIDTH } from "@waveform-playlist/core";
3424
+ function computePaddedFftRange({
3425
+ chunks,
3426
+ fftSize,
3427
+ offsetSamples,
3428
+ durationSamples,
3429
+ samplesPerPixel
3430
+ }) {
3431
+ const startPx = Math.min(...chunks.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH));
3432
+ const endPx = Math.max(...chunks.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH + c.widthPx));
3433
+ const startSample = offsetSamples + Math.floor(startPx * samplesPerPixel);
3434
+ const endSample = Math.min(
3435
+ offsetSamples + durationSamples,
3436
+ offsetSamples + Math.ceil(endPx * samplesPerPixel)
3437
+ );
3438
+ const paddedStart = Math.max(offsetSamples, startSample - fftSize);
3439
+ const paddedEnd = Math.min(offsetSamples + durationSamples, endSample + fftSize);
3440
+ return { paddedStart, paddedEnd };
3441
+ }
3442
+
3422
3443
  // src/worker/createSpectrogramWorker.ts
3423
3444
  var SpectrogramAbortError = class extends Error {
3424
3445
  constructor() {
@@ -3560,15 +3581,16 @@ function createSpectrogramWorker(worker) {
3560
3581
  }
3561
3582
 
3562
3583
  // src/worker/createSpectrogramWorkerPool.ts
3584
+ import { parseSpectrogramCanvasId } from "@waveform-playlist/core";
3563
3585
  function parseChannelFromCanvasId(canvasId) {
3564
- const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
3565
- if (!match) {
3586
+ const parsed = parseSpectrogramCanvasId(canvasId);
3587
+ if (!parsed) {
3566
3588
  console.warn(
3567
3589
  "[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
3568
3590
  );
3569
3591
  return 0;
3570
3592
  }
3571
- return parseInt(match[1], 10);
3593
+ return parsed.channelIndex;
3572
3594
  }
3573
3595
  function defaultPoolSize() {
3574
3596
  return 2;
@@ -3721,7 +3743,7 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3721
3743
  import {
3722
3744
  SPECTROGRAM_DEFAULTS,
3723
3745
  DEFAULT_SPECTROGRAM_COLOR_MAP,
3724
- MAX_CANVAS_WIDTH
3746
+ MAX_CANVAS_WIDTH as MAX_CANVAS_WIDTH2
3725
3747
  } from "@waveform-playlist/core";
3726
3748
 
3727
3749
  // src/orchestrator/color-lut-cache.ts
@@ -3879,7 +3901,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3879
3901
  channelIndex: reg.channelIndex,
3880
3902
  chunkIndex: reg.chunkIndex
3881
3903
  });
3882
- const originPx = reg.globalPixelOffset - reg.chunkIndex * MAX_CANVAS_WIDTH;
3904
+ const originPx = reg.globalPixelOffset - reg.chunkIndex * MAX_CANVAS_WIDTH2;
3883
3905
  const origin = this.clipOrigins.get(reg.clipId);
3884
3906
  if (!origin) {
3885
3907
  this.clipOrigins.set(reg.clipId, { originPx, canvasCount: 1 });
@@ -3887,7 +3909,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3887
3909
  origin.canvasCount += 1;
3888
3910
  if (origin.originPx !== originPx) {
3889
3911
  console.warn(
3890
- "[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"
3912
+ "[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"
3891
3913
  );
3892
3914
  }
3893
3915
  }
@@ -4079,16 +4101,14 @@ var SpectrogramOrchestrator = class extends EventTarget {
4079
4101
  async renderGroupOnce(group, generation, viewport, clip) {
4080
4102
  const first = group[0];
4081
4103
  const fftSize = this.config.fftSize ?? SPECTROGRAM_DEFAULTS.fftSize;
4082
- const clipRelativeOffsets = group.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH);
4083
- const startPx = Math.min(...clipRelativeOffsets);
4084
- const endPx = Math.max(...group.map((c, i) => clipRelativeOffsets[i] + c.widthPx));
4085
- const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
4086
- const endSample = Math.min(
4087
- clip.offsetSamples + clip.durationSamples,
4088
- clip.offsetSamples + Math.ceil(endPx * viewport.samplesPerPixel)
4089
- );
4090
- const paddedStart = Math.max(clip.offsetSamples, startSample - fftSize);
4091
- const paddedEnd = Math.min(clip.offsetSamples + clip.durationSamples, endSample + fftSize);
4104
+ const clipRelativeOffsets = group.map((c) => c.chunkIndex * MAX_CANVAS_WIDTH2);
4105
+ const { paddedStart, paddedEnd } = computePaddedFftRange({
4106
+ chunks: group.map((c) => ({ chunkIndex: c.chunkIndex, widthPx: c.widthPx })),
4107
+ fftSize,
4108
+ offsetSamples: clip.offsetSamples,
4109
+ durationSamples: clip.durationSamples,
4110
+ samplesPerPixel: viewport.samplesPerPixel
4111
+ });
4092
4112
  const { cacheKey: cacheKey2 } = await this.pool.computeFFT(
4093
4113
  {
4094
4114
  clipId: first.clipId,
@@ -4161,6 +4181,7 @@ export {
4161
4181
  SpectrogramAbortError,
4162
4182
  SpectrogramOrchestrator,
4163
4183
  classifyViewport,
4184
+ computePaddedFftRange,
4164
4185
  computeSpectrogram,
4165
4186
  computeSpectrogramMono,
4166
4187
  createSpectrogramWorker,