@micdrop/client 1.4.0 → 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.mjs CHANGED
@@ -1,21 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
6
-
7
- // src/audio/MicRecorder.ts
8
- import EventEmitter2 from "eventemitter3";
9
-
10
- // src/audio/microphone.ts
11
- var microphone_exports = {};
12
- __export(microphone_exports, {
13
- defaultMicThreshold: () => defaultMicThreshold,
14
- micAnalyser: () => micAnalyser,
15
- startMicrophone: () => startMicrophone,
16
- stopMicrophone: () => stopMicrophone
17
- });
18
-
19
1
  // src/audio/utils/AudioAnalyser.ts
20
2
  import EventEmitter from "eventemitter3";
21
3
  var AudioAnalyser = class extends EventEmitter {
@@ -51,8 +33,42 @@ var AudioAnalyser = class extends EventEmitter {
51
33
  }
52
34
  };
53
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));
47
+ }
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();
59
+ }
60
+
54
61
  // src/audio/utils/audioContext.ts
55
- var audioContext = new (window.AudioContext || window.webkitAudioContext)();
62
+ var audioContext;
63
+ function createAudioContext() {
64
+ return new (window.AudioContext || window.webkitAudioContext)();
65
+ }
66
+ if (window.micdropAudioContext) {
67
+ audioContext = window.micdropAudioContext;
68
+ } else {
69
+ window.micdropAudioContext = audioContext = createAudioContext();
70
+ unlock(audioContext);
71
+ }
56
72
 
57
73
  // src/audio/utils/localStorage.ts
58
74
  var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
@@ -67,71 +83,86 @@ function stopStream(stream) {
67
83
  stream?.getTracks().forEach((track) => track.stop());
68
84
  }
69
85
 
70
- // src/audio/microphone.ts
71
- var currentDeviceId;
72
- var audioStream;
73
- var sourceNode;
74
- var micAnalyser = new AudioAnalyser(audioContext);
86
+ // src/audio/mic.ts
87
+ var mic;
75
88
  var defaultMicThreshold = -50;
