@dawcore/spectrogram 0.0.9-next.5 → 0.0.9-next.7

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 {
@@ -188,8 +194,15 @@ declare class SpectrogramOrchestrator extends EventTarget {
188
194
  protected scheduleRender(): void;
189
195
  protected runRenderLoop(): Promise<void>;
190
196
  protected runRender(generation: number): Promise<void>;
191
- protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
192
- 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>;
193
206
  protected renderGroupOnce(group: CanvasEntry[], generation: number, viewport: ViewportState, clip: ClipEntry): Promise<void>;
194
207
  protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
195
208
  protected yieldUntilIdle(): Promise<void>;
@@ -166,6 +166,13 @@ function parseChannelFromCanvasId(canvasId) {
166
166
  function defaultPoolSize() {
167
167
  return 2;
168
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
+ }
169
176
  var MAX_POOL_CHANNELS = 32;
170
177
  function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
171
178
  if (poolSize > MAX_POOL_CHANNELS) {
@@ -215,7 +222,11 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
215
222
  const workerIndex = workers.length;
216
223
  for (const [clipId, entry] of registeredAudio) {
217
224
  if (entry.channelDataArrays.length > workerIndex) {
218
- w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
225
+ w.registerAudioData(
226
+ clipId,
227
+ channelsForWorker(entry.channelDataArrays, workerIndex),
228
+ entry.sampleRate
229
+ );
219
230
  }
220
231
  }
221
232
  workers.push(w);
@@ -243,7 +254,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
243
254
  }
244
255
  const promises = Array.from(
245
256
  { length: channelCount },
246
- (_, 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
+ )
247
265
  );
248
266
  const settled = await Promise.allSettled(promises);
249
267
  const failures = settled.filter((s) => s.status === "rejected");
@@ -287,10 +305,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
287
305
  },
288
306
  registerAudioData(clipId, channelDataArrays, sampleRate) {
289
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
+ }
290
312
  registeredAudio.set(clipId, { channelDataArrays, sampleRate });
291
313
  const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
292
314
  for (let i = 0; i < relevantWorkers; i++) {
293
- workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
315
+ workers[i].registerAudioData(clipId, channelsForWorker(channelDataArrays, i), sampleRate);
294
316
  }
295
317
  },
296
318
  unregisterAudioData(clipId) {
@@ -3771,9 +3793,9 @@ var SpectrogramOrchestrator = class extends EventTarget {
3771
3793
  for (const [trackId, trackCanvases] of canvasesByTrack) {
3772
3794
  try {
3773
3795
  const tiers = classifyViewport(trackCanvases, viewport);
3774
- await this.renderTier(tiers.viewport, generation, viewport);
3796
+ const viewportDrawn = await this.renderTier(tiers.viewport, generation, viewport);
3775
3797
  if (this.generation !== generation || this.disposed) return;
3776
- if (!this.readyDispatched.has(trackId)) {
3798
+ if (viewportDrawn && !this.readyDispatched.has(trackId)) {
3777
3799
  this.readyDispatched.add(trackId);
3778
3800
  this.dispatchEvent(
3779
3801
  new CustomEvent("viewport-ready", { detail: { trackId, generation } })
@@ -3785,7 +3807,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
3785
3807
  } catch (err) {
3786
3808
  if (this.generation !== generation || this.disposed) return;
3787
3809
  if (err instanceof SpectrogramAbortError) {
3788
- return;
3810
+ continue;
3789
3811
  }
3790
3812
  const error = err instanceof Error ? err : new Error(String(err));
3791
3813
  console.warn(
@@ -3799,34 +3821,47 @@ var SpectrogramOrchestrator = class extends EventTarget {
3799
3821
  }
3800
3822
  }
3801
3823
  }
3824
+ /** Renders the tier's groups; false when a group waits for a render already scheduled. */
3802
3825
  async renderTier(canvases, generation, viewport) {
3803
- if (canvases.length === 0) return;
3826
+ let drawn = true;
3827
+ if (canvases.length === 0) return drawn;
3804
3828
  const groups = groupRenderableChunks(canvases);
3805
3829
  for (const group of groups) {
3806
- if (this.generation !== generation || this.disposed) return;
3807
- 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;
3808
3832
  }
3833
+ return drawn;
3809
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
+ */
3810
3841
  async renderGroup(group, generation, viewport) {
3811
- if (group.length === 0) return;
3842
+ if (group.length === 0) return true;
3812
3843
  const first = group[0];
3813
3844
  const clip = this.clips.get(first.clipId);
3814
3845
  if (!clip) {
3815
3846
  console.warn(
3816
3847
  "[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
3817
3848
  );
3818
- return;
3849
+ return true;
3819
3850
  }
3820
3851
  try {
3821
3852
  await this.renderGroupOnce(group, generation, viewport, clip);
3853
+ return true;
3822
3854
  } catch (err) {
3823
3855
  if (this.generation !== generation || this.disposed) throw err;
3856
+ if (err instanceof SpectrogramAbortError) {
3857
+ return !this.renderQueued;
3858
+ }
3824
3859
  if (err instanceof Error && err.message.includes("cache-miss")) {
3825
3860
  console.warn(
3826
3861
  "[dawcore-spectrogram] FFT cache entry evicted between compute and render \u2014 retrying group once"
3827
3862
  );
3828
3863
  await this.renderGroupOnce(group, generation, viewport, clip);
3829
- return;
3864
+ return true;
3830
3865
  }
3831
3866
  throw err;
3832
3867
  }