@dawcore/spectrogram 0.0.9-next.0 → 0.0.9-next.10

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.
@@ -12,7 +12,13 @@ interface SpectrogramWorkerFFTParams {
12
12
  start: number;
13
13
  end: number;
14
14
  };
15
- /** If set, compute only this channel index (used by worker pool). */
15
+ /**
16
+ * If set, compute only the channel at this index of the arrays the worker
17
+ * has for the clip. A worker with one array computes that array whatever
18
+ * this says. The pool gives each worker its one channel at index 0, or
19
+ * (worker 0, registered audio) every channel, and its multi-channel fan-out
20
+ * asks for index 0 (a mono request sends no filter) (#925).
21
+ */
16
22
  channelFilter?: number;
17
23
  }
18
24
  interface SpectrogramWorkerRenderChunksParams {
@@ -47,6 +53,15 @@ interface SpectrogramWorkerApi {
47
53
  unregisterCanvas(canvasId: string): void;
48
54
  registerAudioData(clipId: string, channelDataArrays: Float32Array[], sampleRate: number): void;
49
55
  unregisterAudioData(clipId: string): void;
56
+ /**
57
+ * The ids whose audio is registered now: registered and not unregistered
58
+ * since. An id is whatever the caller registered the audio under (a clip id,
59
+ * or a key per AudioBuffer, as the React provider uses). Empty once
60
+ * terminated; a register after that is ignored. A new array on every call.
61
+ * A caller that pre-registers audio asks this instead of keeping its own
62
+ * list, which would outlive a terminated worker (#821).
63
+ */
64
+ registeredAudioClipIds(): string[];
50
65
  terminate(): void;
51
66
  }
52
67
 
@@ -179,8 +194,15 @@ declare class SpectrogramOrchestrator extends EventTarget {
179
194
  protected scheduleRender(): void;
180
195
  protected runRenderLoop(): Promise<void>;
181
196
  protected runRender(generation: number): Promise<void>;
182
- protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
183
- protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
197
+ /** Renders the tier's groups; false when a group waits for a render already scheduled. */
198
+ protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<boolean>;
199
+ /**
200
+ * Renders one group. Returns false when its computation was dropped and
201
+ * another pass is queued (registerClip queues one for a clip registered again
202
+ * mid-computation that still has canvases, #928): that pass draws it. True
203
+ * otherwise, also when there is nothing to draw or nothing will draw it.
204
+ */
205
+ protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<boolean>;
184
206
  protected renderGroupOnce(group: CanvasEntry[], generation: number, viewport: ViewportState, clip: ClipEntry): Promise<void>;
185
207
  protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
186
208
  protected yieldUntilIdle(): Promise<void>;
@@ -122,6 +122,7 @@ function createSpectrogramWorker(worker) {
122
122
  worker.postMessage({ type: "unregister-canvas", canvasId });
123
123
  },
124
124
  registerAudioData(clipId, channelDataArrays, sampleRate) {
125
+ if (terminated) return;
125
126
  const transferableArrays = channelDataArrays.map((arr) => arr.slice());
126
127
  const transferables = transferableArrays.map((arr) => arr.buffer);
127
128
  worker.postMessage(
@@ -131,11 +132,16 @@ function createSpectrogramWorker(worker) {
131
132
  registeredClipIds.add(clipId);
132
133
  },
133
134
  unregisterAudioData(clipId) {
135
+ if (terminated) return;
134
136
  worker.postMessage({ type: "unregister-audio-data", clipId });
135
137
  registeredClipIds.delete(clipId);
136
138
  },
139
+ registeredAudioClipIds() {
140
+ return [...registeredClipIds];
141
+ },
137
142
  terminate() {
138
143
  terminated = true;
144
+ registeredClipIds.clear();
139
145
  worker.terminate();
140
146
  for (const [, entry] of pending) {
141
147
  entry.reject(new Error("Worker terminated"));
@@ -160,6 +166,13 @@ function parseChannelFromCanvasId(canvasId) {
160
166
  function defaultPoolSize() {
161
167
  return 2;
162
168
  }
169
+ function channelsForWorker(channelDataArrays, workerIndex) {
170
+ if (workerIndex === 0 || channelDataArrays.length === 0) return channelDataArrays;
171
+ return [channelDataArrays[workerIndex]];
172
+ }
173
+ function payloadForChannel(channelDataArrays, channelIndex) {
174
+ return channelDataArrays.length === 0 ? [] : [channelDataArrays[channelIndex]];
175
+ }
163
176
  var MAX_POOL_CHANNELS = 32;
164
177
  function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
165
178
  if (poolSize > MAX_POOL_CHANNELS) {
@@ -209,7 +222,11 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
209
222
  const workerIndex = workers.length;
210
223
  for (const [clipId, entry] of registeredAudio) {
211
224
  if (entry.channelDataArrays.length > workerIndex) {
212
- w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
225
+ w.registerAudioData(
226
+ clipId,
227
+ channelsForWorker(entry.channelDataArrays, workerIndex),
228
+ entry.sampleRate
229
+ );
213
230
  }
214
231
  }
215
232
  workers.push(w);
@@ -237,7 +254,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
237
254
  }
238
255
  const promises = Array.from(
239
256
  { length: channelCount },
240
- (_, i) => ensureWorkerForChannel(i).computeFFT({ ...params, channelFilter: i }, generation)
257
+ (_, i) => ensureWorkerForChannel(i).computeFFT(
258
+ {
259
+ ...params,
260
+ channelDataArrays: payloadForChannel(params.channelDataArrays, i),
261
+ channelFilter: 0
262
+ },
263
+ generation
264
+ )
241
265
  );
242
266
  const settled = await Promise.allSettled(promises);
243
267
  const failures = settled.filter((s) => s.status === "rejected");
@@ -281,10 +305,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
281
305
  },
282
306
  registerAudioData(clipId, channelDataArrays, sampleRate) {
283
307
  if (terminated) return;
308
+ const previousCount = registeredAudio.get(clipId)?.channelDataArrays.length ?? 0;
309
+ for (let i = channelDataArrays.length; i < Math.min(previousCount, workers.length); i++) {
310
+ workers[i].unregisterAudioData(clipId);
311
+ }
284
312
  registeredAudio.set(clipId, { channelDataArrays, sampleRate });
285
313
  const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
286
314
  for (let i = 0; i < relevantWorkers; i++) {
287
- workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
315
+ workers[i].registerAudioData(clipId, channelsForWorker(channelDataArrays, i), sampleRate);
288
316
  }
289
317
  },
290
318
  unregisterAudioData(clipId) {
@@ -294,6 +322,9 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
294
322
  w.unregisterAudioData(clipId);
295
323
  }
296
324
  },
325
+ registeredAudioClipIds() {
326
+ return [...registeredAudio.keys()];
327
+ },
297
328
  terminate() {
298
329
  terminated = true;
299
330
  registeredAudio.clear();
@@ -3762,9 +3793,9 @@ var SpectrogramOrchestrator = class extends EventTarget {
3762
3793
  for (const [trackId, trackCanvases] of canvasesByTrack) {
3763
3794
  try {
3764
3795
  const tiers = classifyViewport(trackCanvases, viewport);
3765
- await this.renderTier(tiers.viewport, generation, viewport);
3796
+ const viewportDrawn = await this.renderTier(tiers.viewport, generation, viewport);
3766
3797
  if (this.generation !== generation || this.disposed) return;
3767
- if (!this.readyDispatched.has(trackId)) {
3798
+ if (viewportDrawn && !this.readyDispatched.has(trackId)) {
3768
3799
  this.readyDispatched.add(trackId);
3769
3800
  this.dispatchEvent(
3770
3801
  new CustomEvent("viewport-ready", { detail: { trackId, generation } })
@@ -3776,7 +3807,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3776
3807
  } catch (err) {
3777
3808
  if (this.generation !== generation || this.disposed) return;
3778
3809
  if (err instanceof SpectrogramAbortError) {
3779
- return;
3810
+ continue;
3780
3811
  }
3781
3812
  const error = err instanceof Error ? err : new Error(String(err));
3782
3813
  console.warn(
@@ -3790,34 +3821,47 @@ var SpectrogramOrchestrator = class extends EventTarget {
3790
3821
  }
3791
3822
  }
3792
3823
  }
3824
+ /** Renders the tier's groups; false when a group waits for a render already scheduled. */
3793
3825
  async renderTier(canvases, generation, viewport) {
3794
- if (canvases.length === 0) return;
3826
+ let drawn = true;
3827
+ if (canvases.length === 0) return drawn;
3795
3828
  const groups = groupRenderableChunks(canvases);
3796
3829
  for (const group of groups) {
3797
- if (this.generation !== generation || this.disposed) return;
3798
- await this.renderGroup(group, generation, viewport);
3830
+ if (this.generation !== generation || this.disposed) return drawn;
3831
+ if (!await this.renderGroup(group, generation, viewport)) drawn = false;
3799
3832
  }
3833
+ return drawn;
3800
3834
  }
3835
+ /**
3836
+ * Renders one group. Returns false when its computation was dropped and
3837
+ * another pass is queued (registerClip queues one for a clip registered again
3838
+ * mid-computation that still has canvases, #928): that pass draws it. True
3839
+ * otherwise, also when there is nothing to draw or nothing will draw it.
3840
+ */
3801
3841
  async renderGroup(group, generation, viewport) {
3802
- if (group.length === 0) return;
3842
+ if (group.length === 0) return true;
3803
3843
  const first = group[0];
3804
3844
  const clip = this.clips.get(first.clipId);
3805
3845
  if (!clip) {
3806
3846
  console.warn(
3807
3847
  "[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
3808
3848
  );
3809
- return;
3849
+ return true;
3810
3850
  }
3811
3851
  try {
3812
3852
  await this.renderGroupOnce(group, generation, viewport, clip);
3853
+ return true;
3813
3854
  } catch (err) {
3814
3855
  if (this.generation !== generation || this.disposed) throw err;
3856
+ if (err instanceof SpectrogramAbortError) {
3857
+ return !this.renderQueued;
3858
+ }
3815
3859
  if (err instanceof Error && err.message.includes("cache-miss")) {
3816
3860
  console.warn(
3817
3861
  "[dawcore-spectrogram] FFT cache entry evicted between compute and render \u2014 retrying group once"
3818
3862
  );
3819
3863
  await this.renderGroupOnce(group, generation, viewport, clip);
3820
- return;
3864
+ return true;
3821
3865
  }
3822
3866
  throw err;
3823
3867
  }