@micdrop/web 1.3.1 → 1.4.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.
- package/dist/index.d.mts +26 -7
- package/dist/index.d.ts +26 -7
- package/dist/index.js +255 -127
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +255 -127
- package/dist/index.mjs.map +1 -1
- package/dist/silero.d.mts +3 -1
- package/dist/silero.d.ts +3 -1
- package/dist/silero.js +26 -21
- package/dist/silero.js.map +1 -1
- package/dist/silero.mjs +31 -22
- package/dist/silero.mjs.map +1 -1
- package/dist/vite.js +1 -1
- package/dist/vite.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -26,18 +26,26 @@ declare class WebAudioSink implements AudioSink {
|
|
|
26
26
|
*
|
|
27
27
|
* An audio worklet delivers the samples at a steady pace, and everything that
|
|
28
28
|
* happens to them afterwards lives in `@micdrop/client`.
|
|
29
|
+
*
|
|
30
|
+
* Starts and stops take turns, so two starts never hold two streams. A stop or
|
|
31
|
+
* a start for another device aborts the start under way, which lets go of
|
|
32
|
+
* whatever it obtained, and the last call wins.
|
|
29
33
|
*/
|
|
30
34
|
declare class WebMic extends MicDriver {
|
|
31
|
-
private
|
|
32
|
-
private source;
|
|
33
|
-
private worklet;
|
|
35
|
+
private state;
|
|
34
36
|
private _deviceId;
|
|
37
|
+
private attempt;
|
|
38
|
+
private queue;
|
|
35
39
|
get isStarted(): boolean;
|
|
36
40
|
get deviceId(): string | undefined;
|
|
37
41
|
get echoCancellation(): boolean | undefined;
|
|
38
42
|
start(deviceId?: string): Promise<void>;
|
|
39
43
|
stop(): Promise<void>;
|
|
40
44
|
getDevices(): Promise<MicdropDevice[]>;
|
|
45
|
+
private enqueue;
|
|
46
|
+
private open;
|
|
47
|
+
/** Tears the running microphone down, if there is one */
|
|
48
|
+
private close;
|
|
41
49
|
private onWorkletMessage;
|
|
42
50
|
private onDeviceChange;
|
|
43
51
|
}
|
|
@@ -49,17 +57,22 @@ declare function listDevices(kind: 'audioinput' | 'audiooutput'): Promise<Micdro
|
|
|
49
57
|
|
|
50
58
|
/**
|
|
51
59
|
* Plays the assistant voice with the Web Audio API.
|
|
60
|
+
*
|
|
61
|
+
* The output device is remembered across calls: one chosen before `start()`
|
|
62
|
+
* applies as soon as it starts, and the echo fallback plays through it too.
|
|
52
63
|
*/
|
|
53
64
|
declare class WebSpeaker extends SpeakerDriver {
|
|
54
|
-
private
|
|
55
|
-
private stream;
|
|
65
|
+
private state;
|
|
56
66
|
private _deviceId;
|
|
57
|
-
private
|
|
58
|
-
private isEchoFallback;
|
|
67
|
+
private routing;
|
|
59
68
|
get isPlaying(): boolean;
|
|
60
69
|
get deviceId(): string | undefined;
|
|
61
70
|
/** The node the sound comes out of, to analyse or process it */
|
|
62
71
|
get output(): AudioNode | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Prepares the audio graph, once, and plays through the switch of a headset
|
|
74
|
+
* whose microphone just opened, at every call
|
|
75
|
+
*/
|
|
63
76
|
start(): Promise<void>;
|
|
64
77
|
play(pcm: Int16Array, sampleRate: number): void;
|
|
65
78
|
stopAudio(): void;
|
|
@@ -72,6 +85,12 @@ declare class WebSpeaker extends SpeakerDriver {
|
|
|
72
85
|
resetEchoFallback(): void;
|
|
73
86
|
changeDevice(deviceId: string): Promise<void>;
|
|
74
87
|
getDevices(): Promise<MicdropDevice[]>;
|
|
88
|
+
/**
|
|
89
|
+
* Plays through a device, after the changes asked for before
|
|
90
|
+
* @param deviceId - The output device
|
|
91
|
+
*/
|
|
92
|
+
private route;
|
|
93
|
+
private applyDevice;
|
|
75
94
|
}
|
|
76
95
|
|
|
77
96
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -26,18 +26,26 @@ declare class WebAudioSink implements AudioSink {
|
|
|
26
26
|
*
|
|
27
27
|
* An audio worklet delivers the samples at a steady pace, and everything that
|
|
28
28
|
* happens to them afterwards lives in `@micdrop/client`.
|
|
29
|
+
*
|
|
30
|
+
* Starts and stops take turns, so two starts never hold two streams. A stop or
|
|
31
|
+
* a start for another device aborts the start under way, which lets go of
|
|
32
|
+
* whatever it obtained, and the last call wins.
|
|
29
33
|
*/
|
|
30
34
|
declare class WebMic extends MicDriver {
|
|
31
|
-
private
|
|
32
|
-
private source;
|
|
33
|
-
private worklet;
|
|
35
|
+
private state;
|
|
34
36
|
private _deviceId;
|
|
37
|
+
private attempt;
|
|
38
|
+
private queue;
|
|
35
39
|
get isStarted(): boolean;
|
|
36
40
|
get deviceId(): string | undefined;
|
|
37
41
|
get echoCancellation(): boolean | undefined;
|
|
38
42
|
start(deviceId?: string): Promise<void>;
|
|
39
43
|
stop(): Promise<void>;
|
|
40
44
|
getDevices(): Promise<MicdropDevice[]>;
|
|
45
|
+
private enqueue;
|
|
46
|
+
private open;
|
|
47
|
+
/** Tears the running microphone down, if there is one */
|
|
48
|
+
private close;
|
|
41
49
|
private onWorkletMessage;
|
|
42
50
|
private onDeviceChange;
|
|
43
51
|
}
|
|
@@ -49,17 +57,22 @@ declare function listDevices(kind: 'audioinput' | 'audiooutput'): Promise<Micdro
|
|
|
49
57
|
|
|
50
58
|
/**
|
|
51
59
|
* Plays the assistant voice with the Web Audio API.
|
|
60
|
+
*
|
|
61
|
+
* The output device is remembered across calls: one chosen before `start()`
|
|
62
|
+
* applies as soon as it starts, and the echo fallback plays through it too.
|
|
52
63
|
*/
|
|
53
64
|
declare class WebSpeaker extends SpeakerDriver {
|
|
54
|
-
private
|
|
55
|
-
private stream;
|
|
65
|
+
private state;
|
|
56
66
|
private _deviceId;
|
|
57
|
-
private
|
|
58
|
-
private isEchoFallback;
|
|
67
|
+
private routing;
|
|
59
68
|
get isPlaying(): boolean;
|
|
60
69
|
get deviceId(): string | undefined;
|
|
61
70
|
/** The node the sound comes out of, to analyse or process it */
|
|
62
71
|
get output(): AudioNode | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Prepares the audio graph, once, and plays through the switch of a headset
|
|
74
|
+
* whose microphone just opened, at every call
|
|
75
|
+
*/
|
|
63
76
|
start(): Promise<void>;
|
|
64
77
|
play(pcm: Int16Array, sampleRate: number): void;
|
|
65
78
|
stopAudio(): void;
|
|
@@ -72,6 +85,12 @@ declare class WebSpeaker extends SpeakerDriver {
|
|
|
72
85
|
resetEchoFallback(): void;
|
|
73
86
|
changeDevice(deviceId: string): Promise<void>;
|
|
74
87
|
getDevices(): Promise<MicdropDevice[]>;
|
|
88
|
+
/**
|
|
89
|
+
* Plays through a device, after the changes asked for before
|
|
90
|
+
* @param deviceId - The output device
|
|
91
|
+
*/
|
|
92
|
+
private route;
|
|
93
|
+
private applyDevice;
|
|
75
94
|
}
|
|
76
95
|
|
|
77
96
|
/**
|
package/dist/index.js
CHANGED
|
@@ -77,14 +77,21 @@ if (window.micdropAudioContext) {
|
|
|
77
77
|
var pcm_processor_worklet_default = "// Hands the microphone signal to the main thread, continuously.\n//\n// Deciding whether someone is speaking, keeping a reserve of audio and cutting\n// it into chunks all happen in @micdrop/client, so this only has to deliver the\n// samples at a steady pace.\n\nconst INTERVAL_MS = 20\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.buffers = []\n this.length = 0\n // `sampleRate` is provided by the worklet scope\n this.frameLength = Math.round((sampleRate * INTERVAL_MS) / 1000)\n }\n\n process(inputs) {\n const input = inputs[0]\n if (!input || input.length === 0) return true\n\n this.buffers.push(new Float32Array(input[0]))\n this.length += input[0].length\n if (this.length < this.frameLength) return true\n\n const frames = new Float32Array(this.length)\n let offset = 0\n for (const buffer of this.buffers) {\n frames.set(buffer, offset)\n offset += buffer.length\n }\n this.buffers.length = 0\n this.length = 0\n\n this.port.postMessage({ type: 'frames', frames, sampleRate }, [\n frames.buffer,\n ])\n return true\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n";
|
|
78
78
|
|
|
79
79
|
// src/audio/pcm-processor.ts
|
|
80
|
-
|
|
81
|
-
if (window.micdropPcmProcessor)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
80
|
+
function initPcmProcessor() {
|
|
81
|
+
if (!window.micdropPcmProcessor) {
|
|
82
|
+
const workletBlob = new Blob([pcm_processor_worklet_default], {
|
|
83
|
+
type: "application/javascript"
|
|
84
|
+
});
|
|
85
|
+
const workletUrl = URL.createObjectURL(workletBlob);
|
|
86
|
+
const loading = audioContext.audioWorklet.addModule(workletUrl);
|
|
87
|
+
window.micdropPcmProcessor = loading;
|
|
88
|
+
loading.catch(() => {
|
|
89
|
+
if (window.micdropPcmProcessor === loading) {
|
|
90
|
+
window.micdropPcmProcessor = void 0;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return window.micdropPcmProcessor;
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
// src/audio/stopStream.ts
|
|
@@ -99,6 +106,8 @@ var lastRelease;
|
|
|
99
106
|
var WebMic = class extends import_client.MicDriver {
|
|
100
107
|
constructor() {
|
|
101
108
|
super(...arguments);
|
|
109
|
+
this.state = { status: "stopped" };
|
|
110
|
+
this.queue = Promise.resolve();
|
|
102
111
|
this.onWorkletMessage = (event) => {
|
|
103
112
|
const { type, frames, sampleRate } = event.data;
|
|
104
113
|
if (type !== "frames") return;
|
|
@@ -109,7 +118,7 @@ var WebMic = class extends import_client.MicDriver {
|
|
|
109
118
|
};
|
|
110
119
|
}
|
|
111
120
|
get isStarted() {
|
|
112
|
-
return
|
|
121
|
+
return this.state.status === "started";
|
|
113
122
|
}
|
|
114
123
|
get deviceId() {
|
|
115
124
|
return this._deviceId;
|
|
@@ -117,71 +126,121 @@ var WebMic = class extends import_client.MicDriver {
|
|
|
117
126
|
// Asking for echo cancellation is not getting it: some browsers and devices
|
|
118
127
|
// do without, and the settings of the track say so
|
|
119
128
|
get echoCancellation() {
|
|
120
|
-
|
|
129
|
+
if (this.state.status !== "started") return void 0;
|
|
130
|
+
const track = this.state.stream.getAudioTracks()[0];
|
|
121
131
|
const setting = track?.getSettings?.().echoCancellation;
|
|
122
132
|
if (setting === void 0) return void 0;
|
|
123
133
|
return setting !== false;
|
|
124
134
|
}
|
|
125
|
-
|
|
126
|
-
if (this.
|
|
127
|
-
|
|
128
|
-
await this.stop();
|
|
135
|
+
start(deviceId) {
|
|
136
|
+
if (this.attempt && this.attempt.requested === deviceId) {
|
|
137
|
+
return this.attempt.done;
|
|
129
138
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
const track = this.stream.getTracks()[0];
|
|
143
|
-
const settled = track?.getCapabilities?.().deviceId;
|
|
144
|
-
this._deviceId = settled && settled !== "default" ? settled : deviceId ?? settled;
|
|
145
|
-
await initPcmProcessor();
|
|
146
|
-
this.worklet = new AudioWorkletNode(audioContext, "pcm-processor");
|
|
147
|
-
this.worklet.port.onmessage = this.onWorkletMessage;
|
|
148
|
-
this.source = audioContext.createMediaStreamSource(this.stream);
|
|
149
|
-
this.source.connect(this.worklet);
|
|
150
|
-
navigator.mediaDevices.addEventListener("devicechange", this.onDeviceChange);
|
|
139
|
+
this.attempt?.controller.abort();
|
|
140
|
+
const controller = new AbortController();
|
|
141
|
+
const done = this.enqueue(() => this.open(deviceId, controller.signal));
|
|
142
|
+
const attempt = { requested: deviceId, controller, done };
|
|
143
|
+
this.attempt = attempt;
|
|
144
|
+
const clear = () => {
|
|
145
|
+
if (this.attempt === attempt) this.attempt = void 0;
|
|
146
|
+
};
|
|
147
|
+
done.then(clear, clear);
|
|
148
|
+
return done;
|
|
151
149
|
}
|
|
152
150
|
async stop() {
|
|
151
|
+
this.attempt?.controller.abort();
|
|
152
|
+
this.attempt = void 0;
|
|
153
|
+
await this.enqueue(async () => this.close());
|
|
154
|
+
}
|
|
155
|
+
async getDevices() {
|
|
156
|
+
return listDevices("audioinput");
|
|
157
|
+
}
|
|
158
|
+
enqueue(operation) {
|
|
159
|
+
const done = this.queue.then(operation);
|
|
160
|
+
this.queue = done.catch(() => void 0);
|
|
161
|
+
return done;
|
|
162
|
+
}
|
|
163
|
+
async open(requested, signal) {
|
|
164
|
+
if (signal.aborted) return;
|
|
165
|
+
const state = this.state;
|
|
166
|
+
if (state.status === "started") {
|
|
167
|
+
if (requested === state.requested || requested === this._deviceId) return;
|
|
168
|
+
this.close();
|
|
169
|
+
}
|
|
170
|
+
this.state = { status: "starting", requested };
|
|
171
|
+
let stream;
|
|
172
|
+
let settled;
|
|
173
|
+
try {
|
|
174
|
+
await waitForRelease(requested, signal);
|
|
175
|
+
if (signal.aborted) return;
|
|
176
|
+
stream = await navigator.mediaDevices.getUserMedia({
|
|
177
|
+
audio: {
|
|
178
|
+
deviceId: { ideal: requested },
|
|
179
|
+
sampleRate: 16e3,
|
|
180
|
+
// not working, it will follow device settings, usually 44.1kHz
|
|
181
|
+
sampleSize: 16,
|
|
182
|
+
channelCount: 1,
|
|
183
|
+
echoCancellation: true,
|
|
184
|
+
noiseSuppression: true
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
const track = stream.getTracks()[0];
|
|
188
|
+
const capability = track?.getCapabilities?.().deviceId;
|
|
189
|
+
settled = capability && capability !== "default" ? capability : requested ?? capability;
|
|
190
|
+
if (signal.aborted) return;
|
|
191
|
+
await initPcmProcessor();
|
|
192
|
+
if (signal.aborted) return;
|
|
193
|
+
const worklet = new AudioWorkletNode(audioContext, "pcm-processor");
|
|
194
|
+
const source = audioContext.createMediaStreamSource(stream);
|
|
195
|
+
worklet.port.onmessage = this.onWorkletMessage;
|
|
196
|
+
source.connect(worklet);
|
|
197
|
+
navigator.mediaDevices.addEventListener(
|
|
198
|
+
"devicechange",
|
|
199
|
+
this.onDeviceChange
|
|
200
|
+
);
|
|
201
|
+
this._deviceId = settled;
|
|
202
|
+
this.state = { status: "started", requested, stream, source, worklet };
|
|
203
|
+
stream = void 0;
|
|
204
|
+
} finally {
|
|
205
|
+
if (stream) releaseStream(stream, settled);
|
|
206
|
+
if (this.state.status === "starting") this.state = { status: "stopped" };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/** Tears the running microphone down, if there is one */
|
|
210
|
+
close() {
|
|
211
|
+
const state = this.state;
|
|
212
|
+
if (state.status !== "started") return;
|
|
213
|
+
this.state = { status: "stopped" };
|
|
153
214
|
navigator.mediaDevices.removeEventListener(
|
|
154
215
|
"devicechange",
|
|
155
216
|
this.onDeviceChange
|
|
156
217
|
);
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
if (this.source) {
|
|
163
|
-
this.source.disconnect();
|
|
164
|
-
this.source = void 0;
|
|
165
|
-
}
|
|
166
|
-
if (this.stream) {
|
|
167
|
-
stopStream(this.stream);
|
|
168
|
-
this.stream = void 0;
|
|
169
|
-
lastRelease = { at: Date.now(), deviceId: this._deviceId };
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
async getDevices() {
|
|
173
|
-
return listDevices("audioinput");
|
|
218
|
+
state.worklet.port.onmessage = null;
|
|
219
|
+
state.worklet.disconnect();
|
|
220
|
+
state.source.disconnect();
|
|
221
|
+
releaseStream(state.stream, this._deviceId);
|
|
174
222
|
}
|
|
175
223
|
};
|
|
176
|
-
|
|
224
|
+
function releaseStream(stream, deviceId) {
|
|
225
|
+
stopStream(stream);
|
|
226
|
+
lastRelease = { at: Date.now(), deviceId };
|
|
227
|
+
}
|
|
228
|
+
async function waitForRelease(deviceId, signal) {
|
|
177
229
|
if (!isFirefox || !lastRelease) return;
|
|
178
230
|
if (deviceId && lastRelease.deviceId && deviceId !== lastRelease.deviceId) {
|
|
179
231
|
return;
|
|
180
232
|
}
|
|
181
233
|
const remaining = lastRelease.at + FIREFOX_RELEASE_DELAY - Date.now();
|
|
182
|
-
if (remaining
|
|
183
|
-
|
|
184
|
-
|
|
234
|
+
if (remaining <= 0 || signal.aborted) return;
|
|
235
|
+
await new Promise((resolve) => {
|
|
236
|
+
const done = () => {
|
|
237
|
+
clearTimeout(timer);
|
|
238
|
+
signal.removeEventListener("abort", done);
|
|
239
|
+
resolve();
|
|
240
|
+
};
|
|
241
|
+
const timer = setTimeout(done, remaining);
|
|
242
|
+
signal.addEventListener("abort", done);
|
|
243
|
+
});
|
|
185
244
|
}
|
|
186
245
|
async function listDevices(kind) {
|
|
187
246
|
const all = await navigator.mediaDevices.enumerateDevices();
|
|
@@ -247,122 +306,191 @@ var WebAudioSink = class {
|
|
|
247
306
|
|
|
248
307
|
// src/audio/WebSpeaker.ts
|
|
249
308
|
var FIREFOX_WARM_UP = 2e3;
|
|
309
|
+
var IS_FIREFOX = typeof navigator !== "undefined" && navigator.userAgent.includes("Firefox");
|
|
250
310
|
var WebSpeaker = class extends import_client2.SpeakerDriver {
|
|
251
311
|
constructor() {
|
|
252
312
|
super(...arguments);
|
|
253
|
-
this.
|
|
313
|
+
this.state = { status: "stopped" };
|
|
314
|
+
// Device changes take turns, so the last one asked for is the one heard
|
|
315
|
+
this.routing = Promise.resolve();
|
|
254
316
|
}
|
|
255
317
|
get isPlaying() {
|
|
256
|
-
return this.stream
|
|
318
|
+
return this.state.status === "started" && this.state.stream.isPlaying;
|
|
257
319
|
}
|
|
258
320
|
get deviceId() {
|
|
259
321
|
return this._deviceId;
|
|
260
322
|
}
|
|
261
323
|
/** The node the sound comes out of, to analyse or process it */
|
|
262
324
|
get output() {
|
|
263
|
-
return this.sink
|
|
325
|
+
return this.state.status === "started" ? this.state.sink.output : void 0;
|
|
264
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Prepares the audio graph, once, and plays through the switch of a headset
|
|
329
|
+
* whose microphone just opened, at every call
|
|
330
|
+
*/
|
|
265
331
|
async start() {
|
|
266
|
-
if (this.
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
332
|
+
if (this.state.status === "started") {
|
|
333
|
+
if (IS_FIREFOX) this.state.stream.warmUp(FIREFOX_WARM_UP);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const sink = new WebAudioSink();
|
|
337
|
+
const stream = new import_client2.Pcm16AudioStream(sink);
|
|
338
|
+
if (IS_FIREFOX) stream.warmUp(FIREFOX_WARM_UP);
|
|
339
|
+
stream.on("StartPlaying", () => this.emit("StartPlaying"));
|
|
340
|
+
stream.on("StopPlaying", () => this.emit("StopPlaying"));
|
|
341
|
+
stream.on("Volume", (volume) => this.emit("Volume", volume));
|
|
342
|
+
sink.output.connect(audioContext.destination);
|
|
343
|
+
this.state = {
|
|
344
|
+
status: "started",
|
|
345
|
+
sink,
|
|
346
|
+
stream,
|
|
347
|
+
output: { kind: "context" }
|
|
348
|
+
};
|
|
349
|
+
const deviceId = this._deviceId;
|
|
350
|
+
if (deviceId) {
|
|
351
|
+
try {
|
|
352
|
+
await this.route(deviceId);
|
|
353
|
+
} catch (error) {
|
|
354
|
+
console.error(
|
|
355
|
+
"[Micdrop] Playing through the chosen output failed",
|
|
356
|
+
error
|
|
357
|
+
);
|
|
358
|
+
if (this._deviceId === deviceId) this._deviceId = void 0;
|
|
359
|
+
}
|
|
271
360
|
}
|
|
272
|
-
this.stream.on("StartPlaying", () => this.emit("StartPlaying"));
|
|
273
|
-
this.stream.on("StopPlaying", () => this.emit("StopPlaying"));
|
|
274
|
-
this.stream.on("Volume", (volume) => this.emit("Volume", volume));
|
|
275
|
-
this.sink.output.connect(audioContext.destination);
|
|
276
361
|
}
|
|
277
362
|
play(pcm, sampleRate) {
|
|
278
|
-
this.
|
|
363
|
+
if (this.state.status !== "started") return;
|
|
364
|
+
this.state.stream.playAudio(pcm, sampleRate);
|
|
279
365
|
}
|
|
280
366
|
stopAudio() {
|
|
281
|
-
this.
|
|
367
|
+
if (this.state.status !== "started") return;
|
|
368
|
+
this.state.stream.stopAudio();
|
|
282
369
|
}
|
|
283
370
|
async stop() {
|
|
284
|
-
this.
|
|
285
|
-
|
|
286
|
-
this.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
this.element.pause();
|
|
291
|
-
this.element.srcObject = null;
|
|
292
|
-
this.element = void 0;
|
|
293
|
-
}
|
|
371
|
+
const state = this.state;
|
|
372
|
+
if (state.status !== "started") return;
|
|
373
|
+
this.state = { status: "stopped" };
|
|
374
|
+
state.stream.destroy();
|
|
375
|
+
state.sink.output.disconnect();
|
|
376
|
+
closeOutput(state.output);
|
|
294
377
|
}
|
|
295
378
|
/**
|
|
296
379
|
* Moves the sound to an audio element, while it plays. The audio context
|
|
297
380
|
* keeps playing until the element is known to be allowed to.
|
|
298
381
|
*/
|
|
299
382
|
async useEchoFallback() {
|
|
300
|
-
const
|
|
301
|
-
if (
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
383
|
+
const state = this.state;
|
|
384
|
+
if (state.status !== "started" || state.output.kind !== "context") {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
const output = createElementOutput("echoFallback");
|
|
388
|
+
output.element.srcObject = output.destination.stream;
|
|
305
389
|
try {
|
|
306
|
-
await element.play();
|
|
390
|
+
await output.element.play();
|
|
391
|
+
if (this._deviceId) await output.element.setSinkId?.(this._deviceId);
|
|
307
392
|
} catch (error) {
|
|
308
393
|
console.error("[Micdrop] Playing through an element failed", error);
|
|
309
|
-
|
|
394
|
+
closeOutput(output);
|
|
310
395
|
return false;
|
|
311
396
|
}
|
|
312
|
-
if (this.
|
|
313
|
-
|
|
314
|
-
element.srcObject = null;
|
|
397
|
+
if (this.state !== state || state.output.kind !== "context") {
|
|
398
|
+
closeOutput(output);
|
|
315
399
|
return false;
|
|
316
400
|
}
|
|
317
|
-
|
|
318
|
-
sink.output.connect(destination);
|
|
319
|
-
this.element = element;
|
|
320
|
-
this.isEchoFallback = true;
|
|
401
|
+
setOutput(state, output);
|
|
321
402
|
return true;
|
|
322
403
|
}
|
|
323
404
|
resetEchoFallback() {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
this.element.pause();
|
|
327
|
-
this.element.srcObject = null;
|
|
328
|
-
this.element = void 0;
|
|
329
|
-
this.sink.output.disconnect();
|
|
330
|
-
this.sink.output.connect(audioContext.destination);
|
|
331
|
-
}
|
|
332
|
-
async changeDevice(deviceId) {
|
|
333
|
-
const sink = this.sink;
|
|
334
|
-
if (!sink) {
|
|
335
|
-
this._deviceId = deviceId;
|
|
405
|
+
const state = this.state;
|
|
406
|
+
if (state.status !== "started" || state.output.kind !== "element" || state.output.reason !== "echoFallback") {
|
|
336
407
|
return;
|
|
337
408
|
}
|
|
338
|
-
|
|
339
|
-
const
|
|
340
|
-
if (
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
this.element = void 0;
|
|
348
|
-
}
|
|
349
|
-
} else {
|
|
350
|
-
if (!this.element) {
|
|
351
|
-
const destination = audioContext.createMediaStreamDestination();
|
|
352
|
-
sink.output.disconnect();
|
|
353
|
-
sink.output.connect(destination);
|
|
354
|
-
this.element = new Audio();
|
|
355
|
-
this.element.autoplay = true;
|
|
356
|
-
this.element.srcObject = destination.stream;
|
|
357
|
-
}
|
|
358
|
-
await this.element.setSinkId?.(deviceId);
|
|
409
|
+
setOutput(state, { kind: "context" });
|
|
410
|
+
const deviceId = this._deviceId;
|
|
411
|
+
if (deviceId) {
|
|
412
|
+
this.route(deviceId).catch(
|
|
413
|
+
(error) => console.error(
|
|
414
|
+
"[Micdrop] Playing through the chosen output failed",
|
|
415
|
+
error
|
|
416
|
+
)
|
|
417
|
+
);
|
|
359
418
|
}
|
|
419
|
+
}
|
|
420
|
+
async changeDevice(deviceId) {
|
|
421
|
+
await this.route(deviceId);
|
|
360
422
|
this._deviceId = deviceId;
|
|
361
423
|
}
|
|
362
424
|
async getDevices() {
|
|
363
425
|
return listDevices("audiooutput");
|
|
364
426
|
}
|
|
427
|
+
/**
|
|
428
|
+
* Plays through a device, after the changes asked for before
|
|
429
|
+
* @param deviceId - The output device
|
|
430
|
+
*/
|
|
431
|
+
route(deviceId) {
|
|
432
|
+
const done = this.routing.then(() => this.applyDevice(deviceId));
|
|
433
|
+
this.routing = done.catch(() => void 0);
|
|
434
|
+
return done;
|
|
435
|
+
}
|
|
436
|
+
async applyDevice(deviceId) {
|
|
437
|
+
const state = this.state;
|
|
438
|
+
if (state.status !== "started") return;
|
|
439
|
+
const devices = await this.getDevices();
|
|
440
|
+
const isDefault = devices.length === 0 || devices[0].id === deviceId;
|
|
441
|
+
if (this.state !== state) return;
|
|
442
|
+
const output = state.output;
|
|
443
|
+
if (output.kind === "element" && output.reason === "echoFallback") {
|
|
444
|
+
await output.element.setSinkId?.(isDefault ? "" : deviceId);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
if (isDefault || "setSinkId" in audioContext) {
|
|
448
|
+
await audioContext.setSinkId?.(isDefault ? "" : deviceId);
|
|
449
|
+
if (this.state !== state) return;
|
|
450
|
+
const current = state.output;
|
|
451
|
+
if (current.kind === "element" && current.reason === "echoFallback") {
|
|
452
|
+
await current.element.setSinkId?.(isDefault ? "" : deviceId);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
setOutput(state, { kind: "context" });
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
let element = output.kind === "element" ? output.element : void 0;
|
|
459
|
+
if (!element) {
|
|
460
|
+
const next = createElementOutput("device");
|
|
461
|
+
next.element.autoplay = true;
|
|
462
|
+
next.element.srcObject = next.destination.stream;
|
|
463
|
+
setOutput(state, next);
|
|
464
|
+
element = next.element;
|
|
465
|
+
}
|
|
466
|
+
await element.setSinkId?.(deviceId);
|
|
467
|
+
}
|
|
365
468
|
};
|
|
469
|
+
function setOutput(state, output) {
|
|
470
|
+
const previous = state.output;
|
|
471
|
+
if (previous === output) return;
|
|
472
|
+
if (previous.kind === "context" && output.kind === "context") return;
|
|
473
|
+
state.sink.output.disconnect(destinationOf(previous));
|
|
474
|
+
state.sink.output.connect(destinationOf(output));
|
|
475
|
+
state.output = output;
|
|
476
|
+
closeOutput(previous);
|
|
477
|
+
}
|
|
478
|
+
function destinationOf(output) {
|
|
479
|
+
return output.kind === "context" ? audioContext.destination : output.destination;
|
|
480
|
+
}
|
|
481
|
+
function createElementOutput(reason) {
|
|
482
|
+
return {
|
|
483
|
+
kind: "element",
|
|
484
|
+
reason,
|
|
485
|
+
element: new Audio(),
|
|
486
|
+
destination: audioContext.createMediaStreamDestination()
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
function closeOutput(output) {
|
|
490
|
+
if (output.kind !== "element") return;
|
|
491
|
+
output.element.pause();
|
|
492
|
+
output.element.srcObject = null;
|
|
493
|
+
}
|
|
366
494
|
|
|
367
495
|
// src/index.ts
|
|
368
496
|
__reExport(index_exports, require("@micdrop/client"), module.exports);
|