@avoid.quest/audio 0.1.0 → 0.1.1-beta.1

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.
Files changed (42) hide show
  1. package/README.md +32 -2
  2. package/dist/effects/index.d.ts +21 -72
  3. package/dist/effects/index.js +212 -505
  4. package/dist/effects/index.js.map +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/midi/index.d.ts +0 -24
  8. package/dist/midi/index.js +222 -258
  9. package/dist/midi/index.js.map +1 -1
  10. package/dist/native-Bern4_PB.js +172 -0
  11. package/dist/native-Bern4_PB.js.map +1 -0
  12. package/dist/pcm-buffer-DSzGHbon.js +436 -0
  13. package/dist/pcm-buffer-DSzGHbon.js.map +1 -0
  14. package/dist/playback/index.d.ts +63 -115
  15. package/dist/playback/index.js +906 -1062
  16. package/dist/playback/index.js.map +1 -1
  17. package/dist/queue/index.d.ts +8 -19
  18. package/dist/queue/index.js +461 -960
  19. package/dist/queue/index.js.map +1 -1
  20. package/dist/recognize/index.d.ts +37 -74
  21. package/dist/recognize/index.js +172 -2715
  22. package/dist/recognize/index.js.map +1 -1
  23. package/dist/routing/index.d.ts +3 -34
  24. package/dist/routing/index.js +413 -536
  25. package/dist/routing/index.js.map +1 -1
  26. package/dist/shazam-gBrX1NEY.d.ts +27 -0
  27. package/dist/sync/index.d.ts +9 -77
  28. package/dist/sync/index.js +320 -542
  29. package/dist/sync/index.js.map +1 -1
  30. package/dist/{worklet-loader-BvEW0oVO.d.ts → worklet-loader-BabwPALw.d.ts} +4 -7
  31. package/dist/{worklet-loader-S7R8ZAmP.js → worklet-loader-f741D1tk.js} +37 -52
  32. package/dist/worklet-loader-f741D1tk.js.map +1 -0
  33. package/dist/worklets/couch-live.js +110 -192
  34. package/dist/worklets/couch-live.js.map +1 -1
  35. package/dist/worklets/meter.js +14 -20
  36. package/dist/worklets/meter.js.map +1 -1
  37. package/dist/worklets/pcm-capture.d.ts +6 -0
  38. package/dist/worklets/pcm-capture.js +52 -0
  39. package/dist/worklets/pcm-capture.js.map +1 -0
  40. package/package.json +18 -4
  41. package/types/hls-unavailable.d.ts +4 -0
  42. package/dist/worklet-loader-S7R8ZAmP.js.map +0 -1
@@ -2,32 +2,31 @@ const DEADBAND_SEC = .001;
2
2
  const SLEW_RATE = .003;
3
3
  const REPLACE_SEC = .2;
4
4
  const JOIN_US = 1e3;
