@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/README.md CHANGED
@@ -45,7 +45,7 @@ The orchestrator renders in three tiers (visible viewport first, then a 25% over
45
45
 
46
46
  ## Entry Points
47
47
 
48
- - `@dawcore/spectrogram` — `computeSpectrogram`, `computeSpectrogramMono`, `getColorMap`, `getFrequencyScale`, worker factories (`createSpectrogramWorker`, `createSpectrogramWorkerPool`, `SpectrogramAbortError`), and the orchestrator re-exports
48
+ - `@dawcore/spectrogram` — `computeSpectrogram`, `computeSpectrogramMono`, `getColorMap`, `getFrequencyScale`, `computePaddedFftRange` (pure window-padded, clip-clamped FFT sample-range math — shared with the React provider's chunk-range computation so both pipelines can't drift), worker factories (`createSpectrogramWorker`, `createSpectrogramWorkerPool`, `SpectrogramAbortError`), and the orchestrator re-exports
49
49
  - `@dawcore/spectrogram/worker/spectrogram.worker` — the Worker module URL target (resolve with `new URL(..., import.meta.url)`)
50
50
  - `@dawcore/spectrogram/orchestrator` — `SpectrogramOrchestrator` and helpers (`classifyViewport`, `groupContiguousChunks`, `ColorLUTCache`) without the computation/worker graph, for consumers with their own FFT pipeline
51
51
 
package/dist/index.d.mts CHANGED
@@ -92,6 +92,38 @@ interface PixelRowToBinParams {
92
92
  */
93
93
  declare function pixelRowToBin(params: PixelRowToBinParams): number;
94
94
 
95
+ /**
96
+ * Pure math for the window-padded, clip-clamped sample range covered by a set of
97
+ * render chunks.
98
+ *
99
+ * Shared by `SpectrogramOrchestrator.renderGroupOnce` and the React
100
+ * `computeChunkSampleRange` so both FFT pipelines compute the *same* range for the
101
+ * same chunks — a padding/clamping change to one used to silently diverge the two
102
+ * (the #554 drift class, with no cross-package test to catch it).
103
+ *
104
+ * Computing per-batch ranges on demand avoids allocating one giant FFT array for a
105
+ * full clip (which OOMs on 1hr+ files). The range is padded by one FFT window on
106
+ * each side to avoid edge artifacts, clamped to the clip's sample bounds.
107
+ */
108
+ interface PaddedFftRangeParams {
109
+ /** Chunks in the render group: clip-relative chunk index + rendered pixel width. */
110
+ chunks: Array<{
111
+ chunkIndex: number;
112
+ widthPx: number;
113
+ }>;
114
+ /** FFT window size (samples) used to pad the range against edge artifacts. */
115
+ fftSize: number;
116
+ /** The clip's start position within its audio file (AudioClip.offsetSamples). */
117
+ offsetSamples: number;
118
+ /** The clip's duration (samples). */
119
+ durationSamples: number;
120
+ samplesPerPixel: number;
121
+ }
122
+ declare function computePaddedFftRange({ chunks, fftSize, offsetSamples, durationSamples, samplesPerPixel, }: PaddedFftRangeParams): {
123
+ paddedStart: number;
124
+ paddedEnd: number;
125
+ };
126
+
95
127
  /**
96
128
  * Error thrown when a spectrogram computation is aborted due to a generation change.
97
129
  * Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
@@ -343,4 +375,4 @@ declare function groupContiguousChunks<T extends ChunkLike>(chunks: T[]): T[][];
343
375
  */
344
376
  declare function groupRenderableChunks<T extends RenderableChunkLike>(chunks: T[]): T[][];
345
377
 
346
- export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, type PixelColumnToFrameParams, type PixelRowToBinParams, type RenderableChunkLike, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks, groupRenderableChunks, pixelColumnToFrame, pixelRowToBin };
378
+ export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, type PaddedFftRangeParams, type PixelColumnToFrameParams, type PixelRowToBinParams, type RenderableChunkLike, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computePaddedFftRange, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks, groupRenderableChunks, pixelColumnToFrame, pixelRowToBin };
package/dist/index.d.ts CHANGED
@@ -92,6 +92,38 @@ interface PixelRowToBinParams {
92
92
  */
93
93
  declare function pixelRowToBin(params: PixelRowToBinParams): number;
94
94
 
95
+ /**
96
+ * Pure math for the window-padded, clip-clamped sample range covered by a set of
97
+ * render chunks.
98
+ *
99
+ * Shared by `SpectrogramOrchestrator.renderGroupOnce` and the React
100
+ * `computeChunkSampleRange` so both FFT pipelines compute the *same* range for the
101
+ * same chunks — a padding/clamping change to one used to silently diverge the two
102
+ * (the #554 drift class, with no cross-package test to catch it).
103
+ *
104
+ * Computing per-batch ranges on demand avoids allocating one giant FFT array for a
105
+ * full clip (which OOMs on 1hr+ files). The range is padded by one FFT window on
106
+ * each side to avoid edge artifacts, clamped to the clip's sample bounds.
107
+ */
108
+ interface PaddedFftRangeParams {
109
+ /** Chunks in the render group: clip-relative chunk index + rendered pixel width. */
110
+ chunks: Array<{
111
+ chunkIndex: number;
112
+ widthPx: number;
113
+ }>;
114
+ /** FFT window size (samples) used to pad the range against edge artifacts. */
115
+ fftSize: number;
116
+ /** The clip's start position within its audio file (AudioClip.offsetSamples). */
117
+ offsetSamples: number;
118
+ /** The clip's duration (samples). */
119
+ durationSamples: number;
120
+ samplesPerPixel: number;
121
+ }
122
+ declare function computePaddedFftRange({ chunks, fftSize, offsetSamples, durationSamples, samplesPerPixel, }: PaddedFftRangeParams): {
123
+ paddedStart: number;
124
+ paddedEnd: number;
125
+ };
126
+
95
127
  /**
96
128
  * Error thrown when a spectrogram computation is aborted due to a generation change.
97
129
  * Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
@@ -343,4 +375,4 @@ declare function groupContiguousChunks<T extends ChunkLike>(chunks: T[]): T[][];
343
375
  */
344
376
  declare function groupRenderableChunks<T extends RenderableChunkLike>(chunks: T[]): T[][];
345
377
 
346
- export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, type PixelColumnToFrameParams, type PixelRowToBinParams, type RenderableChunkLike, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks, groupRenderableChunks, pixelColumnToFrame, pixelRowToBin };
378
+ export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, type PaddedFftRangeParams, type PixelColumnToFrameParams, type PixelRowToBinParams, type RenderableChunkLike, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computePaddedFftRange, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks, groupRenderableChunks, pixelColumnToFrame, pixelRowToBin };
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ __export(src_exports, {
34
34
  SpectrogramAbortError: () => SpectrogramAbortError,
35
35
  SpectrogramOrchestrator: () => SpectrogramOrchestrator,
36
36
  classifyViewport: () => classifyViewport,
37
+ computePaddedFftRange: () => computePaddedFftRange,
37
38
  computeSpectrogram: () => computeSpectrogram,
38
39
  computeSpectrogramMono: () => computeSpectrogramMono,
39
40
  createSpectrogramWorker: () => createSpectrogramWorker,
@@ -3468,6 +3469,27 @@ function pixelRowToBin(params) {
3468
3469
  return Math.min(bin, frequencyBinCount - 1);
3469
3470
  }
3470
3471
 
3472
+ // src/computation/fftSampleRange.ts
3473
+ var import_core = require("@waveform-playlist/core");
3474
+ function computePaddedFftRange({
3475
+ chunks,
3476
+ fftSize,
3477
+ offsetSamples,
3478
+ durationSamples,
3479
+ samplesPerPixel
3480
+ }) {
3481
+ const startPx = Math.min(...chunks.map((c) => c.chunkIndex * import_core.MAX_CANVAS_WIDTH));
3482
+ const endPx = Math.max(...chunks.map((c) => c.chunkIndex * import_core.MAX_CANVAS_WIDTH + c.widthPx));
3483
+ const startSample = offsetSamples + Math.floor(startPx * samplesPerPixel);
3484
+ const endSample = Math.min(
3485
+ offsetSamples + durationSamples,
3486
+ offsetSamples + Math.ceil(endPx * samplesPerPixel)
3487
+ );
3488
+ const paddedStart = Math.max(offsetSamples, startSample - fftSize);
3489
+ const paddedEnd = Math.min(offsetSamples + durationSamples, endSample + fftSize);
3490
+ return { paddedStart, paddedEnd };
3491
+ }
3492
+
3471
3493
  // src/worker/createSpectrogramWorker.ts
3472
3494
  var SpectrogramAbortError = class extends Error {
3473
3495
  constructor() {
@@ -3609,15 +3631,16 @@ function createSpectrogramWorker(worker) {
3609
3631
  }
3610
3632
 
3611
3633
  // src/worker/createSpectrogramWorkerPool.ts
3634
+ var import_core2 = require("@waveform-playlist/core");
3612
3635
  function parseChannelFromCanvasId(canvasId) {
3613
- const match = canvasId.match(/-ch(\d+)-chunk\d+$/);
3614
- if (!match) {
3636
+ const parsed = (0, import_core2.parseSpectrogramCanvasId)(canvasId);
3637
+ if (!parsed) {
3615
3638
  console.warn(
3616
3639
  "[dawcore-spectrogram] canvas ID missing -ch{N}-chunk{M} suffix, routing to worker 0: " + canvasId
3617
3640
  );
3618
3641
  return 0;
3619
3642
  }
3620
- return parseInt(match[1], 10);
3643
+ return parsed.channelIndex;
3621
3644
  }
3622
3645
  function defaultPoolSize() {
3623
3646
  return 2;
@@ -3767,7 +3790,7 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3767
3790
  }
3768
3791
 
3769
3792
  // src/orchestrator/SpectrogramOrchestrator.ts
3770
- var import_core = require("@waveform-playlist/core");
3793
+ var import_core3 = require("@waveform-playlist/core");
3771
3794
 
3772
3795
  // src/orchestrator/color-lut-cache.ts
3773
3796
  var ColorLUTCache = class {
@@ -3875,7 +3898,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3875
3898
  const poolSize = opts.workerPoolSize ?? 2;
3876
3899
  this.pool = createSpectrogramWorkerPool(opts.workerFactory, poolSize);
3877
3900
  this.config = opts.config;
3878
- this.colorMap = opts.colorMap ?? import_core.DEFAULT_SPECTROGRAM_COLOR_MAP;
3901
+ this.colorMap = opts.colorMap ?? import_core3.DEFAULT_SPECTROGRAM_COLOR_MAP;
3879
3902
  this.devicePixelRatio = opts.devicePixelRatio ?? (typeof window !== "undefined" ? window.devicePixelRatio : 1);
3880
3903
  }
3881
3904
  registerClip(reg) {
@@ -3924,7 +3947,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3924
3947
  channelIndex: reg.channelIndex,
3925
3948
  chunkIndex: reg.chunkIndex
3926
3949
  });
3927
- const originPx = reg.globalPixelOffset - reg.chunkIndex * import_core.MAX_CANVAS_WIDTH;
3950
+ const originPx = reg.globalPixelOffset - reg.chunkIndex * import_core3.MAX_CANVAS_WIDTH;
3928
3951
  const origin = this.clipOrigins.get(reg.clipId);
3929
3952
  if (!origin) {
3930
3953
  this.clipOrigins.set(reg.clipId, { originPx, canvasCount: 1 });
@@ -3932,7 +3955,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3932
3955
  origin.canvasCount += 1;
3933
3956
  if (origin.originPx !== originPx) {
3934
3957
  console.warn(
3935
- "[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 * " + import_core.MAX_CANVAS_WIDTH + "px and this clip will render shifted audio"
3958
+ "[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 * " + import_core3.MAX_CANVAS_WIDTH + "px and this clip will render shifted audio"
3936
3959
  );
3937
3960
  }
3938
3961
  }
@@ -4123,17 +4146,15 @@ var SpectrogramOrchestrator = class extends EventTarget {
4123
4146
  }
4124
4147
  async renderGroupOnce(group, generation, viewport, clip) {
4125
4148
  const first = group[0];
4126
- const fftSize = this.config.fftSize ?? import_core.SPECTROGRAM_DEFAULTS.fftSize;
4127
- const clipRelativeOffsets = group.map((c) => c.chunkIndex * import_core.MAX_CANVAS_WIDTH);
4128
- const startPx = Math.min(...clipRelativeOffsets);
4129
- const endPx = Math.max(...group.map((c, i) => clipRelativeOffsets[i] + c.widthPx));
4130
- const startSample = clip.offsetSamples + Math.floor(startPx * viewport.samplesPerPixel);
4131
- const endSample = Math.min(
4132
- clip.offsetSamples + clip.durationSamples,
4133
- clip.offsetSamples + Math.ceil(endPx * viewport.samplesPerPixel)
4134
- );
4135
- const paddedStart = Math.max(clip.offsetSamples, startSample - fftSize);
4136
- const paddedEnd = Math.min(clip.offsetSamples + clip.durationSamples, endSample + fftSize);
4149
+ const fftSize = this.config.fftSize ?? import_core3.SPECTROGRAM_DEFAULTS.fftSize;
4150
+ const clipRelativeOffsets = group.map((c) => c.chunkIndex * import_core3.MAX_CANVAS_WIDTH);
4151
+ const { paddedStart, paddedEnd } = computePaddedFftRange({
4152
+ chunks: group.map((c) => ({ chunkIndex: c.chunkIndex, widthPx: c.widthPx })),
4153
+ fftSize,
4154
+ offsetSamples: clip.offsetSamples,
4155
+ durationSamples: clip.durationSamples,
4156
+ samplesPerPixel: viewport.samplesPerPixel
4157
+ });
4137
4158
  const { cacheKey: cacheKey2 } = await this.pool.computeFFT(
4138
4159
  {
4139
4160
  clipId: first.clipId,
@@ -4159,11 +4180,11 @@ var SpectrogramOrchestrator = class extends EventTarget {
4159
4180
  devicePixelRatio: this.devicePixelRatio,
4160
4181
  samplesPerPixel: viewport.samplesPerPixel,
4161
4182
  colorLUT,
4162
- frequencyScale: String(this.config.frequencyScale ?? import_core.SPECTROGRAM_DEFAULTS.frequencyScale),
4163
- minFrequency: this.config.minFrequency ?? import_core.SPECTROGRAM_DEFAULTS.minFrequency,
4183
+ frequencyScale: String(this.config.frequencyScale ?? import_core3.SPECTROGRAM_DEFAULTS.frequencyScale),
4184
+ minFrequency: this.config.minFrequency ?? import_core3.SPECTROGRAM_DEFAULTS.minFrequency,
4164
4185
  maxFrequency: this.config.maxFrequency ?? clip.sampleRate / 2,
4165
- gainDb: this.config.gainDb ?? import_core.SPECTROGRAM_DEFAULTS.gainDb,
4166
- rangeDb: this.config.rangeDb ?? import_core.SPECTROGRAM_DEFAULTS.rangeDb,
4186
+ gainDb: this.config.gainDb ?? import_core3.SPECTROGRAM_DEFAULTS.gainDb,
4187
+ rangeDb: this.config.rangeDb ?? import_core3.SPECTROGRAM_DEFAULTS.rangeDb,
4167
4188
  channelIndex: first.channelIndex
4168
4189
  },
4169
4190
  generation
@@ -4207,6 +4228,7 @@ function viewportsEqual(a, b) {
4207
4228
  SpectrogramAbortError,
4208
4229
  SpectrogramOrchestrator,
4209
4230
  classifyViewport,
4231
+ computePaddedFftRange,
4210
4232
  computeSpectrogram,
4211
4233
  computeSpectrogramMono,
4212
4234
  createSpectrogramWorker,