@micdrop/client 1.4.1 → 1.5.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.js CHANGED
@@ -36,27 +36,15 @@ __export(index_exports, {
36
36
  CallHandlerErrorCode: () => CallHandlerErrorCode,
37
37
  CallServerCommands: () => CallServerCommands,
38
38
  LocalStorageKeys: () => LocalStorageKeys,
39
- Mic: () => microphone_exports,
39
+ Mic: () => mic,
40
40
  MicRecorder: () => MicRecorder,
41
41
  SileroVAD: () => SileroVAD,
42
- Speaker: () => speaker_exports,
42
+ Speaker: () => speaker,
43
43
  VAD: () => VAD,
44
44
  VolumeVAD: () => VolumeVAD
45
45
  });
46
46
  module.exports = __toCommonJS(index_exports);
47
47
 
48
- // src/audio/MicRecorder.ts
49
- var import_eventemitter32 = __toESM(require("eventemitter3"));
50
-
51
- // src/audio/microphone.ts
52
- var microphone_exports = {};
53
- __export(microphone_exports, {
54
- defaultMicThreshold: () => defaultMicThreshold,
55
- micAnalyser: () => micAnalyser,
56
- startMicrophone: () => startMicrophone,
57
- stopMicrophone: () => stopMicrophone
58
- });
59
-
60
48
  // src/audio/utils/AudioAnalyser.ts
61
49
  var import_eventemitter3 = __toESM(require("eventemitter3"));
62
50
  var AudioAnalyser = class extends import_eventemitter3.default {
@@ -92,8 +80,42 @@ var AudioAnalyser = class extends import_eventemitter3.default {
92
80
  }
93
81
  };
94
82
 
83
+ // src/audio/utils/unlock.ts
84
+ function unlock(audioContext2) {
85
+ const events = ["touchstart", "touchend", "mousedown", "keydown"];
86
+ const unlock2 = async () => {
87
+ unlockSafari(audioContext2);
88
+ if (audioContext2.state === "suspended") {
89
+ await audioContext2.resume();
90
+ }
91
+ events.forEach((e) => document.body.removeEventListener(e, unlock2));
92
+ };
93
+ events.forEach((e) => document.body.addEventListener(e, unlock2, false));
94
+ }
95
+ function unlockSafari(audioContext2) {
96
+ const source = audioContext2.createBufferSource();
97
+ source.buffer = audioContext2.createBuffer(1, 1, 22050);
98
+ source.connect(audioContext2.destination);
99
+ const onEnded = () => {
100
+ source.disconnect();
101
+ source.buffer = null;
102
+ source.removeEventListener("ended", onEnded);
103
+ };
104
+ source.addEventListener("ended", onEnded);
105
+ source.start();
106
+ }
107
+
95
108
  // src/audio/utils/audioContext.ts
96
- var audioContext = new (window.AudioContext || window.webkitAudioContext)();
109
+ var audioContext;
110
+ function createAudioContext() {
111
+ return new (window.AudioContext || window.webkitAudioContext)();
112
+ }
113
+ if (window.micdropAudioContext) {
114
+ audioContext = window.micdropAudioContext;
115
+ } else {
116
+ window.micdropAudioContext = audioContext = createAudioContext();
117
+ unlock(audioContext);
118
+ }
97
119
 
98
120
  // src/audio/utils/localStorage.ts
99
121
  var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
@@ -108,71 +130,86 @@ function stopStream(stream) {
108
130
  stream?.getTracks().forEach((track) => track.stop());
109
131
  }
110
132
 
111
- // src/audio/microphone.ts
112
- var currentDeviceId;
113
- var audioStream;
114
- var sourceNode;
115
- var micAnalyser = new AudioAnalyser(audioContext);
133
+ // src/audio/mic.ts
134
+ var mic;
116
135
  var defaultMicThreshold = -50;
