@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.
package/dist/index.d.mts CHANGED
@@ -125,7 +125,8 @@ declare function computePaddedFftRange({ chunks, fftSize, offsetSamples, duratio
125
125
  };
126
126
 
127
127
  /**
128
- * Error thrown when a spectrogram computation is aborted due to a generation change.
128
+ * Error thrown when a spectrogram computation is aborted: its generation was
129
+ * superseded, or its clip's audio was replaced or unregistered while it ran (#928).
129
130
  * Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
130
131
  */
131
132
  declare class SpectrogramAbortError extends Error {
@@ -143,7 +144,13 @@ interface SpectrogramWorkerFFTParams {
143
144
  start: number;
144
145
  end: number;
145
146
  };
146
- /** If set, compute only this channel index (used by worker pool). */
147
+ /**
148
+ * If set, compute only the channel at this index of the arrays the worker
149
+ * has for the clip. A worker with one array computes that array whatever
150
+ * this says. The pool gives each worker its one channel at index 0, or
151
+ * (worker 0, registered audio) every channel, and its multi-channel fan-out
152
+ * asks for index 0 (a mono request sends no filter) (#925).
153
+ */
147
154
  channelFilter?: number;
148
155
  }
149
156
  interface SpectrogramWorkerRenderChunksParams {
@@ -334,8 +341,15 @@ declare class SpectrogramOrchestrator extends EventTarget {
334
341
  protected scheduleRender(): void;
335
342
  protected runRenderLoop(): Promise<void>;
336
343
  protected runRender(generation: number): Promise<void>;
337
- protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
338
- protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
344
+ /** Renders the tier's groups; false when a group waits for a render already scheduled. */
345
+ protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<boolean>;
346
+ /**
347
+ * Renders one group. Returns false when its computation was dropped and
348
+ * another pass is queued (registerClip queues one for a clip registered again
349
+ * mid-computation that still has canvases, #928): that pass draws it. True
350
+ * otherwise, also when there is nothing to draw or nothing will draw it.
351
+ */
352
+ protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<boolean>;
339
353
  protected renderGroupOnce(group: CanvasEntry[], generation: number, viewport: ViewportState, clip: ClipEntry): Promise<void>;
340
354
  protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
341
355
  protected yieldUntilIdle(): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -125,7 +125,8 @@ declare function computePaddedFftRange({ chunks, fftSize, offsetSamples, duratio
125
125
  };
126
126
 
127
127
  /**
128
- * Error thrown when a spectrogram computation is aborted due to a generation change.
128
+ * Error thrown when a spectrogram computation is aborted: its generation was
129
+ * superseded, or its clip's audio was replaced or unregistered while it ran (#928).
129
130
  * Use `instanceof SpectrogramAbortError` instead of string matching on error messages.
130
131
  */
131
132
  declare class SpectrogramAbortError extends Error {
@@ -143,7 +144,13 @@ interface SpectrogramWorkerFFTParams {
143
144
  start: number;
144
145
  end: number;
145
146
  };
146
- /** If set, compute only this channel index (used by worker pool). */
147
+ /**
148
+ * If set, compute only the channel at this index of the arrays the worker
149
+ * has for the clip. A worker with one array computes that array whatever
150
+ * this says. The pool gives each worker its one channel at index 0, or
151
+ * (worker 0, registered audio) every channel, and its multi-channel fan-out
152
+ * asks for index 0 (a mono request sends no filter) (#925).
153
+ */
147
154
  channelFilter?: number;
148
155
  }
149
156
  interface SpectrogramWorkerRenderChunksParams {
@@ -334,8 +341,15 @@ declare class SpectrogramOrchestrator extends EventTarget {
334
341
  protected scheduleRender(): void;
335
342
  protected runRenderLoop(): Promise<void>;
336
343
  protected runRender(generation: number): Promise<void>;
337
- protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
338
- protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
344
+ /** Renders the tier's groups; false when a group waits for a render already scheduled. */
345
+ protected renderTier(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<boolean>;
346
+ /**
347
+ * Renders one group. Returns false when its computation was dropped and
348
+ * another pass is queued (registerClip queues one for a clip registered again
349
+ * mid-computation that still has canvases, #928): that pass draws it. True
350
+ * otherwise, also when there is nothing to draw or nothing will draw it.
351
+ */
352
+ protected renderGroup(group: CanvasEntry[], generation: number, viewport: ViewportState): Promise<boolean>;
339
353
  protected renderGroupOnce(group: CanvasEntry[], generation: number, viewport: ViewportState, clip: ClipEntry): Promise<void>;
340
354
  protected renderRemainingViaIdle(canvases: CanvasEntry[], generation: number, viewport: ViewportState): Promise<void>;
341
355
  protected yieldUntilIdle(): Promise<void>;
package/dist/index.js CHANGED
@@ -3651,6 +3651,13 @@ function parseChannelFromCanvasId(canvasId) {
3651
3651
  function defaultPoolSize() {
3652
3652
  return 2;
3653
3653
  }
3654
+ function channelsForWorker(channelDataArrays, workerIndex) {
3655
+ if (workerIndex === 0 || channelDataArrays.length === 0) return channelDataArrays;
3656
+ return [channelDataArrays[workerIndex]];
3657
+ }
3658
+ function payloadForChannel(channelDataArrays, channelIndex) {
3659
+ return channelDataArrays.length === 0 ? [] : [channelDataArrays[channelIndex]];
3660
+ }
3654
3661
  var MAX_POOL_CHANNELS = 32;
3655
3662
  function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
3656
3663
  if (poolSize > MAX_POOL_CHANNELS) {
@@ -3700,7 +3707,11 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3700
3707
  const workerIndex = workers.length;
3701
3708
  for (const [clipId, entry] of registeredAudio) {
3702
3709
  if (entry.channelDataArrays.length > workerIndex) {
3703
- w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
3710
+ w.registerAudioData(
3711
+ clipId,
3712
+ channelsForWorker(entry.channelDataArrays, workerIndex),
3713
+ entry.sampleRate
3714
+ );
3704
3715
  }
3705
3716
  }
3706
3717
  workers.push(w);
@@ -3728,7 +3739,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3728
3739
  }
3729
3740
  const promises = Array.from(
3730
3741
  { length: channelCount },
3731
- (_, i) => ensureWorkerForChannel(i).computeFFT({ ...params, channelFilter: i }, generation)
3742
+ (_, i) => ensureWorkerForChannel(i).computeFFT(
3743
+ {
3744
+ ...params,
3745
+ channelDataArrays: payloadForChannel(params.channelDataArrays, i),
3746
+ channelFilter: 0
3747
+ },
3748
+ generation
3749
+ )
3732
3750
  );
3733
3751
  const settled = await Promise.allSettled(promises);
3734
3752
  const failures = settled.filter((s) => s.status === "rejected");
@@ -3772,10 +3790,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3772
3790
  },
3773
3791
  registerAudioData(clipId, channelDataArrays, sampleRate) {
3774
3792
  if (terminated) return;
3793
+ const previousCount = registeredAudio.get(clipId)?.channelDataArrays.length ?? 0;
3794
+ for (let i = channelDataArrays.length; i < Math.min(previousCount, workers.length); i++) {
3795
+ workers[i].unregisterAudioData(clipId);
3796
+ }
3775
3797
  registeredAudio.set(clipId, { channelDataArrays, sampleRate });
3776
3798
  const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
3777
3799
  for (let i = 0; i < relevantWorkers; i++) {
3778
- workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
3800
+ workers[i].registerAudioData(clipId, channelsForWorker(channelDataArrays, i), sampleRate);
3779
3801
  }
3780
3802
  },
3781
3803
  unregisterAudioData(clipId) {
@@ -4093,9 +4115,9 @@ var SpectrogramOrchestrator = class extends EventTarget {
4093
4115
  for (const [trackId, trackCanvases] of canvasesByTrack) {
4094
4116
  try {
4095
4117
  const tiers = classifyViewport(trackCanvases, viewport);
4096
- await this.renderTier(tiers.viewport, generation, viewport);
4118
+ const viewportDrawn = await this.renderTier(tiers.viewport, generation, viewport);
4097
4119
  if (this.generation !== generation || this.disposed) return;
4098
- if (!this.readyDispatched.has(trackId)) {
4120
+ if (viewportDrawn && !this.readyDispatched.has(trackId)) {
4099
4121
  this.readyDispatched.add(trackId);
4100
4122
  this.dispatchEvent(
4101
4123
  new CustomEvent("viewport-ready", { detail: { trackId, generation } })
@@ -4107,7 +4129,7 @@ var SpectrogramOrchestrator = class extends EventTarget {
4107
4129
  } catch (err) {
4108
4130
  if (this.generation !== generation || this.disposed) return;
4109
4131
  if (err instanceof SpectrogramAbortError) {
4110
- return;
4132
+ continue;
4111
4133
  }
4112
4134
  const error = err instanceof Error ? err : new Error(String(err));
4113
4135
  console.warn(
@@ -4121,34 +4143,47 @@ var SpectrogramOrchestrator = class extends EventTarget {
4121
4143
  }
4122
4144
  }
4123
4145
  }
4146
+ /** Renders the tier's groups; false when a group waits for a render already scheduled. */
4124
4147
  async renderTier(canvases, generation, viewport) {
4125
- if (canvases.length === 0) return;
4148
+ let drawn = true;
4149
+ if (canvases.length === 0) return drawn;
4126
4150
  const groups = groupRenderableChunks(canvases);
4127
4151
  for (const group of groups) {
4128
- if (this.generation !== generation || this.disposed) return;
4129
- await this.renderGroup(group, generation, viewport);
4152
+ if (this.generation !== generation || this.disposed) return drawn;
4153
+ if (!await this.renderGroup(group, generation, viewport)) drawn = false;
4130
4154
  }
4155
+ return drawn;
4131
4156
  }
4157
+ /**
4158
+ * Renders one group. Returns false when its computation was dropped and
4159
+ * another pass is queued (registerClip queues one for a clip registered again
4160
+ * mid-computation that still has canvases, #928): that pass draws it. True
4161
+ * otherwise, also when there is nothing to draw or nothing will draw it.
4162
+ */
4132
4163
  async renderGroup(group, generation, viewport) {
4133
- if (group.length === 0) return;
4164
+ if (group.length === 0) return true;
4134
4165
  const first = group[0];
4135
4166
  const clip = this.clips.get(first.clipId);
4136
4167
  if (!clip) {
4137
4168
  console.warn(
4138
4169
  "[dawcore-spectrogram] renderGroup: no clip audio for " + first.clipId + " (canvas " + first.canvasId + ") \u2014 canvas will stay black until registerClip is called"
4139
4170
  );
4140
- return;
4171
+ return true;
4141
4172
  }
4142
4173
  try {
4143
4174
  await this.renderGroupOnce(group, generation, viewport, clip);
4175
+ return true;
4144
4176
  } catch (err) {
4145
4177
  if (this.generation !== generation || this.disposed) throw err;
4178
+ if (err instanceof SpectrogramAbortError) {
4179
+ return !this.renderQueued;
4180
+ }
4146
4181
  if (err instanceof Error && err.message.includes("cache-miss")) {
4147
4182
  console.warn(
4148
4183
  "[dawcore-spectrogram] FFT cache entry evicted between compute and render \u2014 retrying group once"
4149
4184
  );
4150
4185
  await this.renderGroupOnce(group, generation, viewport, clip);
4151
- return;
4186
+ return true;
4152
4187
  }
4153
4188
  throw err;
4154
4189
  }