@dawcore/spectrogram 0.0.9-next.4 → 0.0.9-next.6

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
@@ -143,7 +143,13 @@ interface SpectrogramWorkerFFTParams {
143
143
  start: number;
144
144
  end: number;
145
145
  };
146
- /** If set, compute only this channel index (used by worker pool). */
146
+ /**
147
+ * If set, compute only the channel at this index of the arrays the worker
148
+ * has for the clip. A worker with one array computes that array whatever
149
+ * this says. The pool gives each worker its one channel at index 0, or
150
+ * (worker 0, registered audio) every channel, and its multi-channel fan-out
151
+ * asks for index 0 (a mono request sends no filter) (#925).
152
+ */
147
153
  channelFilter?: number;
148
154
  }
149
155
  interface SpectrogramWorkerRenderChunksParams {
@@ -178,6 +184,15 @@ interface SpectrogramWorkerApi {
178
184
  unregisterCanvas(canvasId: string): void;
179
185
  registerAudioData(clipId: string, channelDataArrays: Float32Array[], sampleRate: number): void;
180
186
  unregisterAudioData(clipId: string): void;
187
+ /**
188
+ * The ids whose audio is registered now: registered and not unregistered
189
+ * since. An id is whatever the caller registered the audio under (a clip id,
190
+ * or a key per AudioBuffer, as the React provider uses). Empty once
191
+ * terminated; a register after that is ignored. A new array on every call.
192
+ * A caller that pre-registers audio asks this instead of keeping its own
193
+ * list, which would outlive a terminated worker (#821).
194
+ */
195
+ registeredAudioClipIds(): string[];
181
196
  terminate(): void;
182
197
  }
183
198
  /**
package/dist/index.d.ts CHANGED
@@ -143,7 +143,13 @@ interface SpectrogramWorkerFFTParams {
143
143
  start: number;
144
144
  end: number;
145
145
  };
146
- /** If set, compute only this channel index (used by worker pool). */
146
+ /**
147
+ * If set, compute only the channel at this index of the arrays the worker
148
+ * has for the clip. A worker with one array computes that array whatever
149
+ * this says. The pool gives each worker its one channel at index 0, or
150
+ * (worker 0, registered audio) every channel, and its multi-channel fan-out
151
+ * asks for index 0 (a mono request sends no filter) (#925).
152
+ */
147
153
  channelFilter?: number;
148
154
  }
149
155
  interface SpectrogramWorkerRenderChunksParams {
@@ -178,6 +184,15 @@ interface SpectrogramWorkerApi {
178
184
  unregisterCanvas(canvasId: string): void;
179
185
  registerAudioData(clipId: string, channelDataArrays: Float32Array[], sampleRate: number): void;
180
186
  unregisterAudioData(clipId: string): void;
187
+ /**
188
+ * The ids whose audio is registered now: registered and not unregistered
189
+ * since. An id is whatever the caller registered the audio under (a clip id,
190
+ * or a key per AudioBuffer, as the React provider uses). Empty once
191
+ * terminated; a register after that is ignored. A new array on every call.
192
+ * A caller that pre-registers audio asks this instead of keeping its own
193
+ * list, which would outlive a terminated worker (#821).
194
+ */
195
+ registeredAudioClipIds(): string[];
181
196
  terminate(): void;
182
197
  }
183
198
  /**
package/dist/index.js CHANGED
@@ -3607,6 +3607,7 @@ function createSpectrogramWorker(worker) {
3607
3607
  worker.postMessage({ type: "unregister-canvas", canvasId });
3608
3608
  },
3609
3609
  registerAudioData(clipId, channelDataArrays, sampleRate) {
3610
+ if (terminated) return;
3610
3611
  const transferableArrays = channelDataArrays.map((arr) => arr.slice());
3611
3612
  const transferables = transferableArrays.map((arr) => arr.buffer);
3612
3613
  worker.postMessage(
@@ -3616,11 +3617,16 @@ function createSpectrogramWorker(worker) {
3616
3617
  registeredClipIds.add(clipId);
3617
3618
  },
3618
3619
  unregisterAudioData(clipId) {
3620
+ if (terminated) return;
3619
3621
  worker.postMessage({ type: "unregister-audio-data", clipId });
3620
3622
  registeredClipIds.delete(clipId);
3621
3623
  },
3624
+ registeredAudioClipIds() {
3625
+ return [...registeredClipIds];
3626
+ },
3622
3627
  terminate() {
3623
3628
  terminated = true;
3629
+ registeredClipIds.clear();
3624
3630
  worker.terminate();
3625
3631
  for (const [, entry] of pending) {
3626
3632
  entry.reject(new Error("Worker terminated"));
@@ -3645,6 +3651,13 @@ function parseChannelFromCanvasId(canvasId) {
3645
3651
  function defaultPoolSize() {
3646
3652
  return 2;
3647
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
+ }
3648
3661
  var MAX_POOL_CHANNELS = 32;
3649
3662
  function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize()) {
3650
3663
  if (poolSize > MAX_POOL_CHANNELS) {
@@ -3694,7 +3707,11 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3694
3707
  const workerIndex = workers.length;
3695
3708
  for (const [clipId, entry] of registeredAudio) {
3696
3709
  if (entry.channelDataArrays.length > workerIndex) {
3697
- w.registerAudioData(clipId, entry.channelDataArrays, entry.sampleRate);
3710
+ w.registerAudioData(
3711
+ clipId,
3712
+ channelsForWorker(entry.channelDataArrays, workerIndex),
3713
+ entry.sampleRate
3714
+ );
3698
3715
  }
3699
3716
  }
3700
3717
  workers.push(w);
@@ -3722,7 +3739,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3722
3739
  }
3723
3740
  const promises = Array.from(
3724
3741
  { length: channelCount },
3725
- (_, 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
+ )
3726
3750
  );
3727
3751
  const settled = await Promise.allSettled(promises);
3728
3752
  const failures = settled.filter((s) => s.status === "rejected");
@@ -3766,10 +3790,14 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3766
3790
  },
3767
3791
  registerAudioData(clipId, channelDataArrays, sampleRate) {
3768
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
+ }
3769
3797
  registeredAudio.set(clipId, { channelDataArrays, sampleRate });
3770
3798
  const relevantWorkers = Math.min(channelDataArrays.length, workers.length);
3771
3799
  for (let i = 0; i < relevantWorkers; i++) {
3772
- workers[i].registerAudioData(clipId, channelDataArrays, sampleRate);
3800
+ workers[i].registerAudioData(clipId, channelsForWorker(channelDataArrays, i), sampleRate);
3773
3801
  }
3774
3802
  },
3775
3803
  unregisterAudioData(clipId) {
@@ -3779,6 +3807,9 @@ function createSpectrogramWorkerPool(createWorker, poolSize = defaultPoolSize())
3779
3807
  w.unregisterAudioData(clipId);
3780
3808
  }
3781
3809
  },
3810
+ registeredAudioClipIds() {
3811
+ return [...registeredAudio.keys()];
3812
+ },
3782
3813
  terminate() {
3783
3814
  terminated = true;
3784
3815
  registeredAudio.clear();