117
- function getMicConstraints(deviceId) {
118
- return {
119
- audio: {
120
- deviceId: { ideal: deviceId },
121
- sampleRate: 16e3,
122
- // not working, it will follow device settings, usually 44.1kHz
123
- sampleSize: 16,
124
- channelCount: 1,
125
- echoCancellation: true,
126
- noiseSuppression: true
127
- }
128
- };
129
- }
130
- async function startMicrophone(deviceId) {
131
- if (!audioContext || !micAnalyser) {
132
- throw new Error("AudioContext not initialized");
133
- }
134
- if (!deviceId) {
135
- deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
136
+ var Mic = class {
137
+ constructor() {
138
+ this.defaultThreshold = defaultMicThreshold;
139
+ this.analyser = new AudioAnalyser(audioContext);
140
+ }
141
+ getMicConstraints(deviceId) {
142
+ return {
143
+ audio: {
144
+ deviceId: { ideal: deviceId },
145
+ sampleRate: 16e3,
146
+ // not working, it will follow device settings, usually 44.1kHz
147
+ sampleSize: 16,
148
+ channelCount: 1,
149
+ echoCancellation: true,
150
+ noiseSuppression: true
151
+ }
152
+ };
136
153
  }
137
- if (audioStream) {
138
- if (currentDeviceId === deviceId) {
139
- return audioStream;
154
+ /**
155
+ * Starts the microphone
156
+ * @param deviceId - The deviceId to use
157
+ * @returns The microphone stream
158
+ */
159
+ async start(deviceId) {
160
+ if (!audioContext || !this.analyser) {
161
+ throw new Error("AudioContext not initialized");
140
162
  }
141
- stopStream(audioStream);
142
- }
143
- audioStream = await navigator.mediaDevices.getUserMedia(
144
- getMicConstraints(deviceId)
145
- );
146
- const track = audioStream.getTracks()[0];
147
- const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
148
- if (newDeviceId) {
149
- localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
150
- currentDeviceId = newDeviceId;
151
- } else {
152
- localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
153
- currentDeviceId = void 0;
154
- }
155
- if (sourceNode) sourceNode.disconnect();
156
- sourceNode = audioContext.createMediaStreamSource(audioStream);
157
- sourceNode.connect(micAnalyser.node);
158
- return audioStream;
159
- }
160
- function stopMicrophone() {
161
- if (sourceNode) {
162
- sourceNode.disconnect();
163
- sourceNode = void 0;
163
+ if (!deviceId) {
164
+ deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
165
+ }
166
+ if (this.audioStream) {
167
+ if (this.currentDeviceId === deviceId) {
168
+ return this.audioStream;
169
+ }
170
+ stopStream(this.audioStream);
171
+ }
172
+ this.audioStream = await navigator.mediaDevices.getUserMedia(
173
+ this.getMicConstraints(deviceId)
174
+ );
175
+ const track = this.audioStream.getTracks()[0];
176
+ const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
177
+ if (newDeviceId) {
178
+ localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
179
+ this.currentDeviceId = newDeviceId;
180
+ } else {
181
+ localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
182
+ this.currentDeviceId = void 0;
183
+ }
184
+ if (this.sourceNode) this.sourceNode.disconnect();
185
+ this.sourceNode = audioContext.createMediaStreamSource(this.audioStream);
186
+ this.sourceNode.connect(this.analyser.node);
187
+ return this.audioStream;
164
188
  }
165
- if (audioStream) {
166
- stopStream(audioStream);
167
- audioStream = void 0;
189
+ /**
190
+ * Stops the microphone
191
+ */
192
+ stop() {
193
+ if (this.sourceNode) {
194
+ this.sourceNode.disconnect();
195
+ this.sourceNode = void 0;
196
+ }
197
+ if (this.audioStream) {
198
+ stopStream(this.audioStream);
199
+ this.audioStream = void 0;
200
+ }
168
201
  }
202
+ };
203
+ if (!window.micdropMic) {
204
+ window.micdropMic = new Mic();
169
205
  }
206
+ mic = window.micdropMic;
170
207
 
171
- // src/audio/utils/createDelayedStream.ts
208
+ // src/audio/MicRecorder.ts
209
+ var import_eventemitter33 = __toESM(require("eventemitter3"));
210
+
211
+ // src/audio/utils/delayedStream.ts
172
212
  function createDelayedStream(stream, delay) {
173
- if (!audioContext) {
174
- throw new Error("AudioContext not initialized");
175
- }
176
213
  const audioSource = audioContext.createMediaStreamSource(stream);
177
214
  const delayNode = audioContext.createDelay(delay);
178
215
  delayNode.delayTime.value = delay;
@@ -182,6 +219,166 @@ function createDelayedStream(stream, delay) {
182
219
  return audioDestination.stream;
183
220
  }
184
221
 
222
+ // src/audio/vad/SileroVAD.ts
223
+ var import_src = require("@ricky0123/vad-web/src");
224
+
225
+ // src/audio/vad/VAD.ts
226
+ var import_eventemitter32 = __toESM(require("eventemitter3"));
227
+ var VAD = class extends import_eventemitter32.default {
228
+ /**
229
+ * Constructor for the VAD class
230
+ * @param threshold - The threshold to use for the VAD
231
+ */
232
+ constructor(threshold = 0) {
233
+ super();
234
+ // Max delay to detect speech, can be used to record delayed stream
235
+ this.delay = 100;
236
+ this.threshold = threshold;
237
+ }
238
+ };
239
+
240
+ // src/audio/vad/SileroVAD.ts
241
+ var SileroVAD = class extends VAD {
242
+ get isStarted() {
243
+ return !!this.vad;
244
+ }
245
+ async start(stream, threshold) {
246
+ if (this.vad) return;
247
+ if (threshold !== void 0) {
248
+ this.threshold = threshold;
249
+ }
250
+ const speechThreshold = Math.min(
251
+ Math.max((this.threshold + 100) / 100, 0),
252
+ 1
253
+ );
254
+ this.vad = await import_src.MicVAD.new({
255
+ stream,
256
+ model: "v5",
257
+ onSpeechStart: () => this.emit("StartSpeaking"),
258
+ onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
259
+ onVADMisfire: () => this.emit("CancelSpeaking"),
260
+ onSpeechEnd: () => this.emit("StopSpeaking"),
261
+ positiveSpeechThreshold: speechThreshold,
262
+ negativeSpeechThreshold: speechThreshold * 0.7,
263
+ minSpeechFrames: 3,
264
+ redemptionFrames: 9
265
+ });
266
+ this.vad.start();
267
+ }
268
+ async stop() {
269
+ if (!this.vad) return;
270
+ this.vad.destroy();
271
+ this.vad = void 0;
272
+ }
273
+ async setThreshold(threshold) {
274
+ if (threshold === this.threshold) return;
275
+ if (this.vad) {
276
+ const stream = this.vad["stream"];
277
+ await this.stop();
278
+ await this.start(stream, threshold);
279
+ }
280
+ }
281
+ };
282
+
283
+ // src/audio/vad/VolumeVAD.ts
284
+ var VolumeVAD = class extends VAD {
285
+ constructor() {
286
+ super();
287
+ this.running = false;
288
+ this.speaking = false;
289
+ this.attemptSpeaking = false;
290
+ this.history = 10;
291
+ this.speakingHistory = [];
292
+ for (let i = 0; i < this.history; i++) {
293
+ this.speakingHistory.push(0);
294
+ }
295
+ }
296
+ getMaxVolume() {
297
+ if (!this.analyser || !this.fftBins) return -Infinity;
298
+ let maxVolume = -Infinity;
299
+ this.analyser.getFloatFrequencyData(this.fftBins);
300
+ for (let i = 4; i < this.fftBins.length; i++) {
301
+ if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
302
+ maxVolume = this.fftBins[i];
303
+ }
304
+ }
305
+ return maxVolume;
306
+ }
307
+ get isStarted() {
308
+ return this.running;
309
+ }
310
+ async start(stream, threshold) {
311
+ if (this.running) return;
312
+ if (threshold !== void 0) {
313
+ this.threshold = threshold;
314
+ }
315
+ this.analyser = audioContext.createAnalyser();
316
+ this.sourceNode = audioContext.createMediaStreamSource(stream);
317
+ this.analyser.fftSize = 512;
318
+ this.analyser.smoothingTimeConstant = 0.1;
319
+ this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
320
+ this.sourceNode.connect(this.analyser);
321
+ this.running = true;
322
+ this.startLoop();
323
+ }
324
+ startLoop() {
325
+ if (!this.running) return;
326
+ const currentVolume = this.getMaxVolume();
327
+ if (currentVolume > this.threshold) {
328
+ if (!this.speaking) {
329
+ let recentHistory = 0;
330
+ for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
331
+ recentHistory += this.speakingHistory[i];
332
+ }
333
+ if (recentHistory >= 2) {
334
+ this.speaking = true;
335
+ this.attemptSpeaking = false;
336
+ this.emit("ConfirmSpeaking");
337
+ } else if (recentHistory === 1) {
338
+ this.attemptSpeaking = true;
339
+ this.emit("StartSpeaking");
340
+ }
341
+ }
342
+ } else if (this.speaking) {
343
+ let totalHistory = 0;
344
+ for (let i = 0; i < this.speakingHistory.length; i++) {
345
+ totalHistory += this.speakingHistory[i];
346
+ }
347
+ if (totalHistory === 0) {
348
+ this.speaking = false;
349
+ this.emit("StopSpeaking");
350
+ }
351
+ } else if (this.attemptSpeaking) {
352
+ let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
353
+ if (lastHistory === 0) {
354
+ this.attemptSpeaking = false;
355
+ this.emit("CancelSpeaking");
356
+ }
357
+ }
358
+ this.speakingHistory.shift();
359
+ this.speakingHistory.push(Number(currentVolume > this.threshold));
360
+ setTimeout(() => this.startLoop(), this.delay);
361
+ }
362
+ async stop() {
363
+ if (!this.running) return;
364
+ this.running = false;
365
+ this.speaking = false;
366
+ if (this.sourceNode) {
367
+ this.sourceNode.disconnect();
368
+ this.sourceNode = void 0;
369
+ }
370
+ if (this.analyser) {
371
+ this.analyser.disconnect();
372
+ this.analyser = void 0;
373
+ }
374
+ this.fftBins = void 0;
375
+ }
376
+ async setThreshold(threshold) {
377
+ if (threshold === this.threshold) return;
378
+ this.threshold = threshold;
379
+ }
380
+ };
381
+
185
382
  // src/audio/MicRecorder.ts
186
383
  var timeSlice = 100;
187
384
  var defaultMicRecorderState = {
@@ -191,9 +388,10 @@ var defaultMicRecorderState = {
191
388
  isSpeaking: false,
192
389
  threshold: defaultMicThreshold
193
390
  };
194
- var MicRecorder = class extends import_eventemitter32.default {
195
- constructor(vad) {
391
+ var MicRecorder = class extends import_eventemitter33.default {
392
+ constructor(vadConfig) {
196
393
  super();
394
+ this.vadConfig = vadConfig;
197
395
  this.audioInfo = this.getAudioInfo();
198
396
  this.speakingConfirmed = false;
199
397
  this.queuedChunks = [];
@@ -234,7 +432,6 @@ var MicRecorder = class extends import_eventemitter32.default {
234
432
  }).bind(this);
235
433
  const threshold = parseFloat(localStorage.getItem("micdrop.micThreshold" /* MicThreshold */) || "0") || defaultMicThreshold;
236
434
  this.state = { ...defaultMicRecorderState, threshold };
237
- this.vad = vad;
238
435
  }
239
436
  async start(stream) {
240
437
  if (this.state.isStarted) {
@@ -246,6 +443,9 @@ var MicRecorder = class extends import_eventemitter32.default {
246
443
  isMuted: false,
247
444
  isSpeaking: false
248
445
  });
446
+ if (!this.vad) {
447
+ this.vad = this.initVAD(this.vadConfig);
448
+ }
249
449
  const delayedStream = createDelayedStream(
250
450
  stream,
251
451
  this.vad.delay / 1e3 + 0.05
@@ -300,9 +500,18 @@ var MicRecorder = class extends import_eventemitter32.default {
300
500
  setThreshold(threshold) {
301
501
  this.changeState({ threshold });
302
502
  localStorage.setItem("micdrop.micThreshold" /* MicThreshold */, threshold.toString());
303
- this.vad.setThreshold(threshold);
503
+ this.vad?.setThreshold(threshold);
504
+ }
505
+ initVAD(vad) {
506
+ if (!vad) return new VolumeVAD();
507
+ if (vad === "volume") return new VolumeVAD();
508
+ if (vad === "silero") return new SileroVAD();
509
+ return vad;
304
510
  }
305
511
  async startSpeakingDetection(stream) {
512
+ if (!this.vad) {
513
+ throw new Error("VAD not initialized");
514
+ }
306
515
  if (this.vad.isStarted) {
307
516
  this.vad.stop();
308
517
  }
@@ -313,6 +522,9 @@ var MicRecorder = class extends import_eventemitter32.default {
313
522
  await this.vad.start(stream, this.state.threshold);
314
523
  }
315
524
  async stopSpeakingDetection() {
525
+ if (!this.vad) {
526
+ throw new Error("VAD not initialized");
527
+ }
316
528
  this.vad.off("StartSpeaking", this.onStartSpeaking);
317
529
  this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
318
530
  this.vad.off("CancelSpeaking", this.onCancelSpeaking);
@@ -350,21 +562,6 @@ var MicRecorder = class extends import_eventemitter32.default {
350
562
  }
351
563
  };
352
564
 
353
- // src/audio/speaker.ts
354
- var speaker_exports = {};
355
- __export(speaker_exports, {
356
- canChangeSpeakerDevice: () => canChangeSpeakerDevice,
357
- changeSpeakerDevice: () => changeSpeakerDevice,
358
- disableStreaming: () => disableStreaming,
359
- enableStreaming: () => enableStreaming,
360
- isStreamingEnabled: () => isStreamingEnabled,
361
- pauseAudio: () => pauseAudio,
362
- playAudio: () => playAudio,
363
- resumeAudio: () => resumeAudio,
364
- speakerAnalyser: () => speakerAnalyser,
365
- stopAudio: () => stopAudio
366
- });
367
-
368
565
  // src/audio/player/SpeakerPlayer.ts
369
566
  var SpeakerPlayer = class {
370
567
  constructor(outputNode) {
@@ -555,7 +752,7 @@ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
555
752
  this.audioElement = void 0;
556
753
  }
557
754
  if (this.mediaStream) {
558
- this.mediaStream.getTracks().forEach((track) => track.stop());
755
+ stopStream(this.mediaStream);
559
756
  this.mediaStream = void 0;
560
757
  }
561
758
  this.mediaSource = void 0;
@@ -620,280 +817,94 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
620
817
  }
621
818
  };
622
819
 
623
- // src/audio/utils/unlock.ts
624
- function unlock(audioContext2) {
625
- const events = ["touchstart", "touchend", "mousedown", "keydown"];
626
- events.forEach((e) => document.body.addEventListener(e, unlock2, false));
627
- async function unlock2() {
628
- unlockSafari(audioContext2);
629
- if (audioContext2.state === "suspended") {
630
- await audioContext2.resume();
820
+ // src/audio/speaker.ts
821
+ var speaker;
822
+ var Speaker = class {
823
+ constructor() {
824
+ this.streamingEnabled = false;
825
+ this.analyser = new AudioAnalyser(audioContext);
826
+ this.audioElement = new Audio();
827
+ this.audioElement.autoplay = true;
828
+ const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
829
+ if (speakerId) {
830
+ this.setSinkId(speakerId).catch((error) => {
831
+ console.error("Failed to set sink ID:", error);
832
+ });
631
833
  }
632
- clean();
834
+ this.setupSpeakerPlayer();
835
+ const destinationNode = audioContext.createMediaStreamDestination();
836
+ this.analyser.node.connect(destinationNode);
837
+ this.audioElement.srcObject = destinationNode.stream;
633
838
  }
634
- function clean() {
635
- events.forEach((e) => document.body.removeEventListener(e, unlock2));
636
- }
637
- }
638
- function unlockSafari(audioContext2) {
639
- const source = audioContext2.createBufferSource();
640
- source.buffer = audioContext2.createBuffer(1, 1, 22050);
641
- source.connect(audioContext2.destination);
642
- const onEnded = () => {
643
- source.disconnect();
644
- source.buffer = null;
645
- source.removeEventListener("ended", onEnded);
646
- };
647
- source.addEventListener("ended", onEnded);
648
- source.start();
649
- }
650
-
651
- // src/audio/speaker.ts
652
- var audioElement;
653
- var player;
654
- var streamingEnabled = false;
655
- var speakerAnalyser = new AudioAnalyser(audioContext);
656
- async function init() {
657
- unlock(audioContext);
658
- audioElement = new Audio();
659
- audioElement.autoplay = true;
660
- const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
661
- if (speakerId) {
839
+ async setupSpeakerPlayer() {
662
840
  try {
663
- await setSinkId(speakerId);
841
+ this.player?.destroy();
842
+ this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
843
+ await this.player?.setup();
664
844
  } catch (error) {
665
- console.error("Failed to set sink ID:", error);
845
+ console.error("Error setting up speaker player", error);
666
846
  }
667
847
  }
668
- await setupSpeakerPlayer();
669
- const destinationNode = audioContext.createMediaStreamDestination();
670
- speakerAnalyser.node.connect(destinationNode);
671
- audioElement.srcObject = destinationNode.stream;
672
- }
673
- init();
674
- async function setupSpeakerPlayer() {
675
- try {
676
- player?.destroy();
677
- player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
678
- await player?.setup();
679
- } catch (error) {
680
- console.error("Error setting up speaker player", error);
681
- }
682
- }
683
- async function isStreamingEnabled() {
684
- return streamingEnabled;
685
- }
686
- async function enableStreaming() {
687
- if (streamingEnabled) return;
688
- streamingEnabled = true;
689
- await setupSpeakerPlayer();
690
- }
691
- async function disableStreaming() {
692
- if (!streamingEnabled) return;
693
- streamingEnabled = false;
694
- await setupSpeakerPlayer();
695
- }
696
- function canChangeSpeakerDevice() {
697
- return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
698
- }
699
- async function setSinkId(speakerId) {
700
- if (speakerId && canChangeSpeakerDevice() && audioElement) {
701
- return audioElement.setSinkId(speakerId);
702
- }
703
- }
704
- async function changeSpeakerDevice(speakerId) {
705
- if (!canChangeSpeakerDevice()) return;
706
- try {
707
- await setSinkId(speakerId);
708
- localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
709
- } catch (error) {
710
- console.error(`Error setting Audio Output to ${speakerId}`, error);
711
- }
712
- }
713
- function pauseAudio() {
714
- if (audioElement) {
715
- audioElement.pause();
716
- }
717
- }
718
- function resumeAudio() {
719
- if (audioElement) {
720
- audioElement.play();
721
- }
722
- }
723
- async function playAudio(blob) {
724
- if (!audioElement || !player) return;
725
- try {
726
- player.addBlob(blob);
727
- if (audioElement.paused) {
728
- audioElement.play();
729
- }
730
- } catch (error) {
731
- console.error("Error playing audio blob", error, blob);
732
- }
733
- }
734
- function stopAudio() {
735
- audioElement?.pause();
736
- player?.stop();
737
- }
738
-
739
- // src/audio/vad/SileroVAD.ts
740
- var import_src = require("@ricky0123/vad-web/src");
741
-
742
- // src/audio/vad/VAD.ts
743
- var import_eventemitter33 = __toESM(require("eventemitter3"));
744
- var VAD = class extends import_eventemitter33.default {
745
- /**
746
- * Constructor for the VAD class
747
- * @param threshold - The threshold to use for the VAD
748
- */
749
- constructor(threshold = 0) {
750
- super();
751
- // Max delay to detect speech, can be used to record delayed stream
752
- this.delay = 100;
753
- this.threshold = threshold;
848
+ get isStreamingEnabled() {
849
+ return this.streamingEnabled;
754
850
  }
755
- };
756
-
757
- // src/audio/vad/SileroVAD.ts
758
- var SileroVAD = class extends VAD {
759
- get isStarted() {
760
- return !!this.vad;
851
+ async enableStreaming() {
852
+ if (this.streamingEnabled) return;
853
+ this.streamingEnabled = true;
854
+ await this.setupSpeakerPlayer();
761
855
  }
762
- async start(stream, threshold) {
763
- if (this.vad) return;
764
- if (threshold !== void 0) {
765
- this.threshold = threshold;
766
- }
767
- const speechThreshold = Math.min(
768
- Math.max((this.threshold + 100) / 100, 0),
769
- 1
770
- );
771
- this.vad = await import_src.MicVAD.new({
772
- stream,
773
- onSpeechStart: () => this.emit("StartSpeaking"),
774
- onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
775
- onVADMisfire: () => this.emit("CancelSpeaking"),
776
- onSpeechEnd: () => this.emit("StopSpeaking"),
777
- positiveSpeechThreshold: speechThreshold,
778
- negativeSpeechThreshold: speechThreshold * 0.7,
779
- // Adjust negative threshold proportionally
780
- redemptionFrames: 5
781
- });
782
- this.vad.start();
856
+ async disableStreaming() {
857
+ if (!this.streamingEnabled) return;
858
+ this.streamingEnabled = false;
859
+ await this.setupSpeakerPlayer();
783
860
  }
784
- async stop() {
785
- if (!this.vad) return;
786
- this.vad.pause();
787
- this.vad = void 0;
861
+ canChangeDevice() {
862
+ return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
788
863
  }
789
- async setThreshold(threshold) {
790
- if (threshold === this.threshold) return;
791
- if (this.vad) {
792
- const stream = this.vad["stream"];
793
- await this.stop();
794
- await this.start(stream, threshold);
864
+ async setSinkId(speakerId) {
865
+ if (speakerId && this.canChangeDevice() && this.audioElement) {
866
+ return this.audioElement.setSinkId(speakerId);
795
867
  }
796
868
  }
797
- };
798
-
799
- // src/audio/vad/VolumeVAD.ts
800
- var VolumeVAD = class extends VAD {
801
- constructor() {
802
- super();
803
- this.running = false;
804
- this.speaking = false;
805
- this.attemptSpeaking = false;
806
- this.history = 10;
807
- this.speakingHistory = [];
808
- for (let i = 0; i < this.history; i++) {
809
- this.speakingHistory.push(0);
869
+ async changeDevice(speakerId) {
870
+ if (!this.canChangeDevice()) return;
871
+ try {
872
+ await this.setSinkId(speakerId);
873
+ localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
874
+ } catch (error) {
875
+ console.error(`Error setting Audio Output to ${speakerId}`, error);
810
876
  }
811
877
  }
812
- getMaxVolume() {
813
- if (!this.analyser || !this.fftBins) return -Infinity;
814
- let maxVolume = -Infinity;
815
- this.analyser.getFloatFrequencyData(this.fftBins);
816
- for (let i = 4; i < this.fftBins.length; i++) {
817
- if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
818
- maxVolume = this.fftBins[i];
819
- }
878
+ pauseAudio() {
879
+ if (this.audioElement) {
880
+ this.audioElement.pause();
820
881
  }
821
- return maxVolume;
822
- }
823
- get isStarted() {
824
- return this.running;
825
882
  }
826
- async start(stream, threshold) {
827
- if (this.running) return;
828
- if (threshold !== void 0) {
829
- this.threshold = threshold;
883
+ resumeAudio() {
884
+ if (this.audioElement) {
885
+ this.audioElement.play();
830
886
  }
831
- this.analyser = audioContext.createAnalyser();
832
- this.sourceNode = audioContext.createMediaStreamSource(stream);
833
- this.analyser.fftSize = 512;
834
- this.analyser.smoothingTimeConstant = 0.1;
835
- this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
836
- this.sourceNode.connect(this.analyser);
837
- this.running = true;
838
- this.startLoop();
839
887
  }
840
- startLoop() {
841
- if (!this.running) return;
842
- const currentVolume = this.getMaxVolume();
843
- if (currentVolume > this.threshold) {
844
- if (!this.speaking) {
845
- let recentHistory = 0;
846
- for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
847
- recentHistory += this.speakingHistory[i];
848
- }
849
- if (recentHistory >= 2) {
850
- this.speaking = true;
851
- this.attemptSpeaking = false;
852
- this.emit("ConfirmSpeaking");
853
- } else if (recentHistory === 1) {
854
- this.attemptSpeaking = true;
855
- this.emit("StartSpeaking");
856
- }
857
- }
858
- } else if (this.speaking) {
859
- let totalHistory = 0;
860
- for (let i = 0; i < this.speakingHistory.length; i++) {
861
- totalHistory += this.speakingHistory[i];
862
- }
863
- if (totalHistory === 0) {
864
- this.speaking = false;
865
- this.emit("StopSpeaking");
866
- }
867
- } else if (this.attemptSpeaking) {
868
- let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
869
- if (lastHistory === 0) {
870
- this.attemptSpeaking = false;
871
- this.emit("CancelSpeaking");
888
+ async playAudio(blob) {
889
+ if (!this.audioElement || !this.player) return;
890
+ try {
891
+ this.player.addBlob(blob);
892
+ if (this.audioElement.paused) {
893
+ this.audioElement.play();
872
894
  }
895
+ } catch (error) {
896
+ console.error("Error playing audio blob", error, blob);
873
897
  }
874
- this.speakingHistory.shift();
875
- this.speakingHistory.push(Number(currentVolume > this.threshold));
876
- setTimeout(() => this.startLoop(), this.delay);
877
- }
878
- async stop() {
879
- if (!this.running) return;
880
- this.running = false;
881
- this.speaking = false;
882
- if (this.sourceNode) {
883
- this.sourceNode.disconnect();
884
- this.sourceNode = void 0;
885
- }
886
- if (this.analyser) {
887
- this.analyser.disconnect();
888
- this.analyser = void 0;
889
- }
890
- this.fftBins = void 0;
891
898
  }
892
- async setThreshold(threshold) {
893
- if (threshold === this.threshold) return;
894
- this.threshold = threshold;
899
+ stopAudio() {
900
+ this.audioElement?.pause();
901
+ this.player?.stop();
895
902
  }
896
903
  };
904
+ if (!window.micdropSpeaker) {
905
+ window.micdropSpeaker = new Speaker();
906
+ }
907
+ speaker = window.micdropSpeaker;
897
908
 
898
909
  // src/call/CallHandler.ts
899
910
  var import_eventemitter34 = __toESM(require("eventemitter3"));
@@ -916,49 +927,23 @@ var CallHandlerError = class extends Error {
916
927
  var CallHandler = class _CallHandler extends import_eventemitter34.default {
917
928
  constructor(options) {
918
929
  super();
930
+ this.options = options;
919
931
  this.conversation = [];
920
932
  this.debug = false;
921
933
  this.startTime = 0;
922
934
  this._isProcessing = false;
923
- this.micRecorder = new MicRecorder(this.getVAD(options?.vad));
924
- this.micRecorder.on("StateChange", () => {
925
- this.notifyStateChange();
926
- });
927
- this.micRecorder.on("Chunk", (blob) => {
928
- this.log(`[Mic] Received chunk`, blob);
929
- this.ws?.send(blob);
930
- });
931
- this.micRecorder.on("StartSpeaking", () => {
932
- this.log("[Mic] Start speaking");
933
- this.ws?.send("startSpeaking" /* StartSpeaking */);
934
- this._isProcessing = false;
935
- this.notifyStateChange();
936
- stopAudio();
937
- });
938
- this.micRecorder.on("StopSpeaking", () => {
939
- this.log("[Mic] Stop speaking");
940
- this.ws?.send("stopSpeaking" /* StopSpeaking */);
941
- this._isProcessing = true;
942
- this.notifyStateChange();
943
- });
944
935
  }
945
936
  static getInstance(options) {
946
- if (!_CallHandler.instance) {
947
- _CallHandler.instance = new _CallHandler(options);
937
+ if (!window.micdropCallHandler) {
938
+ window.micdropCallHandler = new _CallHandler(options);
948
939
  }
949
- return _CallHandler.instance;
950
- }
951
- getVAD(vad) {
952
- if (!vad) return new VolumeVAD();
953
- if (vad === "volume") return new VolumeVAD();
954
- if (vad === "silero") return new SileroVAD();
955
- return vad;
940
+ return window.micdropCallHandler;
956
941
  }
957
942
  get isStarted() {
958
- return this.isWSStarted && this.micRecorder.state.isStarted;
943
+ return this.isWSStarted && this.micRecorder?.state.isStarted || false;
959
944
  }
960
945
  get isStarting() {
961
- return this.isWSStarting || this.micRecorder.state.isStarting;
946
+ return this.isWSStarting || this.micRecorder?.state.isStarting || false;
962
947
  }
963
948
  get isWSStarted() {
964
949
  return this.ws?.readyState === WebSocket.OPEN;
@@ -969,6 +954,15 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
969
954
  get isMicStarted() {
970
955
  return !!this.micStream;
971
956
  }
957
+ get isMicMuted() {
958
+ return this.micRecorder?.state.isMuted ?? false;
959
+ }
960
+ get isMicSpeaking() {
961
+ return this.micRecorder?.state.isSpeaking ?? false;
962
+ }
963
+ get micThreshold() {
964
+ return this.micRecorder?.state.threshold ?? mic.defaultThreshold;
965
+ }
972
966
  get isProcessing() {
973
967
  return this._isProcessing;
974
968
  }
@@ -986,32 +980,57 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
986
980
  console.error("[CallHandler] stop WS", error);
987
981
  }
988
982
  try {
989
- this.micRecorder.stop();
983
+ this.micRecorder?.stop();
990
984
  this.stopMic();
991
985
  } catch (error) {
992
986
  console.error("[CallHandler] stop mic", error);
993
987
  }
994
988
  }
995
989
  pause() {
996
- this.micRecorder.mute();
990
+ this.micRecorder?.mute();
991
+ this._isProcessing = false;
997
992
  this.notifyStateChange();
998
- pauseAudio();
993
+ speaker.pauseAudio();
994
+ this.ws?.send("mute" /* Mute */);
999
995
  }
1000
996
  resume() {
1001
997
  if (this.lastAudioBlob) {
1002
- playAudio(this.lastAudioBlob);
998
+ speaker.playAudio(this.lastAudioBlob);
1003
999
  }
1004
- resumeAudio();
1005
- this.micRecorder.unmute();
1000
+ speaker.resumeAudio();
1001
+ this.micRecorder?.unmute();
1006
1002
  this.notifyStateChange();
1007
1003
  }
1008
1004
  async startMic(deviceId, record = true) {
1009
1005
  try {
1006
+ if (!this.micRecorder) {
1007
+ this.micRecorder = new MicRecorder(this.options?.vad);
1008
+ this.micRecorder.on("StateChange", () => {
1009
+ this.notifyStateChange();
1010
+ });
1011
+ this.micRecorder.on("Chunk", (blob) => {
1012
+ this.log(`[Mic] Received chunk`, blob);
1013
+ this.ws?.send(blob);
1014
+ });
1015
+ this.micRecorder.on("StartSpeaking", () => {
1016
+ this.log("[Mic] Start speaking");
1017
+ this.ws?.send("startSpeaking" /* StartSpeaking */);
1018
+ this._isProcessing = false;
1019
+ this.notifyStateChange();
1020
+ speaker.stopAudio();
1021
+ });
1022
+ this.micRecorder.on("StopSpeaking", () => {
1023
+ this.log("[Mic] Stop speaking");
1024
+ this.ws?.send("stopSpeaking" /* StopSpeaking */);
1025
+ this._isProcessing = true;
1026
+ this.notifyStateChange();
1027
+ });
1028
+ }
1010
1029
  const isRecorderStarted = this.micRecorder.state.isStarted;
1011
1030
  if (isRecorderStarted) {
1012
- this.micRecorder.stop();
1031
+ this.micRecorder?.stop();
1013
1032
  }
1014
- this.micStream = await startMicrophone(deviceId);
1033
+ this.micStream = await mic.start(deviceId);
1015
1034
  this.notifyStateChange();
1016
1035
  if (isRecorderStarted || record) {
1017
1036
  await this.micRecorder.start(this.micStream);
@@ -1023,7 +1042,9 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
1023
1042
  }
1024
1043
  }
1025
1044
  stopMic() {
1026
- stopMicrophone();
1045
+ this.micRecorder?.stop();
1046
+ this.micRecorder = void 0;
1047
+ mic.stop();
1027
1048
  this.micStream = void 0;
1028
1049
  this.notifyStateChange();
1029
1050
  }
@@ -1050,7 +1071,7 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
1050
1071
  this.ws.onmessage = (event) => {
1051
1072
  this.log("[WS]", event.data);
1052
1073
  if (event.data instanceof Blob) {
1053
- playAudio(event.data);
1074
+ speaker.playAudio(event.data);
1054
1075
  this.lastAudioBlob = event.data;
1055
1076
  this._isProcessing = false;
1056
1077
  this.notifyStateChange();
@@ -1072,7 +1093,7 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
1072
1093
  this.notifyStateChange();
1073
1094
  }
1074
1095
  } else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
1075
- enableStreaming();
1096
+ speaker.enableStreaming();
1076
1097
  }
1077
1098
  };
1078
1099
  this.ws.onclose = (event) => {