@micdrop/web 1.3.0 → 1.4.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/dist/index.d.mts +32 -6
- package/dist/index.d.ts +32 -6
- package/dist/index.js +271 -101
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +271 -101
- 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 +25 -20
- package/dist/silero.js.map +1 -1
- package/dist/silero.mjs +30 -21
- package/dist/silero.mjs.map +1 -1
- package/dist/vite.js +1 -1
- package/dist/vite.js.map +1 -1
- package/package.json +4 -4
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,22 +57,40 @@ 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
|
|
67
|
+
private routing;
|
|
58
68
|
get isPlaying(): boolean;
|
|
59
69
|
get deviceId(): string | undefined;
|
|
60
70
|
/** The node the sound comes out of, to analyse or process it */
|
|
61
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
|
+
*/
|
|
62
76
|
start(): Promise<void>;
|
|
63
77
|
play(pcm: Int16Array, sampleRate: number): void;
|
|
64
78
|
stopAudio(): void;
|
|
65
79
|
stop(): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Moves the sound to an audio element, while it plays. The audio context
|
|
82
|
+
* keeps playing until the element is known to be allowed to.
|
|
83
|
+
*/
|
|
84
|
+
useEchoFallback(): Promise<boolean>;
|
|
85
|
+
resetEchoFallback(): void;
|
|
66
86
|
changeDevice(deviceId: string): Promise<void>;
|
|
67
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;
|
|
68
94
|
}
|
|
69
95
|
|
|
70
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,22 +57,40 @@ 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
|
|
67
|
+
private routing;
|
|
58
68
|
get isPlaying(): boolean;
|
|
59
69
|
get deviceId(): string | undefined;
|
|
60
70
|
/** The node the sound comes out of, to analyse or process it */
|
|
61
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
|
+
*/
|
|
62
76
|
start(): Promise<void>;
|
|
63
77
|
play(pcm: Int16Array, sampleRate: number): void;
|
|
64
78
|
stopAudio(): void;
|
|
65
79
|
stop(): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Moves the sound to an audio element, while it plays. The audio context
|
|
82
|
+
* keeps playing until the element is known to be allowed to.
|
|
83
|
+
*/
|
|
84
|
+
useEchoFallback(): Promise<boolean>;
|
|
85
|
+
resetEchoFallback(): void;
|
|
66
86
|
changeDevice(deviceId: string): Promise<void>;
|
|
67
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;
|
|
68
94
|
}
|
|
69
95
|
|
|
70
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,80 +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 {
|
|
311
|
+
constructor() {
|
|
312
|
+
super(...arguments);
|
|
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();
|
|
316
|
+
}
|
|
251
317
|
get isPlaying() {
|
|
252
|
-
return this.stream
|
|
318
|
+
return this.state.status === "started" && this.state.stream.isPlaying;
|
|
253
319
|
}
|
|
254
320
|
get deviceId() {
|
|
255
321
|
return this._deviceId;
|
|
256
322
|
}
|
|
257
323
|
/** The node the sound comes out of, to analyse or process it */
|
|
258
324
|
get output() {
|
|
259
|
-
return this.sink
|
|
325
|
+
return this.state.status === "started" ? this.state.sink.output : void 0;
|
|
260
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
|
+
*/
|
|
261
331
|
async start() {
|
|
262
|
-
if (this.
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
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
|
+
}
|
|
267
360
|
}
|
|
268
|
-
this.stream.on("StartPlaying", () => this.emit("StartPlaying"));
|
|
269
|
-
this.stream.on("StopPlaying", () => this.emit("StopPlaying"));
|
|
270
|
-
this.stream.on("Volume", (volume) => this.emit("Volume", volume));
|
|
271
|
-
this.sink.output.connect(audioContext.destination);
|
|
272
361
|
}
|
|
273
362
|
play(pcm, sampleRate) {
|
|
274
|
-
this.
|
|
363
|
+
if (this.state.status !== "started") return;
|
|
364
|
+
this.state.stream.playAudio(pcm, sampleRate);
|
|
275
365
|
}
|
|
276
366
|
stopAudio() {
|
|
277
|
-
this.
|
|
367
|
+
if (this.state.status !== "started") return;
|
|
368
|
+
this.state.stream.stopAudio();
|
|
278
369
|
}
|
|
279
370
|
async stop() {
|
|
280
|
-
this.
|
|
281
|
-
|
|
282
|
-
this.
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Moves the sound to an audio element, while it plays. The audio context
|
|
380
|
+
* keeps playing until the element is known to be allowed to.
|
|
381
|
+
*/
|
|
382
|
+
async useEchoFallback() {
|
|
383
|
+
const state = this.state;
|
|
384
|
+
if (state.status !== "started" || state.output.kind !== "context") {
|
|
385
|
+
return false;
|
|
288
386
|
}
|
|
387
|
+
const output = createElementOutput("echoFallback");
|
|
388
|
+
output.element.srcObject = output.destination.stream;
|
|
389
|
+
try {
|
|
390
|
+
await output.element.play();
|
|
391
|
+
if (this._deviceId) await output.element.setSinkId?.(this._deviceId);
|
|
392
|
+
} catch (error) {
|
|
393
|
+
console.error("[Micdrop] Playing through an element failed", error);
|
|
394
|
+
closeOutput(output);
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
if (this.state !== state || state.output.kind !== "context") {
|
|
398
|
+
closeOutput(output);
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
setOutput(state, output);
|
|
402
|
+
return true;
|
|
289
403
|
}
|
|
290
|
-
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
293
|
-
this._deviceId = deviceId;
|
|
404
|
+
resetEchoFallback() {
|
|
405
|
+
const state = this.state;
|
|
406
|
+
if (state.status !== "started" || state.output.kind !== "element" || state.output.reason !== "echoFallback") {
|
|
294
407
|
return;
|
|
295
408
|
}
|
|
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
|
+
);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
async changeDevice(deviceId) {
|
|
421
|
+
await this.route(deviceId);
|
|
422
|
+
this._deviceId = deviceId;
|
|
423
|
+
}
|
|
424
|
+
async getDevices() {
|
|
425
|
+
return listDevices("audiooutput");
|
|
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;
|
|
296
439
|
const devices = await this.getDevices();
|
|
297
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
|
+
}
|
|
298
447
|
if (isDefault || "setSinkId" in audioContext) {
|
|
299
448
|
await audioContext.setSinkId?.(isDefault ? "" : deviceId);
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
this.element = void 0;
|
|
306
|
-
}
|
|
307
|
-
} else {
|
|
308
|
-
if (!this.element) {
|
|
309
|
-
const destination = audioContext.createMediaStreamDestination();
|
|
310
|
-
sink.output.disconnect();
|
|
311
|
-
sink.output.connect(destination);
|
|
312
|
-
this.element = new Audio();
|
|
313
|
-
this.element.autoplay = true;
|
|
314
|
-
this.element.srcObject = destination.stream;
|
|
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;
|
|
315
454
|
}
|
|
316
|
-
|
|
455
|
+
setOutput(state, { kind: "context" });
|
|
456
|
+
return;
|
|
317
457
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
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);
|
|
322
467
|
}
|
|
323
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
|
+
}
|
|
324
494
|
|
|
325
495
|
// src/index.ts
|
|
326
496
|
__reExport(index_exports, require("@micdrop/client"), module.exports);
|