@dawcore/spectrogram 0.0.2 → 0.0.4

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
@@ -1,14 +1,58 @@
1
1
  # @dawcore/spectrogram
2
2
 
3
- Framework-agnostic spectrogram computation, worker, and viewport-aware rendering orchestrator for the dawcore family.
3
+ Framework-agnostic spectrogram computation, Web Worker, and viewport-aware rendering orchestrator for the dawcore family.
4
4
 
5
- Used by `@dawcore/components` (the Lit web component layer) and `@waveform-playlist/spectrogram` (the React Provider).
5
+ Used by `@dawcore/components` (the Lit web component layer, behind `<daw-track render-mode="spectrogram">`) and `@waveform-playlist/spectrogram` (the React Provider).
6
6
 
7
- ## Exports
7
+ ## Installation
8
8
 
9
- - `computeSpectrogram`, `getColorMap`, `getFrequencyScale` — pure computation
10
- - `createSpectrogramWorker`, `createSpectrogramWorkerPool` — worker factories
11
- - `SpectrogramOrchestrator` (via `./orchestrator` subpath) — viewport/abort/tier/grouping logic
9
+ ```bash
10
+ npm install @dawcore/spectrogram
11
+ ```
12
+
13
+ No peer dependencies. If you use `@dawcore/components`, you don't install this directly — it's a regular dependency of that package.
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { computeSpectrogram, getColorMap } from '@dawcore/spectrogram';
19
+
20
+ // Pure, synchronous FFT — magnitudes per time/frequency bin
21
+ const data = computeSpectrogram(audioBuffer, { fftSize: 2048 });
22
+ const lut = getColorMap('magma');
23
+ ```
24
+
25
+ For rendering inside an app, use the worker and orchestrator instead of blocking the main thread:
26
+
27
+ ```typescript
28
+ import { SpectrogramOrchestrator } from '@dawcore/spectrogram/orchestrator';
29
+
30
+ const orchestrator = new SpectrogramOrchestrator({
31
+ workerFactory: () =>
32
+ new Worker(new URL('@dawcore/spectrogram/worker/spectrogram.worker', import.meta.url), {
33
+ type: 'module',
34
+ }),
35
+ config: { fftSize: 2048 },
36
+ });
37
+
38
+ orchestrator.registerClip(/* audio data */);
39
+ orchestrator.registerCanvas(/* OffscreenCanvas + metadata */);
40
+ orchestrator.setViewport(/* visible range */);
41
+ orchestrator.addEventListener('viewport-ready', (e) => console.log(e));
42
+ ```
43
+
44
+ The orchestrator renders in three tiers (visible viewport first, then a 25% overscan buffer, then the rest during idle time), aborts stale work when the viewport, config, or color map changes, and groups contiguous chunks so non-adjacent regions never trigger one oversized FFT.
45
+
46
+ ## Entry Points
47
+
48
+ - `@dawcore/spectrogram` — `computeSpectrogram`, `computeSpectrogramMono`, `getColorMap`, `getFrequencyScale`, worker factories (`createSpectrogramWorker`, `createSpectrogramWorkerPool`, `SpectrogramAbortError`), and the orchestrator re-exports
49
+ - `@dawcore/spectrogram/worker/spectrogram.worker` — the Worker module URL target (resolve with `new URL(..., import.meta.url)`)
50
+ - `@dawcore/spectrogram/orchestrator` — `SpectrogramOrchestrator` and helpers (`classifyViewport`, `groupContiguousChunks`, `ColorLUTCache`) without the computation/worker graph, for consumers with their own FFT pipeline
51
+
52
+ ## Examples & Documentation
53
+
54
+ - [`examples/dawcore-native/spectrogram.html`](https://github.com/naomiaro/waveform-playlist/tree/main/examples/dawcore-native) — spectrogram tracks in `<daw-editor>` (`pnpm example:dawcore-native`)
55
+ - Guides: [naomiaro.github.io/waveform-playlist](https://naomiaro.github.io/waveform-playlist/)
12
56
 
13
57
  ## License
14
58
 
package/dist/index.d.mts CHANGED
@@ -47,6 +47,51 @@ type FrequencyScaleName = 'linear' | 'logarithmic' | 'mel' | 'bark' | 'erb';
47
47
  */
48
48
  declare function getFrequencyScale(name: FrequencyScaleName): (f: number, minF: number, maxF: number) => number;
49
49
 
50
+ /**
51
+ * Pure geometry for mapping rendered pixel columns to FFT frames.
52
+ */
53
+ interface PixelColumnToFrameParams {
54
+ /** Clip-relative pixel column (chunk pixel offset + x within the canvas). */
55
+ pixelX: number;
56
+ samplesPerPixel: number;
57
+ /** The clip's start position within its audio file (AudioClip.offsetSamples). */
58
+ clipOffsetSamples: number;
59
+ /** File-absolute sample position where the cached FFT data begins. */
60
+ fftStartSample: number;
61
+ hopSize: number;
62
+ }
63
+ /**
64
+ * The file sample displayed at clip-relative pixel x is
65
+ * `clipOffsetSamples + x * samplesPerPixel`; the FFT frame holding it starts
66
+ * at `fftStartSample + frame * hopSize`. Omitting `clipOffsetSamples` (the
67
+ * pre-#554 behavior) shifts every trimmed clip's display by its offset.
68
+ */
69
+ declare function pixelColumnToFrame(params: PixelColumnToFrameParams): number;
70
+ interface PixelRowToBinParams {
71
+ /** Vertical position, bottom-up: `1 - y / canvasHeight` ∈ (0, 1]. */
72
+ normalizedY: number;
73
+ frequencyBinCount: number;
74
+ sampleRate: number;
75
+ minFrequency: number;
76
+ /** Resolved maximum frequency (> 0); callers default 0 → sampleRate / 2. */
77
+ maxFrequency: number;
78
+ /** Frequency scale mapping (Hz, minF, maxF) → [0, 1]. */
79
+ scaleFn: (f: number, minF: number, maxF: number) => number;
80
+ /** Linear scale uses a direct O(1) mapping instead of the binary search. */
81
+ isLinear: boolean;
82
+ }
83
+ /**
84
+ * Map a canvas row to the FFT bin displayed there.
85
+ *
86
+ * Linear: direct inversion of the scale — `freq = minF + normalizedY·(maxF −
87
+ * minF)` — so the configured frequency range is honored exactly like the
88
+ * non-linear scales (pre-#557 the linear fast path always spanned 0..Nyquist).
89
+ * Non-linear: binary search for the first bin whose scaled frequency reaches
90
+ * `normalizedY`. Both paths clamp to the last bin so the top row
91
+ * (normalizedY === 1) never falls out of range.
92
+ */
93
+ declare function pixelRowToBin(params: PixelRowToBinParams): number;
94
+
50
95
  /**
51
96
  * Error thrown when a spectrogram computation is aborted due to a generation change.
52
97
  * Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
@@ -90,6 +135,12 @@ interface SpectrogramWorkerApi {
90
135
  cacheKey: string;
91
136
  }>;
92
137
  renderChunks(params: SpectrogramWorkerRenderChunksParams, generation?: number): Promise<void>;
138
+ /**
139
+ * Cancel in-flight work from generations OLDER than `generation`.
140
+ * Pass the NEW (current) generation after bumping — the worker treats work
141
+ * as stale iff `workGeneration < latestAbortedGeneration`, so passing the
142
+ * previous generation cancels nothing (#555).
143
+ */
93
144
  abortGeneration(generation: number): void;
94
145
  registerCanvas(canvasId: string, canvas: OffscreenCanvas): void;
95
146
  unregisterCanvas(canvasId: string): void;
@@ -135,10 +186,10 @@ interface CanvasMeta {
135
186
  widthPx: number;
136
187
  }
137
188
  interface ViewportBounds {
138
- visibleStartPx: number;
139
- visibleEndPx: number;
140
- bufferStartPx: number;
141
- bufferEndPx: number;
189
+ readonly visibleStartPx: number;
190
+ readonly visibleEndPx: number;
191
+ readonly bufferStartPx: number;
192
+ readonly bufferEndPx: number;
142
193
  }
143
194
  interface ClassifiedTiers<T extends CanvasMeta> {
144
195
  viewport: T[];
@@ -148,33 +199,40 @@ interface ClassifiedTiers<T extends CanvasMeta> {
148
199
  declare function classifyViewport<T extends CanvasMeta>(canvases: T[], bounds: ViewportBounds): ClassifiedTiers<T>;
149
200
 
150
201
  interface SpectrogramOrchestratorOptions {
151
- workerFactory: () => Worker;
152
- workerPoolSize?: number;
153
- config: SpectrogramConfig;
154
- colorMap?: ColorMapValue;
155
- devicePixelRatio?: number;
202
+ readonly workerFactory: () => Worker;
203
+ readonly workerPoolSize?: number;
204
+ readonly config: SpectrogramConfig;
205
+ readonly colorMap?: ColorMapValue;
206
+ readonly devicePixelRatio?: number;
156
207
  }
157
208
  interface ClipRegistration {
158
- clipId: string;
159
- trackId: string;
160
- channelData: Float32Array[];
161
- sampleRate: number;
162
- durationSamples: number;
163
- offsetSamples: number;
209
+ readonly clipId: string;
210
+ readonly trackId: string;
211
+ readonly channelData: ReadonlyArray<Float32Array>;
212
+ readonly sampleRate: number;
213
+ readonly durationSamples: number;
214
+ readonly offsetSamples: number;
164
215
  }
165
216
  interface CanvasRegistration {
166
- canvasId: string;
167
- canvas: OffscreenCanvas;
168
- clipId: string;
169
- trackId: string;
170
- channelIndex: number;
171
- chunkIndex: number;
172
- globalPixelOffset: number;
173
- widthPx: number;
174
- heightPx: number;
217
+ readonly canvasId: string;
218
+ readonly canvas: OffscreenCanvas;
219
+ readonly clipId: string;
220
+ readonly trackId: string;
221
+ readonly channelIndex: number;
222
+ readonly chunkIndex: number;
223
+ /**
224
+ * TIMELINE-absolute pixel where this chunk starts. Layout contract: every
225
+ * canvas of a clip must satisfy
226
+ * `globalPixelOffset === clipOriginPx + chunkIndex * MAX_CANVAS_WIDTH` —
227
+ * renderGroup's clip-relative sample math depends on it (#554), and
228
+ * registerCanvas warns when a registration violates it.
229
+ */
230
+ readonly globalPixelOffset: number;
231
+ readonly widthPx: number;
232
+ readonly heightPx: number;
175
233
  }
176
234
  interface ViewportState extends ViewportBounds {
177
- samplesPerPixel: number;
235
+ readonly samplesPerPixel: number;
178
236
  }
179
237
  interface ClipEntry {
180
238
  trackId: string;
@@ -202,6 +260,11 @@ declare class SpectrogramOrchestrator extends EventTarget {
202
260
  protected colorLUT: ColorLUTCache;
203
261
  protected disposed: boolean;
204
262
  protected renderInFlight: boolean;
263
+ protected renderQueued: boolean;
264
+ protected clipOrigins: Map<string, {
265
+ originPx: number;
266
+ canvasCount: number;
267
+ }>;
205
268
  protected readyDispatched: Set<string>;
206
269
  constructor(opts: SpectrogramOrchestratorOptions);
207
270
  registerClip(reg: ClipRegistration): void;
@@ -212,22 +275,50 @@ declare class SpectrogramOrchestrator extends EventTarget {
212
275
  setConfig(config: SpectrogramConfig): void;
213
276
  setColorMap(colorMap: ColorMapValue): void;
214
277
  setDevicePixelRatio(dpr: number): void;
278
+ /**
279
+ * Start a new render generation and cancel in-flight work from older ones.
280
+ *
281
+ * The worker treats work as stale iff `generation < latestGeneration`, so
282
+ * the abort message must carry the NEW generation — passing the previous
283
+ * one leaves its in-flight FFTs running to completion (#555).
284
+ */
285
+ protected bumpGenerationAndAbortStale(): void;
215
286
  dispose(): void;
287
+ /**
288
+ * Coalesce render requests: `renderInFlight` is held for the WHOLE async
289
+ * render, not just until the microtask fires — a request arriving
290
+ * mid-render (e.g. a late registerCanvas) queues one follow-up run instead
291
+ * of starting a concurrent runRender for the same generation (#558).
292
+ */
216
293
  protected scheduleRender(): void;
294
+ protected runRenderLoop(): Promise<void>;
217
295
  protected runRender(generation: number): Promise<void>;
218
296
  protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
219
297
  protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
298
+ protected renderGroupOnce(group: CanvasEntry[], generation: number, viewport: ViewportState, clip: ClipEntry): Promise<void>;
220
299
  protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
221
300
  protected yieldUntilIdle(): Promise<void>;
222
301
  }
223
302
 
303
+ /**
304
+ * `viewport-ready` fires at most once per `(generation, trackId)` pair after
305
+ * the viewport-tier FFT completes. Generation bumps (setViewport with a real
306
+ * change, setConfig, setColorMap, setDevicePixelRatio) reset the dispatched-set
307
+ * so the event re-fires on the next render. The `generation` field lets
308
+ * consumers correlate ready events to their own state.
309
+ */
224
310
  interface ViewportReadyDetail {
225
- trackId: string;
311
+ readonly trackId: string;
312
+ readonly generation: number;
226
313
  }
227
314
 
228
315
  interface ChunkLike {
229
316
  chunkIndex: number;
230
317
  }
318
+ interface RenderableChunkLike extends ChunkLike {
319
+ clipId: string;
320
+ channelIndex: number;
321
+ }
231
322
  /**
232
323
  * Group a list of chunks into runs of contiguous chunk indices.
233
324
  *
@@ -239,5 +330,17 @@ interface ChunkLike {
239
330
  * two smaller FFTs (10–11, 14–15) totaling ~32K frames.
240
331
  */
241
332
  declare function groupContiguousChunks<T extends ChunkLike>(chunks: T[]): T[][];
333
+ /**
334
+ * Group render canvases into runs of contiguous chunk indices, partitioned by
335
+ * `(clipId, channelIndex)` first.
336
+ *
337
+ * A track's canvases span multiple channels (one <daw-spectrogram> per channel,
338
+ * chunk indices shared) and multiple clips (chunk indices restart at 0 per
339
+ * clip). Grouping by chunk index alone interleaves them — e.g. stereo
340
+ * [ch0k0, ch1k0, ch0k1, ch1k1] yields the mixed group [ch1k0, ch0k1], which
341
+ * renderGroup then routes to a single channel's worker with a single clip's
342
+ * FFT data (issue #553).
343
+ */
344
+ declare function groupRenderableChunks<T extends RenderableChunkLike>(chunks: T[]): T[][];
242
345
 
243
- export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks };
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 };
package/dist/index.d.ts CHANGED
@@ -47,6 +47,51 @@ type FrequencyScaleName = 'linear' | 'logarithmic' | 'mel' | 'bark' | 'erb';
47
47
  */
48
48
  declare function getFrequencyScale(name: FrequencyScaleName): (f: number, minF: number, maxF: number) => number;
49
49
 
50
+ /**
51
+ * Pure geometry for mapping rendered pixel columns to FFT frames.
52
+ */
53
+ interface PixelColumnToFrameParams {
54
+ /** Clip-relative pixel column (chunk pixel offset + x within the canvas). */
55
+ pixelX: number;
56
+ samplesPerPixel: number;
57
+ /** The clip's start position within its audio file (AudioClip.offsetSamples). */
58
+ clipOffsetSamples: number;
59
+ /** File-absolute sample position where the cached FFT data begins. */
60
+ fftStartSample: number;
61
+ hopSize: number;
62
+ }
63
+ /**
64
+ * The file sample displayed at clip-relative pixel x is
65
+ * `clipOffsetSamples + x * samplesPerPixel`; the FFT frame holding it starts
66
+ * at `fftStartSample + frame * hopSize`. Omitting `clipOffsetSamples` (the
67
+ * pre-#554 behavior) shifts every trimmed clip's display by its offset.
68
+ */
69
+ declare function pixelColumnToFrame(params: PixelColumnToFrameParams): number;
70
+ interface PixelRowToBinParams {
71
+ /** Vertical position, bottom-up: `1 - y / canvasHeight` ∈ (0, 1]. */
72
+ normalizedY: number;
73
+ frequencyBinCount: number;
74
+ sampleRate: number;
75
+ minFrequency: number;
76
+ /** Resolved maximum frequency (> 0); callers default 0 → sampleRate / 2. */
77
+ maxFrequency: number;
78
+ /** Frequency scale mapping (Hz, minF, maxF) → [0, 1]. */
79
+ scaleFn: (f: number, minF: number, maxF: number) => number;
80
+ /** Linear scale uses a direct O(1) mapping instead of the binary search. */
81
+ isLinear: boolean;
82
+ }
83
+ /**
84
+ * Map a canvas row to the FFT bin displayed there.
85
+ *
86
+ * Linear: direct inversion of the scale — `freq = minF + normalizedY·(maxF −
87
+ * minF)` — so the configured frequency range is honored exactly like the
88
+ * non-linear scales (pre-#557 the linear fast path always spanned 0..Nyquist).
89
+ * Non-linear: binary search for the first bin whose scaled frequency reaches
90
+ * `normalizedY`. Both paths clamp to the last bin so the top row
91
+ * (normalizedY === 1) never falls out of range.
92
+ */
93
+ declare function pixelRowToBin(params: PixelRowToBinParams): number;
94
+
50
95
  /**
51
96
  * Error thrown when a spectrogram computation is aborted due to a generation change.
52
97
  * Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
@@ -90,6 +135,12 @@ interface SpectrogramWorkerApi {
90
135
  cacheKey: string;
91
136
  }>;
92
137
  renderChunks(params: SpectrogramWorkerRenderChunksParams, generation?: number): Promise<void>;
138
+ /**
139
+ * Cancel in-flight work from generations OLDER than `generation`.
140
+ * Pass the NEW (current) generation after bumping — the worker treats work
141
+ * as stale iff `workGeneration < latestAbortedGeneration`, so passing the
142
+ * previous generation cancels nothing (#555).
143
+ */
93
144
  abortGeneration(generation: number): void;
94
145
  registerCanvas(canvasId: string, canvas: OffscreenCanvas): void;
95
146
  unregisterCanvas(canvasId: string): void;
@@ -135,10 +186,10 @@ interface CanvasMeta {
135
186
  widthPx: number;
136
187
  }
137
188
  interface ViewportBounds {
138
- visibleStartPx: number;
139
- visibleEndPx: number;
140
- bufferStartPx: number;
141
- bufferEndPx: number;
189
+ readonly visibleStartPx: number;
190
+ readonly visibleEndPx: number;
191
+ readonly bufferStartPx: number;
192
+ readonly bufferEndPx: number;
142
193
  }
143
194
  interface ClassifiedTiers<T extends CanvasMeta> {
144
195
  viewport: T[];
@@ -148,33 +199,40 @@ interface ClassifiedTiers<T extends CanvasMeta> {
148
199
  declare function classifyViewport<T extends CanvasMeta>(canvases: T[], bounds: ViewportBounds): ClassifiedTiers<T>;
149
200
 
150
201
  interface SpectrogramOrchestratorOptions {
151
- workerFactory: () => Worker;
152
- workerPoolSize?: number;
153
- config: SpectrogramConfig;
154
- colorMap?: ColorMapValue;
155
- devicePixelRatio?: number;
202
+ readonly workerFactory: () => Worker;
203
+ readonly workerPoolSize?: number;
204
+ readonly config: SpectrogramConfig;
205
+ readonly colorMap?: ColorMapValue;
206
+ readonly devicePixelRatio?: number;
156
207
  }
157
208
  interface ClipRegistration {
158
- clipId: string;
159
- trackId: string;
160
- channelData: Float32Array[];
161
- sampleRate: number;
162
- durationSamples: number;
163
- offsetSamples: number;
209
+ readonly clipId: string;
210
+ readonly trackId: string;
211
+ readonly channelData: ReadonlyArray<Float32Array>;
212
+ readonly sampleRate: number;
213
+ readonly durationSamples: number;
214
+ readonly offsetSamples: number;
164
215
  }
165
216
  interface CanvasRegistration {
166
- canvasId: string;
167
- canvas: OffscreenCanvas;
168
- clipId: string;
169
- trackId: string;
170
- channelIndex: number;
171
- chunkIndex: number;
172
- globalPixelOffset: number;
173
- widthPx: number;
174
- heightPx: number;
217
+ readonly canvasId: string;
218
+ readonly canvas: OffscreenCanvas;
219
+ readonly clipId: string;
220
+ readonly trackId: string;
221
+ readonly channelIndex: number;
222
+ readonly chunkIndex: number;
223
+ /**
224
+ * TIMELINE-absolute pixel where this chunk starts. Layout contract: every
225
+ * canvas of a clip must satisfy
226
+ * `globalPixelOffset === clipOriginPx + chunkIndex * MAX_CANVAS_WIDTH` —
227
+ * renderGroup's clip-relative sample math depends on it (#554), and
228
+ * registerCanvas warns when a registration violates it.
229
+ */
230
+ readonly globalPixelOffset: number;
231
+ readonly widthPx: number;
232
+ readonly heightPx: number;
175
233
  }
176
234
  interface ViewportState extends ViewportBounds {
177
- samplesPerPixel: number;
235
+ readonly samplesPerPixel: number;
178
236
  }
179
237
  interface ClipEntry {
180
238
  trackId: string;
@@ -202,6 +260,11 @@ declare class SpectrogramOrchestrator extends EventTarget {
202
260
  protected colorLUT: ColorLUTCache;
203
261
  protected disposed: boolean;
204
262
  protected renderInFlight: boolean;
263
+ protected renderQueued: boolean;
264
+ protected clipOrigins: Map<string, {
265
+ originPx: number;
266
+ canvasCount: number;
267
+ }>;
205
268
  protected readyDispatched: Set<string>;
206
269
  constructor(opts: SpectrogramOrchestratorOptions);
207
270
  registerClip(reg: ClipRegistration): void;
@@ -212,22 +275,50 @@ declare class SpectrogramOrchestrator extends EventTarget {
212
275
  setConfig(config: SpectrogramConfig): void;
213
276
  setColorMap(colorMap: ColorMapValue): void;
214
277
  setDevicePixelRatio(dpr: number): void;
278
+ /**
279
+ * Start a new render generation and cancel in-flight work from older ones.
280
+ *
281
+ * The worker treats work as stale iff `generation < latestGeneration`, so
282
+ * the abort message must carry the NEW generation — passing the previous
283
+ * one leaves its in-flight FFTs running to completion (#555).
284
+ */
285
+ protected bumpGenerationAndAbortStale(): void;
215
286
  dispose(): void;
287
+ /**
288
+ * Coalesce render requests: `renderInFlight` is held for the WHOLE async
289
+ * render, not just until the microtask fires — a request arriving
290
+ * mid-render (e.g. a late registerCanvas) queues one follow-up run instead
291
+ * of starting a concurrent runRender for the same generation (#558).
292
+ */
216
293
  protected scheduleRender(): void;
294
+ protected runRenderLoop(): Promise<void>;
217
295
  protected runRender(generation: number): Promise<void>;
218
296
  protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
219
297
  protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
298
+ protected renderGroupOnce(group: CanvasEntry[], generation: number, viewport: ViewportState, clip: ClipEntry): Promise<void>;
220
299
  protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
221
300
  protected yieldUntilIdle(): Promise<void>;
222
301
  }
223
302
 
303
+ /**
304
+ * `viewport-ready` fires at most once per `(generation, trackId)` pair after
305
+ * the viewport-tier FFT completes. Generation bumps (setViewport with a real
306
+ * change, setConfig, setColorMap, setDevicePixelRatio) reset the dispatched-set
307
+ * so the event re-fires on the next render. The `generation` field lets
308
+ * consumers correlate ready events to their own state.
309
+ */
224
310
  interface ViewportReadyDetail {
225
- trackId: string;
311
+ readonly trackId: string;
312
+ readonly generation: number;
226
313
  }
227
314
 
228
315
  interface ChunkLike {
229
316
  chunkIndex: number;
230
317
  }
318
+ interface RenderableChunkLike extends ChunkLike {
319
+ clipId: string;
320
+ channelIndex: number;
321
+ }
231
322
  /**
232
323
  * Group a list of chunks into runs of contiguous chunk indices.
233
324
  *
@@ -239,5 +330,17 @@ interface ChunkLike {
239
330
  * two smaller FFTs (10–11, 14–15) totaling ~32K frames.
240
331
  */
241
332
  declare function groupContiguousChunks<T extends ChunkLike>(chunks: T[]): T[][];
333
+ /**
334
+ * Group render canvases into runs of contiguous chunk indices, partitioned by
335
+ * `(clipId, channelIndex)` first.
336
+ *
337
+ * A track's canvases span multiple channels (one <daw-spectrogram> per channel,
338
+ * chunk indices shared) and multiple clips (chunk indices restart at 0 per
339
+ * clip). Grouping by chunk index alone interleaves them — e.g. stereo
340
+ * [ch0k0, ch1k0, ch0k1, ch1k1] yields the mixed group [ch1k0, ch0k1], which
341
+ * renderGroup then routes to a single channel's worker with a single clip's
342
+ * FFT data (issue #553).
343
+ */
344
+ declare function groupRenderableChunks<T extends RenderableChunkLike>(chunks: T[]): T[][];
242
345
 
243
- export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type FrequencyScaleName, SpectrogramAbortError, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type SpectrogramWorkerApi, type SpectrogramWorkerFFTParams, type SpectrogramWorkerRenderChunksParams, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, computeSpectrogram, computeSpectrogramMono, createSpectrogramWorker, createSpectrogramWorkerPool, getColorMap, getFrequencyScale, groupContiguousChunks };
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 };