@dawcore/spectrogram 0.0.9-next.5 → 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.
@@ -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 {
@@ -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) {