@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.
@@ -36,6 +36,12 @@ interface SpectrogramWorkerApi {
36
36
  cacheKey: string;
37
37
  }>;
38
38
  renderChunks(params: SpectrogramWorkerRenderChunksParams, generation?: number): Promise<void>;
39
+ /**
40
+ * Cancel in-flight work from generations OLDER than `generation`.
41
+ * Pass the NEW (current) generation after bumping — the worker treats work
42
+ * as stale iff `workGeneration < latestAbortedGeneration`, so passing the
43
+ * previous generation cancels nothing (#555).
44
+ */
39
45
  abortGeneration(generation: number): void;
40
46
  registerCanvas(canvasId: string, canvas: OffscreenCanvas): void;
41
47
  unregisterCanvas(canvasId: string): void;
@@ -66,10 +72,10 @@ interface CanvasMeta {
66
72
  widthPx: number;
67
73
  }
68
74
  interface ViewportBounds {
69
- visibleStartPx: number;
70
- visibleEndPx: number;
71
- bufferStartPx: number;
72
- bufferEndPx: number;
75
+ readonly visibleStartPx: number;
76
+ readonly visibleEndPx: number;
77
+ readonly bufferStartPx: number;
78
+ readonly bufferEndPx: number;
73
79
  }
74
80
  interface ClassifiedTiers<T extends CanvasMeta> {
75
81
  viewport: T[];
@@ -79,33 +85,40 @@ interface ClassifiedTiers<T extends CanvasMeta> {
79
85
  declare function classifyViewport<T extends CanvasMeta>(canvases: T[], bounds: ViewportBounds): ClassifiedTiers<T>;
80
86
 
81
87
  interface SpectrogramOrchestratorOptions {
82
- workerFactory: () => Worker;
83
- workerPoolSize?: number;
84
- config: SpectrogramConfig;
85
- colorMap?: ColorMapValue;
86
- devicePixelRatio?: number;
88
+ readonly workerFactory: () => Worker;
89
+ readonly workerPoolSize?: number;
90
+ readonly config: SpectrogramConfig;
91
+ readonly colorMap?: ColorMapValue;
92
+ readonly devicePixelRatio?: number;
87
93
  }
88
94
  interface ClipRegistration {
89
- clipId: string;
90
- trackId: string;
91
- channelData: Float32Array[];
92
- sampleRate: number;
93
- durationSamples: number;
94
- offsetSamples: number;
95
+ readonly clipId: string;
96
+ readonly trackId: string;
97
+ readonly channelData: ReadonlyArray<Float32Array>;
98
+ readonly sampleRate: number;
99
+ readonly durationSamples: number;
100
+ readonly offsetSamples: number;
95
101
  }
96
102
  interface CanvasRegistration {
97
- canvasId: string;
98
- canvas: OffscreenCanvas;
99
- clipId: string;
100
- trackId: string;
101
- channelIndex: number;
102
- chunkIndex: number;
103
- globalPixelOffset: number;
104
- widthPx: number;
105
- heightPx: number;
103
+ readonly canvasId: string;
104
+ readonly canvas: OffscreenCanvas;
105
+ readonly clipId: string;
106
+ readonly trackId: string;
107
+ readonly channelIndex: number;
108
+ readonly chunkIndex: number;
109
+ /**
110
+ * TIMELINE-absolute pixel where this chunk starts. Layout contract: every
111
+ * canvas of a clip must satisfy
112
+ * `globalPixelOffset === clipOriginPx + chunkIndex * MAX_CANVAS_WIDTH` —
113
+ * renderGroup's clip-relative sample math depends on it (#554), and
114
+ * registerCanvas warns when a registration violates it.
115
+ */
116
+ readonly globalPixelOffset: number;
117
+ readonly widthPx: number;
118
+ readonly heightPx: number;
106
119
  }
107
120
  interface ViewportState extends ViewportBounds {
108
- samplesPerPixel: number;
121
+ readonly samplesPerPixel: number;
109
122
  }
110
123
  interface ClipEntry {
111
124
  trackId: string;
@@ -133,6 +146,11 @@ declare class SpectrogramOrchestrator extends EventTarget {
133
146
  protected colorLUT: ColorLUTCache;
134
147
  protected disposed: boolean;
135
148
  protected renderInFlight: boolean;
149
+ protected renderQueued: boolean;
150
+ protected clipOrigins: Map<string, {
151
+ originPx: number;
152
+ canvasCount: number;
153
+ }>;
136
154
  protected readyDispatched: Set<string>;
137
155
  constructor(opts: SpectrogramOrchestratorOptions);
138
156
  registerClip(reg: ClipRegistration): void;
@@ -143,22 +161,50 @@ declare class SpectrogramOrchestrator extends EventTarget {
143
161
  setConfig(config: SpectrogramConfig): void;
144
162
  setColorMap(colorMap: ColorMapValue): void;
145
163
  setDevicePixelRatio(dpr: number): void;
164
+ /**
165
+ * Start a new render generation and cancel in-flight work from older ones.
166
+ *
167
+ * The worker treats work as stale iff `generation < latestGeneration`, so
168
+ * the abort message must carry the NEW generation — passing the previous
169
+ * one leaves its in-flight FFTs running to completion (#555).
170
+ */
171
+ protected bumpGenerationAndAbortStale(): void;
146
172
  dispose(): void;
173
+ /**
174
+ * Coalesce render requests: `renderInFlight` is held for the WHOLE async
175
+ * render, not just until the microtask fires — a request arriving
176
+ * mid-render (e.g. a late registerCanvas) queues one follow-up run instead
177
+ * of starting a concurrent runRender for the same generation (#558).
178
+ */
147
179
  protected scheduleRender(): void;
180
+ protected runRenderLoop(): Promise<void>;
148
181
  protected runRender(generation: number): Promise<void>;
149
182
  protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
150
183
  protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
184
+ protected renderGroupOnce(group: CanvasEntry[], generation: number, viewport: ViewportState, clip: ClipEntry): Promise<void>;
151
185
  protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
152
186
  protected yieldUntilIdle(): Promise<void>;
153
187
  }
154
188
 
189
+ /**
190
+ * `viewport-ready` fires at most once per `(generation, trackId)` pair after
191
+ * the viewport-tier FFT completes. Generation bumps (setViewport with a real
192
+ * change, setConfig, setColorMap, setDevicePixelRatio) reset the dispatched-set
193
+ * so the event re-fires on the next render. The `generation` field lets
194
+ * consumers correlate ready events to their own state.
195
+ */
155
196
  interface ViewportReadyDetail {
156
- trackId: string;
197
+ readonly trackId: string;
198
+ readonly generation: number;
157
199
  }
158
200
 
159
201
  interface ChunkLike {
160
202
  chunkIndex: number;
161
203
  }
204
+ interface RenderableChunkLike extends ChunkLike {
205
+ clipId: string;
206
+ channelIndex: number;
207
+ }
162
208
  /**
163
209
  * Group a list of chunks into runs of contiguous chunk indices.
164
210
  *
@@ -170,5 +216,17 @@ interface ChunkLike {
170
216
  * two smaller FFTs (10–11, 14–15) totaling ~32K frames.
171
217
  */
172
218
  declare function groupContiguousChunks<T extends ChunkLike>(chunks: T[]): T[][];
219
+ /**
220
+ * Group render canvases into runs of contiguous chunk indices, partitioned by
221
+ * `(clipId, channelIndex)` first.
222
+ *
223
+ * A track's canvases span multiple channels (one <daw-spectrogram> per channel,
224
+ * chunk indices shared) and multiple clips (chunk indices restart at 0 per
225
+ * clip). Grouping by chunk index alone interleaves them — e.g. stereo
226
+ * [ch0k0, ch1k0, ch0k1, ch1k1] yields the mixed group [ch1k0, ch0k1], which
227
+ * renderGroup then routes to a single channel's worker with a single clip's
228
+ * FFT data (issue #553).
229
+ */
230
+ declare function groupRenderableChunks<T extends RenderableChunkLike>(chunks: T[]): T[][];
173
231
 
174
- export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, groupContiguousChunks };
232
+ export { type CanvasMeta, type CanvasRegistration, type ChunkLike, type ClassifiedTiers, type ClipRegistration, ColorLUTCache, type RenderableChunkLike, SpectrogramOrchestrator, type SpectrogramOrchestratorOptions, type ViewportBounds, type ViewportReadyDetail, type ViewportState, classifyViewport, groupContiguousChunks, groupRenderableChunks };