@avoid.quest/audio 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +70 -2
- package/dist/effects/index.d.ts +266 -0
- package/dist/effects/index.js +744 -0
- package/dist/effects/index.js.map +1 -0
- package/dist/errors-BT9HxDLn.js +74 -0
- package/dist/errors-BT9HxDLn.js.map +1 -0
- package/dist/errors-ChTQFwuZ.d.ts +53 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/midi/index.d.ts +162 -0
- package/dist/midi/index.js +338 -0
- package/dist/midi/index.js.map +1 -0
- package/dist/playback/index.d.ts +349 -0
- package/dist/playback/index.js +1372 -0
- package/dist/playback/index.js.map +1 -0
- package/dist/queue/index.d.ts +144 -0
- package/dist/queue/index.js +1062 -0
- package/dist/queue/index.js.map +1 -0
- package/dist/recognize/index.d.ts +161 -0
- package/dist/recognize/index.js +2816 -0
- package/dist/recognize/index.js.map +1 -0
- package/dist/routing/index.d.ts +140 -0
- package/dist/routing/index.js +678 -0
- package/dist/routing/index.js.map +1 -0
- package/dist/sync/index.d.ts +282 -0
- package/dist/sync/index.js +1103 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/worklet-loader-BvEW0oVO.d.ts +44 -0
- package/dist/worklet-loader-S7R8ZAmP.js +175 -0
- package/dist/worklet-loader-S7R8ZAmP.js.map +1 -0
- package/dist/worklets/couch-live.d.ts +1 -0
- package/dist/worklets/couch-live.js +365 -0
- package/dist/worklets/couch-live.js.map +1 -0
- package/dist/worklets/meter.d.ts +7 -0
- package/dist/worklets/meter.js +80 -0
- package/dist/worklets/meter.js.map +1 -0
- package/package.json +109 -4
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
import { i as loadAudioWorklet, n as createWorkletBlobUrl, r as createWorkletModuleUrl, t as VITE_AUDIO_WORKLET_OPTIMIZE_DEPS_EXCLUDE } from "../worklet-loader-S7R8ZAmP.js";
|
|
2
|
+
import { Effect, Fiber, Scope, Semaphore } from "effect";
|
|
3
|
+
import { s as AudioRoutingError } from "../errors-BT9HxDLn.js";
|
|
4
|
+
// src/worklets/meter.ts
|
|
5
|
+
const METER_PROCESSOR_NAME = "avoid-meter";
|
|
6
|
+
const DEFAULT_QUANTUM_FRAMES = 128;
|
|
7
|
+
if (typeof registerProcessor === "function") registerProcessor(METER_PROCESSOR_NAME, class AvoidMeterProcessor extends AudioWorkletProcessor {
|
|
8
|
+
leftPeak = 0;
|
|
9
|
+
rightPeak = 0;
|
|
10
|
+
leftSumSq = 0;
|
|
11
|
+
rightSumSq = 0;
|
|
12
|
+
sampleCount = 0;
|
|
13
|
+
reportSamples;
|
|
14
|
+
payload = {
|
|
15
|
+
leftPeak: 0,
|
|
16
|
+
leftRms: 0,
|
|
17
|
+
rightPeak: 0,
|
|
18
|
+
rightRms: 0
|
|
19
|
+
};
|
|
20
|
+
constructor(options) {
|
|
21
|
+
super(options);
|
|
22
|
+
const intervalMs = options?.processorOptions?.reportIntervalMs ?? 50;
|
|
23
|
+
const clampedMs = Math.max(10, Math.min(1e3, intervalMs));
|
|
24
|
+
this.reportSamples = Math.max(DEFAULT_QUANTUM_FRAMES, Math.round(sampleRate * clampedMs / 1e3));
|
|
25
|
+
this.port.addEventListener("message", (event) => {
|
|
26
|
+
if (event.data === "poll") this.flushMeter();
|
|
27
|
+
});
|
|
28
|
+
if (typeof this.port.start === "function") this.port.start();
|
|
29
|
+
}
|
|
30
|
+
flushMeter() {
|
|
31
|
+
const count = this.sampleCount;
|
|
32
|
+
const inv = count > 0 ? 1 / count : 0;
|
|
33
|
+
this.payload.leftPeak = this.leftPeak;
|
|
34
|
+
this.payload.rightPeak = this.rightPeak;
|
|
35
|
+
this.payload.leftRms = Math.sqrt(this.leftSumSq * inv);
|
|
36
|
+
this.payload.rightRms = Math.sqrt(this.rightSumSq * inv);
|
|
37
|
+
this.port.postMessage(this.payload);
|
|
38
|
+
this.leftPeak = 0;
|
|
39
|
+
this.rightPeak = 0;
|
|
40
|
+
this.leftSumSq = 0;
|
|
41
|
+
this.rightSumSq = 0;
|
|
42
|
+
this.sampleCount = 0;
|
|
43
|
+
}
|
|
44
|
+
recordSilence(outputs) {
|
|
45
|
+
const outputLen = (outputs?.[0])?.[0]?.length ?? 0;
|
|
46
|
+
this.sampleCount += outputLen > 0 ? outputLen : DEFAULT_QUANTUM_FRAMES;
|
|
47
|
+
if (this.sampleCount >= this.reportSamples) this.flushMeter();
|
|
48
|
+
}
|
|
49
|
+
process(inputs, outputs) {
|
|
50
|
+
const [input] = inputs;
|
|
51
|
+
const left = input && input.length > 0 ? input[0] : void 0;
|
|
52
|
+
if (!left || left.length === 0) {
|
|
53
|
+
this.recordSilence(outputs);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
const right = input && input.length > 1 && input[1] ? input[1] : left;
|
|
57
|
+
const frames = left.length;
|
|
58
|
+
let lPeak = this.leftPeak;
|
|
59
|
+
let rPeak = this.rightPeak;
|
|
60
|
+
let lSumSq = this.leftSumSq;
|
|
61
|
+
let rSumSq = this.rightSumSq;
|
|
62
|
+
for (let i = 0; i < frames; i += 1) {
|
|
63
|
+
const lSample = left[i] ?? 0;
|
|
64
|
+
const rSample = right[i] ?? 0;
|
|
65
|
+
const lAbs = lSample < 0 ? -lSample : lSample;
|
|
66
|
+
const rAbs = rSample < 0 ? -rSample : rSample;
|
|
67
|
+
if (lAbs > lPeak) lPeak = lAbs;
|
|
68
|
+
if (rAbs > rPeak) rPeak = rAbs;
|
|
69
|
+
lSumSq += lSample * lSample;
|
|
70
|
+
rSumSq += rSample * rSample;
|
|
71
|
+
}
|
|
72
|
+
this.leftPeak = lPeak;
|
|
73
|
+
this.rightPeak = rPeak;
|
|
74
|
+
this.leftSumSq = lSumSq;
|
|
75
|
+
this.rightSumSq = rSumSq;
|
|
76
|
+
this.sampleCount += frames;
|
|
77
|
+
if (this.sampleCount >= this.reportSamples) this.flushMeter();
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
// src/routing/metering.ts
|
|
82
|
+
const DEFAULT_METER_FLOOR_DB = -60;
|
|
83
|
+
const DEFAULT_METER_INTERVAL_MS = 50;
|
|
84
|
+
const DEFAULT_ANALYSER_FFT_SIZE = 256;
|
|
85
|
+
/**
|
|
86
|
+
* Converts a linear amplitude `[0, ∞)` to decibels (`-120 dB` floor for silence).
|
|
87
|
+
*/
|
|
88
|
+
function linearToDb(amplitude) {
|
|
89
|
+
return 20 * Math.log10(Math.max(Math.abs(amplitude), 1e-6));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Normalizes a dB level against a noise floor (default `-60 dB`) into `[0, 1]`.
|
|
93
|
+
*/
|
|
94
|
+
function dbToNormalized(db, floorDb = -60) {
|
|
95
|
+
if (floorDb >= 0) return 0;
|
|
96
|
+
return Math.max(0, Math.min(1, (db - floorDb) / -floorDb));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Computes sample peak and RMS over a time-domain buffer.
|
|
100
|
+
*/
|
|
101
|
+
function computePeakAndRms(samples) {
|
|
102
|
+
if (samples.length === 0) return {
|
|
103
|
+
peak: 0,
|
|
104
|
+
rms: 0
|
|
105
|
+
};
|
|
106
|
+
let peak = 0;
|
|
107
|
+
let sumSq = 0;
|
|
108
|
+
for (const val of samples) {
|
|
109
|
+
const abs = val < 0 ? -val : val;
|
|
110
|
+
if (abs > peak) peak = abs;
|
|
111
|
+
sumSq += val * val;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
peak,
|
|
115
|
+
rms: Math.sqrt(sumSq / samples.length)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function buildStereoMeterReading(frame, floorDb = -60) {
|
|
119
|
+
const leftDb = linearToDb(frame.leftPeak);
|
|
120
|
+
const rightDb = linearToDb(frame.rightPeak);
|
|
121
|
+
return {
|
|
122
|
+
leftDb,
|
|
123
|
+
leftNormalized: dbToNormalized(leftDb, floorDb),
|
|
124
|
+
leftPeak: frame.leftPeak,
|
|
125
|
+
leftRms: frame.leftRms,
|
|
126
|
+
rightDb,
|
|
127
|
+
rightNormalized: dbToNormalized(rightDb, floorDb),
|
|
128
|
+
rightPeak: frame.rightPeak,
|
|
129
|
+
rightRms: frame.rightRms
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const ZERO_READING = buildStereoMeterReading({
|
|
133
|
+
leftPeak: 0,
|
|
134
|
+
leftRms: 0,
|
|
135
|
+
rightPeak: 0,
|
|
136
|
+
rightRms: 0
|
|
137
|
+
});
|
|
138
|
+
/**
|
|
139
|
+
* Loads the standalone `@avoid.quest/audio/worklets/meter` AudioWorklet module
|
|
140
|
+
* into `context` with Blob-URL fallback support.
|
|
141
|
+
*/
|
|
142
|
+
function loadMeterWorklet(context, options = {}) {
|
|
143
|
+
return loadAudioWorklet(context, {
|
|
144
|
+
fallbackToBlob: options.fallbackToBlob ?? true,
|
|
145
|
+
fetchImpl: options.fetchImpl,
|
|
146
|
+
inlineSource: options.inlineSource,
|
|
147
|
+
moduleUrl: options.inlineSource === void 0 ? options.moduleUrl ?? createWorkletModuleUrl("meter") : void 0,
|
|
148
|
+
processorName: METER_PROCESSOR_NAME
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function noopUnsubscribe() {}
|
|
152
|
+
function buildStereoMeter(context, source, scope, options = {}) {
|
|
153
|
+
const mode = options.mode ?? "auto";
|
|
154
|
+
const intervalMs = options.intervalMs ?? 50;
|
|
155
|
+
const fftSize = options.fftSize ?? 256;
|
|
156
|
+
const floorDb = options.floorDb ?? -60;
|
|
157
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
158
|
+
let latest = ZERO_READING;
|
|
159
|
+
let active = false;
|
|
160
|
+
let disposed = false;
|
|
161
|
+
let teardownImpl;
|
|
162
|
+
const emit = (reading) => {
|
|
163
|
+
latest = reading;
|
|
164
|
+
for (const listener of listeners) listener(reading);
|
|
165
|
+
};
|
|
166
|
+
const startAnalyser = () => {
|
|
167
|
+
const splitter = context.createChannelSplitter(2);
|
|
168
|
+
const leftAnalyser = context.createAnalyser();
|
|
169
|
+
const rightAnalyser = context.createAnalyser();
|
|
170
|
+
leftAnalyser.fftSize = fftSize;
|
|
171
|
+
rightAnalyser.fftSize = fftSize;
|
|
172
|
+
const leftBuffer = new Float32Array(fftSize);
|
|
173
|
+
const rightBuffer = new Float32Array(fftSize);
|
|
174
|
+
source.connect(splitter);
|
|
175
|
+
splitter.connect(leftAnalyser, 0);
|
|
176
|
+
splitter.connect(rightAnalyser, 1);
|
|
177
|
+
const timer = setInterval(() => {
|
|
178
|
+
leftAnalyser.getFloatTimeDomainData(leftBuffer);
|
|
179
|
+
rightAnalyser.getFloatTimeDomainData(rightBuffer);
|
|
180
|
+
const left = computePeakAndRms(leftBuffer);
|
|
181
|
+
const right = computePeakAndRms(rightBuffer);
|
|
182
|
+
emit(buildStereoMeterReading({
|
|
183
|
+
leftPeak: left.peak,
|
|
184
|
+
leftRms: left.rms,
|
|
185
|
+
rightPeak: right.peak,
|
|
186
|
+
rightRms: right.rms
|
|
187
|
+
}, floorDb));
|
|
188
|
+
}, intervalMs);
|
|
189
|
+
return () => {
|
|
190
|
+
clearInterval(timer);
|
|
191
|
+
try {
|
|
192
|
+
source.disconnect(splitter);
|
|
193
|
+
} catch {}
|
|
194
|
+
splitter.disconnect();
|
|
195
|
+
leftAnalyser.disconnect();
|
|
196
|
+
rightAnalyser.disconnect();
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
const startWorklet = () => {
|
|
200
|
+
let cancelled = false;
|
|
201
|
+
let workletNode;
|
|
202
|
+
let silentSink;
|
|
203
|
+
let fallbackTeardown;
|
|
204
|
+
const onWorkletMessage = (event) => {
|
|
205
|
+
if (!cancelled && event.data) emit(buildStereoMeterReading(event.data, floorDb));
|
|
206
|
+
};
|
|
207
|
+
const activateFallback = () => {
|
|
208
|
+
if (!cancelled && mode === "auto" && !fallbackTeardown) fallbackTeardown = startAnalyser();
|
|
209
|
+
};
|
|
210
|
+
const startupEffect = loadMeterWorklet(context, options.workletOptions).pipe(Effect.match({
|
|
211
|
+
onFailure: () => {
|
|
212
|
+
activateFallback();
|
|
213
|
+
},
|
|
214
|
+
onSuccess: () => {
|
|
215
|
+
if (cancelled || typeof globalThis.AudioWorkletNode !== "function") return;
|
|
216
|
+
try {
|
|
217
|
+
workletNode = new globalThis.AudioWorkletNode(context, METER_PROCESSOR_NAME, {
|
|
218
|
+
numberOfInputs: 1,
|
|
219
|
+
numberOfOutputs: 1,
|
|
220
|
+
outputChannelCount: [1],
|
|
221
|
+
processorOptions: { reportIntervalMs: intervalMs }
|
|
222
|
+
});
|
|
223
|
+
silentSink = context.createGain();
|
|
224
|
+
silentSink.gain.value = 0;
|
|
225
|
+
workletNode.port.addEventListener("message", onWorkletMessage);
|
|
226
|
+
if (typeof workletNode.port.start === "function") workletNode.port.start();
|
|
227
|
+
source.connect(workletNode);
|
|
228
|
+
workletNode.connect(silentSink);
|
|
229
|
+
if (context.destination) silentSink.connect(context.destination);
|
|
230
|
+
} catch {
|
|
231
|
+
if (workletNode) {
|
|
232
|
+
workletNode.port.removeEventListener("message", onWorkletMessage);
|
|
233
|
+
if (typeof workletNode.port.close === "function") workletNode.port.close();
|
|
234
|
+
try {
|
|
235
|
+
source.disconnect(workletNode);
|
|
236
|
+
} catch {}
|
|
237
|
+
workletNode.disconnect();
|
|
238
|
+
}
|
|
239
|
+
workletNode = void 0;
|
|
240
|
+
silentSink?.disconnect();
|
|
241
|
+
silentSink = void 0;
|
|
242
|
+
activateFallback();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}));
|
|
246
|
+
const startupFiber = Fiber.runIn(scope)(Effect.runFork(startupEffect));
|
|
247
|
+
return () => {
|
|
248
|
+
cancelled = true;
|
|
249
|
+
startupFiber.interruptUnsafe();
|
|
250
|
+
if (workletNode) {
|
|
251
|
+
workletNode.port.removeEventListener("message", onWorkletMessage);
|
|
252
|
+
if (typeof workletNode.port.close === "function") workletNode.port.close();
|
|
253
|
+
try {
|
|
254
|
+
source.disconnect(workletNode);
|
|
255
|
+
} catch {}
|
|
256
|
+
workletNode.disconnect();
|
|
257
|
+
workletNode = void 0;
|
|
258
|
+
}
|
|
259
|
+
if (silentSink) {
|
|
260
|
+
silentSink.disconnect();
|
|
261
|
+
silentSink = void 0;
|
|
262
|
+
}
|
|
263
|
+
fallbackTeardown?.();
|
|
264
|
+
fallbackTeardown = void 0;
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
const activate = () => {
|
|
268
|
+
if (active) return;
|
|
269
|
+
active = true;
|
|
270
|
+
teardownImpl = mode !== "analyser" && typeof globalThis.AudioWorkletNode === "function" && Boolean(context.audioWorklet) ? startWorklet() : startAnalyser();
|
|
271
|
+
};
|
|
272
|
+
const deactivate = () => {
|
|
273
|
+
if (!active) return;
|
|
274
|
+
active = false;
|
|
275
|
+
teardownImpl?.();
|
|
276
|
+
teardownImpl = void 0;
|
|
277
|
+
};
|
|
278
|
+
const subscribe = (listener) => {
|
|
279
|
+
if (disposed) return noopUnsubscribe;
|
|
280
|
+
listeners.add(listener);
|
|
281
|
+
if (listeners.size === 1) activate();
|
|
282
|
+
return () => {
|
|
283
|
+
listeners.delete(listener);
|
|
284
|
+
if (listeners.size === 0) deactivate();
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
const dispose = () => {
|
|
288
|
+
disposed = true;
|
|
289
|
+
listeners.clear();
|
|
290
|
+
deactivate();
|
|
291
|
+
};
|
|
292
|
+
return {
|
|
293
|
+
dispose,
|
|
294
|
+
getLatest: () => latest,
|
|
295
|
+
isActive: () => active,
|
|
296
|
+
subscribe
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Scoped subscriber-driven stereo peak/RMS meter supporting both the standalone
|
|
301
|
+
* `"avoid-meter"` AudioWorklet (wired through a zero-gain sink for Safari/Firefox
|
|
302
|
+
* pull-graph compatibility) and a zero-worklet `ChannelSplitter(2) -> 2x AnalyserNode` fallback.
|
|
303
|
+
* Activates DSP metering only while `listeners.size > 0` and guarantees deterministic
|
|
304
|
+
* node/worklet/timer teardown when the owning Effect `Scope` closes.
|
|
305
|
+
*/
|
|
306
|
+
function createStereoMeter(context, source, options = {}) {
|
|
307
|
+
return Effect.gen(function* () {
|
|
308
|
+
const scope = yield* Scope.Scope;
|
|
309
|
+
const meter = buildStereoMeter(context, source, scope, options);
|
|
310
|
+
yield* Scope.addFinalizer(scope, Effect.sync(() => {
|
|
311
|
+
meter.dispose();
|
|
312
|
+
}));
|
|
313
|
+
return meter;
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
// src/routing/multi-sink.ts
|
|
317
|
+
const MAX_OUTPUT_DELAY_MS = 500;
|
|
318
|
+
const DELAY_RAMP_TIME_CONSTANT_S = .02;
|
|
319
|
+
/**
|
|
320
|
+
* Separately detects `AudioContext.prototype.setSinkId` (Chromium) and
|
|
321
|
+
* `HTMLMediaElement.prototype.setSinkId` (Chromium + Firefox).
|
|
322
|
+
*/
|
|
323
|
+
function detectSinkIdCapabilities() {
|
|
324
|
+
return {
|
|
325
|
+
audioContextSetSinkId: globalThis.AudioContext !== void 0 && "setSinkId" in globalThis.AudioContext.prototype,
|
|
326
|
+
mediaElementSetSinkId: globalThis.HTMLMediaElement !== void 0 && "setSinkId" in globalThis.HTMLMediaElement.prototype
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function clampOutputDelayMs(delayMs, maxDelayMs = 500) {
|
|
330
|
+
if (!Number.isFinite(delayMs)) return 0;
|
|
331
|
+
return Math.max(0, Math.min(maxDelayMs, delayMs));
|
|
332
|
+
}
|
|
333
|
+
function applyDelayParam(delayNode, context, delayMs) {
|
|
334
|
+
const seconds = delayMs / 1e3;
|
|
335
|
+
if (typeof delayNode.delayTime.setTargetAtTime === "function") delayNode.delayTime.setTargetAtTime(seconds, context.currentTime, DELAY_RAMP_TIME_CONSTANT_S);
|
|
336
|
+
else delayNode.delayTime.value = seconds;
|
|
337
|
+
}
|
|
338
|
+
function createDefaultElement() {
|
|
339
|
+
return new globalThis.Audio();
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Creates a `Scope`-managed transactional multi-sink router supporting:
|
|
343
|
+
* - Main output bus (`mainInput -> mainDelay -> context.destination`) + `realtimeInput` bypass
|
|
344
|
+
* - Dedicated headphone CUE bus (`cueInput -> cueDelay -> cueGain -> MediaStreamDestination -> <audio>.setSinkId`)
|
|
345
|
+
* - Arbitrary multi-device output sinks with per-output delay compensation and atomic swap on attach
|
|
346
|
+
* - Effect `Semaphore` serialization and full transactional rollback when `setSinkId` or `element.play()` fails.
|
|
347
|
+
*/
|
|
348
|
+
function createMultiSinkRouter(context, options = {}) {
|
|
349
|
+
return Effect.gen(function* () {
|
|
350
|
+
const maxDelayMs = options.maxDelayMs ?? 500;
|
|
351
|
+
const maxDelaySeconds = maxDelayMs / 1e3;
|
|
352
|
+
const createElement = options.createAudioElement ?? createDefaultElement;
|
|
353
|
+
const capabilities = {
|
|
354
|
+
audioContextSetSinkId: typeof context.setSinkId === "function" || detectSinkIdCapabilities().audioContextSetSinkId,
|
|
355
|
+
mediaElementSetSinkId: detectSinkIdCapabilities().mediaElementSetSinkId
|
|
356
|
+
};
|
|
357
|
+
const mainInput = context.createGain();
|
|
358
|
+
const realtimeInput = context.createGain();
|
|
359
|
+
const mainDelay = context.createDelay(maxDelaySeconds);
|
|
360
|
+
mainDelay.delayTime.value = 0;
|
|
361
|
+
mainInput.connect(mainDelay);
|
|
362
|
+
mainDelay.connect(context.destination);
|
|
363
|
+
realtimeInput.connect(context.destination);
|
|
364
|
+
const cueInput = context.createGain();
|
|
365
|
+
const cueDelay = context.createDelay(maxDelaySeconds);
|
|
366
|
+
cueDelay.delayTime.value = 0;
|
|
367
|
+
const cueGain = context.createGain();
|
|
368
|
+
cueGain.gain.value = 1;
|
|
369
|
+
cueInput.connect(cueDelay);
|
|
370
|
+
cueDelay.connect(cueGain);
|
|
371
|
+
let cueDestination;
|
|
372
|
+
let cueElement;
|
|
373
|
+
let mainSinkId = context.sinkId ?? "";
|
|
374
|
+
let mainDelayMs = 0;
|
|
375
|
+
let cueSinkId;
|
|
376
|
+
let cueDelayMs = 0;
|
|
377
|
+
let cueVolume = 1;
|
|
378
|
+
let disposed = false;
|
|
379
|
+
const deviceSinks = /* @__PURE__ */ new Map();
|
|
380
|
+
const deviceGenerations = /* @__PURE__ */ new Map();
|
|
381
|
+
const deviceSemaphores = /* @__PURE__ */ new Map();
|
|
382
|
+
const txSemaphore = yield* Semaphore.make(1);
|
|
383
|
+
const getDeviceSemaphore = (nodeId) => {
|
|
384
|
+
let sem = deviceSemaphores.get(nodeId);
|
|
385
|
+
if (!sem) {
|
|
386
|
+
sem = Semaphore.makeUnsafe(1);
|
|
387
|
+
deviceSemaphores.set(nodeId, sem);
|
|
388
|
+
}
|
|
389
|
+
return sem;
|
|
390
|
+
};
|
|
391
|
+
const teardownCueOutput = () => {
|
|
392
|
+
if (cueDestination) {
|
|
393
|
+
try {
|
|
394
|
+
cueGain.disconnect(cueDestination);
|
|
395
|
+
} catch {}
|
|
396
|
+
cueDestination = void 0;
|
|
397
|
+
}
|
|
398
|
+
if (cueElement) {
|
|
399
|
+
cueElement.pause();
|
|
400
|
+
cueElement.srcObject = null;
|
|
401
|
+
cueElement = void 0;
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
const configureCueOutput = async (nextCueSinkId) => {
|
|
405
|
+
if (!nextCueSinkId) {
|
|
406
|
+
teardownCueOutput();
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (typeof context.createMediaStreamDestination !== "function") throw new TypeError("AudioContext.createMediaStreamDestination is not supported.");
|
|
410
|
+
const nextDest = context.createMediaStreamDestination();
|
|
411
|
+
const nextEl = createElement();
|
|
412
|
+
if (typeof nextEl.setSinkId !== "function") throw new TypeError("HTMLMediaElement.setSinkId is not supported.");
|
|
413
|
+
nextEl.srcObject = nextDest.stream;
|
|
414
|
+
try {
|
|
415
|
+
await nextEl.setSinkId(nextCueSinkId);
|
|
416
|
+
await nextEl.play();
|
|
417
|
+
} catch (error) {
|
|
418
|
+
nextEl.pause();
|
|
419
|
+
nextEl.srcObject = null;
|
|
420
|
+
throw error;
|
|
421
|
+
}
|
|
422
|
+
teardownCueOutput();
|
|
423
|
+
cueDestination = nextDest;
|
|
424
|
+
cueElement = nextEl;
|
|
425
|
+
cueGain.connect(nextDest);
|
|
426
|
+
};
|
|
427
|
+
const getState = () => {
|
|
428
|
+
const devices = {};
|
|
429
|
+
for (const [nodeId, sink] of deviceSinks) devices[nodeId] = {
|
|
430
|
+
delayMs: sink.delayMs,
|
|
431
|
+
deviceId: sink.deviceId,
|
|
432
|
+
status: sink.status
|
|
433
|
+
};
|
|
434
|
+
return {
|
|
435
|
+
cueDelayMs,
|
|
436
|
+
cueSinkId,
|
|
437
|
+
cueVolume,
|
|
438
|
+
deviceSinks: devices,
|
|
439
|
+
mainDelayMs,
|
|
440
|
+
mainSinkId
|
|
441
|
+
};
|
|
442
|
+
};
|
|
443
|
+
const setMainDelayMs = (delayMs) => {
|
|
444
|
+
mainDelayMs = clampOutputDelayMs(delayMs, maxDelayMs);
|
|
445
|
+
applyDelayParam(mainDelay, context, mainDelayMs);
|
|
446
|
+
return mainDelayMs;
|
|
447
|
+
};
|
|
448
|
+
const setCueDelayMs = (delayMs) => {
|
|
449
|
+
cueDelayMs = clampOutputDelayMs(delayMs, maxDelayMs);
|
|
450
|
+
applyDelayParam(cueDelay, context, cueDelayMs);
|
|
451
|
+
return cueDelayMs;
|
|
452
|
+
};
|
|
453
|
+
const setCueVolume = (volume) => {
|
|
454
|
+
cueVolume = Math.max(0, Math.min(1, Number.isFinite(volume) ? volume : 1));
|
|
455
|
+
cueGain.gain.value = cueVolume;
|
|
456
|
+
return cueVolume;
|
|
457
|
+
};
|
|
458
|
+
const rollbackTransaction = async (prev, mainChanged) => {
|
|
459
|
+
const { cueDelayMs: prevCueDelayMs, cueSinkId: prevCueSinkId, cueVolume: prevCueVolume, mainDelayMs: prevMainDelayMs, mainSinkId: prevMainSinkId } = prev;
|
|
460
|
+
let rolledBack = true;
|
|
461
|
+
if (mainChanged && typeof context.setSinkId === "function") try {
|
|
462
|
+
await context.setSinkId(prevMainSinkId);
|
|
463
|
+
mainSinkId = prevMainSinkId;
|
|
464
|
+
} catch {
|
|
465
|
+
rolledBack = false;
|
|
466
|
+
}
|
|
467
|
+
else mainSinkId = prevMainSinkId;
|
|
468
|
+
setMainDelayMs(prevMainDelayMs);
|
|
469
|
+
setCueDelayMs(prevCueDelayMs);
|
|
470
|
+
setCueVolume(prevCueVolume);
|
|
471
|
+
if (cueSinkId !== prevCueSinkId) try {
|
|
472
|
+
await configureCueOutput(prevCueSinkId);
|
|
473
|
+
cueSinkId = prevCueSinkId;
|
|
474
|
+
} catch {
|
|
475
|
+
rolledBack = false;
|
|
476
|
+
teardownCueOutput();
|
|
477
|
+
cueSinkId = void 0;
|
|
478
|
+
}
|
|
479
|
+
return rolledBack;
|
|
480
|
+
};
|
|
481
|
+
const applyTransaction = (patch) => txSemaphore.withPermits(1)(Effect.uninterruptible(Effect.tryPromise({
|
|
482
|
+
catch: (error) => {
|
|
483
|
+
if (error instanceof AudioRoutingError) return error;
|
|
484
|
+
return new AudioRoutingError({
|
|
485
|
+
message: String(error),
|
|
486
|
+
rolledBack: true
|
|
487
|
+
});
|
|
488
|
+
},
|
|
489
|
+
try: async () => {
|
|
490
|
+
if (disposed) throw new AudioRoutingError({
|
|
491
|
+
deviceId: patch.cueSinkId ?? patch.mainSinkId,
|
|
492
|
+
message: "Multi-sink router has been disposed.",
|
|
493
|
+
rolledBack: true
|
|
494
|
+
});
|
|
495
|
+
const prev = {
|
|
496
|
+
cueDelayMs,
|
|
497
|
+
cueSinkId,
|
|
498
|
+
cueVolume,
|
|
499
|
+
mainDelayMs,
|
|
500
|
+
mainSinkId
|
|
501
|
+
};
|
|
502
|
+
const targetMainSinkId = patch.mainSinkId === void 0 ? prev.mainSinkId : patch.mainSinkId;
|
|
503
|
+
let targetCueSinkId = "cueSinkId" in patch ? patch.cueSinkId : prev.cueSinkId;
|
|
504
|
+
if (targetCueSinkId !== void 0 && targetCueSinkId !== "" && targetCueSinkId === targetMainSinkId) targetCueSinkId = void 0;
|
|
505
|
+
let mainChanged = false;
|
|
506
|
+
try {
|
|
507
|
+
if (targetMainSinkId !== prev.mainSinkId) {
|
|
508
|
+
if (typeof context.setSinkId !== "function") throw new TypeError("AudioContext.setSinkId is not supported.");
|
|
509
|
+
await context.setSinkId(targetMainSinkId);
|
|
510
|
+
mainChanged = true;
|
|
511
|
+
mainSinkId = targetMainSinkId;
|
|
512
|
+
}
|
|
513
|
+
if (targetCueSinkId !== prev.cueSinkId) {
|
|
514
|
+
await configureCueOutput(targetCueSinkId);
|
|
515
|
+
cueSinkId = targetCueSinkId;
|
|
516
|
+
}
|
|
517
|
+
if (patch.mainDelayMs !== void 0) setMainDelayMs(patch.mainDelayMs);
|
|
518
|
+
if (patch.cueDelayMs !== void 0) setCueDelayMs(patch.cueDelayMs);
|
|
519
|
+
if (patch.cueVolume !== void 0) setCueVolume(patch.cueVolume);
|
|
520
|
+
return getState();
|
|
521
|
+
} catch (error) {
|
|
522
|
+
const rolledBack = await rollbackTransaction(prev, mainChanged);
|
|
523
|
+
throw new AudioRoutingError({
|
|
524
|
+
deviceId: patch.cueSinkId ?? patch.mainSinkId,
|
|
525
|
+
message: rolledBack ? `Sink routing transaction failed and rolled back: ${String(error)}` : `Sink routing transaction failed with incomplete rollback: ${String(error)}`,
|
|
526
|
+
rolledBack
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
})));
|
|
531
|
+
const setMainSink = (sinkId) => applyTransaction({ mainSinkId: sinkId });
|
|
532
|
+
const setCueSink = (sinkId) => applyTransaction({ cueSinkId: sinkId });
|
|
533
|
+
const nextDeviceGeneration = (nodeId) => {
|
|
534
|
+
const nextGen = (deviceGenerations.get(nodeId) ?? 0) + 1;
|
|
535
|
+
deviceGenerations.set(nodeId, nextGen);
|
|
536
|
+
return nextGen;
|
|
537
|
+
};
|
|
538
|
+
const teardownDeviceSinkRecord = (sink) => {
|
|
539
|
+
try {
|
|
540
|
+
sink.source.disconnect(sink.inputGain);
|
|
541
|
+
} catch {}
|
|
542
|
+
sink.inputGain.disconnect();
|
|
543
|
+
sink.delayNode.disconnect();
|
|
544
|
+
sink.element.pause();
|
|
545
|
+
sink.element.srcObject = null;
|
|
546
|
+
};
|
|
547
|
+
const detachDeviceSink = (nodeId) => {
|
|
548
|
+
nextDeviceGeneration(nodeId);
|
|
549
|
+
const existing = deviceSinks.get(nodeId);
|
|
550
|
+
if (!existing) return;
|
|
551
|
+
deviceSinks.delete(nodeId);
|
|
552
|
+
teardownDeviceSinkRecord(existing);
|
|
553
|
+
};
|
|
554
|
+
const attachDeviceSink = (nodeId, source, deviceId, delayMs = 0) => Effect.suspend(() => {
|
|
555
|
+
const myGeneration = nextDeviceGeneration(nodeId);
|
|
556
|
+
const isStale = () => disposed || deviceGenerations.get(nodeId) !== myGeneration;
|
|
557
|
+
return getDeviceSemaphore(nodeId).withPermits(1)(Effect.uninterruptible(Effect.tryPromise({
|
|
558
|
+
catch: (error) => {
|
|
559
|
+
if (error instanceof AudioRoutingError) return error;
|
|
560
|
+
return new AudioRoutingError({
|
|
561
|
+
deviceId,
|
|
562
|
+
message: `Failed to attach device sink "${deviceId}": ${String(error)}`,
|
|
563
|
+
rolledBack: true
|
|
564
|
+
});
|
|
565
|
+
},
|
|
566
|
+
try: async () => {
|
|
567
|
+
if (disposed) throw new AudioRoutingError({
|
|
568
|
+
deviceId,
|
|
569
|
+
message: "Multi-sink router has been disposed.",
|
|
570
|
+
rolledBack: true
|
|
571
|
+
});
|
|
572
|
+
if (isStale()) throw new AudioRoutingError({
|
|
573
|
+
deviceId,
|
|
574
|
+
message: `Device sink "${deviceId}" attachment was superseded or cancelled.`,
|
|
575
|
+
rolledBack: true
|
|
576
|
+
});
|
|
577
|
+
if (typeof context.createMediaStreamDestination !== "function") throw new AudioRoutingError({
|
|
578
|
+
deviceId,
|
|
579
|
+
message: "AudioContext.createMediaStreamDestination is not supported.",
|
|
580
|
+
rolledBack: true
|
|
581
|
+
});
|
|
582
|
+
const clampedDelay = clampOutputDelayMs(delayMs, maxDelayMs);
|
|
583
|
+
const destination = context.createMediaStreamDestination();
|
|
584
|
+
const element = createElement();
|
|
585
|
+
if (typeof element.setSinkId !== "function") throw new AudioRoutingError({
|
|
586
|
+
deviceId,
|
|
587
|
+
message: "HTMLMediaElement.setSinkId is not supported.",
|
|
588
|
+
rolledBack: true
|
|
589
|
+
});
|
|
590
|
+
const inputGain = context.createGain();
|
|
591
|
+
const delayNode = context.createDelay(maxDelaySeconds);
|
|
592
|
+
applyDelayParam(delayNode, context, clampedDelay);
|
|
593
|
+
const cleanupStaged = () => {
|
|
594
|
+
try {
|
|
595
|
+
source.disconnect(inputGain);
|
|
596
|
+
} catch {}
|
|
597
|
+
inputGain.disconnect();
|
|
598
|
+
delayNode.disconnect();
|
|
599
|
+
element.pause();
|
|
600
|
+
element.srcObject = null;
|
|
601
|
+
};
|
|
602
|
+
try {
|
|
603
|
+
element.srcObject = destination.stream;
|
|
604
|
+
await element.setSinkId(deviceId);
|
|
605
|
+
if (isStale()) throw new AudioRoutingError({
|
|
606
|
+
deviceId,
|
|
607
|
+
message: `Device sink "${deviceId}" attachment was superseded or cancelled.`,
|
|
608
|
+
rolledBack: true
|
|
609
|
+
});
|
|
610
|
+
await element.play();
|
|
611
|
+
if (isStale()) throw new AudioRoutingError({
|
|
612
|
+
deviceId,
|
|
613
|
+
message: `Device sink "${deviceId}" attachment was superseded or cancelled.`,
|
|
614
|
+
rolledBack: true
|
|
615
|
+
});
|
|
616
|
+
source.connect(inputGain);
|
|
617
|
+
inputGain.connect(delayNode);
|
|
618
|
+
delayNode.connect(destination);
|
|
619
|
+
} catch (error) {
|
|
620
|
+
cleanupStaged();
|
|
621
|
+
if (error instanceof AudioRoutingError) throw error;
|
|
622
|
+
throw new AudioRoutingError({
|
|
623
|
+
deviceId,
|
|
624
|
+
message: `Failed to attach device sink "${deviceId}": ${String(error)}`,
|
|
625
|
+
rolledBack: true
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
const previous = deviceSinks.get(nodeId);
|
|
629
|
+
deviceSinks.set(nodeId, {
|
|
630
|
+
delayMs: clampedDelay,
|
|
631
|
+
delayNode,
|
|
632
|
+
destination,
|
|
633
|
+
deviceId,
|
|
634
|
+
element,
|
|
635
|
+
inputGain,
|
|
636
|
+
source,
|
|
637
|
+
status: "ok"
|
|
638
|
+
});
|
|
639
|
+
if (previous) teardownDeviceSinkRecord(previous);
|
|
640
|
+
return "ok";
|
|
641
|
+
}
|
|
642
|
+
})));
|
|
643
|
+
});
|
|
644
|
+
const dispose = () => {
|
|
645
|
+
disposed = true;
|
|
646
|
+
for (const nodeId of deviceSinks.keys()) detachDeviceSink(nodeId);
|
|
647
|
+
teardownCueOutput();
|
|
648
|
+
mainInput.disconnect();
|
|
649
|
+
realtimeInput.disconnect();
|
|
650
|
+
mainDelay.disconnect();
|
|
651
|
+
cueInput.disconnect();
|
|
652
|
+
cueDelay.disconnect();
|
|
653
|
+
cueGain.disconnect();
|
|
654
|
+
};
|
|
655
|
+
yield* Scope.addFinalizer(yield* Scope.Scope, Effect.sync(() => {
|
|
656
|
+
dispose();
|
|
657
|
+
}));
|
|
658
|
+
return {
|
|
659
|
+
applyTransaction,
|
|
660
|
+
attachDeviceSink,
|
|
661
|
+
capabilities,
|
|
662
|
+
cueInput,
|
|
663
|
+
detachDeviceSink,
|
|
664
|
+
dispose,
|
|
665
|
+
getState,
|
|
666
|
+
mainInput,
|
|
667
|
+
realtimeInput,
|
|
668
|
+
setCueDelayMs,
|
|
669
|
+
setCueSink,
|
|
670
|
+
setCueVolume,
|
|
671
|
+
setMainDelayMs,
|
|
672
|
+
setMainSink
|
|
673
|
+
};
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
export { DEFAULT_ANALYSER_FFT_SIZE, DEFAULT_METER_FLOOR_DB, DEFAULT_METER_INTERVAL_MS, DELAY_RAMP_TIME_CONSTANT_S, MAX_OUTPUT_DELAY_MS, METER_PROCESSOR_NAME, VITE_AUDIO_WORKLET_OPTIMIZE_DEPS_EXCLUDE, buildStereoMeterReading, clampOutputDelayMs, computePeakAndRms, createMultiSinkRouter, createStereoMeter, createWorkletBlobUrl, createWorkletModuleUrl, dbToNormalized, detectSinkIdCapabilities, linearToDb, loadAudioWorklet, loadMeterWorklet };
|
|
677
|
+
|
|
678
|
+
//# sourceMappingURL=index.js.map
|