@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.
@@ -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
 
@@ -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();