@micdrop/client 2.2.10 → 3.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/dist/index.mjs CHANGED
@@ -1,437 +1,452 @@
1
- // src/audio/utils/AudioAnalyser.ts
1
+ // src/audio/Mic.ts
2
2
  import { EventEmitter } from "eventemitter3";
3
- var AudioAnalyser = class extends EventEmitter {
4
- constructor(audioContext2, volumeInterval = 100) {
5
- super();
6
- this.volumeInterval = volumeInterval;
7
- this.volume = -Infinity;
8
- this.fftSize = 512;
9
- this.fftBins = new Float32Array(this.fftSize);
10
- this.node = audioContext2.createAnalyser();
11
- this.node.fftSize = this.fftSize;
12
- this.node.smoothingTimeConstant = 0.1;
13
- this.interval = window.setInterval(() => {
14
- this.volume = this.getMaxVolume();
15
- this.emit("volume", this.volume);
16
- }, this.volumeInterval);
17
- }
18
- stop() {
19
- if (this.interval !== void 0) {
20
- window.clearInterval(this.interval);
21
- }
3
+
4
+ // src/audio/pcm.ts
5
+ function resample(input, inputRate, outputRate) {
6
+ if (inputRate === outputRate || input.length === 0) return input;
7
+ const ratio = inputRate / outputRate;
8
+ const outputLength = Math.round(input.length / ratio);
9
+ const output = new Float32Array(outputLength);
10
+ for (let i = 0; i < outputLength; i++) {
11
+ const position = i * ratio;
12
+ const index = Math.floor(position);
13
+ const nextIndex = Math.min(index + 1, input.length - 1);
14
+ const fraction = position - index;
15
+ output[i] = input[index] * (1 - fraction) + input[nextIndex] * fraction;
16
+ }
17
+ return output;
18
+ }
19
+ function floatToPcm16(input) {
20
+ const output = new Int16Array(input.length);
21
+ for (let i = 0; i < input.length; i++) {
22
+ const sample = Math.max(-1, Math.min(1, input[i]));
23
+ output[i] = sample < 0 ? sample * 32768 : sample * 32767;
22
24
  }
23
- // Return values between -Infinity and 0
24
- getMaxVolume() {
25
- let maxVolume = -Infinity;
26
- this.node.getFloatFrequencyData(this.fftBins);
27
- for (let i = 4, ii = this.fftBins.length; i < ii; i++) {
28
- if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
29
- maxVolume = this.fftBins[i];
30
- }
31
- }
32
- return maxVolume;
25
+ return output;
26
+ }
27
+ function pcm16ToFloat(input) {
28
+ const output = new Float32Array(input.length);
29
+ for (let i = 0; i < input.length; i++) {
30
+ output[i] = input[i] / 32768;
33
31
  }
34
- };
35
-
36
- // src/audio/utils/unlock.ts
37
- function unlock(audioContext2) {
38
- const events = ["touchstart", "touchend", "mousedown", "keydown"];
39
- const unlock2 = async () => {
40
- unlockSafari(audioContext2);
41
- if (audioContext2.state === "suspended") {
42
- await audioContext2.resume();
43
- }
44
- events.forEach((e) => document.body.removeEventListener(e, unlock2));
45
- };
46
- events.forEach((e) => document.body.addEventListener(e, unlock2, false));
32
+ return output;
47
33
  }
48
- function unlockSafari(audioContext2) {
49
- const source = audioContext2.createBufferSource();
50
- source.buffer = audioContext2.createBuffer(1, 1, 22050);
51
- source.connect(audioContext2.destination);
52
- const onEnded = () => {
53
- source.disconnect();
54
- source.buffer = null;
55
- source.removeEventListener("ended", onEnded);
56
- };
57
- source.addEventListener("ended", onEnded);
58
- source.start();
34
+ function concatFloat32(chunks) {
35
+ if (chunks.length === 1) return chunks[0];
36
+ let length = 0;
37
+ for (const chunk of chunks) length += chunk.length;
38
+ const output = new Float32Array(length);
39
+ let offset = 0;
40
+ for (const chunk of chunks) {
41
+ output.set(chunk, offset);
42
+ offset += chunk.length;
43
+ }
44
+ return output;
45
+ }
46
+ function pcm16ToArrayBuffer(pcm) {
47
+ return pcm.buffer.slice(
48
+ pcm.byteOffset,
49
+ pcm.byteOffset + pcm.byteLength
50
+ );
59
51
  }
60
52
 
61
- // src/audio/utils/audioContext.ts
62
- var audioContext;
63
- function createAudioContext() {
64
- return new (window.AudioContext || window.webkitAudioContext)();
53
+ // src/audio/volume.ts
54
+ var FFT_SIZE = 512;
55
+ var BIN_COUNT = FFT_SIZE / 2;
56
+ var FIRST_BIN = 4;
57
+ var SMOOTHING = 0.1;
58
+ var VOLUME_SAMPLE_RATE = 16e3;
59
+ var WINDOW = new Float32Array(FFT_SIZE);
60
+ for (let i = 0; i < FFT_SIZE; i++) {
61
+ const a = 2 * Math.PI * i / (FFT_SIZE - 1);
62
+ WINDOW[i] = 0.42 - 0.5 * Math.cos(a) + 0.08 * Math.cos(2 * a);
65
63
  }
66
- if (window.micdropAudioContext) {
67
- audioContext = window.micdropAudioContext;
68
- } else {
69
- window.micdropAudioContext = audioContext = createAudioContext();
70
- unlock(audioContext);
64
+ var REVERSED = new Uint16Array(FFT_SIZE);
65
+ for (let i = 0; i < FFT_SIZE; i++) {
66
+ let reversed = 0;
67
+ for (let bit = 1; bit < FFT_SIZE; bit <<= 1) {
68
+ reversed = reversed << 1 | (i & bit ? 1 : 0);
69
+ }
70
+ REVERSED[i] = reversed;
71
71
  }
72
-
73
- // src/audio/utils/localStorage.ts
74
- var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
75
- LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
76
- LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
77
- LocalStorageKeys2["VolumeVADOptions"] = "micdrop.VolumeVADOptions";
78
- LocalStorageKeys2["SileroVADOptions"] = "micdrop.SileroVADOptions";
79
- return LocalStorageKeys2;
80
- })(LocalStorageKeys || {});
81
-
82
- // src/audio/utils/stopStream.ts
83
- function stopStream(stream) {
84
- stream?.getTracks().forEach((track) => track.stop());
72
+ var COS = new Float32Array(FFT_SIZE / 2);
73
+ var SIN = new Float32Array(FFT_SIZE / 2);
74
+ for (let i = 0; i < FFT_SIZE / 2; i++) {
75
+ COS[i] = Math.cos(-2 * Math.PI * i / FFT_SIZE);
76
+ SIN[i] = Math.sin(-2 * Math.PI * i / FFT_SIZE);
85
77
  }
86
-
87
- // src/audio/Mic.ts
88
- var Mic = class {
78
+ var VolumeMeter = class {
89
79
  constructor() {
90
- this.analyser = new AudioAnalyser(audioContext);
91
- }
92
- getMicConstraints(deviceId) {
93
- return {
94
- audio: {
95
- deviceId: { ideal: deviceId },
96
- sampleRate: 16e3,
97
- // not working, it will follow device settings, usually 44.1kHz
98
- sampleSize: 16,
99
- channelCount: 1,
100
- echoCancellation: true,
101
- noiseSuppression: true
102
- }
103
- };
80
+ this.real = new Float32Array(FFT_SIZE);
81
+ this.imaginary = new Float32Array(FFT_SIZE);
82
+ this.magnitudes = new Float32Array(BIN_COUNT);
83
+ this.smoothed = new Float32Array(BIN_COUNT);
84
+ this.hasPrevious = false;
104
85
  }
105
86
  /**
106
- * Starts the microphone
107
- * @param deviceId - The deviceId to use
108
- * @returns The microphone stream
87
+ * Measures a window of audio
88
+ * @param samples - At least 512 mono samples at 16 kHz, in the -1..1 range.
89
+ * Only the last 512 are looked at.
90
+ * @returns The level in decibels, between -Infinity (silence) and 0
109
91
  */
110
- async start(deviceId) {
111
- if (!audioContext || !this.analyser) {
112
- throw new Error("AudioContext not initialized");
92
+ measure(samples) {
93
+ if (samples.length === 0) return -Infinity;
94
+ const offset = Math.max(0, samples.length - FFT_SIZE);
95
+ const { real, imaginary } = this;
96
+ for (let i = 0; i < FFT_SIZE; i++) {
97
+ const sample = offset + i < samples.length ? samples[offset + i] : 0;
98
+ real[REVERSED[i]] = sample * WINDOW[i];
99
+ imaginary[REVERSED[i]] = 0;
113
100
  }
114
- if (!deviceId) {
115
- deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
116
- }
117
- if (this.audioStream) {
118
- if (this.deviceId === deviceId) {
119
- return this.audioStream;
101
+ for (let size = 2; size <= FFT_SIZE; size <<= 1) {
102
+ const half = size >> 1;
103
+ const step = FFT_SIZE / size;
104
+ for (let start = 0; start < FFT_SIZE; start += size) {
105
+ for (let i = 0; i < half; i++) {
106
+ const even = start + i;
107
+ const odd = even + half;
108
+ const cos = COS[i * step];
109
+ const sin = SIN[i * step];
110
+ const realOdd = real[odd] * cos - imaginary[odd] * sin;
111
+ const imaginaryOdd = real[odd] * sin + imaginary[odd] * cos;
112
+ real[odd] = real[even] - realOdd;
113
+ imaginary[odd] = imaginary[even] - imaginaryOdd;
114
+ real[even] += realOdd;
115
+ imaginary[even] += imaginaryOdd;
116
+ }
120
117
  }
121
- stopStream(this.audioStream);
122
118
  }
123
- this.audioStream = await navigator.mediaDevices.getUserMedia(
124
- this.getMicConstraints(deviceId)
125
- );
126
- const track = this.audioStream.getTracks()[0];
127
- let newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
128
- if (newDeviceId) {
129
- if (newDeviceId === "default" && deviceId) {
130
- newDeviceId = deviceId;
119
+ for (let k = 0; k < BIN_COUNT; k++) {
120
+ this.magnitudes[k] = Math.sqrt(real[k] * real[k] + imaginary[k] * imaginary[k]) / FFT_SIZE;
121
+ }
122
+ if (this.hasPrevious) {
123
+ for (let k = 0; k < BIN_COUNT; k++) {
124
+ this.smoothed[k] = SMOOTHING * this.smoothed[k] + (1 - SMOOTHING) * this.magnitudes[k];
131
125
  }
132
- localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
133
- this.deviceId = newDeviceId;
134
126
  } else {
135
- localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
136
- this.deviceId = void 0;
127
+ this.smoothed.set(this.magnitudes);
128
+ this.hasPrevious = true;
137
129
  }
138
- if (this.sourceNode) this.sourceNode.disconnect();
139
- this.sourceNode = audioContext.createMediaStreamSource(this.audioStream);
140
- this.sourceNode.connect(this.analyser.node);
141
- return this.audioStream;
130
+ let loudest = -Infinity;
131
+ for (let k = FIRST_BIN; k < BIN_COUNT; k++) {
132
+ const magnitude = this.smoothed[k];
133
+ if (magnitude === 0) continue;
134
+ const decibels = 20 * Math.log10(magnitude);
135
+ if (decibels > loudest && decibels < 0) loudest = decibels;
136
+ }
137
+ return loudest;
138
+ }
139
+ /** Forgets the previous window, for when capture stops and starts again */
140
+ reset() {
141
+ this.hasPrevious = false;
142
+ this.smoothed.fill(0);
143
+ }
144
+ };
145
+ function getVolume(samples) {
146
+ return new VolumeMeter().measure(samples);
147
+ }
148
+
149
+ // src/audio/Mic.ts
150
+ var VOLUME_INTERVAL = 100;
151
+ var VOLUME_WINDOW = 512;
152
+ var RATE_CHECK_DURATION = 2e3;
153
+ var RATE_TOLERANCE = 0.15;
154
+ var MicController = class extends EventEmitter {
155
+ constructor() {
156
+ super(...arguments);
157
+ this.meter = new VolumeMeter();
158
+ this.pending = [];
159
+ this.pendingLength = 0;
160
+ this.pendingRate = VOLUME_SAMPLE_RATE;
161
+ this.rateCheckedAt = 0;
162
+ this.rateCheckSamples = 0;
163
+ this.rateWarned = false;
164
+ /** Last level measured, in decibels, between -Infinity and 0 */
165
+ this.volume = -Infinity;
166
+ this.onFrames = (frames, sampleRate) => {
167
+ this.emit("Frames", frames, sampleRate);
168
+ this.checkRate(frames.length, sampleRate);
169
+ if (sampleRate !== this.pendingRate) {
170
+ this.pending = [];
171
+ this.pendingLength = 0;
172
+ this.pendingRate = sampleRate;
173
+ this.meter.reset();
174
+ }
175
+ this.pending.push(frames);
176
+ this.pendingLength += frames.length;
177
+ const interval = Math.round(VOLUME_INTERVAL / 1e3 * sampleRate);
178
+ if (this.pendingLength < interval) return;
179
+ const needed = Math.ceil(VOLUME_WINDOW * sampleRate / VOLUME_SAMPLE_RATE);
180
+ const gathered = concatFloat32(this.pending);
181
+ const window = gathered.subarray(Math.max(0, gathered.length - needed));
182
+ this.pending = [];
183
+ this.pendingLength = 0;
184
+ this.volume = this.meter.measure(
185
+ resample(window, sampleRate, VOLUME_SAMPLE_RATE)
186
+ );
187
+ this.emit("Volume", this.volume);
188
+ };
189
+ this.onDeviceChange = () => {
190
+ this.emit("DeviceChange");
191
+ };
192
+ this.onError = (error) => {
193
+ this.emit("Error", error);
194
+ };
142
195
  }
143
196
  /**
144
- * Stops the microphone
197
+ * Replaces the audio driver, to record with another library
198
+ * @param driver - The driver to record with
145
199
  */
146
- stop() {
147
- if (this.sourceNode) {
148
- this.sourceNode.disconnect();
149
- this.sourceNode = void 0;
200
+ setDriver(driver) {
201
+ if (this.driver === driver) return;
202
+ if (this.driver) {
203
+ this.driver.off("Frames", this.onFrames);
204
+ this.driver.off("DeviceChange", this.onDeviceChange);
205
+ this.driver.off("Error", this.onError);
150
206
  }
151
- if (this.audioStream) {
152
- stopStream(this.audioStream);
153
- this.audioStream = void 0;
207
+ this.driver = driver;
208
+ driver.on("Frames", this.onFrames);
209
+ driver.on("DeviceChange", this.onDeviceChange);
210
+ driver.on("Error", this.onError);
211
+ }
212
+ getDriver() {
213
+ if (!this.driver) {
214
+ throw new Error(
215
+ "No microphone driver. Import @micdrop/web in a browser or @micdrop/react-native on a phone, or call Mic.setDriver()."
216
+ );
217
+ }
218
+ return this.driver;
219
+ }
220
+ get isStarted() {
221
+ return this.driver?.isStarted ?? false;
222
+ }
223
+ get deviceId() {
224
+ return this.driver?.deviceId;
225
+ }
226
+ /**
227
+ * Asks for the microphone permission and starts capturing
228
+ * @param deviceId - Input device to record from, defaults to the system one
229
+ */
230
+ async start(deviceId) {
231
+ this.resetVolume();
232
+ await this.getDriver().start(deviceId);
233
+ }
234
+ /** Stops capturing and releases the microphone */
235
+ async stop() {
236
+ await this.driver?.stop();
237
+ this.resetVolume();
238
+ }
239
+ /** Lists the input devices the system offers */
240
+ async getDevices() {
241
+ if (!this.driver) return [];
242
+ return this.driver.getDevices();
243
+ }
244
+ /**
245
+ * Times the captured audio against the rate it claims to be at.
246
+ *
247
+ * A microphone that announces 16 kHz while delivering 48 kHz sends speech to
248
+ * the server three times too slow, which comes back as a short transcript in
249
+ * a random language. That is invisible otherwise, so it is worth saying out
250
+ * loud once.
251
+ * @param samples - How many samples just arrived
252
+ * @param sampleRate - The rate they are said to be at
253
+ */
254
+ checkRate(samples, sampleRate) {
255
+ if (this.rateWarned) return;
256
+ const now = Date.now();
257
+ if (this.rateCheckedAt === 0) {
258
+ this.rateCheckedAt = now;
259
+ return;
154
260
  }
261
+ this.rateCheckSamples += samples;
262
+ const elapsed = now - this.rateCheckedAt;
263
+ if (elapsed < RATE_CHECK_DURATION) return;
264
+ const delivered = this.rateCheckSamples / elapsed * 1e3;
265
+ this.rateCheckedAt = now;
266
+ this.rateCheckSamples = 0;
267
+ if (Math.abs(delivered - sampleRate) / sampleRate <= RATE_TOLERANCE) return;
268
+ this.rateWarned = true;
269
+ console.warn(
270
+ `[Micdrop] The microphone announces ${sampleRate} Hz but delivers about ${Math.round(delivered)} Hz. Speech is being sent at the wrong speed, so transcription will be poor.`
271
+ );
272
+ }
273
+ resetVolume() {
274
+ this.pending = [];
275
+ this.pendingLength = 0;
276
+ this.meter.reset();
277
+ this.volume = -Infinity;
278
+ this.rateCheckedAt = 0;
279
+ this.rateCheckSamples = 0;
155
280
  }
156
281
  };
157
282
 
158
283
  // src/audio/Speaker.ts
159
- import { EventEmitter as EventEmitter3 } from "eventemitter3";
160
-
161
- // src/audio/stream/AudioStream.ts
162
284
  import { EventEmitter as EventEmitter2 } from "eventemitter3";
163
- var AudioStream = class extends EventEmitter2 {
164
- constructor() {
165
- super();
166
- this.isPlaying = false;
167
- this.outputNode = audioContext.createGain();
168
- }
169
- setIsPlaying(isPlaying) {
170
- if (this.isPlaying === isPlaying) return;
171
- this.isPlaying = isPlaying;
172
- this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
173
- }
174
- };
175
285
 
176
- // src/audio/stream/Pcm16AudioStream.ts
177
- var SAMPLE_RATE = 16e3;
178
- var CHANNELS = 1;
179
- var PREBUFFER_DURATION = 100;
180
- var QUIET_FLUSH_DELAY = 600;
181
- var UTTERANCE_END_DELAY = 300;
182
- var Pcm16AudioStream = class extends AudioStream {
286
+ // src/storage.ts
287
+ var MicdropStorageKeys = /* @__PURE__ */ ((MicdropStorageKeys2) => {
288
+ MicdropStorageKeys2["MicDevice"] = "micdrop.micDevice";
289
+ MicdropStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
290
+ MicdropStorageKeys2["VolumeVADOptions"] = "micdrop.VolumeVADOptions";
291
+ MicdropStorageKeys2["SileroVADOptions"] = "micdrop.SileroVADOptions";
292
+ return MicdropStorageKeys2;
293
+ })(MicdropStorageKeys || {});
294
+ var MemoryStorage = class {
183
295
  constructor() {
184
- super();
185
- this.blobQueue = [];
186
- this.processing = false;
187
- this.sourceNodes = [];
188
- this.nextStartTime = 0;
189
- this.prebuffering = true;
190
- this.prebuffer = [];
191
- this.prebufferDuration = 0;
296
+ this.values = /* @__PURE__ */ new Map();
192
297
  }
193
- start() {
194
- return this.outputNode;
298
+ getItem(key) {
299
+ return this.values.get(key) ?? null;
195
300
  }
196
- async playAudio(blob) {
197
- this.blobQueue.push(blob);
198
- if (this.processing) return;
199
- this.processing = true;
200
- try {
201
- while (this.blobQueue.length > 0) {
202
- const next = this.blobQueue.shift();
203
- try {
204
- const arrayBuffer = await next.arrayBuffer();
205
- const audioBuffer = this.createAudioBuffer(arrayBuffer);
206
- if (audioBuffer) this.handleBuffer(audioBuffer);
207
- } catch (error) {
208
- console.error("Failed to play audio chunk:", error);
209
- }
210
- }
211
- } finally {
212
- this.processing = false;
213
- }
301
+ setItem(key, value) {
302
+ this.values.set(key, value);
214
303
  }
215
- stopAudio() {
216
- for (const node of this.sourceNodes) {
217
- try {
218
- node.stop();
219
- node.disconnect();
220
- } catch (error) {
221
- }
222
- }
223
- this.sourceNodes = [];
224
- this.blobQueue = [];
225
- this.nextStartTime = 0;
226
- this.resetPrebuffer();
227
- this.setIsPlaying(false);
228
- }
229
- // Accumulate audio until enough is buffered, then start playing. Once
230
- // playing, schedule incoming buffers directly.
231
- handleBuffer(audioBuffer) {
232
- if (this.endTimer !== void 0) {
233
- clearTimeout(this.endTimer);
234
- this.endTimer = void 0;
235
- }
236
- if (!this.prebuffering) {
237
- this.scheduleBuffer(audioBuffer);
238
- return;
239
- }
240
- this.prebuffer.push(audioBuffer);
241
- this.prebufferDuration += audioBuffer.duration * 1e3;
242
- if (this.prebufferDuration >= PREBUFFER_DURATION) {
243
- this.flushPrebuffer();
244
- return;
245
- }
246
- if (this.quietTimer !== void 0) clearTimeout(this.quietTimer);
247
- this.quietTimer = setTimeout(() => this.flushPrebuffer(), QUIET_FLUSH_DELAY);
248
- }
249
- flushPrebuffer() {
250
- if (this.quietTimer !== void 0) {
251
- clearTimeout(this.quietTimer);
252
- this.quietTimer = void 0;
253
- }
254
- this.prebuffering = false;
255
- const buffers = this.prebuffer;
256
- this.prebuffer = [];
257
- this.prebufferDuration = 0;
258
- for (const audioBuffer of buffers) this.scheduleBuffer(audioBuffer);
304
+ removeItem(key) {
305
+ this.values.delete(key);
259
306
  }
260
- resetPrebuffer() {
261
- if (this.quietTimer !== void 0) {
262
- clearTimeout(this.quietTimer);
263
- this.quietTimer = void 0;
307
+ };
308
+ var current = new MemoryStorage();
309
+ function setMicdropStorage(storage2) {
310
+ current = storage2;
311
+ }
312
+ var storage = {
313
+ getItem: (key) => {
314
+ try {
315
+ return current.getItem(key);
316
+ } catch {
317
+ return null;
264
318
  }
265
- if (this.endTimer !== void 0) {
266
- clearTimeout(this.endTimer);
267
- this.endTimer = void 0;
319
+ },
320
+ setItem: (key, value) => {
321
+ try {
322
+ current.setItem(key, value);
323
+ } catch {
268
324
  }
269
- this.prebuffering = true;
270
- this.prebuffer = [];
271
- this.prebufferDuration = 0;
272
- }
273
- createAudioBuffer(arrayBuffer) {
325
+ },
326
+ removeItem: (key) => {
274
327
  try {
275
- const int16Data = new Int16Array(arrayBuffer);
276
- const numSamples = int16Data.length;
277
- if (numSamples === 0) {
278
- return null;
279
- }
280
- const audioBuffer = audioContext.createBuffer(
281
- CHANNELS,
282
- numSamples,
283
- SAMPLE_RATE
284
- );
285
- const channelData = audioBuffer.getChannelData(0);
286
- for (let i = 0; i < numSamples; i++) {
287
- channelData[i] = int16Data[i] / 32768;
288
- }
289
- return audioBuffer;
290
- } catch (error) {
291
- console.error("Error creating AudioBuffer:", error);
292
- return null;
328
+ current.removeItem(key);
329
+ } catch {
293
330
  }
294
331
  }
295
- scheduleBuffer(audioBuffer) {
296
- const sourceNode = audioContext.createBufferSource();
297
- sourceNode.buffer = audioBuffer;
298
- sourceNode.connect(this.outputNode);
299
- const now = audioContext.currentTime;
300
- const startTime = Math.max(this.nextStartTime, now);
301
- this.nextStartTime = startTime + audioBuffer.duration;
302
- sourceNode.onended = () => {
303
- const index = this.sourceNodes.indexOf(sourceNode);
304
- if (index !== -1) this.sourceNodes.splice(index, 1);
305
- if (this.sourceNodes.length === 0 && this.blobQueue.length === 0) {
306
- this.nextStartTime = 0;
307
- this.setIsPlaying(false);
308
- if (this.endTimer !== void 0) clearTimeout(this.endTimer);
309
- this.endTimer = setTimeout(() => {
310
- this.endTimer = void 0;
311
- this.resetPrebuffer();
312
- }, UTTERANCE_END_DELAY);
313
- }
314
- };
315
- this.sourceNodes.push(sourceNode);
316
- sourceNode.start(startTime);
317
- this.setIsPlaying(true);
318
- }
319
332
  };
320
333
 
321
334
  // src/audio/Speaker.ts
322
- var Speaker = class extends EventEmitter3 {
335
+ var SpeakerController = class extends EventEmitter2 {
323
336
  constructor() {
324
337
  super(...arguments);
325
- this.isPlaying = false;
326
- this.analyser = new AudioAnalyser(audioContext);
327
- // Set by MicdropClient, useful to detect default device
328
- this.devices = [];
338
+ /** Last level measured, in decibels, between -Infinity and 0 */
339
+ this.volume = -Infinity;
340
+ this.onStartPlaying = () => this.emit("StartPlaying");
341
+ this.onStopPlaying = () => {
342
+ this.volume = -Infinity;
343
+ this.emit("StopPlaying");
344
+ };
345
+ this.onVolume = (volume) => {
346
+ this.volume = volume;
347
+ this.emit("Volume", volume);
348
+ };
329
349
  }
330
- // Used when audiocontext sinkId is not available
331
- async start() {
332
- if (this.audioStream) return;
333
- this.audioStream = new Pcm16AudioStream();
334
- this.audioStream.on("StartPlaying", () => this.setIsPlaying(true));
335
- this.audioStream.on("StopPlaying", () => this.setIsPlaying(false));
336
- this.audioSourceNode = this.audioStream.start();
337
- const deviceId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) ?? void 0;
338
- await this.setDevice(deviceId);
339
- }
340
- async playAudio(blob) {
341
- this.start();
342
- try {
343
- await this.audioStream?.playAudio(blob);
344
- } catch (error) {
345
- console.error("Error playing audio blob", error, blob);
350
+ /**
351
+ * Replaces the audio driver, to play sound with another library
352
+ * @param driver - The driver to play with
353
+ */
354
+ setDriver(driver) {
355
+ if (this.driver === driver) return;
356
+ if (this.driver) {
357
+ this.driver.off("StartPlaying", this.onStartPlaying);
358
+ this.driver.off("StopPlaying", this.onStopPlaying);
359
+ this.driver.off("Volume", this.onVolume);
346
360
  }
347
- }
348
- async stopAudio() {
349
- if (!this.isPlaying) return;
350
- this.audioStream?.stopAudio();
351
- }
352
- async changeDevice(deviceId) {
353
- await this.setDevice(deviceId);
354
- if (this.isDefaultDevice) {
355
- localStorage.removeItem("micdrop.speakerDevice" /* SpeakerDevice */);
356
- } else {
357
- localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, deviceId);
361
+ this.driver = driver;
362
+ driver.on("StartPlaying", this.onStartPlaying);
363
+ driver.on("StopPlaying", this.onStopPlaying);
364
+ driver.on("Volume", this.onVolume);
365
+ }
366
+ getDriver() {
367
+ if (!this.driver) {
368
+ throw new Error(
369
+ "No speaker driver. Import @micdrop/web in a browser or @micdrop/react-native on a phone, or call Speaker.setDriver()."
370
+ );
358
371
  }
372
+ return this.driver;
359
373
  }
360
- get isDefaultDevice() {
361
- return !this.deviceId || this.devices.length === 0 || this.deviceId === this.devices[0].deviceId;
374
+ get isPlaying() {
375
+ return this.driver?.isPlaying ?? false;
362
376
  }
363
- setIsPlaying(isPlaying) {
364
- if (this.isPlaying === isPlaying) return;
365
- this.isPlaying = isPlaying;
366
- this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
377
+ get deviceId() {
378
+ return this.driver?.deviceId;
367
379
  }
368
- async setDevice(deviceId) {
369
- if (!this.audioSourceNode) {
370
- console.error(
371
- "audioSourceNode not initialized, setDevice called before start"
372
- );
373
- return;
380
+ /** Prepares the audio graph, called before the first chunk arrives */
381
+ async start() {
382
+ const driver = this.getDriver();
383
+ await driver.start();
384
+ const saved = storage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
385
+ if (saved && saved !== driver.deviceId) {
386
+ try {
387
+ await driver.changeDevice(saved);
388
+ } catch {
389
+ storage.removeItem("micdrop.speakerDevice" /* SpeakerDevice */);
390
+ }
374
391
  }
392
+ }
393
+ /**
394
+ * Queues samples for playback, right after the ones already queued
395
+ * @param pcm - Mono samples as 16 bits integers, or the bytes holding them
396
+ * @param sampleRate - Sample rate of `pcm`, in Hz
397
+ */
398
+ playAudio(pcm, sampleRate = 16e3) {
375
399
  try {
376
- this.deviceId = deviceId;
377
- const defaultDevice = this.isDefaultDevice;
378
- const sinkId = !deviceId || defaultDevice ? "" : deviceId;
379
- if (defaultDevice || "setSinkId" in audioContext) {
380
- await audioContext.setSinkId?.(sinkId);
381
- this.audioSourceNode.disconnect();
382
- this.audioSourceNode.connect(audioContext.destination);
383
- this.audioSourceNode.connect(this.analyser.node);
384
- if (this.audioElementDestination) {
385
- this.audioElementDestination.pause();
386
- this.audioElementDestination.srcObject = null;
387
- this.audioElementDestination = void 0;
388
- }
389
- } else {
390
- if (!this.audioElementDestination) {
391
- const mediaStreamDestination = audioContext.createMediaStreamDestination();
392
- this.audioSourceNode.disconnect();
393
- this.audioSourceNode.connect(mediaStreamDestination);
394
- this.audioSourceNode.connect(this.analyser.node);
395
- this.audioElementDestination = new Audio();
396
- this.audioElementDestination.autoplay = true;
397
- this.audioElementDestination.srcObject = mediaStreamDestination.stream;
398
- }
399
- await this.audioElementDestination.setSinkId(sinkId);
400
- }
400
+ const samples = pcm instanceof Int16Array ? pcm : new Int16Array(pcm);
401
+ this.getDriver().play(samples, sampleRate);
401
402
  } catch (error) {
402
- console.error(`Error setting Audio Output to ${deviceId}`, error);
403
+ console.error("[Micdrop] Error playing audio", error);
403
404
  }
404
405
  }
406
+ /** Drops everything queued and stops playing immediately */
407
+ stopAudio() {
408
+ this.driver?.stopAudio();
409
+ }
410
+ /** Releases the audio graph */
411
+ async stop() {
412
+ await this.driver?.stop();
413
+ this.volume = -Infinity;
414
+ }
415
+ /**
416
+ * Plays through another output
417
+ * @param deviceId - Output device, or route on a phone
418
+ */
419
+ async changeDevice(deviceId) {
420
+ await this.getDriver().changeDevice(deviceId);
421
+ storage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, deviceId);
422
+ }
423
+ /** Lists the output devices the system offers */
424
+ async getDevices() {
425
+ if (!this.driver) return [];
426
+ return this.driver.getDevices();
427
+ }
405
428
  };
406
429
 
407
430
  // src/audio/MicRecorder.ts
408
- import { EventEmitter as EventEmitter5 } from "eventemitter3";
409
-
410
- // src/audio/utils/delayedStream.ts
411
- function createDelayedStream(stream, delay) {
412
- const audioSource = audioContext.createMediaStreamSource(stream);
413
- const delayNode = audioContext.createDelay(delay);
414
- delayNode.delayTime.value = delay;
415
- const audioDestination = audioContext.createMediaStreamDestination();
416
- audioSource.connect(delayNode);
417
- delayNode.connect(audioDestination);
418
- return audioDestination.stream;
419
- }
431
+ import { EventEmitter as EventEmitter4 } from "eventemitter3";
420
432
 
421
433
  // src/audio/vad/VAD.ts
422
- import { EventEmitter as EventEmitter4 } from "eventemitter3";
434
+ import { EventEmitter as EventEmitter3 } from "eventemitter3";
423
435
  var VADStatus = /* @__PURE__ */ ((VADStatus2) => {
424
436
  VADStatus2[VADStatus2["Silence"] = 0] = "Silence";
425
437
  VADStatus2[VADStatus2["MaybeSpeaking"] = 1] = "MaybeSpeaking";
426
438
  VADStatus2[VADStatus2["Speaking"] = 2] = "Speaking";
427
439
  return VADStatus2;
428
440
  })(VADStatus || {});
429
- var VAD = class extends EventEmitter4 {
441
+ var VAD = class extends EventEmitter3 {
430
442
  constructor() {
431
443
  super(...arguments);
432
444
  this.name = this.constructor.name;
433
445
  this.status = 0 /* Silence */;
434
- // Max delay to detect speech, can be used to record delayed stream
446
+ // Worst case time this VAD takes to notice that someone started speaking.
447
+ // That much audio is kept in reserve and sent along with the turn, so the
448
+ // beginning of a sentence reaches the server rather than being cut off.
449
+ // A VAD that needs several samples to make up its mind has to say so here.
435
450
  this.delay = 100;
436
451
  }
437
452
  /**
@@ -514,13 +529,13 @@ var MultipleVAD = class extends VAD {
514
529
  get isPaused() {
515
530
  return this.vads.every((vad) => vad.isPaused);
516
531
  }
517
- async start(stream) {
532
+ async start(mic) {
518
533
  for (const vad of this.vads) {
519
534
  if (vad.isStarted) {
520
535
  continue;
521
536
  }
522
537
  vad.on("ChangeStatus", this.onStatusChange);
523
- await vad.start(stream);
538
+ await vad.start(mic);
524
539
  }
525
540
  }
526
541
  async stop() {
@@ -542,87 +557,96 @@ var MultipleVAD = class extends VAD {
542
557
  };
543
558
 
544
559
  // src/audio/vad/SileroVAD.ts
545
- import { MicVAD } from "@ricky0123/vad-web";
560
+ var SILERO_SAMPLE_RATE = 16e3;
561
+ var SILERO_FRAME_SAMPLES = 512;
562
+ var MS_PER_FRAME = SILERO_FRAME_SAMPLES / SILERO_SAMPLE_RATE * 1e3;
546
563
  var defaultOptions = {
547
564
  positiveSpeechThreshold: 0.18,
548
565
  negativeSpeechThreshold: 0.11,
549
- minSpeechFrames: 8,
550
- redemptionFrames: 20
566
+ minSpeechFrames: 4,
567
+ redemptionFrames: 8
551
568
  };
569
+ var loadModel;
570
+ function setSileroModelLoader(loader) {
571
+ loadModel = loader;
572
+ }
552
573
  var SileroVAD = class extends VAD {
553
574
  constructor(options) {
554
575
  super();
555
576
  this.name = "SileroVAD";
556
577
  this.options = defaultOptions;
578
+ // The model decides on the first window it hears speech in, so it is quick.
579
+ // The reserve still covers a few windows, since scoring runs asynchronously
580
+ // and may lag behind the audio on a busy device.
581
+ this.delay = 3 * MS_PER_FRAME;
557
582
  this._isPaused = false;
583
+ this.buffer = [];
584
+ this.bufferLength = 0;
585
+ this.bufferRate = SILERO_SAMPLE_RATE;
586
+ this.speaking = false;
587
+ this.speechFrames = 0;
588
+ this.silenceFrames = 0;
589
+ this.processing = false;
590
+ this.onFrames = (frames, sampleRate) => {
591
+ if (this._isPaused || !this.model) return;
592
+ if (sampleRate !== this.bufferRate) {
593
+ this.buffer = [];
594
+ this.bufferLength = 0;
595
+ this.bufferRate = sampleRate;
596
+ }
597
+ this.buffer.push(frames);
598
+ this.bufferLength += frames.length;
599
+ this.drain();
600
+ };
558
601
  if (options) {
559
602
  this.setOptions(options);
560
603
  } else {
561
- const savedOptions = localStorage.getItem(
562
- "micdrop.SileroVADOptions" /* SileroVADOptions */
563
- );
604
+ const saved = storage.getItem("micdrop.SileroVADOptions" /* SileroVADOptions */);
564
605
  try {
565
- this.setOptions(savedOptions ? JSON.parse(savedOptions) : {});
566
- } catch (error) {
606
+ this.setOptions(saved ? JSON.parse(saved) : {});
607
+ } catch {
567
608
  this.setOptions({});
568
609
  }
569
610
  }
570
611
  }
571
612
  get isStarted() {
572
- return !!this.vad;
613
+ return !!this.mic;
573
614
  }
574
615
  get isPaused() {
575
616
  return this._isPaused;
576
617
  }
577
- async start(stream) {
578
- if (this.vad) return;
579
- this.vad = await MicVAD.new({
580
- getStream: async () => stream,
581
- pauseStream: async () => {
582
- },
583
- resumeStream: async () => stream,
584
- model: "v5",
585
- submitUserSpeechOnPause: true,
586
- baseAssetPath: "https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@latest/dist/",
587
- // onnxruntime-web@1.23.2 is not hosted on jsdelivr (its dist exceeds the
588
- // CDN size limit, so every file 404s). unpkg serves it, so the ONNX wasm
589
- // backend is loaded from there instead.
590
- onnxWASMBasePath: "https://unpkg.com/onnxruntime-web@1.23.2/dist/",
591
- onSpeechStart: () => {
592
- this.emit("StartSpeaking");
593
- },
594
- onSpeechRealStart: () => {
595
- this.emit("ConfirmSpeaking");
596
- },
597
- onVADMisfire: () => {
598
- this.emit("CancelSpeaking");
599
- },
600
- onSpeechEnd: () => {
601
- this.emit("StopSpeaking");
602
- },
603
- ...this.options
604
- });
605
- this.vad.start();
618
+ async start(mic) {
619
+ if (this.mic) return;
620
+ if (!loadModel) {
621
+ throw new Error(
622
+ "No Silero model. Add `import '@micdrop/web/silero'` in a browser or `import '@micdrop/react-native/silero'` on a phone, or call setSileroModelLoader()."
623
+ );
624
+ }
625
+ this.model = await loadModel();
626
+ this.resetState();
627
+ this.mic = mic;
628
+ mic.on("Frames", this.onFrames);
606
629
  }
607
630
  async stop() {
608
- if (!this.vad) return;
609
- this.vad.destroy();
610
- this.vad = void 0;
631
+ this.mic?.off("Frames", this.onFrames);
632
+ this.mic = void 0;
633
+ this.closeOpenTurn();
634
+ this.resetState();
635
+ await this.model?.release();
636
+ this.model = void 0;
611
637
  }
612
638
  async pause() {
613
- if (!this.vad || this._isPaused) return;
639
+ if (this._isPaused) return;
614
640
  this._isPaused = true;
615
- this.vad.pause();
641
+ this.closeOpenTurn();
642
+ this.resetState();
616
643
  }
617
644
  async resume() {
618
- if (!this.vad || !this._isPaused) return;
619
645
  this._isPaused = false;
620
- this.vad.start();
621
646
  }
622
647
  setOptions(options) {
623
648
  this.options = { ...this.options, ...options };
624
- this.vad?.setOptions(this.options);
625
- localStorage.setItem(
649
+ storage.setItem(
626
650
  "micdrop.SileroVADOptions" /* SileroVADOptions */,
627
651
  JSON.stringify(this.options)
628
652
  );
@@ -630,7 +654,79 @@ var SileroVAD = class extends VAD {
630
654
  resetOptions() {
631
655
  this.setOptions(defaultOptions);
632
656
  }
657
+ // The model is asynchronous, so windows are scored one after the other rather
658
+ // than raced: its answer for a window depends on the ones before it.
659
+ async drain() {
660
+ if (this.processing) return;
661
+ this.processing = true;
662
+ try {
663
+ const needed = Math.ceil(
664
+ SILERO_FRAME_SAMPLES * this.bufferRate / SILERO_SAMPLE_RATE
665
+ );
666
+ while (this.bufferLength >= needed && this.model && !this._isPaused) {
667
+ const gathered = concatFloat32(this.buffer);
668
+ const window = gathered.subarray(0, needed);
669
+ const rest = gathered.subarray(needed);
670
+ this.buffer = rest.length ? [rest] : [];
671
+ this.bufferLength = rest.length;
672
+ const frame = resample(window, this.bufferRate, SILERO_SAMPLE_RATE);
673
+ const probability = await this.model.process(
674
+ frame.length === SILERO_FRAME_SAMPLES ? frame : fit(frame, SILERO_FRAME_SAMPLES)
675
+ );
676
+ this.onProbability(probability);
677
+ }
678
+ } catch (error) {
679
+ console.error("[Micdrop] Silero VAD failed", error);
680
+ } finally {
681
+ this.processing = false;
682
+ }
683
+ }
684
+ // Ported from the reference implementation of the algorithm
685
+ // https://docs.vad.ricky0123.com/user-guide/algorithm/
686
+ onProbability(probability) {
687
+ const { positiveSpeechThreshold, negativeSpeechThreshold } = this.options;
688
+ if (probability >= positiveSpeechThreshold) {
689
+ this.silenceFrames = 0;
690
+ this.speechFrames++;
691
+ if (!this.speaking) {
692
+ this.speaking = true;
693
+ this.emit("StartSpeaking");
694
+ }
695
+ if (this.speechFrames === this.options.minSpeechFrames) {
696
+ this.emit("ConfirmSpeaking");
697
+ }
698
+ return;
699
+ }
700
+ if (probability < negativeSpeechThreshold && this.speaking) {
701
+ this.silenceFrames++;
702
+ if (this.silenceFrames < this.options.redemptionFrames) return;
703
+ const confirmed = this.speechFrames >= this.options.minSpeechFrames;
704
+ this.speaking = false;
705
+ this.speechFrames = 0;
706
+ this.silenceFrames = 0;
707
+ this.emit(confirmed ? "StopSpeaking" : "CancelSpeaking");
708
+ }
709
+ }
710
+ closeOpenTurn() {
711
+ if (!this.speaking) return;
712
+ const confirmed = this.speechFrames >= this.options.minSpeechFrames;
713
+ this.emit(confirmed ? "StopSpeaking" : "CancelSpeaking");
714
+ }
715
+ resetState() {
716
+ this.model?.reset();
717
+ this.buffer = [];
718
+ this.bufferLength = 0;
719
+ this.speaking = false;
720
+ this.speechFrames = 0;
721
+ this.silenceFrames = 0;
722
+ }
633
723
  };
724
+ function fit(frame, length) {
725
+ if (frame.length === length) return frame;
726
+ const fitted = new Float32Array(length);
727
+ fitted.set(frame.subarray(0, length));
728
+ return fitted;
729
+ }
634
730
 
635
731
  // src/audio/vad/VolumeVAD.ts
636
732
  var defaultOptions2 = {
@@ -642,138 +738,92 @@ var VolumeVAD = class extends VAD {
642
738
  super();
643
739
  this.name = "VolumeVAD";
644
740
  this.options = defaultOptions2;
645
- this._isPaused = false;
741
+ // The microphone reports a level about every 100 ms, and two consecutive loud
742
+ // ones are needed before a turn opens. Speech can therefore begin up to two
743
+ // reports before it is noticed, and a third is kept as a margin: without that
744
+ // reserve the first syllable of a sentence never reaches the server.
745
+ this.delay = 300;
646
746
  this.running = false;
747
+ this._isPaused = false;
647
748
  this.speaking = false;
648
749
  this.attemptSpeaking = false;
649
750
  this.speakingHistory = [];
751
+ this.onVolume = (volume) => {
752
+ if (!this.running) return;
753
+ const isLoud = volume > this.options.threshold;
754
+ if (isLoud) {
755
+ if (!this.speaking) {
756
+ let recentHistory = 0;
757
+ for (let i = Math.max(0, this.speakingHistory.length - 3); i < this.speakingHistory.length; i++) {
758
+ recentHistory += this.speakingHistory[i];
759
+ }
760
+ if (recentHistory >= 2) {
761
+ this.speaking = true;
762
+ this.attemptSpeaking = false;
763
+ this.emit("ConfirmSpeaking");
764
+ } else if (recentHistory === 1 && !this.attemptSpeaking) {
765
+ this.attemptSpeaking = true;
766
+ this.emit("StartSpeaking");
767
+ }
768
+ }
769
+ } else if (this.speaking) {
770
+ let totalHistory = 0;
771
+ for (let i = 0; i < this.speakingHistory.length; i++) {
772
+ totalHistory += this.speakingHistory[i];
773
+ }
774
+ if (totalHistory === 0) {
775
+ this.speaking = false;
776
+ this.emit("StopSpeaking");
777
+ }
778
+ } else if (this.attemptSpeaking) {
779
+ const lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
780
+ if (lastHistory === 0) {
781
+ this.attemptSpeaking = false;
782
+ this.emit("CancelSpeaking");
783
+ }
784
+ }
785
+ this.speakingHistory.shift();
786
+ this.speakingHistory.push(Number(isLoud));
787
+ };
650
788
  if (options) {
651
789
  this.setOptions(options);
652
790
  } else {
653
- const savedOptions = localStorage.getItem(
654
- "micdrop.VolumeVADOptions" /* VolumeVADOptions */
655
- );
791
+ const saved = storage.getItem("micdrop.VolumeVADOptions" /* VolumeVADOptions */);
656
792
  try {
657
- this.setOptions(savedOptions ? JSON.parse(savedOptions) : {});
658
- } catch (error) {
793
+ this.setOptions(saved ? JSON.parse(saved) : {});
794
+ } catch {
659
795
  this.setOptions({});
660
796
  }
661
797
  }
662
798
  }
663
- getMaxVolume() {
664
- if (!this.analyser || !this.fftBins) return -Infinity;
665
- let maxVolume = -Infinity;
666
- this.analyser.getFloatFrequencyData(this.fftBins);
667
- for (let i = 4; i < this.fftBins.length; i++) {
668
- if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
669
- maxVolume = this.fftBins[i];
670
- }
671
- }
672
- return maxVolume;
673
- }
674
799
  get isStarted() {
675
800
  return this.running;
676
801
  }
677
802
  get isPaused() {
678
803
  return this._isPaused;
679
804
  }
680
- async start(stream) {
681
- if (this.running || this._isPaused) return;
682
- this.analyser = audioContext.createAnalyser();
683
- this.sourceNode = audioContext.createMediaStreamSource(stream);
684
- this.analyser.fftSize = 512;
685
- this.analyser.smoothingTimeConstant = 0.1;
686
- this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
687
- this.sourceNode.connect(this.analyser);
688
- this.running = true;
689
- this.startLoop();
690
- }
691
- startLoop() {
692
- if (!this.running) return;
693
- const currentVolume = this.getMaxVolume();
694
- if (currentVolume > this.options.threshold) {
695
- if (!this.speaking) {
696
- let recentHistory = 0;
697
- for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
698
- recentHistory += this.speakingHistory[i];
699
- }
700
- if (recentHistory >= 2) {
701
- this.speaking = true;
702
- this.attemptSpeaking = false;
703
- this.emit("ConfirmSpeaking");
704
- } else if (recentHistory === 1) {
705
- this.attemptSpeaking = true;
706
- this.emit("StartSpeaking");
707
- }
708
- }
709
- } else if (this.speaking) {
710
- let totalHistory = 0;
711
- for (let i = 0; i < this.speakingHistory.length; i++) {
712
- totalHistory += this.speakingHistory[i];
713
- }
714
- if (totalHistory === 0) {
715
- this.speaking = false;
716
- this.emit("StopSpeaking");
717
- }
718
- } else if (this.attemptSpeaking) {
719
- let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
720
- if (lastHistory === 0) {
721
- this.attemptSpeaking = false;
722
- this.emit("CancelSpeaking");
723
- }
724
- }
725
- this.speakingHistory.shift();
726
- this.speakingHistory.push(Number(currentVolume > this.options.threshold));
727
- setTimeout(() => this.startLoop(), this.delay);
728
- }
729
- resetHistory() {
730
- this.speaking = false;
731
- this.attemptSpeaking = false;
732
- for (let i = 0; i < this.speakingHistory.length; i++) {
733
- this.speakingHistory[i] = 0;
734
- }
805
+ async start(mic) {
806
+ if (this.mic) return;
807
+ this.mic = mic;
808
+ mic.on("Volume", this.onVolume);
809
+ this.running = !this._isPaused;
735
810
  }
736
811
  async stop() {
737
- if (!this.running) return;
812
+ this.mic?.off("Volume", this.onVolume);
813
+ this.mic = void 0;
738
814
  this.running = false;
739
- this.resetHistory();
740
- switch (this.status) {
741
- case 2 /* Speaking */:
742
- this.emit("StopSpeaking");
743
- break;
744
- case 1 /* MaybeSpeaking */:
745
- this.emit("CancelSpeaking");
746
- break;
747
- }
748
- if (this.sourceNode) {
749
- this.sourceNode.disconnect();
750
- this.sourceNode = void 0;
751
- }
752
- if (this.analyser) {
753
- this.analyser.disconnect();
754
- this.analyser = void 0;
755
- }
756
- this.fftBins = void 0;
815
+ this.reset();
757
816
  }
758
817
  async pause() {
759
- if (!this.running || this._isPaused) return;
818
+ if (this._isPaused) return;
760
819
  this._isPaused = true;
761
820
  this.running = false;
762
- this.resetHistory();
763
- switch (this.status) {
764
- case 2 /* Speaking */:
765
- this.emit("StopSpeaking");
766
- break;
767
- case 1 /* MaybeSpeaking */:
768
- this.emit("CancelSpeaking");
769
- break;
770
- }
821
+ this.reset();
771
822
  }
772
823
  async resume() {
773
824
  if (!this._isPaused) return;
774
825
  this._isPaused = false;
775
- this.running = true;
776
- this.startLoop();
826
+ this.running = !!this.mic;
777
827
  }
778
828
  setOptions(options) {
779
829
  if (typeof options.history === "number" && options.history < 3) {
@@ -786,7 +836,7 @@ var VolumeVAD = class extends VAD {
786
836
  while (this.speakingHistory.length < this.options.history) {
787
837
  this.speakingHistory.push(0);
788
838
  }
789
- localStorage.setItem(
839
+ storage.setItem(
790
840
  "micdrop.VolumeVADOptions" /* VolumeVADOptions */,
791
841
  JSON.stringify(this.options)
792
842
  );
@@ -794,6 +844,22 @@ var VolumeVAD = class extends VAD {
794
844
  resetOptions() {
795
845
  this.setOptions(defaultOptions2);
796
846
  }
847
+ reset() {
848
+ const status = this.status;
849
+ this.speaking = false;
850
+ this.attemptSpeaking = false;
851
+ for (let i = 0; i < this.speakingHistory.length; i++) {
852
+ this.speakingHistory[i] = 0;
853
+ }
854
+ switch (status) {
855
+ case 2 /* Speaking */:
856
+ this.emit("StopSpeaking");
857
+ break;
858
+ case 1 /* MaybeSpeaking */:
859
+ this.emit("CancelSpeaking");
860
+ break;
861
+ }
862
+ }
797
863
  };
798
864
 
799
865
  // src/audio/vad/getVAD.ts
@@ -815,34 +881,36 @@ function equalVADConfig(a, b) {
815
881
  return false;
816
882
  }
817
883
 
818
- // raw:./pcm-processor-worklet
819
- var pcm_processor_worklet_default = "const INTERVAL_MS = 100\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.audioBuffer = []\n this.isRecording = false\n this.sourceSampleRate = 16000\n this.targetSampleRate = 16000\n this.resamplingRatio = 1\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n\n // Listen for messages\n this.port.onmessage = this.handleMessage.bind(this)\n }\n\n handleMessage(event) {\n if (event.data.type === 'configure') {\n this.sourceSampleRate = event.data.sourceSampleRate\n this.targetSampleRate = event.data.targetSampleRate\n this.resamplingRatio = this.sourceSampleRate / this.targetSampleRate\n // Recalculate chunk interval for the source sample rate\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n } else if (event.data.type === 'start') {\n this.isRecording = true\n } else if (event.data.type === 'stop') {\n this.isRecording = false\n // Clear any remaining audio buffer when stopping\n this.audioBuffer.length = 0\n }\n }\n\n process(inputs) {\n const input = inputs[0]\n\n if (input && input.length > 0 && this.isRecording) {\n const inputChannel = input[0]\n\n // Store audio data in buffer\n this.audioBuffer.push(new Float32Array(inputChannel))\n\n // Check if we have enough data for a chunk\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n\n if (totalSamples >= this.CHUNK_INTERVAL_SAMPLES) {\n this.processChunk()\n }\n }\n\n return true\n }\n\n processChunk() {\n if (this.audioBuffer.length === 0) return\n\n // Calculate total samples\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n if (totalSamples === 0) return\n\n // Merge all buffers\n const mergedBuffer = new Float32Array(totalSamples)\n let offset = 0\n for (const buffer of this.audioBuffer) {\n mergedBuffer.set(buffer, offset)\n offset += buffer.length\n }\n\n // Resample to target sample rate if necessary\n const resampledBuffer = this.resample(mergedBuffer)\n\n // Convert float32 to int16 PCM\n const pcmData = this.floatToPCM16(resampledBuffer)\n\n // Clear the buffer\n this.audioBuffer.length = 0\n\n // Send PCM chunk to main thread\n this.port.postMessage({\n type: 'chunk',\n data: pcmData,\n })\n }\n\n resample(inputBuffer) {\n // If no resampling needed, return original buffer\n if (this.resamplingRatio === 1) {\n return inputBuffer\n }\n\n // Calculate output length\n const outputLength = Math.round(inputBuffer.length / this.resamplingRatio)\n const outputBuffer = new Float32Array(outputLength)\n\n // Simple linear interpolation resampling\n for (let i = 0; i < outputLength; i++) {\n const srcIndex = i * this.resamplingRatio\n const srcIndexFloor = Math.floor(srcIndex)\n const srcIndexCeil = Math.min(srcIndexFloor + 1, inputBuffer.length - 1)\n const fraction = srcIndex - srcIndexFloor\n\n // Linear interpolation\n outputBuffer[i] =\n inputBuffer[srcIndexFloor] * (1 - fraction) +\n inputBuffer[srcIndexCeil] * fraction\n }\n\n return outputBuffer\n }\n\n floatToPCM16(floatData) {\n const pcmData = new Int16Array(floatData.length)\n for (let i = 0; i < floatData.length; i++) {\n const sample = Math.max(-1, Math.min(1, floatData[i]))\n pcmData[i] = sample < 0 ? sample * 0x8000 : sample * 0x7fff\n }\n return pcmData\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n";
820
-
821
- // src/audio/pcm-processor.ts
822
- async function initPcmProcessor() {
823
- if (window.micdropPcmProcessor) return;
824
- window.micdropPcmProcessor = true;
825
- const workletBlob = new Blob([pcm_processor_worklet_default], {
826
- type: "application/javascript"
827
- });
828
- const workletUrl = URL.createObjectURL(workletBlob);
829
- await audioContext.audioWorklet.addModule(workletUrl);
830
- }
831
-
832
884
  // src/audio/MicRecorder.ts
833
- var SAMPLE_RATE2 = 16e3;
885
+ var SAMPLE_RATE = 16e3;
886
+ var CHUNK_INTERVAL = 100;
887
+ var DEFAULT_TURN_MAX_WAIT = 4e3;
834
888
  var defaultMicRecorderState = {
835
889
  isStarting: false,
836
890
  isStarted: false,
837
891
  isSpeaking: false
838
892
  };
839
- var MicRecorder = class extends EventEmitter5 {
840
- constructor(vadConfig) {
893
+ var MicRecorder = class extends EventEmitter4 {
894
+ constructor(vadConfig, turnDetector, turnMaxWait = DEFAULT_TURN_MAX_WAIT) {
841
895
  super();
842
896
  this.vadConfig = vadConfig;
897
+ this.turnDetector = turnDetector;
898
+ this.turnMaxWait = turnMaxWait;
899
+ this.isRecording = false;
843
900
  this.speakingConfirmed = false;
901
+ this.sourceSampleRate = SAMPLE_RATE;
902
+ this.reserve = [];
903
+ this.reserveLength = 0;
904
+ this.buffer = [];
905
+ this.bufferLength = 0;
844
906
  this.queuedChunks = [];
845
- this.start = async (stream) => {
907
+ // The turn is open between the first confirmed word and the moment the
908
+ // server is told to answer, which can span several pauses
909
+ this.turnOpen = false;
910
+ this.turnListening = false;
911
+ // Bumped whenever the turn moves on, so a late answer is dropped
912
+ this.turnRound = 0;
913
+ this.start = async (mic) => {
846
914
  if (this.state.isStarted) {
847
915
  throw new Error("MicRecorder is already started");
848
916
  }
@@ -851,21 +919,10 @@ var MicRecorder = class extends EventEmitter5 {
851
919
  isStarting: true,
852
920
  isSpeaking: false
853
921
  });
854
- this.stream = stream;
855
- this.speakingConfirmed = false;
856
- await initPcmProcessor();
857
- this.workletNode = new AudioWorkletNode(audioContext, "pcm-processor");
858
- this.workletNode.port.onmessage = this.onWorkletMessage;
859
- this.workletNode.port.postMessage({
860
- type: "configure",
861
- sourceSampleRate: audioContext.sampleRate,
862
- targetSampleRate: SAMPLE_RATE2
863
- });
864
- const delayedStream = createDelayedStream(stream, this.vad.delay / 1e3);
865
- this.delayedStream = delayedStream;
866
- this.source = audioContext.createMediaStreamSource(delayedStream);
867
- this.source.connect(this.workletNode);
868
- await this.vad.start(stream);
922
+ this.mic = mic;
923
+ this.resetBuffers();
924
+ mic.on("Frames", this.onFrames);
925
+ await this.vad.start(mic);
869
926
  this.vad.on("StartSpeaking", this.onStartSpeaking);
870
927
  this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
871
928
  this.vad.on("CancelSpeaking", this.onCancelSpeaking);
@@ -885,79 +942,213 @@ var MicRecorder = class extends EventEmitter5 {
885
942
  isStarted: false,
886
943
  isSpeaking: false
887
944
  });
888
- this.stream = void 0;
945
+ this.cancelPendingClose();
889
946
  try {
947
+ this.mic?.off("Frames", this.onFrames);
948
+ this.mic = void 0;
890
949
  this.vad.stop();
891
950
  this.vad.off("StartSpeaking", this.onStartSpeaking);
892
951
  this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
893
952
  this.vad.off("CancelSpeaking", this.onCancelSpeaking);
894
953
  this.vad.off("StopSpeaking", this.onStopSpeaking);
895
- if (this.workletNode) {
896
- this.workletNode.disconnect();
897
- this.workletNode = void 0;
898
- }
899
- if (this.source) {
900
- this.source.disconnect();
901
- this.source = void 0;
902
- }
903
- if (this.delayedStream) {
904
- stopStream(this.delayedStream);
905
- this.delayedStream = void 0;
906
- }
907
954
  } catch (err) {
908
955
  console.error(err);
909
956
  }
957
+ this.resetBuffers();
958
+ };
959
+ /**
960
+ * Changes what decides when a turn is over
961
+ * @param turnDetector - The new detector, or nothing to leave it to the VAD
962
+ */
963
+ this.changeTurnDetector = (turnDetector) => {
964
+ if (turnDetector === this.turnDetector) return;
965
+ if (this.turnOpen) {
966
+ this.endTurn();
967
+ }
968
+ this.turnDetector = turnDetector;
910
969
  };
911
970
  this.changeVad = (vadConfig) => {
912
971
  if (equalVADConfig(vadConfig, this.vadConfig)) return;
913
- const stream = this.stream;
914
- if (stream) {
972
+ const mic = this.mic;
973
+ if (mic) {
915
974
  this.stop();
916
975
  }
917
976
  this.vadConfig = vadConfig;
918
977
  this.vad = getVAD(vadConfig);
919
- if (stream) {
920
- this.start(stream);
978
+ if (mic) {
979
+ this.start(mic);
921
980
  }
922
981
  };
923
- this.onStartSpeaking = async () => {
982
+ this.onFrames = (frames, sampleRate) => {
983
+ if (this.turnListening) {
984
+ this.turnDetector?.push(frames, sampleRate);
985
+ }
986
+ if (sampleRate !== this.sourceSampleRate) {
987
+ this.flushChunks();
988
+ this.sourceSampleRate = sampleRate;
989
+ this.reserve = [];
990
+ this.reserveLength = 0;
991
+ }
992
+ if (this.isRecording) {
993
+ this.buffer.push(frames);
994
+ this.bufferLength += frames.length;
995
+ this.flushChunks();
996
+ return;
997
+ }
998
+ this.reserve.push(frames);
999
+ this.reserveLength += frames.length;
1000
+ const reserveMax = Math.round(this.vad.delay / 1e3 * sampleRate);
1001
+ while (this.reserve.length > 1 && this.reserveLength - this.reserve[0].length >= reserveMax) {
1002
+ this.reserveLength -= this.reserve[0].length;
1003
+ this.reserve.shift();
1004
+ }
1005
+ };
1006
+ this.onStartSpeaking = () => {
924
1007
  this.speakingConfirmed = false;
925
1008
  this.queuedChunks.length = 0;
926
- this.workletNode?.port.postMessage({ type: "start" });
1009
+ this.isRecording = true;
1010
+ this.buffer = this.reserve;
1011
+ this.bufferLength = this.reserveLength;
1012
+ this.reserve = [];
1013
+ this.reserveLength = 0;
1014
+ if (this.turnDetector && !this.turnListening) {
1015
+ this.turnDetector.reset();
1016
+ this.turnListening = true;
1017
+ for (const frames of this.buffer) {
1018
+ this.turnDetector.push(frames, this.sourceSampleRate);
1019
+ }
1020
+ }
1021
+ this.flushChunks();
927
1022
  };
928
- this.onConfirmSpeaking = async () => {
1023
+ this.onConfirmSpeaking = () => {
929
1024
  this.speakingConfirmed = true;
930
1025
  this.changeState({ isSpeaking: true });
931
- this.emit("StartSpeaking");
932
- };
933
- this.onCancelSpeaking = async () => {
1026
+ this.cancelPendingClose();
1027
+ if (!this.turnOpen) {
1028
+ this.turnOpen = true;
1029
+ this.emit("StartSpeaking");
1030
+ }
1031
+ for (const chunk of this.queuedChunks) {
1032
+ this.emit("Chunk", chunk);
1033
+ }
934
1034
  this.queuedChunks.length = 0;
935
- this.workletNode?.port.postMessage({ type: "stop" });
936
1035
  };
937
- this.onStopSpeaking = async () => {
938
- this.speakingConfirmed = false;
939
- this.changeState({ isSpeaking: false });
940
- this.emit("StopSpeaking");
941
- this.workletNode?.port.postMessage({ type: "stop" });
1036
+ this.onCancelSpeaking = () => {
1037
+ this.stopRecording();
1038
+ if (!this.turnOpen) {
1039
+ this.turnListening = false;
1040
+ }
942
1041
  };
943
- this.onWorkletMessage = (event) => {
944
- const { type, data } = event.data;
945
- if (type === "chunk") {
946
- const pcmData = data;
947
- if (!this.speakingConfirmed) {
948
- this.queuedChunks.push(pcmData);
949
- return;
950
- }
951
- for (const chunk of this.queuedChunks) {
952
- this.emit("Chunk", chunk);
953
- }
954
- this.queuedChunks.length = 0;
955
- this.emit("Chunk", pcmData);
1042
+ this.onStopSpeaking = () => {
1043
+ const wasConfirmed = this.speakingConfirmed;
1044
+ this.changeState({ isSpeaking: false });
1045
+ if (wasConfirmed) {
1046
+ this.flushChunks(true);
956
1047
  }
1048
+ this.stopRecording();
1049
+ this.closeTurn();
957
1050
  };
958
1051
  this.state = defaultMicRecorderState;
959
1052
  this.vad = getVAD(vadConfig);
960
1053
  }
1054
+ /**
1055
+ * Ends the turn, unless the detector hears an unfinished sentence.
1056
+ *
1057
+ * A microphone that is no longer listening ends it straight away: a muted or
1058
+ * paused call has nothing more to say, whatever the sentence sounded like.
1059
+ */
1060
+ async closeTurn() {
1061
+ if (!this.turnDetector || !this.turnOpen || !this.vad.isStarted || this.vad.isPaused) {
1062
+ this.endTurn();
1063
+ return;
1064
+ }
1065
+ const round = this.turnRound;
1066
+ try {
1067
+ const { complete } = await this.turnDetector.predict();
1068
+ if (round !== this.turnRound) return;
1069
+ if (complete) {
1070
+ this.endTurn();
1071
+ return;
1072
+ }
1073
+ } catch (error) {
1074
+ console.error("[Micdrop] Turn detection failed", error);
1075
+ this.endTurn();
1076
+ return;
1077
+ }
1078
+ this.cancelPendingClose();
1079
+ this.turnTimer = setTimeout(() => {
1080
+ this.turnTimer = void 0;
1081
+ this.endTurn();
1082
+ }, this.turnMaxWait);
1083
+ }
1084
+ endTurn() {
1085
+ this.cancelPendingClose();
1086
+ this.turnOpen = false;
1087
+ this.turnListening = false;
1088
+ this.emit("StopSpeaking");
1089
+ }
1090
+ /** Drops the pending close, both the timer and the answer still owed */
1091
+ cancelPendingClose() {
1092
+ this.turnRound++;
1093
+ if (this.turnTimer) {
1094
+ clearTimeout(this.turnTimer);
1095
+ this.turnTimer = void 0;
1096
+ }
1097
+ }
1098
+ stopRecording() {
1099
+ this.isRecording = false;
1100
+ this.speakingConfirmed = false;
1101
+ this.queuedChunks.length = 0;
1102
+ this.buffer = [];
1103
+ this.bufferLength = 0;
1104
+ }
1105
+ /**
1106
+ * Emits every complete chunk waiting in the buffer
1107
+ * @param final - Also emit the incomplete last chunk
1108
+ */
1109
+ flushChunks(final = false) {
1110
+ const chunkLength = Math.round(
1111
+ CHUNK_INTERVAL / 1e3 * this.sourceSampleRate
1112
+ );
1113
+ if (this.bufferLength === 0) return;
1114
+ if (this.bufferLength < chunkLength && !final) return;
1115
+ const merged = concatFloat32(this.buffer);
1116
+ let offset = 0;
1117
+ while (merged.length - offset >= chunkLength) {
1118
+ this.emitChunk(merged.subarray(offset, offset + chunkLength));
1119
+ offset += chunkLength;
1120
+ }
1121
+ const rest = merged.subarray(offset);
1122
+ if (final && rest.length > 0) {
1123
+ this.emitChunk(rest);
1124
+ this.buffer = [];
1125
+ this.bufferLength = 0;
1126
+ return;
1127
+ }
1128
+ this.buffer = rest.length ? [rest] : [];
1129
+ this.bufferLength = rest.length;
1130
+ }
1131
+ emitChunk(samples) {
1132
+ const pcm = floatToPcm16(
1133
+ resample(samples, this.sourceSampleRate, SAMPLE_RATE)
1134
+ );
1135
+ if (!this.speakingConfirmed) {
1136
+ this.queuedChunks.push(pcm);
1137
+ return;
1138
+ }
1139
+ this.emit("Chunk", pcm);
1140
+ }
1141
+ resetBuffers() {
1142
+ this.isRecording = false;
1143
+ this.speakingConfirmed = false;
1144
+ this.turnOpen = false;
1145
+ this.turnListening = false;
1146
+ this.reserve = [];
1147
+ this.reserveLength = 0;
1148
+ this.buffer = [];
1149
+ this.bufferLength = 0;
1150
+ this.queuedChunks.length = 0;
1151
+ }
961
1152
  changeState(state) {
962
1153
  const hasChanged = Object.keys(state).some(
963
1154
  (key) => this.state[key] !== state[key]
@@ -968,18 +1159,182 @@ var MicRecorder = class extends EventEmitter5 {
968
1159
  }
969
1160
  };
970
1161
 
1162
+ // src/audio/Pcm16AudioStream.ts
1163
+ import { EventEmitter as EventEmitter5 } from "eventemitter3";
1164
+ var PREBUFFER_DURATION = 100;
1165
+ var QUIET_FLUSH_DELAY = 600;
1166
+ var UTTERANCE_END_DELAY = 300;
1167
+ var VOLUME_INTERVAL2 = 100;
1168
+ var Pcm16AudioStream = class extends EventEmitter5 {
1169
+ constructor(sink) {
1170
+ super();
1171
+ this.sink = sink;
1172
+ this.isPlaying = false;
1173
+ this.sources = [];
1174
+ this.nextStartTime = 0;
1175
+ this.prebuffering = true;
1176
+ this.prebuffer = [];
1177
+ this.prebufferDuration = 0;
1178
+ this.volumeTimers = [];
1179
+ this.meter = new VolumeMeter();
1180
+ }
1181
+ /**
1182
+ * Queues samples for playback
1183
+ * @param pcm - Mono samples as 16 bits integers
1184
+ * @param sampleRate - Sample rate of `pcm`, in Hz
1185
+ */
1186
+ playAudio(pcm, sampleRate) {
1187
+ if (pcm.length === 0) return;
1188
+ const samples = resample(
1189
+ pcm16ToFloat(pcm),
1190
+ sampleRate,
1191
+ this.sink.sampleRate
1192
+ );
1193
+ this.handleSamples(samples);
1194
+ }
1195
+ /** Drops everything queued and stops playing immediately */
1196
+ stopAudio() {
1197
+ for (const source of this.sources) {
1198
+ try {
1199
+ source.stop();
1200
+ } catch (error) {
1201
+ }
1202
+ }
1203
+ this.sources = [];
1204
+ this.nextStartTime = 0;
1205
+ this.resetPrebuffer();
1206
+ this.setIsPlaying(false);
1207
+ this.clearVolumeTimers();
1208
+ }
1209
+ // Accumulate audio until enough is buffered, then start playing. Once
1210
+ // playing, schedule incoming samples directly.
1211
+ handleSamples(samples) {
1212
+ if (this.endTimer !== void 0) {
1213
+ clearTimeout(this.endTimer);
1214
+ this.endTimer = void 0;
1215
+ }
1216
+ if (!this.prebuffering) {
1217
+ this.scheduleSamples(samples);
1218
+ return;
1219
+ }
1220
+ this.prebuffer.push(samples);
1221
+ this.prebufferDuration += samples.length / this.sink.sampleRate * 1e3;
1222
+ if (this.prebufferDuration >= PREBUFFER_DURATION) {
1223
+ this.flushPrebuffer();
1224
+ return;
1225
+ }
1226
+ if (this.quietTimer !== void 0) clearTimeout(this.quietTimer);
1227
+ this.quietTimer = setTimeout(() => this.flushPrebuffer(), QUIET_FLUSH_DELAY);
1228
+ }
1229
+ flushPrebuffer() {
1230
+ if (this.quietTimer !== void 0) {
1231
+ clearTimeout(this.quietTimer);
1232
+ this.quietTimer = void 0;
1233
+ }
1234
+ this.prebuffering = false;
1235
+ const buffers = this.prebuffer;
1236
+ this.prebuffer = [];
1237
+ this.prebufferDuration = 0;
1238
+ for (const samples of buffers) this.scheduleSamples(samples);
1239
+ }
1240
+ resetPrebuffer() {
1241
+ if (this.quietTimer !== void 0) {
1242
+ clearTimeout(this.quietTimer);
1243
+ this.quietTimer = void 0;
1244
+ }
1245
+ if (this.endTimer !== void 0) {
1246
+ clearTimeout(this.endTimer);
1247
+ this.endTimer = void 0;
1248
+ }
1249
+ this.prebuffering = true;
1250
+ this.prebuffer = [];
1251
+ this.prebufferDuration = 0;
1252
+ }
1253
+ scheduleSamples(samples) {
1254
+ const duration = samples.length / this.sink.sampleRate;
1255
+ const now = this.sink.currentTime;
1256
+ const startTime = Math.max(this.nextStartTime, now);
1257
+ this.nextStartTime = startTime + duration;
1258
+ const source = this.sink.schedule(samples, startTime, () => {
1259
+ const index = this.sources.indexOf(source);
1260
+ if (index !== -1) this.sources.splice(index, 1);
1261
+ if (this.sources.length === 0) {
1262
+ this.nextStartTime = 0;
1263
+ this.setIsPlaying(false);
1264
+ if (this.endTimer !== void 0) clearTimeout(this.endTimer);
1265
+ this.endTimer = setTimeout(() => {
1266
+ this.endTimer = void 0;
1267
+ this.resetPrebuffer();
1268
+ }, UTTERANCE_END_DELAY);
1269
+ }
1270
+ });
1271
+ this.sources.push(source);
1272
+ this.scheduleVolume(samples, startTime - now);
1273
+ this.setIsPlaying(true);
1274
+ }
1275
+ // Report the level of what is being heard rather than of what just arrived:
1276
+ // a burst of chunks would otherwise light up the meter all at once and leave
1277
+ // it dark for the rest of the sentence.
1278
+ scheduleVolume(samples, delay) {
1279
+ const step = Math.max(
1280
+ 1,
1281
+ Math.round(VOLUME_INTERVAL2 / 1e3 * this.sink.sampleRate)
1282
+ );
1283
+ for (let offset = 0; offset < samples.length; offset += step) {
1284
+ const slice = samples.subarray(offset, offset + step);
1285
+ const at = delay * 1e3 + offset / this.sink.sampleRate * 1e3;
1286
+ const timer = setTimeout(
1287
+ () => {
1288
+ const index = this.volumeTimers.indexOf(timer);
1289
+ if (index !== -1) this.volumeTimers.splice(index, 1);
1290
+ this.emit(
1291
+ "Volume",
1292
+ this.meter.measure(
1293
+ resample(slice, this.sink.sampleRate, VOLUME_SAMPLE_RATE)
1294
+ )
1295
+ );
1296
+ },
1297
+ Math.max(0, at)
1298
+ );
1299
+ this.volumeTimers.push(timer);
1300
+ }
1301
+ }
1302
+ clearVolumeTimers() {
1303
+ for (const timer of this.volumeTimers) clearTimeout(timer);
1304
+ this.volumeTimers = [];
1305
+ this.meter.reset();
1306
+ }
1307
+ setIsPlaying(isPlaying) {
1308
+ if (this.isPlaying === isPlaying) return;
1309
+ this.isPlaying = isPlaying;
1310
+ if (!isPlaying) {
1311
+ this.clearVolumeTimers();
1312
+ this.emit("Volume", -Infinity);
1313
+ }
1314
+ this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
1315
+ }
1316
+ };
1317
+
1318
+ // src/audio/types.ts
1319
+ import { EventEmitter as EventEmitter6 } from "eventemitter3";
1320
+ var MicDriver = class extends EventEmitter6 {
1321
+ };
1322
+ var SpeakerDriver = class extends EventEmitter6 {
1323
+ };
1324
+
971
1325
  // src/audio/index.ts
972
- if (!window.micdropMic) {
973
- window.micdropMic = new Mic();
1326
+ var globalScope = globalThis;
1327
+ if (!globalScope.micdropMic) {
1328
+ globalScope.micdropMic = new MicController();
974
1329
  }
975
- var Mic2 = window.micdropMic;
976
- if (!window.micdropSpeaker) {
977
- window.micdropSpeaker = new Speaker();
1330
+ if (!globalScope.micdropSpeaker) {
1331
+ globalScope.micdropSpeaker = new SpeakerController();
978
1332
  }
979
- var Speaker2 = window.micdropSpeaker;
1333
+ var Mic = globalScope.micdropMic;
1334
+ var Speaker = globalScope.micdropSpeaker;
980
1335
 
981
1336
  // src/client/MicdropClient.ts
982
- import { EventEmitter as EventEmitter6 } from "eventemitter3";
1337
+ import { EventEmitter as EventEmitter7 } from "eventemitter3";
983
1338
 
984
1339
  // ../server/src/types.ts
985
1340
  var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
@@ -1017,7 +1372,7 @@ var MicdropClientError = class extends Error {
1017
1372
  }
1018
1373
  };
1019
1374
  function getClientErrorFromWSCloseEvent(event) {
1020
- const code = getClientErrorFromWSCloseEventCode(event.code);
1375
+ const code = getClientErrorFromWSCloseEventCode(event.code ?? 1006);
1021
1376
  return code && new MicdropClientError(code, event.reason);
1022
1377
  }
1023
1378
  function getClientErrorFromWSCloseEventCode(code) {
@@ -1047,7 +1402,7 @@ var DEFAULT_RECONNECT_OPTIONS = {
1047
1402
  delayMs: 1e3,
1048
1403
  connectionTimeout: 5e3
1049
1404
  };
1050
- var MicdropClient = class extends EventEmitter6 {
1405
+ var MicdropClient = class extends EventEmitter7 {
1051
1406
  constructor(options = {}) {
1052
1407
  super();
1053
1408
  this.options = options;
@@ -1072,7 +1427,7 @@ var MicdropClient = class extends EventEmitter6 {
1072
1427
  this._isReconnecting = false;
1073
1428
  this.reconnectAttempt = 0;
1074
1429
  if (this.reconnectTimer) {
1075
- window.clearTimeout(this.reconnectTimer);
1430
+ clearTimeout(this.reconnectTimer);
1076
1431
  this.reconnectTimer = void 0;
1077
1432
  }
1078
1433
  if (!this.micRecorder || options?.vad) {
@@ -1086,7 +1441,7 @@ var MicdropClient = class extends EventEmitter6 {
1086
1441
  this._isPaused = false;
1087
1442
  this._isReconnecting = false;
1088
1443
  if (this.reconnectTimer) {
1089
- window.clearTimeout(this.reconnectTimer);
1444
+ clearTimeout(this.reconnectTimer);
1090
1445
  this.reconnectTimer = void 0;
1091
1446
  }
1092
1447
  try {
@@ -1094,13 +1449,12 @@ var MicdropClient = class extends EventEmitter6 {
1094
1449
  } catch (error) {
1095
1450
  console.error("[MicdropClient] Error stopping WebSocket", error);
1096
1451
  }
1452
+ Speaker.stopAudio();
1097
1453
  try {
1098
- this.micRecorder?.stop();
1099
- this.stopMic();
1454
+ await this.stopMic();
1100
1455
  } catch (error) {
1101
1456
  console.error("[MicdropClient] Error stopping microphone", error);
1102
1457
  }
1103
- Speaker2.stopAudio();
1104
1458
  };
1105
1459
  this.mute = () => {
1106
1460
  this.vad?.pause();
@@ -1108,7 +1462,7 @@ var MicdropClient = class extends EventEmitter6 {
1108
1462
  this.notifyStateChange();
1109
1463
  };
1110
1464
  this.unmute = () => {
1111
- if (!this.isPaused && !(this.options.disableInterruption && Speaker2.isPlaying)) {
1465
+ if (!this.isPaused && !(this.options.disableInterruption && Speaker.isPlaying)) {
1112
1466
  this.vad?.resume();
1113
1467
  }
1114
1468
  this._isMuted = false;
@@ -1120,7 +1474,7 @@ var MicdropClient = class extends EventEmitter6 {
1120
1474
  this._isPaused = true;
1121
1475
  this._isProcessing = false;
1122
1476
  this.notifyStateChange();
1123
- Speaker2.stopAudio();
1477
+ Speaker.stopAudio();
1124
1478
  this.ws?.send("Mute" /* Mute */);
1125
1479
  };
1126
1480
  this.resume = () => {
@@ -1144,20 +1498,24 @@ var MicdropClient = class extends EventEmitter6 {
1144
1498
  this.micRecorder.changeVad(options.vad);
1145
1499
  }
1146
1500
  } else {
1147
- this.micRecorder = new MicRecorder(this.options.vad);
1501
+ this.micRecorder = new MicRecorder(
1502
+ this.options.vad,
1503
+ this.options.turnDetector,
1504
+ this.options.turnMaxWait
1505
+ );
1148
1506
  this.micRecorder.on("StateChange", () => {
1149
1507
  this.notifyStateChange();
1150
1508
  });
1151
- this.micRecorder.on("Chunk", (blob) => {
1152
- this.log(`[MicdropClient] User audio chunk`, blob);
1153
- this.ws?.send(blob);
1509
+ this.micRecorder.on("Chunk", (chunk) => {
1510
+ this.log("User audio chunk", chunk.length);
1511
+ this.ws?.send(pcm16ToArrayBuffer(chunk));
1154
1512
  });
1155
1513
  this.micRecorder.on("StartSpeaking", () => {
1156
1514
  this.log("User start speaking");
1157
1515
  this.ws?.send("StartSpeaking" /* StartSpeaking */);
1158
1516
  this._isProcessing = false;
1159
1517
  this.notifyStateChange();
1160
- Speaker2.stopAudio();
1518
+ Speaker.stopAudio();
1161
1519
  });
1162
1520
  this.micRecorder.on("StopSpeaking", () => {
1163
1521
  this.log("User stop speaking");
@@ -1168,11 +1526,16 @@ var MicdropClient = class extends EventEmitter6 {
1168
1526
  this.notifyStateChange();
1169
1527
  });
1170
1528
  }
1171
- const micStream = await Mic2.start(options.deviceId);
1172
- this.micStream = micStream;
1173
- await this.micRecorder.start(micStream);
1529
+ const deviceId = options.deviceId ?? storage.getItem("micdrop.micDevice" /* MicDevice */) ?? void 0;
1530
+ await Mic.start(deviceId);
1531
+ if (Mic.deviceId) {
1532
+ storage.setItem("micdrop.micDevice" /* MicDevice */, Mic.deviceId);
1533
+ } else {
1534
+ storage.removeItem("micdrop.micDevice" /* MicDevice */);
1535
+ }
1536
+ await this.micRecorder.start(Mic);
1537
+ await Speaker.start();
1174
1538
  await this.updateDevices();
1175
- await Speaker2.start();
1176
1539
  this.notifyStateChange();
1177
1540
  } catch (error) {
1178
1541
  this.setError(
@@ -1185,41 +1548,48 @@ var MicdropClient = class extends EventEmitter6 {
1185
1548
  throw error;
1186
1549
  }
1187
1550
  };
1551
+ /**
1552
+ * Changes what decides when a turn is over, during a call or before one
1553
+ * @param turnDetector - The new detector, or nothing to leave it to the VAD
1554
+ */
1555
+ this.setTurnDetector = (turnDetector) => {
1556
+ this.options.turnDetector = turnDetector;
1557
+ this.micRecorder?.changeTurnDetector(turnDetector);
1558
+ };
1559
+ /**
1560
+ * Says how long a held turn waits for the rest of the sentence.
1561
+ *
1562
+ * The way out of a wrong verdict: a detector can hear an unfinished sentence
1563
+ * where there is none, and the speaker who never comes back still gets an
1564
+ * answer once this runs out.
1565
+ * @param milliseconds - How long to wait past the detector asking to
1566
+ */
1567
+ this.setTurnMaxWait = (milliseconds) => {
1568
+ this.options.turnMaxWait = milliseconds;
1569
+ if (this.micRecorder) {
1570
+ this.micRecorder.turnMaxWait = milliseconds;
1571
+ }
1572
+ };
1188
1573
  this.changeMicDevice = async (deviceId) => {
1189
1574
  await this.startMic({ deviceId });
1190
1575
  };
1191
1576
  this.changeSpeakerDevice = async (deviceId) => {
1192
- await Speaker2.changeDevice(deviceId);
1577
+ await Speaker.changeDevice(deviceId);
1193
1578
  this.notifyStateChange();
1194
1579
  };
1195
1580
  this.updateDevices = async () => {
1196
- const rawDevices = await navigator.mediaDevices.enumerateDevices();
1197
- if (rawDevices.length === 0) return;
1198
- const defaultDevices = rawDevices.filter(
1199
- (device) => device.deviceId === "default"
1200
- );
1201
- const devices = rawDevices.filter((device) => device.deviceId !== "default");
1202
- const defaultDevicesIndexes = defaultDevices.map(
1203
- (device) => devices.findIndex(
1204
- (d) => d.groupId === device.groupId && d.kind === device.kind
1205
- )
1206
- ).filter((index) => index !== -1);
1207
- for (const index of defaultDevicesIndexes) {
1208
- const device = devices[index];
1209
- devices.splice(index, 1);
1210
- devices.unshift(device);
1211
- }
1212
- this.micDevices = devices.filter((device) => device.kind === "audioinput");
1213
- this.speakerDevices = devices.filter(
1214
- (device) => device.kind === "audiooutput"
1215
- );
1216
- Speaker2.devices = this.speakerDevices;
1581
+ const [micDevices, speakerDevices] = await Promise.all([
1582
+ Mic.getDevices(),
1583
+ Speaker.getDevices()
1584
+ ]);
1585
+ this.micDevices = micDevices;
1586
+ this.speakerDevices = speakerDevices;
1217
1587
  this.notifyStateChange();
1218
1588
  };
1219
1589
  this.onWSOpen = () => {
1220
1590
  this.log("WebSocket opened");
1221
1591
  if (this.connectionTimer) {
1222
- window.clearTimeout(this.connectionTimer);
1592
+ clearTimeout(this.connectionTimer);
1223
1593
  this.connectionTimer = void 0;
1224
1594
  }
1225
1595
  this._isReconnecting = false;
@@ -1234,36 +1604,36 @@ var MicdropClient = class extends EventEmitter6 {
1234
1604
  }
1235
1605
  };
1236
1606
  this.onWSMessage = (event) => {
1237
- this.log("Received message:", event.data);
1238
- if (event.data instanceof Blob) {
1607
+ const data = event.data;
1608
+ if (data instanceof ArrayBuffer) {
1609
+ this.log("Received audio", data.byteLength);
1239
1610
  if (this.isPaused || this.isUserSpeaking) return;
1240
- Speaker2.playAudio(event.data);
1611
+ Speaker.playAudio(new Int16Array(data));
1241
1612
  this._isProcessing = false;
1242
1613
  this.notifyStateChange();
1243
- } else if (typeof event.data !== "string") {
1244
- console.warn(`[MicdropClient] Unknown message type: ${event.data}`);
1245
- } else if (event.data.startsWith("Message" /* Message */)) {
1614
+ return;
1615
+ }
1616
+ if (typeof data !== "string") {
1617
+ console.warn(`[MicdropClient] Unknown message type: ${data}`);
1618
+ return;
1619
+ }
1620
+ this.log("Received message:", data);
1621
+ if (data.startsWith("Message" /* Message */)) {
1246
1622
  try {
1247
1623
  const message = JSON.parse(
1248
- event.data.substring("Message" /* Message */.length + 1)
1624
+ data.substring("Message" /* Message */.length + 1)
1249
1625
  );
1250
1626
  this.addMessage(message);
1251
1627
  } catch (error) {
1252
- console.error(
1253
- "[MicdropClient] Error parsing message:",
1254
- event.data,
1255
- error
1256
- );
1628
+ console.error("[MicdropClient] Error parsing message:", data, error);
1257
1629
  }
1258
- } else if (event.data === "EndCall" /* EndCall */) {
1630
+ } else if (data === "EndCall" /* EndCall */) {
1259
1631
  this.emit("EndCall");
1260
- } else if (event.data === "SkipAnswer" /* SkipAnswer */) {
1632
+ } else if (data === "SkipAnswer" /* SkipAnswer */) {
1261
1633
  this._isProcessing = false;
1262
1634
  this.notifyStateChange();
1263
- } else if (event.data === "CancelLastUserMessage" /* CancelLastUserMessage */) {
1264
- const lastMessage = this.conversation.findLastIndex(
1265
- (message) => message.role === "user"
1266
- );
1635
+ } else if (data === "CancelLastUserMessage" /* CancelLastUserMessage */) {
1636
+ const lastMessage = this.conversation.map((message) => message.role).lastIndexOf("user");
1267
1637
  if (lastMessage !== -1) {
1268
1638
  this.conversation = this.conversation.filter(
1269
1639
  (_, index) => index !== lastMessage
@@ -1271,24 +1641,20 @@ var MicdropClient = class extends EventEmitter6 {
1271
1641
  this._isProcessing = false;
1272
1642
  this.notifyStateChange();
1273
1643
  }
1274
- } else if (event.data.startsWith("ToolCall" /* ToolCall */)) {
1644
+ } else if (data.startsWith("ToolCall" /* ToolCall */)) {
1275
1645
  try {
1276
1646
  const toolCall = JSON.parse(
1277
- event.data.substring("ToolCall" /* ToolCall */.length + 1)
1647
+ data.substring("ToolCall" /* ToolCall */.length + 1)
1278
1648
  );
1279
1649
  this.emit("ToolCall", toolCall);
1280
1650
  } catch (error) {
1281
- console.error(
1282
- "[MicdropClient] Error parsing tool call:",
1283
- event.data,
1284
- error
1285
- );
1651
+ console.error("[MicdropClient] Error parsing tool call:", data, error);
1286
1652
  }
1287
1653
  }
1288
1654
  };
1289
1655
  this.onWSClose = (event) => {
1290
- this.log("WebSocket closed", event);
1291
- const error = getClientErrorFromWSCloseEvent(event);
1656
+ this.log("WebSocket closed", event?.code, event?.reason);
1657
+ const error = getClientErrorFromWSCloseEvent(event ?? {});
1292
1658
  if (error) {
1293
1659
  this.setError(error);
1294
1660
  }
@@ -1302,7 +1668,7 @@ var MicdropClient = class extends EventEmitter6 {
1302
1668
  this.stop();
1303
1669
  }
1304
1670
  };
1305
- this.onWSError = (event) => {
1671
+ this.onWSError = () => {
1306
1672
  this.setError(new MicdropClientError("Connection" /* Connection */));
1307
1673
  };
1308
1674
  this.onSpeakerStartPlaying = () => {
@@ -1325,9 +1691,9 @@ var MicdropClient = class extends EventEmitter6 {
1325
1691
  this.notifyStateChange();
1326
1692
  }
1327
1693
  };
1328
- Speaker2.on("StartPlaying", this.onSpeakerStartPlaying);
1329
- Speaker2.on("StopPlaying", this.onSpeakerStopPlaying);
1330
- navigator.mediaDevices.addEventListener("devicechange", this.updateDevices);
1694
+ Speaker.on("StartPlaying", this.onSpeakerStartPlaying);
1695
+ Speaker.on("StopPlaying", this.onSpeakerStopPlaying);
1696
+ Mic.on("DeviceChange", this.updateDevices);
1331
1697
  }
1332
1698
  get vad() {
1333
1699
  return this.micRecorder?.vad;
@@ -1354,13 +1720,13 @@ var MicdropClient = class extends EventEmitter6 {
1354
1720
  return this.isMicStarted && !this.isPaused && !this.isProcessing && !this.isMicMuted && !this.isUserSpeaking && !this.isAssistantSpeaking;
1355
1721
  }
1356
1722
  get isWSStarted() {
1357
- return this.ws?.readyState === WebSocket.OPEN;
1723
+ return this.ws?.readyState === 1;
1358
1724
  }
1359
1725
  get isWSStarting() {
1360
- return this.ws?.readyState === WebSocket.CONNECTING;
1726
+ return this.ws?.readyState === 0;
1361
1727
  }
1362
1728
  get isMicStarted() {
1363
- return !!this.micStream;
1729
+ return Mic.isStarted;
1364
1730
  }
1365
1731
  get isMicMuted() {
1366
1732
  return this.vad?.isPaused ?? false;
@@ -1369,13 +1735,13 @@ var MicdropClient = class extends EventEmitter6 {
1369
1735
  return this.micRecorder?.state.isSpeaking ?? false;
1370
1736
  }
1371
1737
  get isAssistantSpeaking() {
1372
- return Speaker2.isPlaying;
1738
+ return Speaker.isPlaying;
1373
1739
  }
1374
1740
  get micDeviceId() {
1375
- return Mic2.deviceId;
1741
+ return Mic.deviceId;
1376
1742
  }
1377
1743
  get speakerDeviceId() {
1378
- return Speaker2.deviceId;
1744
+ return Speaker.deviceId;
1379
1745
  }
1380
1746
  get state() {
1381
1747
  return {
@@ -1398,11 +1764,10 @@ var MicdropClient = class extends EventEmitter6 {
1398
1764
  speakerDevices: this.speakerDevices
1399
1765
  };
1400
1766
  }
1401
- stopMic() {
1767
+ async stopMic() {
1402
1768
  this.micRecorder?.stop();
1403
1769
  this.micRecorder = void 0;
1404
- Mic2.stop();
1405
- this.micStream = void 0;
1770
+ await Mic.stop();
1406
1771
  this.notifyStateChange();
1407
1772
  }
1408
1773
  async startWS() {
@@ -1418,22 +1783,22 @@ var MicdropClient = class extends EventEmitter6 {
1418
1783
  throw new MicdropClientError("MissingUrl" /* MissingUrl */);
1419
1784
  }
1420
1785
  this.ws = new WebSocket(this.options.url);
1421
- this.ws.binaryType = "blob";
1786
+ this.ws.binaryType = "arraybuffer";
1422
1787
  this.notifyStateChange();
1423
1788
  this.ws.onopen = this.onWSOpen;
1424
1789
  this.ws.onmessage = this.onWSMessage;
1425
1790
  this.ws.onclose = this.onWSClose;
1426
1791
  this.ws.onerror = this.onWSError;
1427
- this.connectionTimer = window.setTimeout(() => {
1792
+ this.connectionTimer = setTimeout(() => {
1428
1793
  this.connectionTimer = void 0;
1429
- if (this.ws?.readyState === WebSocket.CONNECTING) {
1794
+ if (this.ws?.readyState === 0) {
1430
1795
  this.log("WebSocket connection timeout");
1431
1796
  this.ws.close();
1432
1797
  }
1433
1798
  }, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
1434
1799
  } catch (error) {
1435
1800
  this.setError(
1436
- new MicdropClientError(
1801
+ error instanceof MicdropClientError ? error : new MicdropClientError(
1437
1802
  "Connection" /* Connection */,
1438
1803
  error?.message
1439
1804
  )
@@ -1444,11 +1809,11 @@ var MicdropClient = class extends EventEmitter6 {
1444
1809
  }
1445
1810
  stopWS() {
1446
1811
  if (this.connectionTimer) {
1447
- window.clearTimeout(this.connectionTimer);
1812
+ clearTimeout(this.connectionTimer);
1448
1813
  this.connectionTimer = void 0;
1449
1814
  }
1450
1815
  if (!this.ws) return;
1451
- if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {
1816
+ if (this.ws.readyState === 1 || this.ws.readyState === 0) {
1452
1817
  this.ws.close();
1453
1818
  }
1454
1819
  this.ws = void 0;
@@ -1468,9 +1833,9 @@ var MicdropClient = class extends EventEmitter6 {
1468
1833
  this.vad?.pause?.();
1469
1834
  }
1470
1835
  if (this.reconnectTimer) {
1471
- window.clearTimeout(this.reconnectTimer);
1836
+ clearTimeout(this.reconnectTimer);
1472
1837
  }
1473
- this.reconnectTimer = window.setTimeout(async () => {
1838
+ this.reconnectTimer = setTimeout(async () => {
1474
1839
  try {
1475
1840
  await this.startWS();
1476
1841
  } catch {
@@ -1500,13 +1865,16 @@ var MicdropClient = class extends EventEmitter6 {
1500
1865
  };
1501
1866
 
1502
1867
  // src/Micdrop.ts
1503
- if (!window.micdropClient) {
1504
- window.micdropClient = new MicdropClient();
1868
+ var globalScope2 = globalThis;
1869
+ if (!globalScope2.micdropClient) {
1870
+ globalScope2.micdropClient = new MicdropClient();
1505
1871
  }
1506
- var Micdrop = window.micdropClient;
1872
+ var Micdrop = globalScope2.micdropClient;
1507
1873
  export {
1508
- LocalStorageKeys,
1509
- Mic2 as Mic,
1874
+ DEFAULT_TURN_MAX_WAIT,
1875
+ Mic,
1876
+ MicController,
1877
+ MicDriver,
1510
1878
  MicRecorder,
1511
1879
  Micdrop,
1512
1880
  MicdropClient,
@@ -1514,16 +1882,33 @@ export {
1514
1882
  MicdropClientError,
1515
1883
  MicdropClientErrorCode,
1516
1884
  MicdropServerCommands,
1885
+ MicdropStorageKeys,
1517
1886
  MultipleVAD,
1887
+ Pcm16AudioStream,
1888
+ SILERO_FRAME_SAMPLES,
1889
+ SILERO_SAMPLE_RATE,
1518
1890
  SileroVAD,
1519
- Speaker2 as Speaker,
1891
+ Speaker,
1892
+ SpeakerController,
1893
+ SpeakerDriver,
1520
1894
  VAD,
1521
1895
  VADStatus,
1896
+ VOLUME_SAMPLE_RATE,
1897
+ VolumeMeter,
1522
1898
  VolumeVAD,
1899
+ concatFloat32,
1523
1900
  equalVADConfig,
1901
+ floatToPcm16,
1524
1902
  getClientErrorFromWSCloseEvent,
1525
1903
  getClientErrorFromWSCloseEventCode,
1526
1904
  getVAD,
1527
- isRecoverableError
1905
+ getVolume,
1906
+ isRecoverableError,
1907
+ pcm16ToArrayBuffer,
1908
+ pcm16ToFloat,
1909
+ resample,
1910
+ setMicdropStorage,
1911
+ setSileroModelLoader,
1912
+ storage
1528
1913
  };
1529
1914
  //# sourceMappingURL=index.mjs.map