5
- function noStats() {
6
- return {
7
- chunks: 0,
8
- corrections: 0,
9
- errorMax: Number.NEGATIVE_INFINITY,
10
- errorMin: Number.POSITIVE_INFINITY,
11
- filled: 0,
12
- lost: 0,
13
- skipped: 0,
14
- slack: Number.POSITIVE_INFINITY
15
- };
5
+ const noStats = () => ({
6
+ chunks: 0,
7
+ corrections: 0,
8
+ errorMax: Number.NEGATIVE_INFINITY,
9
+ errorMin: Number.POSITIVE_INFINITY,
10
+ filled: 0,
11
+ lost: 0,
12
+ skipped: 0,
13
+ slack: Number.POSITIVE_INFINITY
14
+ });
15
+ function resetStats(s) {
16
+ s.chunks = 0;
17
+ s.corrections = 0;
18
+ s.errorMax = Number.NEGATIVE_INFINITY;
19
+ s.errorMin = Number.POSITIVE_INFINITY;
20
+ s.filled = 0;
21
+ s.lost = 0;
22
+ s.skipped = 0;
23
+ s.slack = Number.POSITIVE_INFINITY;
16
24
  }
17
- function interpolateSample(samples, nextChunk, channelIndex, index, fraction) {
18
- const sample = samples[index] ?? 0;
19
- if (fraction === 0) return sample;
20
- if (index + 1 < samples.length) return sample + ((samples[index + 1] ?? sample) - sample) * fraction;
21
- const nextSamples = nextChunk?.channels[channelIndex] ?? nextChunk?.channels[0];
22
- return sample + ((nextSamples && nextSamples.length > 0 ? nextSamples[0] ?? sample : sample) - sample) * fraction;
25
+ function interpolateSample(samples, nextChunk, ch, idx, frac) {
26
+ const s = samples[idx] ?? 0;
27
+ if (frac === 0) return s;
28
+ return s + ((idx + 1 < samples.length ? samples[idx + 1] ?? s : (nextChunk?.channels[ch] ?? nextChunk?.channels[0])?.[0] ?? s) - s) * frac;
23
29
  }
24
- /**
25
- * Sample-accurate audio timeline used inside the `"couch-live"` AudioWorklet.
26
- * Backed by a fixed-capacity ring buffer (zero `Array.prototype.shift()`, zero
27
- * allocations during `render()`), dropping the oldest chunk on overflow,
28
- * slewing smoothly at ±0.3% outside a 1 ms deadband, and hard re-placing beyond
29
- * 200 ms error.
30
- */
31
30
  var LiveTimeline = class {
32
31
  capacity;
33
32
  ring;
@@ -45,9 +44,8 @@ var LiveTimeline = class {
45
44
  stats = noStats();
46
45
  constructor(sampleRate, capacity = 256) {
47
46
  this.sampleRate = sampleRate;
48
- const normalizedCapacity = Number.isFinite(capacity) ? Math.max(2, Math.floor(capacity)) : 256;
49
- this.capacity = normalizedCapacity;
50
- this.ring = Array.from({ length: normalizedCapacity });
47
+ this.capacity = Number.isFinite(capacity) ? Math.max(2, Math.floor(capacity)) : 256;
48
+ this.ring = Array.from({ length: this.capacity });
51
49
  }
52
50
  get length() {
53
51
  return this.count;
@@ -56,37 +54,20 @@ var LiveTimeline = class {
56
54
  return this.capacity;
57
55
  }
58
56
  reset() {
59
- for (let i = 0; i < this.capacity; i += 1) this.ring[i] = void 0;
57
+ this.ring.fill(void 0);
60
58
  this.head = 0;
61
59
  this.tail = 0;
62
60
  this.count = 0;
63
- this.position = 0;
64
- this.playing = false;
65
61
  this.dry = false;
66
- this.settling = false;
67
- this.correcting = 0;
68
- this.rate = 1;
69
62
  this.endStampUs = void 0;
70
- this.resetStatsInPlace();
63
+ this.stopSlew(true);
64
+ resetStats(this.stats);
71
65
  }
72
66
  takeStats() {
73
- const snapshot = {
74
- chunks: this.stats.chunks,
75
- corrections: this.stats.corrections,
76
- errorMax: this.stats.errorMax,
77
- errorMin: this.stats.errorMin,
78
- filled: this.stats.filled,
79
- lost: this.stats.lost,
80
- skipped: this.stats.skipped,
81
- slack: this.stats.slack
82
- };
83
- this.resetStatsInPlace();
84
- return snapshot;
67
+ const snap = noStats();
68
+ this.takeStatsInto(snap);
69
+ return snap;
85
70
  }
86
- /**
87
- * Copies accumulated stats into a preallocated target object and resets
88
- * counters in place with zero heap allocation (safe for AudioWorklet use).
89
- */
90
71
  takeStatsInto(target) {
91
72
  target.chunks = this.stats.chunks;
92
73
  target.corrections = this.stats.corrections;
@@ -96,47 +77,22 @@ var LiveTimeline = class {
96
77
  target.lost = this.stats.lost;
97
78
  target.skipped = this.stats.skipped;
98
79
  target.slack = this.stats.slack;
99
- this.resetStatsInPlace();
80
+ resetStats(this.stats);
100
81
  }
101
- resetStatsInPlace() {
102
- this.stats.chunks = 0;
103
- this.stats.corrections = 0;
104
- this.stats.errorMax = Number.NEGATIVE_INFINITY;
105
- this.stats.errorMin = Number.POSITIVE_INFINITY;
106
- this.stats.filled = 0;
107
- this.stats.lost = 0;
108
- this.stats.skipped = 0;
109
- this.stats.slack = Number.POSITIVE_INFINITY;
82
+ stopSlew(resetPos = false) {
83
+ this.playing = false;
84
+ this.settling = false;
85
+ this.correcting = 0;
86
+ this.rate = 1;
87
+ if (resetPos) this.position = 0;
110
88
  }
111
- peek(offset) {
112
- if (offset < 0 || offset >= this.count) return;
113
- return this.ring[(this.head + offset) % this.capacity];
89
+ at(offset) {
90
+ return offset < this.count ? this.ring[(this.head + offset) % this.capacity] : void 0;
114
91
  }
115
- dequeue() {
116
- if (this.count === 0) return;
117
- const item = this.ring[this.head];
92
+ dropHead() {
118
93
  this.ring[this.head] = void 0;
119
94
  this.head = (this.head + 1) % this.capacity;
120
95
  this.count -= 1;
121
- return item;
122
- }
123
- enqueue(chunk) {
124
- if (this.count === this.capacity) {
125
- this.ring[this.head] = void 0;
126
- this.head = (this.head + 1) % this.capacity;
127
- this.count -= 1;
128
- this.stats.lost += 1;
129
- if (this.playing) {
130
- this.playing = false;
131
- this.position = 0;
132
- this.settling = false;
133
- this.correcting = 0;
134
- this.rate = 1;
135
- }
136
- }
137
- this.ring[this.tail] = chunk;
138
- this.tail = (this.tail + 1) % this.capacity;
139
- this.count += 1;
140
96
  }
141
97
  push(chunk, next) {
142
98
  if ((chunk.channels[0]?.length ?? 0) === 0) return;
@@ -146,17 +102,24 @@ var LiveTimeline = class {
146
102
  this.stats.slack = Math.min(this.stats.slack, slack);
147
103
  if (slack < 0 || this.dry && follows) this.stats.lost += 1;
148
104
  this.dry = false;
149
- this.enqueue(chunk);
105
+ if (this.count === this.capacity) {
106
+ this.dropHead();
107
+ this.stats.lost += 1;
108
+ if (this.playing) this.stopSlew(true);
109
+ }
110
+ this.ring[this.tail] = chunk;
111
+ this.tail = (this.tail + 1) % this.capacity;
112
+ this.count += 1;
150
113
  }
151
114
  render(frame, output) {
152
115
  const frames = output[0]?.length ?? 0;
153
116
  if (this.playing) this.steer(frame);
154
- const channelCount = output.length;
117
+ const chCount = output.length;
155
118
  for (let i = 0; i < frames; i += 1) {
156
119
  if (!this.playing && !this.place(frame + i)) {
157
- for (let c = 0; c < channelCount; c += 1) {
158
- const outChannel = output[c];
159
- if (outChannel) outChannel[i] = 0;
120
+ for (let c = 0; c < chCount; c += 1) {
121
+ const outCh = output[c];
122
+ if (outCh) outCh[i] = 0;
160
123
  }
161
124
  if (this.dry) this.stats.filled += 1;
162
125
  continue;
@@ -166,98 +129,75 @@ var LiveTimeline = class {
166
129
  }
167
130
  queued() {
168
131
  let frames = -this.position;
169
- for (let i = 0; i < this.count; i += 1) {
170
- const chunk = this.ring[(this.head + i) % this.capacity];
171
- frames += chunk?.channels[0]?.length ?? 0;
172
- }
132
+ for (let i = 0; i < this.count; i += 1) frames += this.ring[(this.head + i) % this.capacity]?.channels[0]?.length ?? 0;
173
133
  return frames / this.rate;
174
134
  }
175
135
  place(frame) {
176
136
  while (this.count > 0) {
177
- const chunk = this.peek(0);
137
+ const chunk = this.at(0);
178
138
  if (!chunk) return false;
179
139
  const offset = Math.round(frame - chunk.frame);
180
- const length = chunk.channels[0]?.length ?? 0;
140
+ const len = chunk.channels[0]?.length ?? 0;
181
141
  if (offset < 0) return false;
182
- if (offset < length) {
142
+ if (offset < len) {
183
143
  this.stats.skipped += offset;
184
144
  this.position = offset;
185
145
  this.playing = true;
186
146
  return true;
187
147
  }
188
- this.stats.skipped += length;
189
- this.dequeue();
148
+ this.stats.skipped += len;
149
+ this.dropHead();
190
150
  }
191
151
  return false;
192
152
  }
193
153
  steer(frame) {
194
- const chunk = this.peek(0);
154
+ const chunk = this.at(0);
195
155
  if (!chunk) return;
196
- const error = chunk.frame + this.position - frame;
197
- this.stats.errorMin = Math.min(this.stats.errorMin, error);
198
- this.stats.errorMax = Math.max(this.stats.errorMax, error);
199
- if (Math.abs(error) > REPLACE_SEC * this.sampleRate) {
200
- this.playing = false;
201
- this.settling = false;
202
- this.correcting = 0;
203
- this.rate = 1;
156
+ const err = chunk.frame + this.position - frame;
157
+ this.stats.errorMin = Math.min(this.stats.errorMin, err);
158
+ this.stats.errorMax = Math.max(this.stats.errorMax, err);
159
+ if (Math.abs(err) > REPLACE_SEC * this.sampleRate) {
160
+ this.stopSlew();
204
161
  return;
205
162
  }
206
- const sign = Math.sign(error);
207
- if (this.correcting === 0 && Math.abs(error) > DEADBAND_SEC * this.sampleRate) {
163
+ const sign = Math.sign(err);
164
+ if (this.correcting === 0 && Math.abs(err) > DEADBAND_SEC * this.sampleRate) {
208
165
  this.correcting = sign;
209
166
  this.settling = false;
210
167
  this.rate = 1 - sign * SLEW_RATE;
211
168
  this.stats.corrections += 1;
212
169
  } else if (this.correcting !== 0 && sign !== this.correcting) this.settling = true;
213
170
  }
214
- advancePosition(chunk, currentFrameIndex) {
215
- this.position += this.rate;
216
- if (this.settling) {
217
- const whole = Math.round(this.position);
218
- if (Math.abs(this.position - whole) < SLEW_RATE) {
219
- this.position = whole;
220
- this.rate = 1;
221
- this.correcting = 0;
222
- this.settling = false;
223
- }
224
- }
225
- const length = chunk.channels[0]?.length ?? 0;
226
- if (this.position < length) return;
227
- this.position -= length;
228
- this.endStampUs = chunk.stampUs + length / this.sampleRate * 1e6;
229
- this.dequeue();
230
- const nextChunk = this.peek(0);
231
- if (nextChunk) {
232
- const nextFrameIndex = currentFrameIndex + 1;
233
- const boundaryError = nextChunk.frame + this.position - nextFrameIndex;
234
- if (Math.abs(boundaryError) > REPLACE_SEC * this.sampleRate) {
235
- this.playing = false;
236
- this.settling = false;
237
- this.correcting = 0;
238
- this.rate = 1;
239
- this.position = 0;
240
- }
241
- return;
242
- }
243
- this.playing = false;
244
- this.dry = true;
245
- this.correcting = 0;
246
- this.settling = false;
247
- this.rate = 1;
248
- }
249
- read(output, i, currentFrameIndex) {
250
- const chunk = this.peek(0);
171
+ read(output, i, frameIdx) {
172
+ const chunk = this.at(0);
251
173
  if (!chunk) return;
252
- const next = this.peek(1);
253
- const index = Math.floor(this.position);
254
- const fraction = this.position - index;
255
- for (let c = 0; c < output.length; c += 1) {
256
- const outChannel = output[c];
174
+ const next = this.at(1);
175
+ const idx = Math.floor(this.position);
176
+ const frac = this.position - idx;
177
+ const chCount = output.length;
178
+ for (let c = 0; c < chCount; c += 1) {
179
+ const outCh = output[c];
257
180
  const samples = chunk.channels[c] ?? chunk.channels[0];
258
- if (outChannel && samples) outChannel[i] = interpolateSample(samples, next, c, index, fraction);
181
+ if (outCh && samples) outCh[i] = interpolateSample(samples, next, c, idx, frac);
259
182
  }
260
- this.advancePosition(chunk, currentFrameIndex);
183
+ this.position += this.rate;
184
+ const whole = Math.round(this.position);
185
+ if (this.settling && Math.abs(this.position - whole) < SLEW_RATE) {
186
+ this.position = whole;
187
+ this.rate = 1;
188
+ this.correcting = 0;
189
+ this.settling = false;
190
+ }
191
+ const len = chunk.channels[0]?.length ?? 0;
192
+ if (this.position < len) return;
193
+ this.position -= len;
194
+ this.endStampUs = chunk.stampUs + len / this.sampleRate * 1e6;
195
+ this.dropHead();
196
+ const nextChunk = this.at(0);
197
+ if (!nextChunk) {
198
+ this.dry = true;
199
+ this.stopSlew();
200
+ } else if (Math.abs(nextChunk.frame + this.position - (frameIdx + 1)) > REPLACE_SEC * this.sampleRate) this.stopSlew(true);
261
201
  }
262
202
  };
263
203
  // src/worklets/couch-live.ts
@@ -266,16 +206,7 @@ if (typeof registerProcessor === "function") {
266
206
  const reportFrames = sampleRate / 10;
267
207
  registerProcessor(COUCH_LIVE_PROCESSOR_NAME, class CouchLiveProcessor extends AudioWorkletProcessor {
268
208
  timeline = new LiveTimeline(sampleRate);
269
- reportStats = {
270
- chunks: 0,
271
- corrections: 0,
272
- errorMax: Number.NEGATIVE_INFINITY,
273
- errorMin: Number.POSITIVE_INFINITY,
274
- filled: 0,
275
- lost: 0,
276
- skipped: 0,
277
- slack: Number.POSITIVE_INFINITY
278
- };
209
+ reportStats = noStats();
279
210
  reportPayload;
280
211
  epoch = 0;
281
212
  next = currentFrame;
@@ -283,39 +214,26 @@ if (typeof registerProcessor === "function") {
283
214
  feed;
284
215
  disposed = false;
285
216
  onFeedMessage = (msgEvent) => {
286
- if (this.disposed) return;
287
217
  const msg = msgEvent.data;
288
- if (!msg || typeof msg !== "object") return;
289
- if (msg.type === "dispose") {
290
- this.dispose();
291
- return;
292
- }
293
- if (msg.type === "chunk") {
294
- if (msg.chunk) this.timeline.push(msg.chunk, this.next);
295
- return;
296
- }
297
- if (msg.type === "reset") {
218
+ if (this.disposed || !msg || typeof msg !== "object") return;
219
+ if (msg.type === "dispose") this.dispose();
220
+ else if (msg.type === "chunk" && msg.chunk) this.timeline.push(msg.chunk, this.next);
221
+ else if (msg.type === "reset") {
298
222
  this.timeline.reset();
299
223
  this.epoch = typeof msg.epoch === "number" ? msg.epoch : 0;
300
224
  }
301
225
  };
302
226
  onPortMessage = (event) => {
303
- if (this.disposed) return;
304
227
  const { data } = event;
305
- if (!data || typeof data !== "object") return;
306
- if (data.type === "dispose") {
307
- this.dispose();
308
- return;
228
+ if (this.disposed || !data || typeof data !== "object") return;
229
+ if (data.type === "dispose") this.dispose();
230
+ else if (data.type === "poll") this.flushReport();
231
+ else if (data.port) {
232
+ this.detachFeed();
233
+ this.feed = data.port;
234
+ this.feed.addEventListener("message", this.onFeedMessage);
235
+ this.feed.start?.();
309
236
  }
310
- if (data.type === "poll") {
311
- this.flushReport();
312
- return;
313
- }
314
- if (!data.port) return;
315
- this.detachFeed();
316
- this.feed = data.port;
317
- this.feed.addEventListener("message", this.onFeedMessage);
318
- if (typeof this.feed.start === "function") this.feed.start();
319
237
  };
320
238
  constructor() {
321
239
  super();
@@ -324,12 +242,12 @@ if (typeof registerProcessor === "function") {
324
242
  stats: this.reportStats
325
243
  };
326
244
  this.port.addEventListener("message", this.onPortMessage);
327
- if (typeof this.port.start === "function") this.port.start();
245
+ this.port.start?.();
328
246
  }
329
247
  detachFeed() {
330
248
  if (!this.feed) return;
331
249
  this.feed.removeEventListener("message", this.onFeedMessage);
332
- if (typeof this.feed.close === "function") this.feed.close();
250
+ this.feed.close?.();
333
251
  this.feed = void 0;
334
252
  }
335
253
  dispose() {
@@ -338,7 +256,7 @@ if (typeof registerProcessor === "function") {
338
256
  this.detachFeed();
339
257
  this.timeline.reset();
340
258
  this.port.removeEventListener("message", this.onPortMessage);
341
- if (typeof this.port.close === "function") this.port.close();
259
+ this.port.close?.();
342
260
  }
343
261
  flushReport() {
344
262
  if (this.disposed || !this.feed) return;
@@ -1 +1 @@
1
- {"version":3,"file":"couch-live.js","names":[],"sources":["../../src/sync/live-timeline.ts","../../src/worklets/couch-live.ts"],"sourcesContent":["export interface LiveChunk {\n readonly frame: number;\n readonly stampUs: number;\n readonly channels: readonly Float32Array[];\n}\n\nexport type LiveMessage =\n | { readonly type: \"chunk\"; readonly chunk: LiveChunk }\n | { readonly type: \"reset\"; readonly epoch: number }\n | { readonly type: \"dispose\" };\n\nexport interface LiveClock {\n readonly offset: number;\n readonly sampleRate: number;\n}\n\nexport interface PageClockEstimator {\n readonly saw: (pageMs: number, ownMs?: number) => void;\n readonly pageMs: (ownMs?: number) => number;\n}\n\nexport interface LiveStats {\n chunks: number;\n lost: number;\n slack: number;\n filled: number;\n skipped: number;\n errorMin: number;\n errorMax: number;\n corrections: number;\n}\n\nexport const DEFAULT_LIVE_TIMELINE_CAPACITY = 256;\n\nconst DEADBAND_SEC = 0.001;\nconst SLEW_RATE = 0.003;\nconst REPLACE_SEC = 0.2;\nconst JOIN_US = 1000;\n\n/**\n * Maps a page performance timestamp (ms) to the corresponding AudioContext frame.\n */\nexport function contextFrame(clock: LiveClock, pageMs: number): number {\n return (clock.offset + pageMs / 1000) * clock.sampleRate;\n}\n\n/**\n * Estimates Worker-vs-Page monotonic clock skew using the minimum observed offset.\n */\nexport function pageClock(): PageClockEstimator {\n let aheadMs = Number.POSITIVE_INFINITY;\n return {\n pageMs: (ownMs = performance.now()): number => ownMs - aheadMs,\n saw: (pageMs: number, ownMs = performance.now()): void => {\n aheadMs = Math.min(aheadMs, ownMs - pageMs);\n },\n };\n}\n\nexport function noStats(): LiveStats {\n return {\n chunks: 0,\n corrections: 0,\n errorMax: Number.NEGATIVE_INFINITY,\n errorMin: Number.POSITIVE_INFINITY,\n filled: 0,\n lost: 0,\n skipped: 0,\n slack: Number.POSITIVE_INFINITY,\n };\n}\n\nexport function addStats(totals: LiveStats, stats: LiveStats): void {\n totals.chunks += stats.chunks;\n totals.lost += stats.lost;\n totals.slack = Math.min(totals.slack, stats.slack);\n totals.filled += stats.filled;\n totals.skipped += stats.skipped;\n totals.errorMin = Math.min(totals.errorMin, stats.errorMin);\n totals.errorMax = Math.max(totals.errorMax, stats.errorMax);\n totals.corrections += stats.corrections;\n}\n\nfunction interpolateSample(\n samples: Float32Array,\n nextChunk: LiveChunk | undefined,\n channelIndex: number,\n index: number,\n fraction: number\n): number {\n const sample = samples[index] ?? 0;\n if (fraction === 0) {\n return sample;\n }\n if (index + 1 < samples.length) {\n const following = samples[index + 1] ?? sample;\n return sample + (following - sample) * fraction;\n }\n const nextSamples =\n nextChunk?.channels[channelIndex] ?? nextChunk?.channels[0];\n const following =\n nextSamples && nextSamples.length > 0 ? (nextSamples[0] ?? sample) : sample;\n return sample + (following - sample) * fraction;\n}\n\n/**\n * Sample-accurate audio timeline used inside the `\"couch-live\"` AudioWorklet.\n * Backed by a fixed-capacity ring buffer (zero `Array.prototype.shift()`, zero\n * allocations during `render()`), dropping the oldest chunk on overflow,\n * slewing smoothly at ±0.3% outside a 1 ms deadband, and hard re-placing beyond\n * 200 ms error.\n */\nexport class LiveTimeline {\n private readonly capacity: number;\n private readonly ring: (LiveChunk | undefined)[];\n private head = 0;\n private tail = 0;\n private count = 0;\n private position = 0;\n private playing = false;\n private dry = false;\n private endStampUs: number | undefined;\n private correcting = 0;\n private settling = false;\n private rate = 1;\n private readonly sampleRate: number;\n\n stats: LiveStats = noStats();\n\n constructor(\n sampleRate: number,\n capacity: number = DEFAULT_LIVE_TIMELINE_CAPACITY\n ) {\n this.sampleRate = sampleRate;\n const normalizedCapacity = Number.isFinite(capacity)\n ? Math.max(2, Math.floor(capacity))\n : DEFAULT_LIVE_TIMELINE_CAPACITY;\n this.capacity = normalizedCapacity;\n this.ring = Array.from<LiveChunk | undefined>({\n length: normalizedCapacity,\n });\n }\n\n get length(): number {\n return this.count;\n }\n\n get maxCapacity(): number {\n return this.capacity;\n }\n\n reset(): void {\n for (let i = 0; i < this.capacity; i += 1) {\n this.ring[i] = undefined;\n }\n this.head = 0;\n this.tail = 0;\n this.count = 0;\n this.position = 0;\n this.playing = false;\n this.dry = false;\n this.settling = false;\n this.correcting = 0;\n this.rate = 1;\n this.endStampUs = undefined;\n this.resetStatsInPlace();\n }\n\n takeStats(): LiveStats {\n const snapshot: LiveStats = {\n chunks: this.stats.chunks,\n corrections: this.stats.corrections,\n errorMax: this.stats.errorMax,\n errorMin: this.stats.errorMin,\n filled: this.stats.filled,\n lost: this.stats.lost,\n skipped: this.stats.skipped,\n slack: this.stats.slack,\n };\n this.resetStatsInPlace();\n return snapshot;\n }\n\n /**\n * Copies accumulated stats into a preallocated target object and resets\n * counters in place with zero heap allocation (safe for AudioWorklet use).\n */\n takeStatsInto(target: LiveStats): void {\n target.chunks = this.stats.chunks;\n target.corrections = this.stats.corrections;\n target.errorMax = this.stats.errorMax;\n target.errorMin = this.stats.errorMin;\n target.filled = this.stats.filled;\n target.lost = this.stats.lost;\n target.skipped = this.stats.skipped;\n target.slack = this.stats.slack;\n this.resetStatsInPlace();\n }\n\n private resetStatsInPlace(): void {\n this.stats.chunks = 0;\n this.stats.corrections = 0;\n this.stats.errorMax = Number.NEGATIVE_INFINITY;\n this.stats.errorMin = Number.POSITIVE_INFINITY;\n this.stats.filled = 0;\n this.stats.lost = 0;\n this.stats.skipped = 0;\n this.stats.slack = Number.POSITIVE_INFINITY;\n }\n\n private peek(offset: number): LiveChunk | undefined {\n if (offset < 0 || offset >= this.count) {\n return undefined;\n }\n return this.ring[(this.head + offset) % this.capacity];\n }\n\n private dequeue(): LiveChunk | undefined {\n if (this.count === 0) {\n return undefined;\n }\n const item = this.ring[this.head];\n this.ring[this.head] = undefined;\n this.head = (this.head + 1) % this.capacity;\n this.count -= 1;\n return item;\n }\n\n private enqueue(chunk: LiveChunk): void {\n if (this.count === this.capacity) {\n // Ring buffer full: drop the oldest chunk in O(1) without allocating\n this.ring[this.head] = undefined;\n this.head = (this.head + 1) % this.capacity;\n this.count -= 1;\n this.stats.lost += 1;\n if (this.playing) {\n this.playing = false;\n this.position = 0;\n this.settling = false;\n this.correcting = 0;\n this.rate = 1;\n }\n }\n this.ring[this.tail] = chunk;\n this.tail = (this.tail + 1) % this.capacity;\n this.count += 1;\n }\n\n push(chunk: LiveChunk, next: number): void {\n const length = chunk.channels[0]?.length ?? 0;\n if (length === 0) {\n return;\n }\n const slack = this.playing ? this.queued() : chunk.frame - next;\n const follows =\n this.endStampUs !== undefined &&\n Math.abs(chunk.stampUs - this.endStampUs) <= JOIN_US;\n this.stats.chunks += 1;\n this.stats.slack = Math.min(this.stats.slack, slack);\n if (slack < 0 || (this.dry && follows)) {\n this.stats.lost += 1;\n }\n this.dry = false;\n this.enqueue(chunk);\n }\n\n render(frame: number, output: Float32Array[]): void {\n const frames = output[0]?.length ?? 0;\n if (this.playing) {\n this.steer(frame);\n }\n const channelCount = output.length;\n for (let i = 0; i < frames; i += 1) {\n if (!this.playing && !this.place(frame + i)) {\n for (let c = 0; c < channelCount; c += 1) {\n const outChannel = output[c];\n if (outChannel) {\n outChannel[i] = 0;\n }\n }\n if (this.dry) {\n this.stats.filled += 1;\n }\n continue;\n }\n this.read(output, i, frame + i);\n }\n }\n\n private queued(): number {\n let frames = -this.position;\n for (let i = 0; i < this.count; i += 1) {\n const chunk = this.ring[(this.head + i) % this.capacity];\n frames += chunk?.channels[0]?.length ?? 0;\n }\n return frames / this.rate;\n }\n\n private place(frame: number): boolean {\n while (this.count > 0) {\n const chunk = this.peek(0);\n if (!chunk) {\n return false;\n }\n const offset = Math.round(frame - chunk.frame);\n const length = chunk.channels[0]?.length ?? 0;\n if (offset < 0) {\n return false;\n }\n if (offset < length) {\n this.stats.skipped += offset;\n this.position = offset;\n this.playing = true;\n return true;\n }\n this.stats.skipped += length;\n this.dequeue();\n }\n return false;\n }\n\n private steer(frame: number): void {\n const chunk = this.peek(0);\n if (!chunk) {\n return;\n }\n const error = chunk.frame + this.position - frame;\n this.stats.errorMin = Math.min(this.stats.errorMin, error);\n this.stats.errorMax = Math.max(this.stats.errorMax, error);\n if (Math.abs(error) > REPLACE_SEC * this.sampleRate) {\n this.playing = false;\n this.settling = false;\n this.correcting = 0;\n this.rate = 1;\n return;\n }\n const sign = Math.sign(error);\n if (\n this.correcting === 0 &&\n Math.abs(error) > DEADBAND_SEC * this.sampleRate\n ) {\n this.correcting = sign;\n this.settling = false;\n this.rate = 1 - sign * SLEW_RATE;\n this.stats.corrections += 1;\n } else if (this.correcting !== 0 && sign !== this.correcting) {\n this.settling = true;\n }\n }\n\n private advancePosition(chunk: LiveChunk, currentFrameIndex: number): void {\n this.position += this.rate;\n if (this.settling) {\n const whole = Math.round(this.position);\n if (Math.abs(this.position - whole) < SLEW_RATE) {\n this.position = whole;\n this.rate = 1;\n this.correcting = 0;\n this.settling = false;\n }\n }\n const length = chunk.channels[0]?.length ?? 0;\n if (this.position < length) {\n return;\n }\n this.position -= length;\n this.endStampUs = chunk.stampUs + (length / this.sampleRate) * 1e6;\n this.dequeue();\n const nextChunk = this.peek(0);\n if (nextChunk) {\n const nextFrameIndex = currentFrameIndex + 1;\n const boundaryError = nextChunk.frame + this.position - nextFrameIndex;\n if (Math.abs(boundaryError) > REPLACE_SEC * this.sampleRate) {\n this.playing = false;\n this.settling = false;\n this.correcting = 0;\n this.rate = 1;\n this.position = 0;\n }\n return;\n }\n this.playing = false;\n this.dry = true;\n this.correcting = 0;\n this.settling = false;\n this.rate = 1;\n }\n\n private read(\n output: Float32Array[],\n i: number,\n currentFrameIndex: number\n ): void {\n const chunk = this.peek(0);\n if (!chunk) {\n return;\n }\n const next = this.peek(1);\n const index = Math.floor(this.position);\n const fraction = this.position - index;\n\n for (let c = 0; c < output.length; c += 1) {\n const outChannel = output[c];\n const samples = chunk.channels[c] ?? chunk.channels[0];\n if (outChannel && samples) {\n outChannel[i] = interpolateSample(samples, next, c, index, fraction);\n }\n }\n this.advancePosition(chunk, currentFrameIndex);\n }\n}\n","// Standalone \"couch-live\" AudioWorkletProcessor.\n// LiveTimeline is inlined by tsdown so dist/worklets/couch-live.js has ZERO imports.\nimport { LiveTimeline } from \"../sync/live-timeline.ts\";\nimport type { LiveMessage, LiveStats } from \"../sync/live-timeline.ts\";\n\ndeclare const sampleRate: number;\ndeclare const currentFrame: number;\ndeclare function registerProcessor(\n name: string,\n processor: new () => object\n): void;\ndeclare class AudioWorkletProcessor {\n readonly port: MessagePort;\n}\n\nexport const COUCH_LIVE_PROCESSOR_NAME = \"couch-live\" as const;\n\nif (typeof registerProcessor === \"function\") {\n const reportFrames = sampleRate / 10;\n\n registerProcessor(\n COUCH_LIVE_PROCESSOR_NAME,\n class CouchLiveProcessor extends AudioWorkletProcessor {\n private readonly timeline = new LiveTimeline(sampleRate);\n private readonly reportStats: LiveStats = {\n chunks: 0,\n corrections: 0,\n errorMax: Number.NEGATIVE_INFINITY,\n errorMin: Number.POSITIVE_INFINITY,\n filled: 0,\n lost: 0,\n skipped: 0,\n slack: Number.POSITIVE_INFINITY,\n };\n private readonly reportPayload: { epoch: number; stats: LiveStats };\n private epoch = 0;\n private next = currentFrame;\n private reported = currentFrame;\n private feed: MessagePort | undefined;\n private disposed = false;\n\n private readonly onFeedMessage = (\n msgEvent: MessageEvent<LiveMessage | null | undefined>\n ): void => {\n if (this.disposed) {\n return;\n }\n const msg = msgEvent.data;\n if (!msg || typeof msg !== \"object\") {\n return;\n }\n if (msg.type === \"dispose\") {\n this.dispose();\n return;\n }\n if (msg.type === \"chunk\") {\n if (msg.chunk) {\n this.timeline.push(msg.chunk, this.next);\n }\n return;\n }\n if (msg.type === \"reset\") {\n this.timeline.reset();\n this.epoch = typeof msg.epoch === \"number\" ? msg.epoch : 0;\n }\n };\n\n private readonly onPortMessage = (\n event: MessageEvent<\n { port?: MessagePort; type?: string } | null | undefined\n >\n ): void => {\n if (this.disposed) {\n return;\n }\n const { data } = event;\n if (!data || typeof data !== \"object\") {\n return;\n }\n if (data.type === \"dispose\") {\n this.dispose();\n return;\n }\n if (data.type === \"poll\") {\n this.flushReport();\n return;\n }\n if (!data.port) {\n return;\n }\n this.detachFeed();\n this.feed = data.port;\n this.feed.addEventListener(\n \"message\",\n this.onFeedMessage as EventListener\n );\n if (typeof this.feed.start === \"function\") {\n this.feed.start();\n }\n };\n\n constructor() {\n super();\n this.reportPayload = { epoch: 0, stats: this.reportStats };\n this.port.addEventListener(\n \"message\",\n this.onPortMessage as EventListener\n );\n if (typeof this.port.start === \"function\") {\n this.port.start();\n }\n }\n\n private detachFeed(): void {\n if (!this.feed) {\n return;\n }\n this.feed.removeEventListener(\n \"message\",\n this.onFeedMessage as EventListener\n );\n if (typeof this.feed.close === \"function\") {\n this.feed.close();\n }\n this.feed = undefined;\n }\n\n private dispose(): void {\n if (this.disposed) {\n return;\n }\n this.disposed = true;\n this.detachFeed();\n this.timeline.reset();\n this.port.removeEventListener(\n \"message\",\n this.onPortMessage as EventListener\n );\n if (typeof this.port.close === \"function\") {\n this.port.close();\n }\n }\n\n private flushReport(): void {\n if (this.disposed || !this.feed) {\n return;\n }\n this.timeline.takeStatsInto(this.reportStats);\n this.reportPayload.epoch = this.epoch;\n // oxlint-disable-next-line unicorn/require-post-message-target-origin\n this.feed.postMessage(this.reportPayload);\n }\n\n process(_inputs: Float32Array[][], outputs: Float32Array[][]): boolean {\n if (this.disposed) {\n return false;\n }\n const [output] = outputs;\n if (!output) {\n return true;\n }\n this.timeline.render(currentFrame, output);\n this.next = currentFrame + (output[0]?.length ?? 0);\n if (this.next - this.reported >= reportFrames) {\n this.reported = this.next;\n this.flushReport();\n }\n return true;\n }\n }\n );\n}\n"],"mappings":"AAkCA,MAAM,eAAe;AACrB,MAAM,YAAY;AAClB,MAAM,cAAc;AACpB,MAAM,UAAU;AAsBhB,SAAgB,UAAqB;CACnC,OAAO;EACL,QAAQ;EACR,aAAa;EACb,UAAU,OAAO;EACjB,UAAU,OAAO;EACjB,QAAQ;EACR,MAAM;EACN,SAAS;EACT,OAAO,OAAO;CAChB;AACF;AAaA,SAAS,kBACP,SACA,WACA,cACA,OACA,UACQ;CACR,MAAM,SAAS,QAAQ,UAAU;CACjC,IAAI,aAAa,GACf,OAAO;CAET,IAAI,QAAQ,IAAI,QAAQ,QAEtB,OAAO,WADW,QAAQ,QAAQ,MAAM,UACX,UAAU;CAEzC,MAAM,cACJ,WAAW,SAAS,iBAAiB,WAAW,SAAS;CAG3D,OAAO,WADL,eAAe,YAAY,SAAS,IAAK,YAAY,MAAM,SAAU,UAC1C,UAAU;AACzC;;;;;;;;AASA,IAAa,eAAb,MAA0B;CACP;CACA;CACT,OAAO;CACP,OAAO;CACP,QAAQ;CACR,WAAW;CACX,UAAU;CACV,MAAM;CACN;CACA,aAAa;CACb,WAAW;CACX,OAAO;CACE;CAEjB,QAAmB,QAAQ;CAE3B,YACE,YACA,WAAA,KACA;EACA,KAAK,aAAa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KAAK;GAEhC,KAAK,KAAK,YAAY,KAAK,aAAa;EAC1C;EAEA,QAAQ,SAA2B,SAAoC;GACrE,IAAI,KAAK,UACP,OAAO;GAET,MAAM,CAAC,UAAU;GACjB,IAAI,CAAC,QACH,OAAO;GAET,KAAK,SAAS,OAAO,cAAc,MAAM;GACzC,KAAK,OAAO,gBAAgB,OAAO,EAAE,EAAE,UAAU;GACjD,IAAI,KAAK,OAAO,KAAK,YAAY,cAAc;IAC7C,KAAK,WAAW,KAAK;IACrB,KAAK,YAAY;GACnB;GACA,OAAO;EACT;CACF,CACF;AACF"}
1
+ {"version":3,"file":"couch-live.js","names":[],"sources":["../../src/sync/live-timeline.ts","../../src/worklets/couch-live.ts"],"sourcesContent":["export interface LiveChunk {\n readonly frame: number;\n readonly stampUs: number;\n readonly channels: readonly Float32Array[];\n}\n\nexport type LiveMessage =\n | { readonly type: \"chunk\"; readonly chunk: LiveChunk }\n | { readonly type: \"reset\"; readonly epoch: number }\n | { readonly type: \"dispose\" };\n\nexport interface LiveClock {\n readonly offset: number;\n readonly sampleRate: number;\n}\n\nexport interface PageClockEstimator {\n readonly saw: (pageMs: number, ownMs?: number) => void;\n readonly pageMs: (ownMs?: number) => number;\n}\n\nexport interface LiveStats {\n chunks: number;\n lost: number;\n slack: number;\n filled: number;\n skipped: number;\n errorMin: number;\n errorMax: number;\n corrections: number;\n}\n\nexport const DEFAULT_LIVE_TIMELINE_CAPACITY = 256;\n\nconst DEADBAND_SEC = 0.001;\nconst SLEW_RATE = 0.003;\nconst REPLACE_SEC = 0.2;\nconst JOIN_US = 1000;\n\nexport const contextFrame = (clock: LiveClock, pageMs: number): number =>\n (clock.offset + pageMs / 1000) * clock.sampleRate;\n\nexport function pageClock(): PageClockEstimator {\n let aheadMs = Number.POSITIVE_INFINITY;\n return {\n pageMs: (ownMs = performance.now()): number => ownMs - aheadMs,\n saw: (pageMs: number, ownMs = performance.now()): void => {\n aheadMs = Math.min(aheadMs, ownMs - pageMs);\n },\n };\n}\n\nexport const noStats = (): LiveStats => ({\n chunks: 0,\n corrections: 0,\n errorMax: Number.NEGATIVE_INFINITY,\n errorMin: Number.POSITIVE_INFINITY,\n filled: 0,\n lost: 0,\n skipped: 0,\n slack: Number.POSITIVE_INFINITY,\n});\n\nexport function addStats(totals: LiveStats, stats: LiveStats): void {\n totals.chunks += stats.chunks;\n totals.lost += stats.lost;\n totals.slack = Math.min(totals.slack, stats.slack);\n totals.filled += stats.filled;\n totals.skipped += stats.skipped;\n totals.errorMin = Math.min(totals.errorMin, stats.errorMin);\n totals.errorMax = Math.max(totals.errorMax, stats.errorMax);\n totals.corrections += stats.corrections;\n}\n\nfunction resetStats(s: LiveStats): void {\n s.chunks = 0;\n s.corrections = 0;\n s.errorMax = Number.NEGATIVE_INFINITY;\n s.errorMin = Number.POSITIVE_INFINITY;\n s.filled = 0;\n s.lost = 0;\n s.skipped = 0;\n s.slack = Number.POSITIVE_INFINITY;\n}\n\nfunction interpolateSample(\n samples: Float32Array,\n nextChunk: LiveChunk | undefined,\n ch: number,\n idx: number,\n frac: number\n): number {\n const s = samples[idx] ?? 0;\n if (frac === 0) {\n return s;\n }\n const after =\n idx + 1 < samples.length\n ? (samples[idx + 1] ?? s)\n : ((nextChunk?.channels[ch] ?? nextChunk?.channels[0])?.[0] ?? s);\n return s + (after - s) * frac;\n}\n\nexport class LiveTimeline {\n private readonly capacity: number;\n private readonly ring: (LiveChunk | undefined)[];\n private head = 0;\n private tail = 0;\n private count = 0;\n private position = 0;\n private playing = false;\n private dry = false;\n private endStampUs: number | undefined;\n private correcting = 0;\n private settling = false;\n private rate = 1;\n private readonly sampleRate: number;\n\n stats: LiveStats = noStats();\n\n constructor(\n sampleRate: number,\n capacity: number = DEFAULT_LIVE_TIMELINE_CAPACITY\n ) {\n this.sampleRate = sampleRate;\n this.capacity = Number.isFinite(capacity)\n ? Math.max(2, Math.floor(capacity))\n : DEFAULT_LIVE_TIMELINE_CAPACITY;\n this.ring = Array.from<LiveChunk | undefined>({ length: this.capacity });\n }\n\n get length(): number {\n return this.count;\n }\n\n get maxCapacity(): number {\n return this.capacity;\n }\n\n reset(): void {\n this.ring.fill(undefined);\n this.head = 0;\n this.tail = 0;\n this.count = 0;\n this.dry = false;\n this.endStampUs = undefined;\n this.stopSlew(true);\n resetStats(this.stats);\n }\n\n takeStats(): LiveStats {\n const snap = noStats();\n this.takeStatsInto(snap);\n return snap;\n }\n\n takeStatsInto(target: LiveStats): void {\n target.chunks = this.stats.chunks;\n target.corrections = this.stats.corrections;\n target.errorMax = this.stats.errorMax;\n target.errorMin = this.stats.errorMin;\n target.filled = this.stats.filled;\n target.lost = this.stats.lost;\n target.skipped = this.stats.skipped;\n target.slack = this.stats.slack;\n resetStats(this.stats);\n }\n\n private stopSlew(resetPos = false): void {\n this.playing = false;\n this.settling = false;\n this.correcting = 0;\n this.rate = 1;\n if (resetPos) {\n this.position = 0;\n }\n }\n\n private at(offset: number): LiveChunk | undefined {\n return offset < this.count\n ? this.ring[(this.head + offset) % this.capacity]\n : undefined;\n }\n\n private dropHead(): void {\n this.ring[this.head] = undefined;\n this.head = (this.head + 1) % this.capacity;\n this.count -= 1;\n }\n\n push(chunk: LiveChunk, next: number): void {\n if ((chunk.channels[0]?.length ?? 0) === 0) {\n return;\n }\n const slack = this.playing ? this.queued() : chunk.frame - next;\n const follows =\n this.endStampUs !== undefined &&\n Math.abs(chunk.stampUs - this.endStampUs) <= JOIN_US;\n this.stats.chunks += 1;\n this.stats.slack = Math.min(this.stats.slack, slack);\n if (slack < 0 || (this.dry && follows)) {\n this.stats.lost += 1;\n }\n this.dry = false;\n if (this.count === this.capacity) {\n this.dropHead();\n this.stats.lost += 1;\n if (this.playing) {\n this.stopSlew(true);\n }\n }\n this.ring[this.tail] = chunk;\n this.tail = (this.tail + 1) % this.capacity;\n this.count += 1;\n }\n\n render(frame: number, output: Float32Array[]): void {\n const frames = output[0]?.length ?? 0;\n if (this.playing) {\n this.steer(frame);\n }\n const chCount = output.length;\n for (let i = 0; i < frames; i += 1) {\n if (!this.playing && !this.place(frame + i)) {\n for (let c = 0; c < chCount; c += 1) {\n const outCh = output[c];\n if (outCh) {\n outCh[i] = 0;\n }\n }\n if (this.dry) {\n this.stats.filled += 1;\n }\n continue;\n }\n this.read(output, i, frame + i);\n }\n }\n\n private queued(): number {\n let frames = -this.position;\n for (let i = 0; i < this.count; i += 1) {\n frames +=\n this.ring[(this.head + i) % this.capacity]?.channels[0]?.length ?? 0;\n }\n return frames / this.rate;\n }\n\n private place(frame: number): boolean {\n while (this.count > 0) {\n const chunk = this.at(0);\n if (!chunk) {\n return false;\n }\n const offset = Math.round(frame - chunk.frame);\n const len = chunk.channels[0]?.length ?? 0;\n if (offset < 0) {\n return false;\n }\n if (offset < len) {\n this.stats.skipped += offset;\n this.position = offset;\n this.playing = true;\n return true;\n }\n this.stats.skipped += len;\n this.dropHead();\n }\n return false;\n }\n\n private steer(frame: number): void {\n const chunk = this.at(0);\n if (!chunk) {\n return;\n }\n const err = chunk.frame + this.position - frame;\n this.stats.errorMin = Math.min(this.stats.errorMin, err);\n this.stats.errorMax = Math.max(this.stats.errorMax, err);\n if (Math.abs(err) > REPLACE_SEC * this.sampleRate) {\n this.stopSlew();\n return;\n }\n const sign = Math.sign(err);\n if (\n this.correcting === 0 &&\n Math.abs(err) > DEADBAND_SEC * this.sampleRate\n ) {\n this.correcting = sign;\n this.settling = false;\n this.rate = 1 - sign * SLEW_RATE;\n this.stats.corrections += 1;\n } else if (this.correcting !== 0 && sign !== this.correcting) {\n this.settling = true;\n }\n }\n\n private read(output: Float32Array[], i: number, frameIdx: number): void {\n const chunk = this.at(0);\n if (!chunk) {\n return;\n }\n const next = this.at(1);\n const idx = Math.floor(this.position);\n const frac = this.position - idx;\n const chCount = output.length;\n for (let c = 0; c < chCount; c += 1) {\n const outCh = output[c];\n const samples = chunk.channels[c] ?? chunk.channels[0];\n if (outCh && samples) {\n outCh[i] = interpolateSample(samples, next, c, idx, frac);\n }\n }\n this.position += this.rate;\n const whole = Math.round(this.position);\n if (this.settling && Math.abs(this.position - whole) < SLEW_RATE) {\n this.position = whole;\n this.rate = 1;\n this.correcting = 0;\n this.settling = false;\n }\n const len = chunk.channels[0]?.length ?? 0;\n if (this.position < len) {\n return;\n }\n this.position -= len;\n this.endStampUs = chunk.stampUs + (len / this.sampleRate) * 1e6;\n this.dropHead();\n const nextChunk = this.at(0);\n if (!nextChunk) {\n this.dry = true;\n this.stopSlew();\n } else if (\n Math.abs(nextChunk.frame + this.position - (frameIdx + 1)) >\n REPLACE_SEC * this.sampleRate\n ) {\n this.stopSlew(true);\n }\n }\n}\n","// Standalone \"couch-live\" AudioWorkletProcessor (LiveTimeline inlined by tsdown).\nimport { LiveTimeline, noStats } from \"../sync/live-timeline.ts\";\nimport type { LiveMessage, LiveStats } from \"../sync/live-timeline.ts\";\n\ndeclare const sampleRate: number;\ndeclare const currentFrame: number;\ndeclare function registerProcessor(\n name: string,\n processor: new () => object\n): void;\ndeclare class AudioWorkletProcessor {\n readonly port: MessagePort;\n}\n\nexport const COUCH_LIVE_PROCESSOR_NAME = \"couch-live\" as const;\n\nif (typeof registerProcessor === \"function\") {\n const reportFrames = sampleRate / 10;\n\n registerProcessor(\n COUCH_LIVE_PROCESSOR_NAME,\n class CouchLiveProcessor extends AudioWorkletProcessor {\n private readonly timeline = new LiveTimeline(sampleRate);\n private readonly reportStats: LiveStats = noStats();\n private readonly reportPayload: { epoch: number; stats: LiveStats };\n private epoch = 0;\n private next = currentFrame;\n private reported = currentFrame;\n private feed: MessagePort | undefined;\n private disposed = false;\n\n private readonly onFeedMessage = (\n msgEvent: MessageEvent<LiveMessage | null | undefined>\n ): void => {\n const msg = msgEvent.data;\n if (this.disposed || !msg || typeof msg !== \"object\") {\n return;\n }\n if (msg.type === \"dispose\") {\n this.dispose();\n } else if (msg.type === \"chunk\" && msg.chunk) {\n this.timeline.push(msg.chunk, this.next);\n } else if (msg.type === \"reset\") {\n this.timeline.reset();\n this.epoch = typeof msg.epoch === \"number\" ? msg.epoch : 0;\n }\n };\n\n private readonly onPortMessage = (\n event: MessageEvent<\n { port?: MessagePort; type?: string } | null | undefined\n >\n ): void => {\n const { data } = event;\n if (this.disposed || !data || typeof data !== \"object\") {\n return;\n }\n if (data.type === \"dispose\") {\n this.dispose();\n } else if (data.type === \"poll\") {\n this.flushReport();\n } else if (data.port) {\n this.detachFeed();\n this.feed = data.port;\n this.feed.addEventListener(\n \"message\",\n this.onFeedMessage as EventListener\n );\n this.feed.start?.();\n }\n };\n\n constructor() {\n super();\n this.reportPayload = { epoch: 0, stats: this.reportStats };\n this.port.addEventListener(\n \"message\",\n this.onPortMessage as EventListener\n );\n this.port.start?.();\n }\n\n private detachFeed(): void {\n if (!this.feed) {\n return;\n }\n this.feed.removeEventListener(\n \"message\",\n this.onFeedMessage as EventListener\n );\n this.feed.close?.();\n this.feed = undefined;\n }\n\n private dispose(): void {\n if (this.disposed) {\n return;\n }\n this.disposed = true;\n this.detachFeed();\n this.timeline.reset();\n this.port.removeEventListener(\n \"message\",\n this.onPortMessage as EventListener\n );\n this.port.close?.();\n }\n\n private flushReport(): void {\n if (this.disposed || !this.feed) {\n return;\n }\n this.timeline.takeStatsInto(this.reportStats);\n this.reportPayload.epoch = this.epoch;\n // oxlint-disable-next-line unicorn/require-post-message-target-origin\n this.feed.postMessage(this.reportPayload);\n }\n\n process(_inputs: Float32Array[][], outputs: Float32Array[][]): boolean {\n if (this.disposed) {\n return false;\n }\n const [output] = outputs;\n if (!output) {\n return true;\n }\n this.timeline.render(currentFrame, output);\n this.next = currentFrame + (output[0]?.length ?? 0);\n if (this.next - this.reported >= reportFrames) {\n this.reported = this.next;\n this.flushReport();\n }\n return true;\n }\n }\n );\n}\n"],"mappings":"AAkCA,MAAM,eAAe;AACrB,MAAM,YAAY;AAClB,MAAM,cAAc;AACpB,MAAM,UAAU;AAehB,MAAa,iBAA4B;CACvC,QAAQ;CACR,aAAa;CACb,UAAU,OAAO;CACjB,UAAU,OAAO;CACjB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O;IAAG,OAAO,KAAK;GAAY;GACzD,KAAK,KAAK,iBACR,WACA,KAAK,aACP;GACA,KAAK,KAAK,QAAQ;EACpB;EAEQ,aAAmB;GACzB,IAAI,CAAC,KAAK,MACR;GAEF,KAAK,KAAK,oBACR,WACA,KAAK,aACP;GACA,KAAK,KAAK,QAAQ;GAClB,KAAK,OAAO,KAAA;EACd;EAEQ,UAAgB;GACtB,IAAI,KAAK,UACP;GAEF,KAAK,WAAW;GAChB,KAAK,WAAW;GAChB,KAAK,SAAS,MAAM;GACpB,KAAK,KAAK,oBACR,WACA,KAAK,aACP;GACA,KAAK,KAAK,QAAQ;EACpB;EAEQ,cAAoB;GAC1B,IAAI,KAAK,YAAY,CAAC,KAAK,MACzB;GAEF,KAAK,SAAS,cAAc,KAAK,WAAW;GAC5C,KAAK,cAAc,QAAQ,KAAK;GAEhC,KAAK,KAAK,YAAY,KAAK,aAAa;EAC1C;EAEA,QAAQ,SAA2B,SAAoC;GACrE,IAAI,KAAK,UACP,OAAO;GAET,MAAM,CAAC,UAAU;GACjB,IAAI,CAAC,QACH,OAAO;GAET,KAAK,SAAS,OAAO,cAAc,MAAM;GACzC,KAAK,OAAO,gBAAgB,OAAO,EAAE,EAAE,UAAU;GACjD,IAAI,KAAK,OAAO,KAAK,YAAY,cAAc;IAC7C,KAAK,WAAW,KAAK;IACrB,KAAK,YAAY;GACnB;GACA,OAAO;EACT;CACF,CACF;AACF"}
@@ -16,17 +16,15 @@ if (typeof registerProcessor === "function") registerProcessor(METER_PROCESSOR_N
16
16
  };
17
17
  constructor(options) {
18
18
  super(options);
19
- const intervalMs = options?.processorOptions?.reportIntervalMs ?? 50;
20
- const clampedMs = Math.max(10, Math.min(1e3, intervalMs));
19
+ const clampedMs = Math.max(10, Math.min(1e3, options?.processorOptions?.reportIntervalMs ?? 50));
21
20
  this.reportSamples = Math.max(DEFAULT_QUANTUM_FRAMES, Math.round(sampleRate * clampedMs / 1e3));
22
21
  this.port.addEventListener("message", (event) => {
23
22
  if (event.data === "poll") this.flushMeter();
24
23
  });
25
- if (typeof this.port.start === "function") this.port.start();
24
+ this.port.start?.();
26
25
  }
27
26
  flushMeter() {
28
- const count = this.sampleCount;
29
- const inv = count > 0 ? 1 / count : 0;
27
+ const inv = this.sampleCount > 0 ? 1 / this.sampleCount : 0;
30
28
  this.payload.leftPeak = this.leftPeak;
31
29
  this.payload.rightPeak = this.rightPeak;
32
30
  this.payload.leftRms = Math.sqrt(this.leftSumSq * inv);
@@ -38,33 +36,29 @@ if (typeof registerProcessor === "function") registerProcessor(METER_PROCESSOR_N
38
36
  this.rightSumSq = 0;
39
37
  this.sampleCount = 0;
40
38
  }
41
- recordSilence(outputs) {
42
- const outputLen = (outputs?.[0])?.[0]?.length ?? 0;
43
- this.sampleCount += outputLen > 0 ? outputLen : DEFAULT_QUANTUM_FRAMES;
44
- if (this.sampleCount >= this.reportSamples) this.flushMeter();
45
- }
46
39
  process(inputs, outputs) {
47
- const [input] = inputs;
48
- const left = input && input.length > 0 ? input[0] : void 0;
40
+ const left = inputs[0]?.[0];
49
41
  if (!left || left.length === 0) {
50
- this.recordSilence(outputs);
42
+ const outLen = outputs?.[0]?.[0]?.length ?? 0;
43
+ this.sampleCount += outLen > 0 ? outLen : DEFAULT_QUANTUM_FRAMES;
44
+ if (this.sampleCount >= this.reportSamples) this.flushMeter();
51
45
  return true;
52
46
  }
53
- const right = input && input.length > 1 && input[1] ? input[1] : left;
47
+ const right = inputs[0]?.[1] ?? left;
54
48
  const frames = left.length;
55
49
  let lPeak = this.leftPeak;
56
50
  let rPeak = this.rightPeak;
57
51
  let lSumSq = this.leftSumSq;
58
52
  let rSumSq = this.rightSumSq;
59
53
  for (let i = 0; i < frames; i += 1) {
60
- const lSample = left[i] ?? 0;
61
- const rSample = right[i] ?? 0;
62
- const lAbs = lSample < 0 ? -lSample : lSample;
63
- const rAbs = rSample < 0 ? -rSample : rSample;
54
+ const l = left[i] ?? 0;
55
+ const r = right[i] ?? 0;
56
+ const lAbs = l < 0 ? -l : l;
57
+ const rAbs = r < 0 ? -r : r;
64
58
  if (lAbs > lPeak) lPeak = lAbs;
65
59
  if (rAbs > rPeak) rPeak = rAbs;
66
- lSumSq += lSample * lSample;
67
- rSumSq += rSample * rSample;
60
+ lSumSq += l * l;
61
+ rSumSq += r * r;
68
62
  }
69
63
  this.leftPeak = lPeak;
70
64
  this.rightPeak = rPeak;
@@ -1 +1 @@
1
- {"version":3,"file":"meter.js","names":[],"sources":["../../src/worklets/meter.ts"],"sourcesContent":["// Standalone AudioWorkletProcessor for stereo peak & RMS metering.\n// Zero imports: compiles to a self-contained zero-import ESM file.\n\ndeclare const sampleRate: number;\ndeclare function registerProcessor(\n name: string,\n processor: new (options?: {\n processorOptions?: { reportIntervalMs?: number };\n }) => object\n): void;\ndeclare class AudioWorkletProcessor {\n readonly port: MessagePort;\n constructor(options?: { processorOptions?: { reportIntervalMs?: number } });\n}\n\nexport const METER_PROCESSOR_NAME = \"avoid-meter\" as const;\n\nexport interface MeterWorkletFrame {\n readonly leftPeak: number;\n readonly rightPeak: number;\n readonly leftRms: number;\n readonly rightRms: number;\n}\n\nconst DEFAULT_QUANTUM_FRAMES = 128;\n\nif (typeof registerProcessor === \"function\") {\n registerProcessor(\n METER_PROCESSOR_NAME,\n class AvoidMeterProcessor extends AudioWorkletProcessor {\n private leftPeak = 0;\n private rightPeak = 0;\n private leftSumSq = 0;\n private rightSumSq = 0;\n private sampleCount = 0;\n private readonly reportSamples: number;\n private readonly payload = {\n leftPeak: 0,\n leftRms: 0,\n rightPeak: 0,\n rightRms: 0,\n };\n\n constructor(options?: {\n processorOptions?: { reportIntervalMs?: number };\n }) {\n super(options);\n const intervalMs = options?.processorOptions?.reportIntervalMs ?? 50;\n const clampedMs = Math.max(10, Math.min(1000, intervalMs));\n this.reportSamples = Math.max(\n DEFAULT_QUANTUM_FRAMES,\n Math.round((sampleRate * clampedMs) / 1000)\n );\n this.port.addEventListener(\"message\", (event: MessageEvent) => {\n if (event.data === \"poll\") {\n this.flushMeter();\n }\n });\n if (typeof this.port.start === \"function\") {\n this.port.start();\n }\n }\n\n private flushMeter(): void {\n const count = this.sampleCount;\n const inv = count > 0 ? 1 / count : 0;\n this.payload.leftPeak = this.leftPeak;\n this.payload.rightPeak = this.rightPeak;\n this.payload.leftRms = Math.sqrt(this.leftSumSq * inv);\n this.payload.rightRms = Math.sqrt(this.rightSumSq * inv);\n // oxlint-disable-next-line unicorn/require-post-message-target-origin\n this.port.postMessage(this.payload);\n this.leftPeak = 0;\n this.rightPeak = 0;\n this.leftSumSq = 0;\n this.rightSumSq = 0;\n this.sampleCount = 0;\n }\n\n private recordSilence(outputs?: Float32Array[][]): void {\n const output = outputs?.[0];\n const outputLen = output?.[0]?.length ?? 0;\n this.sampleCount += outputLen > 0 ? outputLen : DEFAULT_QUANTUM_FRAMES;\n if (this.sampleCount >= this.reportSamples) {\n this.flushMeter();\n }\n }\n\n process(inputs: Float32Array[][], outputs?: Float32Array[][]): boolean {\n const [input] = inputs;\n const left = input && input.length > 0 ? input[0] : undefined;\n\n if (!left || left.length === 0) {\n // Inactive/disconnected input still represents silence: advance\n // sampleCount and flush zeros instead of freezing the last peak.\n this.recordSilence(outputs);\n return true;\n }\n\n const right = input && input.length > 1 && input[1] ? input[1] : left;\n const frames = left.length;\n\n let lPeak = this.leftPeak;\n let rPeak = this.rightPeak;\n let lSumSq = this.leftSumSq;\n let rSumSq = this.rightSumSq;\n\n for (let i = 0; i < frames; i += 1) {\n const lSample = left[i] ?? 0;\n const rSample = right[i] ?? 0;\n const lAbs = lSample < 0 ? -lSample : lSample;\n const rAbs = rSample < 0 ? -rSample : rSample;\n if (lAbs > lPeak) {\n lPeak = lAbs;\n }\n if (rAbs > rPeak) {\n rPeak = rAbs;\n }\n lSumSq += lSample * lSample;\n rSumSq += rSample * rSample;\n }\n\n this.leftPeak = lPeak;\n this.rightPeak = rPeak;\n this.leftSumSq = lSumSq;\n this.rightSumSq = rSumSq;\n this.sampleCount += frames;\n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AAK,WAAW;CAEpB;CAEA,QAAQ,QAA0B,SAAqC;EACrE,MAAM,CAAC,SAAS;EAChB,MAAM,OAAO,SAAS,MAAM,SAAS,IAAI,MAAM,KAAK,KAAA;EAEpD,IAAI,CAAC,QAAQ,KAAK,WAAW,GAAG;GAG9B,KAAK,cAAc,OAAO;GAC1B,OAAO;EACT;EAEA,MAAM,QAAQ,SAAS,MAAM,SAAS,KAAK,MAAM,KAAK,MAAM,KAAK;EACjE,MAAM,SAAS,KAAK;EAEpB,IAAI,QAAQ,KAAK;EACjB,IAAI,QAAQ,KAAK;EACjB,IAAI,SAAS,KAAK;EAClB,IAAI,SAAS,KAAK;EAElB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,KAAK,GAAG;GAClC,MAAM,UAAU,KAAK,MAAM;GAC3B,MAAM,UAAU,MAAM,MAAM;GAC5B,MAAM,OAAO,UAAU,IAAI,CAAC,UAAU;GACtC,MAAM,OAAO,UAAU,IAAI,CAAC,UAAU;GACtC,IAAI,OAAO,OACT,QAAQ;GAEV,IAAI,OAAO,OACT,QAAQ;GAEV,UAAU,UAAU;GACpB,UAAU,UAAU;EACtB;EAEA,KAAK,WAAW;EAChB,KAAK,YAAY;EACjB,KAAK,YAAY;EACjB,KAAK,aAAa;EAClB,KAAK,eAAe;EAEpB,IAAI,KAAK,eAAe,KAAK,eAC3B,KAAK,WAAW;EAGlB,OAAO;CACT;AACF,CACF"}
1
+ {"version":3,"file":"meter.js","names":[],"sources":["../../src/worklets/meter.ts"],"sourcesContent":["// Standalone zero-import AudioWorkletProcessor for stereo peak & RMS metering.\n\ndeclare const sampleRate: number;\ndeclare function registerProcessor(\n name: string,\n processor: new (options?: {\n processorOptions?: { reportIntervalMs?: number };\n }) => object\n): void;\ndeclare class AudioWorkletProcessor {\n readonly port: MessagePort;\n constructor(options?: { processorOptions?: { reportIntervalMs?: number } });\n}\n\nexport const METER_PROCESSOR_NAME = \"avoid-meter\" as const;\n\nexport interface MeterWorkletFrame {\n readonly leftPeak: number;\n readonly rightPeak: number;\n readonly leftRms: number;\n readonly rightRms: number;\n}\n\nconst DEFAULT_QUANTUM_FRAMES = 128;\n\nif (typeof registerProcessor === \"function\") {\n registerProcessor(\n METER_PROCESSOR_NAME,\n class AvoidMeterProcessor extends AudioWorkletProcessor {\n private leftPeak = 0;\n private rightPeak = 0;\n private leftSumSq = 0;\n private rightSumSq = 0;\n private sampleCount = 0;\n private readonly reportSamples: number;\n private readonly payload = {\n leftPeak: 0,\n leftRms: 0,\n rightPeak: 0,\n rightRms: 0,\n };\n\n constructor(options?: {\n processorOptions?: { reportIntervalMs?: number };\n }) {\n super(options);\n const clampedMs = Math.max(\n 10,\n Math.min(1000, options?.processorOptions?.reportIntervalMs ?? 50)\n );\n this.reportSamples = Math.max(\n DEFAULT_QUANTUM_FRAMES,\n Math.round((sampleRate * clampedMs) / 1000)\n );\n this.port.addEventListener(\"message\", (event: MessageEvent) => {\n if (event.data === \"poll\") {\n this.flushMeter();\n }\n });\n this.port.start?.();\n }\n\n private flushMeter(): void {\n const inv = this.sampleCount > 0 ? 1 / this.sampleCount : 0;\n this.payload.leftPeak = this.leftPeak;\n this.payload.rightPeak = this.rightPeak;\n this.payload.leftRms = Math.sqrt(this.leftSumSq * inv);\n this.payload.rightRms = Math.sqrt(this.rightSumSq * inv);\n // oxlint-disable-next-line unicorn/require-post-message-target-origin\n this.port.postMessage(this.payload);\n this.leftPeak = 0;\n this.rightPeak = 0;\n this.leftSumSq = 0;\n this.rightSumSq = 0;\n this.sampleCount = 0;\n }\n\n process(inputs: Float32Array[][], outputs?: Float32Array[][]): boolean {\n const left = inputs[0]?.[0];\n if (!left || left.length === 0) {\n const outLen = outputs?.[0]?.[0]?.length ?? 0;\n this.sampleCount += outLen > 0 ? outLen : DEFAULT_QUANTUM_FRAMES;\n if (this.sampleCount >= this.reportSamples) {\n this.flushMeter();\n }\n return true;\n }\n\n const right = inputs[0]?.[1] ?? left;\n const frames = left.length;\n let lPeak = this.leftPeak;\n let rPeak = this.rightPeak;\n let lSumSq = this.leftSumSq;\n let rSumSq = this.rightSumSq;\n\n for (let i = 0; i < frames; i += 1) {\n const l = left[i] ?? 0;\n const r = right[i] ?? 0;\n const lAbs = l < 0 ? -l : l;\n const rAbs = r < 0 ? -r : r;\n if (lAbs > lPeak) {\n lPeak = lAbs;\n }\n if (rAbs > rPeak) {\n rPeak = rAbs;\n }\n lSumSq += l * l;\n rSumSq += r * r;\n }\n\n this.leftPeak = lPeak;\n this.rightPeak = rPeak;\n this.leftSumSq = lSumSq;\n this.rightSumSq = rSumSq;\n this.sampleCount += frames;\n if (this.sampleCount >= this.reportSamples) {\n this.flushMeter();\n }\n return true;\n }\n }\n );\n}\n"],"mappings":";AAcA,MAAa,uBAAuB;AASpC,MAAM,yBAAyB;AAE/B,IAAI,OAAO,sBAAsB,YAC/B,kBACE,sBACA,MAAM,4BAA4B,sBAAsB;CAC9C,WAAW;CACX,YAAY;CACZ,YAAY;CACZ,aAAa;CACb,cAAc;CACL;CACA,UAAU;EACzB,UAAU;EACV,SAAS;EACT,WAAW;EACX,UAAU;CACZ;CAEA,YAAY,SAET;EACD,MAAM,OAAO;EACb,MAAM,YAAY,KAAK,IACrB,IACA,KAAK,IAAI,KAAM,SAAS,kBAAkB,oBAAoB,EAAE,CAClE;EACA,KAAK,gBAAgB,KAAK,IACxB,wBACA,KAAK,MAAO,aAAa,YAAa,GAAI,CAC5C;EACA,KAAK,KAAK,iBAAiB,YAAY,UAAwB;GAC7D,IAAI,MAAM,SAAS,QACjB,KAAK,WAAW;EAEpB,CAAC;EACD,KAAK,KAAK,QAAQ;CACpB;CAEQ,aAAmB;EACzB,MAAM,MAAM,KAAK,cAAc,IAAI,IAAI,KAAK,cAAc;EAC1D,KAAK,QAAQ,WAAW,KAAK;EAC7B,KAAK,QAAQ,YAAY,KAAK;EAC9B,KAAK,QAAQ,UAAU,KAAK,KAAK,KAAK,YAAY,GAAG;EACrD,KAAK,QAAQ,WAAW,KAAK,KAAK,KAAK,aAAa,GAAG;EAEvD,KAAK,KAAK,YAAY,KAAK,OAAO;EAClC,KAAK,WAAW;EAChB,KAAK,YAAY;EACjB,KAAK,YAAY;EACjB,KAAK,aAAa;EAClB,KAAK,cAAc;CACrB;CAEA,QAAQ,QAA0B,SAAqC;EACrE,MAAM,OAAO,OAAO,EAAE,GAAG;EACzB,IAAI,CAAC,QAAQ,KAAK,WAAW,GAAG;GAC9B,MAAM,SAAS,UAAU,EAAE,GAAG,EAAE,EAAE,UAAU;GAC5C,KAAK,eAAe,SAAS,IAAI,SAAS;GAC1C,IAAI,KAAK,eAAe,KAAK,eAC3B,KAAK,WAAW;GAElB,OAAO;EACT;EAEA,MAAM,QAAQ,OAAO,EAAE,GAAG,MAAM;EAChC,MAAM,SAAS,KAAK;EACpB,IAAI,QAAQ,KAAK;EACjB,IAAI,QAAQ,KAAK;EACjB,IAAI,SAAS,KAAK;EAClB,IAAI,SAAS,KAAK;EAElB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,KAAK,GAAG;GAClC,MAAM,IAAI,KAAK,MAAM;GACrB,MAAM,IAAI,MAAM,MAAM;GACtB,MAAM,OAAO,IAAI,IAAI,CAAC,IAAI;GAC1B,MAAM,OAAO,IAAI,IAAI,CAAC,IAAI;GAC1B,IAAI,OAAO,OACT,QAAQ;GAEV,IAAI,OAAO,OACT,QAAQ;GAEV,UAAU,IAAI;GACd,UAAU,IAAI;EAChB;EAEA,KAAK,WAAW;EAChB,KAAK,YAAY;EACjB,KAAK,YAAY;EACjB,KAAK,aAAa;EAClB,KAAK,eAAe;EACpB,IAAI,KAAK,eAAe,KAAK,eAC3B,KAAK,WAAW;EAElB,OAAO;CACT;AACF,CACF"}
@@ -0,0 +1,6 @@
1
+ export declare const PCM_CAPTURE_PROCESSOR_NAME: "avoid-pcm-capture";
2
+ export declare const PCM_CAPTURE_DEFAULT_CHUNK_FRAMES = 2048;
3
+ /** Main-thread → processor message that ends the processor's lifetime. */
4
+ export declare const PCM_CAPTURE_STOP_MESSAGE: "stop";
5
+ /** Clamps a requested chunk size to whole frames within `[128, 16384]`. */
6
+ export declare function clampPcmChunkFrames(chunkFrames: number | undefined): number;