76
- function getMicConstraints(deviceId) {
77
- return {
78
- audio: {
79
- deviceId: { ideal: deviceId },
80
- sampleRate: 16e3,
81
- // not working, it will follow device settings, usually 44.1kHz
82
- sampleSize: 16,
83
- channelCount: 1,
84
- echoCancellation: true,
85
- noiseSuppression: true
86
- }
87
- };
88
- }
89
- async function startMicrophone(deviceId) {
90
- if (!audioContext || !micAnalyser) {
91
- throw new Error("AudioContext not initialized");
92
- }
93
- if (!deviceId) {
94
- deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
89
+ var Mic = class {
90
+ constructor() {
91
+ this.defaultThreshold = defaultMicThreshold;
92
+ this.analyser = new AudioAnalyser(audioContext);
93
+ }
94
+ getMicConstraints(deviceId) {
95
+ return {
96
+ audio: {
97
+ deviceId: { ideal: deviceId },
98
+ sampleRate: 16e3,
99
+ // not working, it will follow device settings, usually 44.1kHz
100
+ sampleSize: 16,
101
+ channelCount: 1,
102
+ echoCancellation: true,
103
+ noiseSuppression: true
104
+ }
105
+ };
95
106
  }
96
- if (audioStream) {
97
- if (currentDeviceId === deviceId) {
98
- return audioStream;
107
+ /**
108
+ * Starts the microphone
109
+ * @param deviceId - The deviceId to use
110
+ * @returns The microphone stream
111
+ */
112
+ async start(deviceId) {
113
+ if (!audioContext || !this.analyser) {
114
+ throw new Error("AudioContext not initialized");
99
115
  }
100
- stopStream(audioStream);
101
- }
102
- audioStream = await navigator.mediaDevices.getUserMedia(
103
- getMicConstraints(deviceId)
104
- );
105
- const track = audioStream.getTracks()[0];
106
- const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
107
- if (newDeviceId) {
108
- localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
109
- currentDeviceId = newDeviceId;
110
- } else {
111
- localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
112
- currentDeviceId = void 0;
113
- }
114
- if (sourceNode) sourceNode.disconnect();
115
- sourceNode = audioContext.createMediaStreamSource(audioStream);
116
- sourceNode.connect(micAnalyser.node);
117
- return audioStream;
118
- }
119
- function stopMicrophone() {
120
- if (sourceNode) {
121
- sourceNode.disconnect();
122
- sourceNode = void 0;
116
+ if (!deviceId) {
117
+ deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
118
+ }
119
+ if (this.audioStream) {
120
+ if (this.currentDeviceId === deviceId) {
121
+ return this.audioStream;
122
+ }
123
+ stopStream(this.audioStream);
124
+ }
125
+ this.audioStream = await navigator.mediaDevices.getUserMedia(
126
+ this.getMicConstraints(deviceId)
127
+ );
128
+ const track = this.audioStream.getTracks()[0];
129
+ const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
130
+ if (newDeviceId) {
131
+ localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
132
+ this.currentDeviceId = newDeviceId;
133
+ } else {
134
+ localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
135
+ this.currentDeviceId = void 0;
136
+ }
137
+ if (this.sourceNode) this.sourceNode.disconnect();
138
+ this.sourceNode = audioContext.createMediaStreamSource(this.audioStream);
139
+ this.sourceNode.connect(this.analyser.node);
140
+ return this.audioStream;
123
141
  }
124
- if (audioStream) {
125
- stopStream(audioStream);
126
- audioStream = void 0;
142
+ /**
143
+ * Stops the microphone
144
+ */
145
+ stop() {
146
+ if (this.sourceNode) {
147
+ this.sourceNode.disconnect();
148
+ this.sourceNode = void 0;
149
+ }
150
+ if (this.audioStream) {
151
+ stopStream(this.audioStream);
152
+ this.audioStream = void 0;
153
+ }
127
154
  }
155
+ };
156
+ if (!window.micdropMic) {
157
+ window.micdropMic = new Mic();
128
158
  }
159
+ mic = window.micdropMic;
129
160
 
130
- // src/audio/utils/createDelayedStream.ts
161
+ // src/audio/MicRecorder.ts
162
+ import EventEmitter3 from "eventemitter3";
163
+
164
+ // src/audio/utils/delayedStream.ts
131
165
  function createDelayedStream(stream, delay) {
132
- if (!audioContext) {
133
- throw new Error("AudioContext not initialized");
134
- }
135
166
  const audioSource = audioContext.createMediaStreamSource(stream);
136
167
  const delayNode = audioContext.createDelay(delay);
137
168
  delayNode.delayTime.value = delay;
@@ -141,6 +172,166 @@ function createDelayedStream(stream, delay) {
141
172
  return audioDestination.stream;
142
173
  }
143
174
 
175
+ // src/audio/vad/SileroVAD.ts
176
+ import { MicVAD } from "@ricky0123/vad-web/src";
177
+
178
+ // src/audio/vad/VAD.ts
179
+ import EventEmitter2 from "eventemitter3";
180
+ var VAD = class extends EventEmitter2 {
181
+ /**
182
+ * Constructor for the VAD class
183
+ * @param threshold - The threshold to use for the VAD
184
+ */
185
+ constructor(threshold = 0) {
186
+ super();
187
+ // Max delay to detect speech, can be used to record delayed stream
188
+ this.delay = 100;
189
+ this.threshold = threshold;
190
+ }
191
+ };
192
+
193
+ // src/audio/vad/SileroVAD.ts
194
+ var SileroVAD = class extends VAD {
195
+ get isStarted() {
196
+ return !!this.vad;
197
+ }
198
+ async start(stream, threshold) {
199
+ if (this.vad) return;
200
+ if (threshold !== void 0) {
201
+ this.threshold = threshold;
202
+ }
203
+ const speechThreshold = Math.min(
204
+ Math.max((this.threshold + 100) / 100, 0),
205
+ 1
206
+ );
207
+ this.vad = await MicVAD.new({
208
+ stream,
209
+ model: "v5",
210
+ onSpeechStart: () => this.emit("StartSpeaking"),
211
+ onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
212
+ onVADMisfire: () => this.emit("CancelSpeaking"),
213
+ onSpeechEnd: () => this.emit("StopSpeaking"),
214
+ positiveSpeechThreshold: speechThreshold,
215
+ negativeSpeechThreshold: speechThreshold * 0.7,
216
+ minSpeechFrames: 3,
217
+ redemptionFrames: 9
218
+ });
219
+ this.vad.start();
220
+ }
221
+ async stop() {
222
+ if (!this.vad) return;
223
+ this.vad.destroy();
224
+ this.vad = void 0;
225
+ }
226
+ async setThreshold(threshold) {
227
+ if (threshold === this.threshold) return;
228
+ if (this.vad) {
229
+ const stream = this.vad["stream"];
230
+ await this.stop();
231
+ await this.start(stream, threshold);
232
+ }
233
+ }
234
+ };
235
+
236
+ // src/audio/vad/VolumeVAD.ts
237
+ var VolumeVAD = class extends VAD {
238
+ constructor() {
239
+ super();
240
+ this.running = false;
241
+ this.speaking = false;
242
+ this.attemptSpeaking = false;
243
+ this.history = 10;
244
+ this.speakingHistory = [];
245
+ for (let i = 0; i < this.history; i++) {
246
+ this.speakingHistory.push(0);
247
+ }
248
+ }
249
+ getMaxVolume() {
250
+ if (!this.analyser || !this.fftBins) return -Infinity;
251
+ let maxVolume = -Infinity;
252
+ this.analyser.getFloatFrequencyData(this.fftBins);
253
+ for (let i = 4; i < this.fftBins.length; i++) {
254
+ if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
255
+ maxVolume = this.fftBins[i];
256
+ }
257
+ }
258
+ return maxVolume;
259
+ }
260
+ get isStarted() {
261
+ return this.running;
262
+ }
263
+ async start(stream, threshold) {
264
+ if (this.running) return;
265
+ if (threshold !== void 0) {
266
+ this.threshold = threshold;
267
+ }
268
+ this.analyser = audioContext.createAnalyser();
269
+ this.sourceNode = audioContext.createMediaStreamSource(stream);
270
+ this.analyser.fftSize = 512;
271
+ this.analyser.smoothingTimeConstant = 0.1;
272
+ this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
273
+ this.sourceNode.connect(this.analyser);
274
+ this.running = true;
275
+ this.startLoop();
276
+ }
277
+ startLoop() {
278
+ if (!this.running) return;
279
+ const currentVolume = this.getMaxVolume();
280
+ if (currentVolume > this.threshold) {
281
+ if (!this.speaking) {
282
+ let recentHistory = 0;
283
+ for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
284
+ recentHistory += this.speakingHistory[i];
285
+ }
286
+ if (recentHistory >= 2) {
287
+ this.speaking = true;
288
+ this.attemptSpeaking = false;
289
+ this.emit("ConfirmSpeaking");
290
+ } else if (recentHistory === 1) {
291
+ this.attemptSpeaking = true;
292
+ this.emit("StartSpeaking");
293
+ }
294
+ }
295
+ } else if (this.speaking) {
296
+ let totalHistory = 0;
297
+ for (let i = 0; i < this.speakingHistory.length; i++) {
298
+ totalHistory += this.speakingHistory[i];
299
+ }
300
+ if (totalHistory === 0) {
301
+ this.speaking = false;
302
+ this.emit("StopSpeaking");
303
+ }
304
+ } else if (this.attemptSpeaking) {
305
+ let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
306
+ if (lastHistory === 0) {
307
+ this.attemptSpeaking = false;
308
+ this.emit("CancelSpeaking");
309
+ }
310
+ }
311
+ this.speakingHistory.shift();
312
+ this.speakingHistory.push(Number(currentVolume > this.threshold));
313
+ setTimeout(() => this.startLoop(), this.delay);
314
+ }
315
+ async stop() {
316
+ if (!this.running) return;
317
+ this.running = false;
318
+ this.speaking = false;
319
+ if (this.sourceNode) {
320
+ this.sourceNode.disconnect();
321
+ this.sourceNode = void 0;
322
+ }
323
+ if (this.analyser) {
324
+ this.analyser.disconnect();
325
+ this.analyser = void 0;
326
+ }
327
+ this.fftBins = void 0;
328
+ }
329
+ async setThreshold(threshold) {
330
+ if (threshold === this.threshold) return;
331
+ this.threshold = threshold;
332
+ }
333
+ };
334
+
144
335
  // src/audio/MicRecorder.ts
145
336
  var timeSlice = 100;
146
337
  var defaultMicRecorderState = {
@@ -150,16 +341,21 @@ var defaultMicRecorderState = {
150
341
  isSpeaking: false,
151
342
  threshold: defaultMicThreshold
152
343
  };
153
- var MicRecorder = class extends EventEmitter2 {
154
- constructor(vad) {
344
+ var MicRecorder = class extends EventEmitter3 {
345
+ constructor(vadConfig) {
155
346
  super();
347
+ this.vadConfig = vadConfig;
156
348
  this.audioInfo = this.getAudioInfo();
157
349
  this.speakingConfirmed = false;
158
350
  this.queuedChunks = [];
159
351
  this.onStartSpeaking = (async () => {
160
352
  if (!this.recorder || this.state.isMuted) return;
161
- this.recorder.start(timeSlice);
162
- this.queuedChunks.length = 0;
353
+ try {
354
+ this.recorder.start(timeSlice);
355
+ this.queuedChunks.length = 0;
356
+ } catch (error) {
357
+ console.error(error);
358
+ }
163
359
  }).bind(this);
164
360
  this.onConfirmSpeaking = (async () => {
165
361
  if (this.state.isMuted) return;
@@ -169,19 +365,26 @@ var MicRecorder = class extends EventEmitter2 {
169
365
  }).bind(this);
170
366
  this.onCancelSpeaking = (async () => {
171
367
  if (!this.recorder) return;
172
- this.recorder.stop();
368
+ try {
369
+ this.recorder.stop();
370
+ } catch (error) {
371
+ console.error(error);
372
+ }
173
373
  this.queuedChunks.length = 0;
174
374
  }).bind(this);
175
375
  this.onStopSpeaking = (async () => {
176
376
  if (!this.recorder) return;
177
- this.recorder.stop();
377
+ try {
378
+ this.recorder.stop();
379
+ } catch (error) {
380
+ console.error(error);
381
+ }
178
382
  this.changeState({ isSpeaking: false });
179
383
  this.emit("StopSpeaking");
180
384
  this.speakingConfirmed = false;
181
385
  }).bind(this);
182
386
  const threshold = parseFloat(localStorage.getItem("micdrop.micThreshold" /* MicThreshold */) || "0") || defaultMicThreshold;
183
387
  this.state = { ...defaultMicRecorderState, threshold };
184
- this.vad = vad;
185
388
  }
186
389
  async start(stream) {
187
390
  if (this.state.isStarted) {
@@ -193,6 +396,9 @@ var MicRecorder = class extends EventEmitter2 {
193
396
  isMuted: false,
194
397
  isSpeaking: false
195
398
  });
399
+ if (!this.vad) {
400
+ this.vad = this.initVAD(this.vadConfig);
401
+ }
196
402
  const delayedStream = createDelayedStream(
197
403
  stream,
198
404
  this.vad.delay / 1e3 + 0.05
@@ -247,9 +453,18 @@ var MicRecorder = class extends EventEmitter2 {
247
453
  setThreshold(threshold) {
248
454
  this.changeState({ threshold });
249
455
  localStorage.setItem("micdrop.micThreshold" /* MicThreshold */, threshold.toString());
250
- this.vad.setThreshold(threshold);
456
+ this.vad?.setThreshold(threshold);
457
+ }
458
+ initVAD(vad) {
459
+ if (!vad) return new VolumeVAD();
460
+ if (vad === "volume") return new VolumeVAD();
461
+ if (vad === "silero") return new SileroVAD();
462
+ return vad;
251
463
  }
252
464
  async startSpeakingDetection(stream) {
465
+ if (!this.vad) {
466
+ throw new Error("VAD not initialized");
467
+ }
253
468
  if (this.vad.isStarted) {
254
469
  this.vad.stop();
255
470
  }
@@ -260,6 +475,9 @@ var MicRecorder = class extends EventEmitter2 {
260
475
  await this.vad.start(stream, this.state.threshold);
261
476
  }
262
477
  async stopSpeakingDetection() {
478
+ if (!this.vad) {
479
+ throw new Error("VAD not initialized");
480
+ }
263
481
  this.vad.off("StartSpeaking", this.onStartSpeaking);
264
482
  this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
265
483
  this.vad.off("CancelSpeaking", this.onCancelSpeaking);
@@ -297,21 +515,6 @@ var MicRecorder = class extends EventEmitter2 {
297
515
  }
298
516
  };
299
517
 
300
- // src/audio/speaker.ts
301
- var speaker_exports = {};
302
- __export(speaker_exports, {
303
- canChangeSpeakerDevice: () => canChangeSpeakerDevice,
304
- changeSpeakerDevice: () => changeSpeakerDevice,
305
- disableStreaming: () => disableStreaming,
306
- enableStreaming: () => enableStreaming,
307
- isStreamingEnabled: () => isStreamingEnabled,
308
- pauseAudio: () => pauseAudio,
309
- playAudio: () => playAudio,
310
- resumeAudio: () => resumeAudio,
311
- speakerAnalyser: () => speakerAnalyser,
312
- stopAudio: () => stopAudio
313
- });
314
-
315
518
  // src/audio/player/SpeakerPlayer.ts
316
519
  var SpeakerPlayer = class {
317
520
  constructor(outputNode) {
@@ -502,7 +705,7 @@ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
502
705
  this.audioElement = void 0;
503
706
  }
504
707
  if (this.mediaStream) {
505
- this.mediaStream.getTracks().forEach((track) => track.stop());
708
+ stopStream(this.mediaStream);
506
709
  this.mediaStream = void 0;
507
710
  }
508
711
  this.mediaSource = void 0;
@@ -567,280 +770,94 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
567
770
  }
568
771
  };
569
772
 
570
- // src/audio/utils/unlock.ts
571
- function unlock(audioContext2) {
572
- const events = ["touchstart", "touchend", "mousedown", "keydown"];
573
- events.forEach((e) => document.body.addEventListener(e, unlock2, false));
574
- async function unlock2() {
575
- unlockSafari(audioContext2);
576
- if (audioContext2.state === "suspended") {
577
- await audioContext2.resume();
773
+ // src/audio/speaker.ts
774
+ var speaker;
775
+ var Speaker = class {
776
+ constructor() {
777
+ this.streamingEnabled = false;
778
+ this.analyser = new AudioAnalyser(audioContext);
779
+ this.audioElement = new Audio();
780
+ this.audioElement.autoplay = true;
781
+ const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
782
+ if (speakerId) {
783
+ this.setSinkId(speakerId).catch((error) => {
784
+ console.error("Failed to set sink ID:", error);
785
+ });
578
786
  }
579
- clean();
580
- }
581
- function clean() {
582
- events.forEach((e) => document.body.removeEventListener(e, unlock2));
787
+ this.setupSpeakerPlayer();
788
+ const destinationNode = audioContext.createMediaStreamDestination();
789
+ this.analyser.node.connect(destinationNode);
790
+ this.audioElement.srcObject = destinationNode.stream;
583
791
  }
584
- }
585
- function unlockSafari(audioContext2) {
586
- const source = audioContext2.createBufferSource();
587
- source.buffer = audioContext2.createBuffer(1, 1, 22050);
588
- source.connect(audioContext2.destination);
589
- const onEnded = () => {
590
- source.disconnect();
591
- source.buffer = null;
592
- source.removeEventListener("ended", onEnded);
593
- };
594
- source.addEventListener("ended", onEnded);
595
- source.start();
596
- }
597
-
598
- // src/audio/speaker.ts
599
- var audioElement;
600
- var player;
601
- var streamingEnabled = false;
602
- var speakerAnalyser = new AudioAnalyser(audioContext);
603
- async function init() {
604
- unlock(audioContext);
605
- audioElement = new Audio();
606
- audioElement.autoplay = true;
607
- const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
608
- if (speakerId) {
792
+ async setupSpeakerPlayer() {
609
793
  try {
610
- await setSinkId(speakerId);
794
+ this.player?.destroy();
795
+ this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
796
+ await this.player?.setup();
611
797
  } catch (error) {
612
- console.error("Failed to set sink ID:", error);
798
+ console.error("Error setting up speaker player", error);
613
799
  }
614
800
  }
615
- await setupSpeakerPlayer();
616
- const destinationNode = audioContext.createMediaStreamDestination();
617
- speakerAnalyser.node.connect(destinationNode);
618
- audioElement.srcObject = destinationNode.stream;
619
- }
620
- init();
621
- async function setupSpeakerPlayer() {
622
- try {
623
- player?.destroy();
624
- player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
625
- await player?.setup();
626
- } catch (error) {
627
- console.error("Error setting up speaker player", error);
628
- }
629
- }
630
- async function isStreamingEnabled() {
631
- return streamingEnabled;
632
- }
633
- async function enableStreaming() {
634
- if (streamingEnabled) return;
635
- streamingEnabled = true;
636
- await setupSpeakerPlayer();
637
- }
638
- async function disableStreaming() {
639
- if (!streamingEnabled) return;
640
- streamingEnabled = false;
641
- await setupSpeakerPlayer();
642
- }
643
- function canChangeSpeakerDevice() {
644
- return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
645
- }
646
- async function setSinkId(speakerId) {
647
- if (speakerId && canChangeSpeakerDevice() && audioElement) {
648
- return audioElement.setSinkId(speakerId);
801
+ get isStreamingEnabled() {
802
+ return this.streamingEnabled;
649
803
  }
650
- }
651
- async function changeSpeakerDevice(speakerId) {
652
- if (!canChangeSpeakerDevice()) return;
653
- try {
654
- await setSinkId(speakerId);
655
- localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
656
- } catch (error) {
657
- console.error(`Error setting Audio Output to ${speakerId}`, error);
804
+ async enableStreaming() {
805
+ if (this.streamingEnabled) return;
806
+ this.streamingEnabled = true;
807
+ await this.setupSpeakerPlayer();
658
808
  }
659
- }
660
- function pauseAudio() {
661
- if (audioElement) {
662
- audioElement.pause();
809
+ async disableStreaming() {
810
+ if (!this.streamingEnabled) return;
811
+ this.streamingEnabled = false;
812
+ await this.setupSpeakerPlayer();
663
813
  }
664
- }
665
- function resumeAudio() {
666
- if (audioElement) {
667
- audioElement.play();
814
+ canChangeDevice() {
815
+ return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
668
816
  }
669
- }
670
- async function playAudio(blob) {
671
- if (!audioElement || !player) return;
672
- try {
673
- player.addBlob(blob);
674
- if (audioElement.paused) {
675
- audioElement.play();
817
+ async setSinkId(speakerId) {
818
+ if (speakerId && this.canChangeDevice() && this.audioElement) {
819
+ return this.audioElement.setSinkId(speakerId);
676
820
  }
677
- } catch (error) {
678
- console.error("Error playing audio blob", error, blob);
679
821
  }
680
- }
681
- function stopAudio() {
682
- audioElement?.pause();
683
- player?.stop();
684
- }
685
-
686
- // src/audio/vad/SileroVAD.ts
687
- import { MicVAD } from "@ricky0123/vad-web/src";
688
-
689
- // src/audio/vad/VAD.ts
690
- import EventEmitter3 from "eventemitter3";
691
- var VAD = class extends EventEmitter3 {
692
- /**
693
- * Constructor for the VAD class
694
- * @param threshold - The threshold to use for the VAD
695
- */
696
- constructor(threshold = 0) {
697
- super();
698
- // Max delay to detect speech, can be used to record delayed stream
699
- this.delay = 100;
700
- this.threshold = threshold;
701
- }
702
- };
703
-
704
- // src/audio/vad/SileroVAD.ts
705
- var SileroVAD = class extends VAD {
706
- get isStarted() {
707
- return !!this.vad;
708
- }
709
- async start(stream, threshold) {
710
- if (this.vad) return;
711
- if (threshold !== void 0) {
712
- this.threshold = threshold;
713
- }
714
- const speechThreshold = Math.min(
715
- Math.max((this.threshold + 100) / 100, 0),
716
- 1
717
- );
718
- this.vad = await MicVAD.new({
719
- stream,
720
- onSpeechStart: () => this.emit("StartSpeaking"),
721
- onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
722
- onVADMisfire: () => this.emit("CancelSpeaking"),
723
- onSpeechEnd: () => this.emit("StopSpeaking"),
724
- positiveSpeechThreshold: speechThreshold,
725
- negativeSpeechThreshold: speechThreshold * 0.7,
726
- // Adjust negative threshold proportionally
727
- redemptionFrames: 5
728
- });
729
- this.vad.start();
730
- }
731
- async stop() {
732
- if (!this.vad) return;
733
- this.vad.pause();
734
- this.vad = void 0;
735
- }
736
- async setThreshold(threshold) {
737
- if (threshold === this.threshold) return;
738
- if (this.vad) {
739
- const stream = this.vad["stream"];
740
- await this.stop();
741
- await this.start(stream, threshold);
742
- }
743
- }
744
- };
745
-
746
- // src/audio/vad/VolumeVAD.ts
747
- var VolumeVAD = class extends VAD {
748
- constructor() {
749
- super();
750
- this.running = false;
751
- this.speaking = false;
752
- this.attemptSpeaking = false;
753
- this.history = 10;
754
- this.speakingHistory = [];
755
- for (let i = 0; i < this.history; i++) {
756
- this.speakingHistory.push(0);
822
+ async changeDevice(speakerId) {
823
+ if (!this.canChangeDevice()) return;
824
+ try {
825
+ await this.setSinkId(speakerId);
826
+ localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
827
+ } catch (error) {
828
+ console.error(`Error setting Audio Output to ${speakerId}`, error);
757
829
  }
758
830
  }
759
- getMaxVolume() {
760
- if (!this.analyser || !this.fftBins) return -Infinity;
761
- let maxVolume = -Infinity;
762
- this.analyser.getFloatFrequencyData(this.fftBins);
763
- for (let i = 4; i < this.fftBins.length; i++) {
764
- if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
765
- maxVolume = this.fftBins[i];
766
- }
831
+ pauseAudio() {
832
+ if (this.audioElement) {
833
+ this.audioElement.pause();
767
834
  }
768
- return maxVolume;
769
835
  }
770
- get isStarted() {
771
- return this.running;
772
- }
773
- async start(stream, threshold) {
774
- if (this.running) return;
775
- if (threshold !== void 0) {
776
- this.threshold = threshold;
836
+ resumeAudio() {
837
+ if (this.audioElement) {
838
+ this.audioElement.play();
777
839
  }
778
- this.analyser = audioContext.createAnalyser();
779
- this.sourceNode = audioContext.createMediaStreamSource(stream);
780
- this.analyser.fftSize = 512;
781
- this.analyser.smoothingTimeConstant = 0.1;
782
- this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
783
- this.sourceNode.connect(this.analyser);
784
- this.running = true;
785
- this.startLoop();
786
840
  }
787
- startLoop() {
788
- if (!this.running) return;
789
- const currentVolume = this.getMaxVolume();
790
- if (currentVolume > this.threshold) {
791
- if (!this.speaking) {
792
- let recentHistory = 0;
793
- for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
794
- recentHistory += this.speakingHistory[i];
795
- }
796
- if (recentHistory >= 2) {
797
- this.speaking = true;
798
- this.attemptSpeaking = false;
799
- this.emit("ConfirmSpeaking");
800
- } else if (recentHistory === 1) {
801
- this.attemptSpeaking = true;
802
- this.emit("StartSpeaking");
803
- }
804
- }
805
- } else if (this.speaking) {
806
- let totalHistory = 0;
807
- for (let i = 0; i < this.speakingHistory.length; i++) {
808
- totalHistory += this.speakingHistory[i];
809
- }
810
- if (totalHistory === 0) {
811
- this.speaking = false;
812
- this.emit("StopSpeaking");
813
- }
814
- } else if (this.attemptSpeaking) {
815
- let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
816
- if (lastHistory === 0) {
817
- this.attemptSpeaking = false;
818
- this.emit("CancelSpeaking");
841
+ async playAudio(blob) {
842
+ if (!this.audioElement || !this.player) return;
843
+ try {
844
+ this.player.addBlob(blob);
845
+ if (this.audioElement.paused) {
846
+ this.audioElement.play();
819
847
  }
848
+ } catch (error) {
849
+ console.error("Error playing audio blob", error, blob);
820
850
  }
821
- this.speakingHistory.shift();
822
- this.speakingHistory.push(Number(currentVolume > this.threshold));
823
- setTimeout(() => this.startLoop(), this.delay);
824
- }
825
- async stop() {
826
- if (!this.running) return;
827
- this.running = false;
828
- this.speaking = false;
829
- if (this.sourceNode) {
830
- this.sourceNode.disconnect();
831
- this.sourceNode = void 0;
832
- }
833
- if (this.analyser) {
834
- this.analyser.disconnect();
835
- this.analyser = void 0;
836
- }
837
- this.fftBins = void 0;
838
851
  }
839
- async setThreshold(threshold) {
840
- if (threshold === this.threshold) return;
841
- this.threshold = threshold;
852
+ stopAudio() {
853
+ this.audioElement?.pause();
854
+ this.player?.stop();
842
855
  }
843
856
  };
857
+ if (!window.micdropSpeaker) {
858
+ window.micdropSpeaker = new Speaker();
859
+ }
860
+ speaker = window.micdropSpeaker;
844
861
 
845
862
  // src/call/CallHandler.ts
846
863
  import EventEmitter4 from "eventemitter3";
@@ -863,49 +880,23 @@ var CallHandlerError = class extends Error {
863
880
  var CallHandler = class _CallHandler extends EventEmitter4 {
864
881
  constructor(options) {
865
882
  super();
883
+ this.options = options;
866
884
  this.conversation = [];
867
885
  this.debug = false;
868
886
  this.startTime = 0;
869
887
  this._isProcessing = false;
870
- this.micRecorder = new MicRecorder(this.getVAD(options?.vad));
871
- this.micRecorder.on("StateChange", () => {
872
- this.notifyStateChange();
873
- });
874
- this.micRecorder.on("Chunk", (blob) => {
875
- this.log(`[Mic] Received chunk`, blob);
876
- this.ws?.send(blob);
877
- });
878
- this.micRecorder.on("StartSpeaking", () => {
879
- this.log("[Mic] Start speaking");
880
- this.ws?.send("startSpeaking" /* StartSpeaking */);
881
- this._isProcessing = false;
882
- this.notifyStateChange();
883
- stopAudio();
884
- });
885
- this.micRecorder.on("StopSpeaking", () => {
886
- this.log("[Mic] Stop speaking");
887
- this.ws?.send("stopSpeaking" /* StopSpeaking */);
888
- this._isProcessing = true;
889
- this.notifyStateChange();
890
- });
891
888
  }
892
889
  static getInstance(options) {
893
- if (!_CallHandler.instance) {
894
- _CallHandler.instance = new _CallHandler(options);
890
+ if (!window.micdropCallHandler) {
891
+ window.micdropCallHandler = new _CallHandler(options);
895
892
  }
896
- return _CallHandler.instance;
897
- }
898
- getVAD(vad) {
899
- if (!vad) return new VolumeVAD();
900
- if (vad === "volume") return new VolumeVAD();
901
- if (vad === "silero") return new SileroVAD();
902
- return vad;
893
+ return window.micdropCallHandler;
903
894
  }
904
895
  get isStarted() {
905
- return this.isWSStarted && this.micRecorder.state.isStarted;
896
+ return this.isWSStarted && this.micRecorder?.state.isStarted || false;
906
897
  }
907
898
  get isStarting() {
908
- return this.isWSStarting || this.micRecorder.state.isStarting;
899
+ return this.isWSStarting || this.micRecorder?.state.isStarting || false;
909
900
  }
910
901
  get isWSStarted() {
911
902
  return this.ws?.readyState === WebSocket.OPEN;
@@ -916,6 +907,15 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
916
907
  get isMicStarted() {
917
908
  return !!this.micStream;
918
909
  }
910
+ get isMicMuted() {
911
+ return this.micRecorder?.state.isMuted ?? false;
912
+ }
913
+ get isMicSpeaking() {
914
+ return this.micRecorder?.state.isSpeaking ?? false;
915
+ }
916
+ get micThreshold() {
917
+ return this.micRecorder?.state.threshold ?? mic.defaultThreshold;
918
+ }
919
919
  get isProcessing() {
920
920
  return this._isProcessing;
921
921
  }
@@ -933,32 +933,57 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
933
933
  console.error("[CallHandler] stop WS", error);
934
934
  }
935
935
  try {
936
- this.micRecorder.stop();
936
+ this.micRecorder?.stop();
937
937
  this.stopMic();
938
938
  } catch (error) {
939
939
  console.error("[CallHandler] stop mic", error);
940
940
  }
941
941
  }
942
942
  pause() {
943
- this.micRecorder.mute();
943
+ this.micRecorder?.mute();
944
+ this._isProcessing = false;
944
945
  this.notifyStateChange();
945
- pauseAudio();
946
+ speaker.pauseAudio();
947
+ this.ws?.send("mute" /* Mute */);
946
948
  }
947
949
  resume() {
948
950
  if (this.lastAudioBlob) {
949
- playAudio(this.lastAudioBlob);
951
+ speaker.playAudio(this.lastAudioBlob);
950
952
  }
951
- resumeAudio();
952
- this.micRecorder.unmute();
953
+ speaker.resumeAudio();
954
+ this.micRecorder?.unmute();
953
955
  this.notifyStateChange();
954
956
  }
955
957
  async startMic(deviceId, record = true) {
956
958
  try {
959
+ if (!this.micRecorder) {
960
+ this.micRecorder = new MicRecorder(this.options?.vad);
961
+ this.micRecorder.on("StateChange", () => {
962
+ this.notifyStateChange();
963
+ });
964
+ this.micRecorder.on("Chunk", (blob) => {
965
+ this.log(`[Mic] Received chunk`, blob);
966
+ this.ws?.send(blob);
967
+ });
968
+ this.micRecorder.on("StartSpeaking", () => {
969
+ this.log("[Mic] Start speaking");
970
+ this.ws?.send("startSpeaking" /* StartSpeaking */);
971
+ this._isProcessing = false;
972
+ this.notifyStateChange();
973
+ speaker.stopAudio();
974
+ });
975
+ this.micRecorder.on("StopSpeaking", () => {
976
+ this.log("[Mic] Stop speaking");
977
+ this.ws?.send("stopSpeaking" /* StopSpeaking */);
978
+ this._isProcessing = true;
979
+ this.notifyStateChange();
980
+ });
981
+ }
957
982
  const isRecorderStarted = this.micRecorder.state.isStarted;
958
983
  if (isRecorderStarted) {
959
- this.micRecorder.stop();
984
+ this.micRecorder?.stop();
960
985
  }
961
- this.micStream = await startMicrophone(deviceId);
986
+ this.micStream = await mic.start(deviceId);
962
987
  this.notifyStateChange();
963
988
  if (isRecorderStarted || record) {
964
989
  await this.micRecorder.start(this.micStream);
@@ -970,7 +995,9 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
970
995
  }
971
996
  }
972
997
  stopMic() {
973
- stopMicrophone();
998
+ this.micRecorder?.stop();
999
+ this.micRecorder = void 0;
1000
+ mic.stop();
974
1001
  this.micStream = void 0;
975
1002
  this.notifyStateChange();
976
1003
  }
@@ -997,7 +1024,7 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
997
1024
  this.ws.onmessage = (event) => {
998
1025
  this.log("[WS]", event.data);
999
1026
  if (event.data instanceof Blob) {
1000
- playAudio(event.data);
1027
+ speaker.playAudio(event.data);
1001
1028
  this.lastAudioBlob = event.data;
1002
1029
  this._isProcessing = false;
1003
1030
  this.notifyStateChange();
@@ -1019,7 +1046,7 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
1019
1046
  this.notifyStateChange();
1020
1047
  }
1021
1048
  } else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
1022
- enableStreaming();
1049
+ speaker.enableStreaming();
1023
1050
  }
1024
1051
  };
1025
1052
  this.ws.onclose = (event) => {
@@ -1081,10 +1108,10 @@ export {
1081
1108
  CallHandlerErrorCode,
1082
1109
  CallServerCommands,
1083
1110
  LocalStorageKeys,
1084
- microphone_exports as Mic,
1111
+ mic as Mic,
1085
1112
  MicRecorder,
1086
1113
  SileroVAD,
1087
- speaker_exports as Speaker,
1114
+ speaker as Speaker,
1088
1115
  VAD,
1089
1116
  VolumeVAD
1090
1117
  };