@micdrop/client 1.9.0 → 2.0.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
@@ -84,8 +84,7 @@ function stopStream(stream) {
84
84
  stream?.getTracks().forEach((track) => track.stop());
85
85
  }
86
86
 
87
- // src/audio/mic.ts
88
- var mic;
87
+ // src/audio/Mic.ts
89
88
  var Mic = class {
90
89
  constructor() {
91
90
  this.analyser = new AudioAnalyser(audioContext);
@@ -116,7 +115,7 @@ var Mic = class {
116
115
  deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
117
116
  }
118
117
  if (this.audioStream) {
119
- if (this.currentDeviceId === deviceId) {
118
+ if (this.deviceId === deviceId) {
120
119
  return this.audioStream;
121
120
  }
122
121
  stopStream(this.audioStream);
@@ -128,10 +127,10 @@ var Mic = class {
128
127
  const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
129
128
  if (newDeviceId) {
130
129
  localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
131
- this.currentDeviceId = newDeviceId;
130
+ this.deviceId = newDeviceId;
132
131
  } else {
133
132
  localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
134
- this.currentDeviceId = void 0;
133
+ this.deviceId = void 0;
135
134
  }
136
135
  if (this.sourceNode) this.sourceNode.disconnect();
137
136
  this.sourceNode = audioContext.createMediaStreamSource(this.audioStream);
@@ -152,14 +151,199 @@ var Mic = class {
152
151
  }
153
152
  }
154
153
  };
155
- if (!window.micdropMic) {
156
- window.micdropMic = new Mic();
157
- }
158
- mic = window.micdropMic;
159
154
 
160
- // src/audio/MicRecorder.ts
155
+ // src/audio/Speaker.ts
161
156
  import EventEmitter3 from "eventemitter3";
162
157
 
158
+ // src/audio/stream/AudioStream.ts
159
+ import EventEmitter2 from "eventemitter3";
160
+ var AudioStream = class extends EventEmitter2 {
161
+ constructor() {
162
+ super();
163
+ this.isPlaying = false;
164
+ this.outputNode = audioContext.createGain();
165
+ }
166
+ setIsPlaying(isPlaying) {
167
+ if (this.isPlaying === isPlaying) return;
168
+ this.isPlaying = isPlaying;
169
+ this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
170
+ }
171
+ };
172
+
173
+ // src/audio/stream/Pcm16AudioStream.ts
174
+ var SAMPLE_RATE = 16e3;
175
+ var CHANNELS = 1;
176
+ var Pcm16AudioStream = class extends AudioStream {
177
+ constructor() {
178
+ super();
179
+ this.audioQueue = [];
180
+ this.isProcessingQueue = false;
181
+ }
182
+ start() {
183
+ return this.outputNode;
184
+ }
185
+ async playAudio(blob) {
186
+ try {
187
+ const arrayBuffer = await blob.arrayBuffer();
188
+ const audioBuffer = this.createAudioBuffer(arrayBuffer);
189
+ if (audioBuffer) {
190
+ this.audioQueue.push(audioBuffer);
191
+ this.processQueue();
192
+ }
193
+ } catch (error) {
194
+ console.error("Failed to play audio chunk:", error);
195
+ }
196
+ }
197
+ stopAudio() {
198
+ if (this.currentSourceNode) {
199
+ try {
200
+ this.currentSourceNode.stop();
201
+ this.currentSourceNode.disconnect();
202
+ } catch (error) {
203
+ }
204
+ this.currentSourceNode = void 0;
205
+ }
206
+ this.audioQueue = [];
207
+ this.isProcessingQueue = false;
208
+ this.setIsPlaying(false);
209
+ }
210
+ createAudioBuffer(arrayBuffer) {
211
+ try {
212
+ const int16Data = new Int16Array(arrayBuffer);
213
+ const numSamples = int16Data.length;
214
+ if (numSamples === 0) {
215
+ return null;
216
+ }
217
+ const audioBuffer = audioContext.createBuffer(
218
+ CHANNELS,
219
+ numSamples,
220
+ SAMPLE_RATE
221
+ );
222
+ const channelData = audioBuffer.getChannelData(0);
223
+ for (let i = 0; i < numSamples; i++) {
224
+ channelData[i] = int16Data[i] / 32768;
225
+ }
226
+ return audioBuffer;
227
+ } catch (error) {
228
+ console.error("Error creating AudioBuffer:", error);
229
+ return null;
230
+ }
231
+ }
232
+ processQueue() {
233
+ if (this.isProcessingQueue || this.audioQueue.length === 0) {
234
+ return;
235
+ }
236
+ this.isProcessingQueue = true;
237
+ this.playNextBuffer();
238
+ }
239
+ playNextBuffer() {
240
+ if (this.audioQueue.length === 0) {
241
+ this.isProcessingQueue = false;
242
+ this.setIsPlaying(false);
243
+ return;
244
+ }
245
+ const audioBuffer = this.audioQueue.shift();
246
+ this.currentSourceNode = audioContext.createBufferSource();
247
+ this.currentSourceNode.buffer = audioBuffer;
248
+ this.currentSourceNode.connect(this.outputNode);
249
+ this.currentSourceNode.onended = () => {
250
+ this.currentSourceNode = void 0;
251
+ this.playNextBuffer();
252
+ };
253
+ this.currentSourceNode.start(0);
254
+ this.setIsPlaying(true);
255
+ }
256
+ };
257
+
258
+ // src/audio/Speaker.ts
259
+ var Speaker = class extends EventEmitter3 {
260
+ constructor() {
261
+ super(...arguments);
262
+ this.isPlaying = false;
263
+ this.analyser = new AudioAnalyser(audioContext);
264
+ // Set by MicdropClient, useful to detect default device
265
+ this.devices = [];
266
+ }
267
+ // Used when audiocontext sinkId is not available
268
+ async start() {
269
+ if (this.audioStream) return;
270
+ this.audioStream = new Pcm16AudioStream();
271
+ this.audioStream.on("StartPlaying", () => this.setIsPlaying(true));
272
+ this.audioStream.on("StopPlaying", () => this.setIsPlaying(false));
273
+ this.audioSourceNode = this.audioStream.start();
274
+ const deviceId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) ?? void 0;
275
+ await this.setDevice(deviceId);
276
+ }
277
+ async playAudio(blob) {
278
+ this.start();
279
+ try {
280
+ await this.audioStream?.playAudio(blob);
281
+ } catch (error) {
282
+ console.error("Error playing audio blob", error, blob);
283
+ }
284
+ }
285
+ async stopAudio() {
286
+ if (!this.isPlaying) return;
287
+ this.audioStream?.stopAudio();
288
+ }
289
+ async changeDevice(deviceId) {
290
+ await this.setDevice(deviceId);
291
+ if (this.isDefaultDevice) {
292
+ localStorage.removeItem("micdrop.speakerDevice" /* SpeakerDevice */);
293
+ } else {
294
+ localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, deviceId);
295
+ }
296
+ }
297
+ get isDefaultDevice() {
298
+ return !this.deviceId || this.devices.length === 0 || this.deviceId === this.devices[0].deviceId;
299
+ }
300
+ setIsPlaying(isPlaying) {
301
+ if (this.isPlaying === isPlaying) return;
302
+ this.isPlaying = isPlaying;
303
+ this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
304
+ }
305
+ async setDevice(deviceId) {
306
+ if (!this.audioSourceNode) {
307
+ console.error(
308
+ "audioSourceNode not initialized, setDevice called before start"
309
+ );
310
+ return;
311
+ }
312
+ try {
313
+ this.deviceId = deviceId;
314
+ const defaultDevice = this.isDefaultDevice;
315
+ const sinkId = !deviceId || defaultDevice ? "" : deviceId;
316
+ if (defaultDevice || "setSinkId" in audioContext) {
317
+ await audioContext.setSinkId?.(sinkId);
318
+ this.audioSourceNode.disconnect();
319
+ this.audioSourceNode.connect(audioContext.destination);
320
+ this.audioSourceNode.connect(this.analyser.node);
321
+ if (this.audioElementDestination) {
322
+ this.audioElementDestination.pause();
323
+ this.audioElementDestination.srcObject = null;
324
+ this.audioElementDestination = void 0;
325
+ }
326
+ } else {
327
+ if (!this.audioElementDestination) {
328
+ const mediaStreamDestination = audioContext.createMediaStreamDestination();
329
+ this.audioSourceNode.disconnect();
330
+ this.audioSourceNode.connect(mediaStreamDestination);
331
+ this.audioSourceNode.connect(this.analyser.node);
332
+ this.audioElementDestination = new Audio();
333
+ this.audioElementDestination.autoplay = true;
334
+ this.audioElementDestination.srcObject = mediaStreamDestination.stream;
335
+ }
336
+ await this.audioElementDestination.setSinkId(sinkId);
337
+ }
338
+ } catch (error) {
339
+ console.error(`Error setting Audio Output to ${deviceId}`, error);
340
+ }
341
+ }
342
+ };
343
+
344
+ // src/audio/MicRecorder.ts
345
+ import EventEmitter5 from "eventemitter3";
346
+
163
347
  // src/audio/utils/delayedStream.ts
164
348
  function createDelayedStream(stream, delay) {
165
349
  const audioSource = audioContext.createMediaStreamSource(stream);
@@ -172,16 +356,17 @@ function createDelayedStream(stream, delay) {
172
356
  }
173
357
 
174
358
  // src/audio/vad/VAD.ts
175
- import EventEmitter2 from "eventemitter3";
359
+ import EventEmitter4 from "eventemitter3";
176
360
  var VADStatus = /* @__PURE__ */ ((VADStatus2) => {
177
361
  VADStatus2[VADStatus2["Silence"] = 0] = "Silence";
178
362
  VADStatus2[VADStatus2["MaybeSpeaking"] = 1] = "MaybeSpeaking";
179
363
  VADStatus2[VADStatus2["Speaking"] = 2] = "Speaking";
180
364
  return VADStatus2;
181
365
  })(VADStatus || {});
182
- var VAD = class extends EventEmitter2 {
366
+ var VAD = class extends EventEmitter4 {
183
367
  constructor() {
184
368
  super(...arguments);
369
+ this.name = this.constructor.name;
185
370
  this.status = 0 /* Silence */;
186
371
  // Max delay to detect speech, can be used to record delayed stream
187
372
  this.delay = 100;
@@ -226,8 +411,38 @@ var VAD = class extends EventEmitter2 {
226
411
  var MultipleVAD = class extends VAD {
227
412
  constructor(vads) {
228
413
  super();
414
+ this.onStatusChange = () => {
415
+ const isAllSilence = this.vads.every(
416
+ (vad) => vad.status === 0 /* Silence */
417
+ );
418
+ const isAllSpeaking = this.vads.every(
419
+ (vad) => vad.status === 2 /* Speaking */
420
+ );
421
+ switch (this.status) {
422
+ case 0 /* Silence */:
423
+ if (isAllSilence) break;
424
+ this.emit("StartSpeaking");
425
+ if (isAllSpeaking) {
426
+ this.emit("ConfirmSpeaking");
427
+ }
428
+ break;
429
+ case 1 /* MaybeSpeaking */:
430
+ if (isAllSilence) {
431
+ this.emit("CancelSpeaking");
432
+ break;
433
+ }
434
+ if (isAllSpeaking) {
435
+ this.emit("ConfirmSpeaking");
436
+ }
437
+ break;
438
+ case 2 /* Speaking */:
439
+ if (isAllSilence) {
440
+ this.emit("StopSpeaking");
441
+ }
442
+ break;
443
+ }
444
+ };
229
445
  this.vads = vads.map((vad) => getVAD(vad));
230
- this.onStatusChange = this.onStatusChange.bind(this);
231
446
  }
232
447
  get isStarted() {
233
448
  return this.vads.some((vad) => vad.isStarted);
@@ -241,37 +456,6 @@ var MultipleVAD = class extends VAD {
241
456
  await vad.start(stream);
242
457
  }
243
458
  }
244
- onStatusChange() {
245
- const isAllSilence = this.vads.every(
246
- (vad) => vad.status === 0 /* Silence */
247
- );
248
- const isAllSpeaking = this.vads.every(
249
- (vad) => vad.status === 2 /* Speaking */
250
- );
251
- switch (this.status) {
252
- case 0 /* Silence */:
253
- if (isAllSilence) break;
254
- this.emit("StartSpeaking");
255
- if (isAllSpeaking) {
256
- this.emit("ConfirmSpeaking");
257
- }
258
- break;
259
- case 1 /* MaybeSpeaking */:
260
- if (isAllSilence) {
261
- this.emit("CancelSpeaking");
262
- break;
263
- }
264
- if (isAllSpeaking) {
265
- this.emit("ConfirmSpeaking");
266
- }
267
- break;
268
- case 2 /* Speaking */:
269
- if (isAllSilence) {
270
- this.emit("StopSpeaking");
271
- }
272
- break;
273
- }
274
- }
275
459
  async stop() {
276
460
  for (const vad of this.vads) {
277
461
  vad.off("ChangeStatus", this.onStatusChange);
@@ -281,7 +465,7 @@ var MultipleVAD = class extends VAD {
281
465
  };
282
466
 
283
467
  // src/audio/vad/SileroVAD.ts
284
- import { MicVAD } from "@ricky0123/vad-web/src";
468
+ import { MicVAD } from "@ricky0123/vad-web";
285
469
  var defaultOptions = {
286
470
  positiveSpeechThreshold: 0.18,
287
471
  negativeSpeechThreshold: 0.11,
@@ -313,6 +497,7 @@ var SileroVAD = class extends VAD {
313
497
  this.vad = await MicVAD.new({
314
498
  stream,
315
499
  model: "v5",
500
+ submitUserSpeechOnPause: true,
316
501
  onSpeechStart: () => this.emit("StartSpeaking"),
317
502
  onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
318
503
  onVADMisfire: () => this.emit("CancelSpeaking"),
@@ -464,8 +649,9 @@ var VolumeVAD = class extends VAD {
464
649
  };
465
650
 
466
651
  // src/audio/vad/getVAD.ts
652
+ var DefaultVAD = VolumeVAD;
467
653
  function getVAD(vad) {
468
- if (!vad) return new VolumeVAD();
654
+ if (!vad) return new DefaultVAD();
469
655
  if (vad === "volume") return new VolumeVAD();
470
656
  if (vad === "silero") return new SileroVAD();
471
657
  if (Array.isArray(vad)) {
@@ -482,13 +668,74 @@ var defaultMicRecorderState = {
482
668
  isMuted: false,
483
669
  isSpeaking: false
484
670
  };
485
- var MicRecorder = class extends EventEmitter3 {
671
+ var MicRecorder = class extends EventEmitter5 {
486
672
  constructor(vadConfig) {
487
673
  super();
488
674
  this.audioInfo = this.getAudioInfo();
489
675
  this.speakingConfirmed = false;
490
676
  this.queuedChunks = [];
491
- this.onStartSpeaking = (async () => {
677
+ this.start = async (stream) => {
678
+ if (this.state.isStarted) {
679
+ throw new Error("MicRecorder is already started");
680
+ }
681
+ try {
682
+ this.changeState({
683
+ isStarting: true,
684
+ isMuted: false,
685
+ isSpeaking: false
686
+ });
687
+ const delayedStream = createDelayedStream(
688
+ stream,
689
+ this.vad.delay / 1e3 + 0.05
690
+ );
691
+ this.delayedStream = delayedStream;
692
+ if (this.recorder) {
693
+ this.recorder.stop();
694
+ }
695
+ this.recorder = new window.MediaRecorder(delayedStream, {
696
+ mimeType: this.audioInfo.mimeType,
697
+ audioBitsPerSecond: 128e3
698
+ });
699
+ this.recorder.ondataavailable = this.onDataAvailable;
700
+ await this.startSpeakingDetection(stream);
701
+ this.changeState({
702
+ isStarting: false,
703
+ isStarted: true
704
+ });
705
+ } catch (err) {
706
+ this.stop();
707
+ console.error(err);
708
+ }
709
+ };
710
+ this.mute = () => {
711
+ this.changeState({ isMuted: true, isSpeaking: false });
712
+ this.recorder?.stop();
713
+ };
714
+ this.unmute = () => {
715
+ this.changeState({ isMuted: false });
716
+ };
717
+ this.stop = () => {
718
+ this.changeState({
719
+ isStarting: false,
720
+ isStarted: false,
721
+ isMuted: false,
722
+ isSpeaking: false
723
+ });
724
+ try {
725
+ this.stopSpeakingDetection();
726
+ if (this.recorder) {
727
+ this.recorder.stop();
728
+ this.recorder = void 0;
729
+ }
730
+ if (this.delayedStream) {
731
+ stopStream(this.delayedStream);
732
+ this.delayedStream = void 0;
733
+ }
734
+ } catch (err) {
735
+ console.error(err);
736
+ }
737
+ };
738
+ this.onStartSpeaking = async () => {
492
739
  if (!this.recorder || this.state.isMuted) return;
493
740
  try {
494
741
  this.recorder.start(timeSlice);
@@ -496,14 +743,14 @@ var MicRecorder = class extends EventEmitter3 {
496
743
  } catch (error) {
497
744
  console.error(error);
498
745
  }
499
- }).bind(this);
500
- this.onConfirmSpeaking = (async () => {
746
+ };
747
+ this.onConfirmSpeaking = async () => {
501
748
  if (this.state.isMuted) return;
502
749
  this.emit("StartSpeaking");
503
750
  this.changeState({ isSpeaking: true });
504
751
  this.speakingConfirmed = true;
505
- }).bind(this);
506
- this.onCancelSpeaking = (async () => {
752
+ };
753
+ this.onCancelSpeaking = async () => {
507
754
  if (this.state.isMuted) return;
508
755
  try {
509
756
  this.recorder?.stop();
@@ -511,8 +758,8 @@ var MicRecorder = class extends EventEmitter3 {
511
758
  console.error(error);
512
759
  }
513
760
  this.queuedChunks.length = 0;
514
- }).bind(this);
515
- this.onStopSpeaking = (async () => {
761
+ };
762
+ this.onStopSpeaking = async () => {
516
763
  if (this.state.isMuted) return;
517
764
  try {
518
765
  this.recorder?.stop();
@@ -522,70 +769,21 @@ var MicRecorder = class extends EventEmitter3 {
522
769
  this.changeState({ isSpeaking: false });
523
770
  this.emit("StopSpeaking");
524
771
  this.speakingConfirmed = false;
525
- }).bind(this);
526
- this.state = defaultMicRecorderState;
527
- this.vad = getVAD(vadConfig);
528
- }
529
- async start(stream) {
530
- if (this.state.isStarted) {
531
- throw new Error("MicRecorder is already started");
532
- }
533
- try {
534
- this.changeState({
535
- isStarting: true,
536
- isMuted: false,
537
- isSpeaking: false
538
- });
539
- const delayedStream = createDelayedStream(
540
- stream,
541
- this.vad.delay / 1e3 + 0.05
542
- );
543
- this.delayedStream = delayedStream;
544
- if (this.recorder) {
545
- this.recorder.stop();
546
- }
547
- this.recorder = new window.MediaRecorder(delayedStream, {
548
- mimeType: this.audioInfo.mimeType,
549
- audioBitsPerSecond: 128e3
550
- });
551
- this.recorder.ondataavailable = this.onDataAvailable.bind(this);
552
- await this.startSpeakingDetection(stream);
553
- this.changeState({
554
- isStarting: false,
555
- isStarted: true
556
- });
557
- } catch (err) {
558
- this.stop();
559
- console.error(err);
560
- }
561
- }
562
- mute() {
563
- this.changeState({ isMuted: true, isSpeaking: false });
564
- this.recorder?.stop();
565
- }
566
- unmute() {
567
- this.changeState({ isMuted: false });
568
- }
569
- stop() {
570
- this.changeState({
571
- isStarting: false,
572
- isStarted: false,
573
- isMuted: false,
574
- isSpeaking: false
575
- });
576
- try {
577
- this.stopSpeakingDetection();
578
- if (this.recorder) {
579
- this.recorder.stop();
580
- this.recorder = void 0;
772
+ };
773
+ this.onDataAvailable = async (blobEvent) => {
774
+ if (this.state.isMuted) return;
775
+ if (!this.speakingConfirmed) {
776
+ this.queuedChunks.push(blobEvent.data);
777
+ return;
581
778
  }
582
- if (this.delayedStream) {
583
- stopStream(this.delayedStream);
584
- this.delayedStream = void 0;
779
+ for (const chunk of this.queuedChunks) {
780
+ this.emit("Chunk", chunk);
585
781
  }
586
- } catch (err) {
587
- console.error(err);
588
- }
782
+ this.queuedChunks.length = 0;
783
+ this.emit("Chunk", blobEvent.data);
784
+ };
785
+ this.state = defaultMicRecorderState;
786
+ this.vad = getVAD(vadConfig);
589
787
  }
590
788
  async startSpeakingDetection(stream) {
591
789
  if (this.vad.isStarted) {
@@ -604,18 +802,6 @@ var MicRecorder = class extends EventEmitter3 {
604
802
  this.vad.off("StopSpeaking", this.onStopSpeaking);
605
803
  await this.vad.stop();
606
804
  }
607
- async onDataAvailable(blobEvent) {
608
- if (this.state.isMuted) return;
609
- if (!this.speakingConfirmed) {
610
- this.queuedChunks.push(blobEvent.data);
611
- return;
612
- }
613
- for (const chunk of this.queuedChunks) {
614
- this.emit("Chunk", chunk);
615
- }
616
- this.queuedChunks.length = 0;
617
- this.emit("Chunk", blobEvent.data);
618
- }
619
805
  changeState(state) {
620
806
  const hasChanged = Object.keys(state).some(
621
807
  (key) => this.state[key] !== state[key]
@@ -636,580 +822,222 @@ var MicRecorder = class extends EventEmitter3 {
636
822
  }
637
823
  };
638
824
 
639
- // src/audio/speaker.ts
640
- import EventEmitter5 from "eventemitter3";
641
-
642
- // src/audio/player/SpeakerPlayer.ts
643
- import { EventEmitter as EventEmitter4 } from "eventemitter3";
644
- var SpeakerPlayer = class extends EventEmitter4 {
645
- constructor(outputNode) {
646
- super();
647
- this.outputNode = outputNode;
648
- this.isPlaying = false;
649
- const ctx = outputNode.context;
650
- if (!(ctx instanceof AudioContext)) {
651
- throw new Error("OutputNode must be connected to an AudioContext");
652
- }
653
- this.audioContext = ctx;
654
- }
655
- destroy() {
656
- this.removeAllListeners();
657
- }
658
- changeIsPlaying(isPlaying) {
659
- if (this.isPlaying == isPlaying) return;
660
- this.isPlaying = isPlaying;
661
- this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
662
- }
663
- };
664
-
665
- // src/audio/player/SpeakerConcatPlayer.ts
666
- var SpeakerConcatPlayer = class extends SpeakerPlayer {
667
- constructor() {
668
- super(...arguments);
669
- this.pendingBlobs = [];
670
- this.prevSourceDuration = 0;
671
- this.isConvertingBlob = false;
672
- }
673
- async setup() {
674
- }
675
- replaceSource() {
676
- if (!this.nextSource) return;
677
- const time = performance.now() / 1e3;
678
- const source = this.nextSource;
679
- source.connect(this.outputNode);
680
- source.onended = () => {
681
- if (source !== this.currentSource) return;
682
- this.prevSourceDuration = source.buffer?.duration ?? 0;
683
- this.sourceStartTime = void 0;
684
- this.currentSource = void 0;
685
- source.disconnect();
686
- if (this.nextSource) {
687
- this.replaceSource();
688
- } else {
689
- this.changeIsPlaying(false);
690
- }
691
- };
692
- const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
693
- source.start(0, offset);
694
- if (!this.sourceStartTime) {
695
- this.sourceStartTime = time;
696
- }
697
- this.currentSource?.stop();
698
- this.currentSource?.disconnect();
699
- this.currentSource = source;
700
- this.nextSource = void 0;
701
- }
702
- async convertNextBlob() {
703
- if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
704
- this.isConvertingBlob = true;
705
- const blob = this.pendingBlobs.shift();
706
- try {
707
- const arrayBuffer = await blob.arrayBuffer();
708
- this.currentBuffer = this.currentBuffer ? this.concatArrayBuffers(this.currentBuffer, arrayBuffer) : arrayBuffer;
709
- const audioBuffer = await this.audioContext.decodeAudioData(
710
- // Copy the buffer to avoid mutating the original
711
- this.copyArrayBuffer(this.currentBuffer)
712
- );
713
- const source = this.audioContext.createBufferSource();
714
- source.buffer = audioBuffer;
715
- this.nextSource = source;
716
- this.replaceSource();
717
- } catch (error) {
718
- console.error("Error converting blob", error);
719
- } finally {
720
- this.isConvertingBlob = false;
721
- }
722
- this.convertNextBlob();
723
- }
724
- concatArrayBuffers(buffer1, buffer2) {
725
- const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
726
- tmp.set(new Uint8Array(buffer1), 0);
727
- tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
728
- return tmp.buffer;
729
- }
730
- copyArrayBuffer(buffer) {
731
- const destination = new ArrayBuffer(buffer.byteLength);
732
- new Uint8Array(destination).set(new Uint8Array(buffer));
733
- return destination;
734
- }
735
- addBlob(blob) {
736
- this.changeIsPlaying(true);
737
- this.pendingBlobs.push(blob);
738
- this.convertNextBlob();
739
- }
740
- stop() {
741
- this.changeIsPlaying(false);
742
- this.pendingBlobs.length = 0;
743
- this.sourceStartTime = void 0;
744
- this.prevSourceDuration = 0;
745
- this.currentSource?.stop();
746
- this.currentSource?.disconnect();
747
- this.currentSource = void 0;
748
- this.nextSource = void 0;
749
- this.currentBuffer = void 0;
750
- this.isConvertingBlob = false;
751
- }
752
- destroy() {
753
- super.destroy();
754
- this.stop();
755
- }
756
- };
757
-
758
- // src/audio/player/SpeakerMediaSourcePlayer.ts
759
- var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
760
- constructor() {
761
- super(...arguments);
762
- this.isProcessingQueue = false;
763
- this.pendingBlobs = [];
764
- }
765
- async setup() {
766
- if (this.audioElement || this.mediaSource) return;
767
- this.audioElement = new Audio();
768
- this.audioElement.autoplay = true;
769
- this.audioElement.muted = true;
770
- this.mediaSource = new MediaSource();
771
- if (!this.mediaSource) {
772
- throw new Error("Failed to create MediaSource");
773
- }
774
- this.audioElement.src = URL.createObjectURL(this.mediaSource);
775
- await new Promise((resolve) => {
776
- this.mediaSource?.addEventListener("sourceopen", () => {
777
- const sourceBuffer = this.mediaSource?.addSourceBuffer("audio/mpeg");
778
- if (!sourceBuffer) throw new Error("Failed to create SourceBuffer");
779
- this.sourceBuffer = sourceBuffer;
780
- sourceBuffer.addEventListener("updateend", () => this.processQueue());
781
- this.processQueue();
782
- resolve();
783
- });
784
- });
785
- await new Promise((resolve) => {
786
- const onTimeUpdate = () => {
787
- this.audioElement?.removeEventListener("timeupdate", onTimeUpdate);
788
- if (!this.audioElement) {
789
- throw new Error("Audio element or audio context not found");
790
- }
791
- const mediaStream = this.audioElement.captureStream();
792
- if (mediaStream.getAudioTracks().length === 0) return;
793
- this.mediaStream = mediaStream;
794
- this.sourceNode = this.audioContext.createMediaStreamSource(mediaStream);
795
- this.sourceNode.connect(this.outputNode);
796
- this.audioElement.currentTime = 0;
797
- resolve();
798
- };
799
- this.audioElement?.addEventListener("timeupdate", onTimeUpdate);
800
- });
801
- }
802
- async processQueue() {
803
- if (!this.sourceBuffer || !this.mediaSource || this.pendingBlobs.length === 0 || this.isProcessingQueue || this.sourceBuffer.updating) {
804
- return;
805
- }
806
- this.isProcessingQueue = true;
807
- const blob = this.pendingBlobs.shift();
808
- try {
809
- const arrayBuffer = await blob.arrayBuffer();
810
- if (this.isProcessingQueue) {
811
- this.sourceBuffer.appendBuffer(arrayBuffer);
812
- }
813
- } catch (error) {
814
- console.error("Error appending buffer", error);
815
- } finally {
816
- this.isProcessingQueue = false;
817
- }
818
- }
819
- addBlob(blob) {
820
- this.pendingBlobs.push(blob);
821
- if (this.sourceBuffer && !this.sourceBuffer.updating && !this.isProcessingQueue) {
822
- this.processQueue();
823
- }
824
- }
825
- stop() {
826
- this.destroy();
827
- this.setup();
828
- }
829
- destroy() {
830
- super.destroy();
831
- if (this.sourceNode) {
832
- this.sourceNode.disconnect();
833
- this.sourceNode = void 0;
834
- }
835
- this.pendingBlobs.length = 0;
836
- if (this.mediaSource) {
837
- try {
838
- if (this.mediaSource.readyState === "open") {
839
- this.mediaSource.endOfStream();
840
- }
841
- } catch (error) {
842
- console.error("Error cleaning up media source", error);
843
- }
844
- }
845
- if (this.audioElement) {
846
- this.audioElement.pause();
847
- this.audioElement.src = "";
848
- this.audioElement = void 0;
849
- }
850
- if (this.mediaStream) {
851
- stopStream(this.mediaStream);
852
- this.mediaStream = void 0;
853
- }
854
- this.mediaSource = void 0;
855
- this.sourceBuffer = void 0;
856
- }
857
- };
858
- SpeakerMediaSourcePlayer.isCompatible = typeof MediaSource !== "undefined" && MediaSource.isTypeSupported("audio/mpeg");
859
-
860
- // src/audio/player/SpeakerSequencePlayer.ts
861
- var SpeakerSequencePlayer = class extends SpeakerPlayer {
862
- constructor() {
863
- super(...arguments);
864
- this.pendingBlobs = [];
865
- this.pendingSources = [];
866
- this.isConvertingBlob = false;
867
- }
868
- async setup() {
869
- }
870
- processQueue() {
871
- if (this.pendingSources.length === 0 || this.currentSource) return;
872
- const source = this.pendingSources.shift();
873
- this.currentSource = source;
874
- source.connect(this.outputNode);
875
- source.onended = () => {
876
- source.disconnect();
877
- this.currentSource = void 0;
878
- if (this.pendingSources.length === 0) {
879
- this.changeIsPlaying(false);
880
- } else {
881
- this.processQueue();
882
- }
883
- };
884
- source.start(0);
885
- }
886
- async convertNextBlob() {
887
- if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
888
- this.isConvertingBlob = true;
889
- const blob = this.pendingBlobs.shift();
890
- try {
891
- const arrayBuffer = await blob.arrayBuffer();
892
- const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
893
- const source = this.audioContext.createBufferSource();
894
- source.buffer = audioBuffer;
895
- this.pendingSources.push(source);
896
- this.processQueue();
897
- } catch (error) {
898
- console.error("Error converting blob", error);
899
- } finally {
900
- this.isConvertingBlob = false;
901
- }
902
- this.convertNextBlob();
903
- }
904
- addBlob(blob) {
905
- this.changeIsPlaying(true);
906
- this.pendingBlobs.push(blob);
907
- this.convertNextBlob();
908
- }
909
- stop() {
910
- this.pendingBlobs.length = 0;
911
- this.pendingSources.length = 0;
912
- this.currentSource?.stop();
913
- this.currentSource?.disconnect();
914
- this.currentSource = void 0;
915
- }
916
- destroy() {
917
- super.destroy();
918
- this.stop();
919
- }
920
- };
921
-
922
- // src/audio/speaker.ts
923
- var speaker;
924
- var Speaker = class extends EventEmitter5 {
925
- constructor() {
926
- super();
927
- this.isPlaying = false;
928
- this.streamingEnabled = false;
929
- this.analyser = new AudioAnalyser(audioContext);
930
- this.audioElement = new Audio();
931
- this.audioElement.autoplay = true;
932
- const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
933
- if (speakerId) {
934
- this.setSinkId(speakerId).catch((error) => {
935
- console.error("Failed to set sink ID:", error);
936
- });
937
- }
938
- this.setupSpeakerPlayer();
939
- const destinationNode = audioContext.createMediaStreamDestination();
940
- this.analyser.node.connect(destinationNode);
941
- this.audioElement.srcObject = destinationNode.stream;
942
- }
943
- async setupSpeakerPlayer() {
944
- try {
945
- this.player?.destroy();
946
- this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
947
- await this.player?.setup();
948
- this.player.on("StartPlaying", () => this.changeIsPlaying(true));
949
- this.player.on("StopPlaying", () => this.changeIsPlaying(false));
950
- } catch (error) {
951
- console.error("Error setting up speaker player", error);
952
- }
953
- }
954
- get isStreamingEnabled() {
955
- return this.streamingEnabled;
956
- }
957
- changeIsPlaying(isPlaying) {
958
- if (this.isPlaying == isPlaying) return;
959
- this.isPlaying = isPlaying;
960
- this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
961
- }
962
- async enableStreaming() {
963
- if (this.streamingEnabled) return;
964
- this.streamingEnabled = true;
965
- await this.setupSpeakerPlayer();
966
- }
967
- async disableStreaming() {
968
- if (!this.streamingEnabled) return;
969
- this.streamingEnabled = false;
970
- await this.setupSpeakerPlayer();
971
- }
972
- canChangeDevice() {
973
- return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
974
- }
975
- async setSinkId(speakerId) {
976
- if (speakerId && this.canChangeDevice() && this.audioElement) {
977
- return this.audioElement.setSinkId(speakerId);
978
- }
979
- }
980
- async changeDevice(speakerId) {
981
- if (!this.canChangeDevice()) return;
982
- try {
983
- await this.setSinkId(speakerId);
984
- localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
985
- } catch (error) {
986
- console.error(`Error setting Audio Output to ${speakerId}`, error);
987
- }
988
- }
989
- pauseAudio() {
990
- if (this.audioElement) {
991
- this.audioElement.pause();
992
- }
993
- }
994
- resumeAudio() {
995
- if (this.audioElement) {
996
- this.audioElement.play();
997
- }
998
- }
999
- async playAudio(blob) {
1000
- if (!this.audioElement || !this.player) return;
1001
- try {
1002
- this.player.addBlob(blob);
1003
- if (this.audioElement.paused) {
1004
- this.audioElement.play();
1005
- }
1006
- } catch (error) {
1007
- console.error("Error playing audio blob", error, blob);
1008
- }
1009
- }
1010
- stopAudio() {
1011
- this.audioElement?.pause();
1012
- this.player?.stop();
1013
- }
1014
- };
825
+ // src/audio/index.ts
826
+ if (!window.micdropMic) {
827
+ window.micdropMic = new Mic();
828
+ }
829
+ var Mic2 = window.micdropMic;
1015
830
  if (!window.micdropSpeaker) {
1016
831
  window.micdropSpeaker = new Speaker();
1017
832
  }
1018
- speaker = window.micdropSpeaker;
833
+ var Speaker2 = window.micdropSpeaker;
1019
834
 
1020
- // src/call/CallClient.ts
835
+ // src/client/MicdropClient.ts
1021
836
  import EventEmitter6 from "eventemitter3";
1022
837
 
1023
- // src/call/CallClientError.ts
1024
- var CallClientErrorCode = /* @__PURE__ */ ((CallClientErrorCode2) => {
1025
- CallClientErrorCode2["Mic"] = "Mic";
1026
- CallClientErrorCode2["Unauthorized"] = "Unauthorized";
1027
- CallClientErrorCode2["Error"] = "Error";
1028
- return CallClientErrorCode2;
1029
- })(CallClientErrorCode || {});
1030
- var CallClientError = class extends Error {
1031
- constructor(code = "Error" /* Error */) {
1032
- super();
838
+ // src/client/MicdropClientError.ts
839
+ var MicdropClientErrorCode = /* @__PURE__ */ ((MicdropClientErrorCode2) => {
840
+ MicdropClientErrorCode2["MissingUrl"] = "MissingUrl";
841
+ MicdropClientErrorCode2["Mic"] = "Mic";
842
+ MicdropClientErrorCode2["Connection"] = "Connection";
843
+ MicdropClientErrorCode2["InternalServer"] = "InternalServer";
844
+ MicdropClientErrorCode2["BadRequest"] = "BadRequest";
845
+ MicdropClientErrorCode2["Unauthorized"] = "Unauthorized";
846
+ MicdropClientErrorCode2["NotFound"] = "NotFound";
847
+ MicdropClientErrorCode2["Unknown"] = "Unknown";
848
+ return MicdropClientErrorCode2;
849
+ })(MicdropClientErrorCode || {});
850
+ var MicdropClientError = class extends Error {
851
+ constructor(code = "Unknown" /* Unknown */, message) {
852
+ super(message);
853
+ this.name = `MicdropClientError:${code}`;
1033
854
  this.code = code;
1034
855
  }
1035
856
  };
857
+ function getClientErrorFromWSCloseEvent(event) {
858
+ const code = getClientErrorFromWSCloseEventCode(event.code);
859
+ return code && new MicdropClientError(code, event.reason);
860
+ }
861
+ function getClientErrorFromWSCloseEventCode(code) {
862
+ if (code === 1011) {
863
+ return "InternalServer" /* InternalServer */;
864
+ } else if (code >= 1001 && code < 1011 && code !== 1005) {
865
+ return "Connection" /* Connection */;
866
+ } else if (code === 4401) {
867
+ return "Unauthorized" /* Unauthorized */;
868
+ } else if (code === 4404) {
869
+ return "NotFound" /* NotFound */;
870
+ } else if (code === 4400) {
871
+ return "BadRequest" /* BadRequest */;
872
+ } else if (code >= 4e3) {
873
+ return "Unknown" /* Unknown */;
874
+ } else {
875
+ return void 0;
876
+ }
877
+ }
1036
878
 
1037
- // src/call/CallClient.ts
1038
- var CallClient = class _CallClient extends EventEmitter6 {
1039
- constructor(options) {
879
+ // src/client/MicdropClient.ts
880
+ var MicdropClient = class extends EventEmitter6 {
881
+ constructor(options = {}) {
1040
882
  super();
1041
883
  this.options = options;
1042
884
  this.conversation = [];
1043
- this.debug = false;
885
+ this.speakerDevices = [];
886
+ this.micDevices = [];
1044
887
  this.startTime = 0;
1045
888
  this._isProcessing = false;
1046
889
  this._isPaused = false;
1047
- this.vad = getVAD(this.options?.vad);
1048
- this.onSpeakerStartPlaying = this.onSpeakerStartPlaying.bind(this);
1049
- this.onSpeakerStopPlaying = this.onSpeakerStopPlaying.bind(this);
1050
- speaker.on("StartPlaying", this.onSpeakerStartPlaying);
1051
- speaker.on("StopPlaying", this.onSpeakerStopPlaying);
1052
- }
1053
- static getInstance(options) {
1054
- if (!window.micdropCallClient) {
1055
- window.micdropCallClient = new _CallClient(options);
1056
- }
1057
- return window.micdropCallClient;
1058
- }
1059
- get isStarted() {
1060
- return this.isWSStarted && this.micRecorder?.state.isStarted || false;
1061
- }
1062
- get isStarting() {
1063
- return this.isWSStarting || this.micRecorder?.state.isStarting || false;
1064
- }
1065
- get isPaused() {
1066
- return this._isPaused;
1067
- }
1068
- get isProcessing() {
1069
- return this._isProcessing && !this.isPaused;
1070
- }
1071
- get isListening() {
1072
- return this.isMicStarted && !this.isPaused && !this.isProcessing && !this.isMicMuted && !this.isUserSpeaking && !this.isAssistantSpeaking;
1073
- }
1074
- get isWSStarted() {
1075
- return this.ws?.readyState === WebSocket.OPEN;
1076
- }
1077
- get isWSStarting() {
1078
- return this.ws?.readyState === WebSocket.CONNECTING;
1079
- }
1080
- get isMicStarted() {
1081
- return !!this.micStream;
1082
- }
1083
- get isMicMuted() {
1084
- return this.micRecorder?.state.isMuted ?? false;
1085
- }
1086
- get isUserSpeaking() {
1087
- return this.micRecorder?.state.isSpeaking ?? false;
1088
- }
1089
- get isAssistantSpeaking() {
1090
- return speaker.isPlaying;
1091
- }
1092
- async start() {
1093
- this.startTime = Date.now();
1094
- this.conversation = [];
1095
- this._isProcessing = true;
1096
- await this.startMic(void 0, true);
1097
- await this.startWS();
1098
- }
1099
- async stop() {
1100
- try {
1101
- this.stopWS();
1102
- } catch (error) {
1103
- console.error("[CallClient] stop WS", error);
1104
- }
1105
- try {
1106
- this.micRecorder?.stop();
1107
- this.stopMic();
1108
- } catch (error) {
1109
- console.error("[CallClient] stop mic", error);
1110
- }
1111
- }
1112
- pause() {
1113
- if (this.isPaused) return;
1114
- this.micRecorder?.mute();
1115
- this._isPaused = true;
1116
- this._isProcessing = false;
1117
- this.notifyStateChange();
1118
- speaker.pauseAudio();
1119
- this.ws?.send("Mute" /* Mute */);
1120
- }
1121
- resume() {
1122
- if (!this.isPaused) return;
1123
- this.micRecorder?.unmute();
1124
- this._isPaused = false;
1125
- this.notifyStateChange();
1126
- }
1127
- async destroy() {
1128
- this.stop();
1129
- speaker.off("StartPlaying", this.onSpeakerStartPlaying);
1130
- speaker.off("StopPlaying", this.onSpeakerStopPlaying);
1131
- }
1132
- async startMic(deviceId, record = true) {
1133
- try {
1134
- if (!this.micRecorder) {
1135
- this.micRecorder = new MicRecorder(this.vad);
1136
- this.micRecorder.on("StateChange", () => {
1137
- this.notifyStateChange();
1138
- });
1139
- this.micRecorder.on("Chunk", (blob) => {
1140
- this.log(`[Mic] Chunk`, blob);
1141
- this.ws?.send(blob);
1142
- });
1143
- this.micRecorder.on("StartSpeaking", () => {
1144
- this.log("[Mic] Start speaking");
1145
- this.ws?.send("StartSpeaking" /* StartSpeaking */);
1146
- this._isProcessing = false;
1147
- this.notifyStateChange();
1148
- speaker.stopAudio();
1149
- });
1150
- this.micRecorder.on("StopSpeaking", () => {
1151
- this.log("[Mic] Stop speaking");
1152
- this.ws?.send("StopSpeaking" /* StopSpeaking */);
1153
- if (this.isWSStarted) {
1154
- this._isProcessing = true;
1155
- }
1156
- this.notifyStateChange();
1157
- });
890
+ this.start = async (options) => {
891
+ this.error = void 0;
892
+ this.options = { ...this.options, ...options };
893
+ this.startTime = Date.now();
894
+ this.conversation = [];
895
+ this._isProcessing = true;
896
+ this._isPaused = false;
897
+ await this.startMic({ record: true });
898
+ await this.startWS();
899
+ };
900
+ this.stop = async () => {
901
+ this._isProcessing = false;
902
+ this._isPaused = false;
903
+ try {
904
+ this.stopWS();
905
+ } catch (error) {
906
+ console.error("[MicdropClient] Error stopping WebSocket", error);
1158
907
  }
1159
- const isRecorderStarted = this.micRecorder.state.isStarted;
1160
- if (isRecorderStarted) {
908
+ try {
1161
909
  this.micRecorder?.stop();
910
+ this.stopMic();
911
+ } catch (error) {
912
+ console.error("[MicdropClient] Error stopping microphone", error);
1162
913
  }
1163
- this.micStream = await mic.start(deviceId);
914
+ Speaker2.stopAudio();
915
+ };
916
+ this.pause = () => {
917
+ if (this.isPaused) return;
918
+ this.micRecorder?.mute();
919
+ this._isPaused = true;
920
+ this._isProcessing = false;
1164
921
  this.notifyStateChange();
1165
- if (isRecorderStarted || record) {
1166
- await this.micRecorder.start(this.micStream);
922
+ Speaker2.stopAudio();
923
+ this.ws?.send("Mute" /* Mute */);
924
+ };
925
+ this.resume = () => {
926
+ if (!this.isPaused) return;
927
+ this.micRecorder?.unmute();
928
+ this._isPaused = false;
929
+ this.notifyStateChange();
930
+ };
931
+ this.startMic = async ({
932
+ vad,
933
+ deviceId,
934
+ record = true
935
+ }) => {
936
+ this.error = void 0;
937
+ if (vad) {
938
+ this.options.vad = vad;
1167
939
  }
1168
- } catch (error) {
1169
- console.error("[CallClient] startMic", error);
1170
- this.emit("Error", new CallClientError("Mic" /* Mic */));
1171
- this.stop();
1172
- }
1173
- }
1174
- stopMic() {
1175
- this.micRecorder?.stop();
1176
- this.micRecorder = void 0;
1177
- mic.stop();
1178
- this.micStream = void 0;
1179
- this.notifyStateChange();
1180
- }
1181
- async startWS() {
1182
- if (this.ws) {
1183
- throw new Error("[CallClient] startWS: WebSocket is already started");
1184
- }
1185
- if (!this.isMicStarted) {
1186
- throw new Error("[CallClient] startWS: Microphone is not started");
1187
- }
1188
- if (!this.url) {
1189
- throw new Error("[CallClient] startWS: URL is not set");
1190
- }
1191
- this.ws = new WebSocket(this.url);
1192
- this.ws.binaryType = "blob";
1193
- this.notifyStateChange();
1194
- this.ws.onopen = () => {
1195
- this.log("[WS] Opened");
940
+ try {
941
+ if (!this.micRecorder) {
942
+ this.micRecorder = new MicRecorder(this.options.vad);
943
+ this.micRecorder.on("StateChange", () => {
944
+ this.notifyStateChange();
945
+ });
946
+ this.micRecorder.on("Chunk", (blob) => {
947
+ this.log(`[MicdropClient] User audio chunk`, blob);
948
+ this.ws?.send(blob);
949
+ });
950
+ this.micRecorder.on("StartSpeaking", () => {
951
+ this.log("User start speaking");
952
+ this.ws?.send("StartSpeaking" /* StartSpeaking */);
953
+ this._isProcessing = false;
954
+ this.notifyStateChange();
955
+ Speaker2.stopAudio();
956
+ });
957
+ this.micRecorder.on("StopSpeaking", () => {
958
+ this.log("User stop speaking");
959
+ this.ws?.send("StopSpeaking" /* StopSpeaking */);
960
+ if (this.isWSStarted) {
961
+ this._isProcessing = true;
962
+ }
963
+ this.notifyStateChange();
964
+ });
965
+ }
966
+ const isRecorderStarted = this.micRecorder.state.isStarted;
967
+ if (isRecorderStarted) {
968
+ this.micRecorder?.stop();
969
+ }
970
+ const micStream = await Mic2.start(deviceId);
971
+ this.micStream = micStream;
972
+ if (isRecorderStarted || record) {
973
+ await this.micRecorder.start(micStream);
974
+ }
975
+ await this.updateDevices();
976
+ await Speaker2.start();
977
+ this.notifyStateChange();
978
+ } catch (error) {
979
+ this.setError(
980
+ new MicdropClientError(
981
+ "Mic" /* Mic */,
982
+ error?.message
983
+ )
984
+ );
985
+ await this.stop();
986
+ throw error;
987
+ }
988
+ };
989
+ this.changeMicDevice = async (deviceId) => {
990
+ await this.startMic({ deviceId });
991
+ };
992
+ this.changeSpeakerDevice = async (deviceId) => {
993
+ await Speaker2.changeDevice(deviceId);
994
+ this.notifyStateChange();
995
+ };
996
+ this.updateDevices = async () => {
997
+ const rawDevices = await navigator.mediaDevices.enumerateDevices();
998
+ if (rawDevices.length === 0) return;
999
+ const defaultDevices = rawDevices.filter(
1000
+ (device) => device.deviceId === "default"
1001
+ );
1002
+ const devices = rawDevices.filter((device) => device.deviceId !== "default");
1003
+ const defaultDevicesIndexes = defaultDevices.map(
1004
+ (device) => devices.findIndex(
1005
+ (d) => d.groupId === device.groupId && d.kind === device.kind
1006
+ )
1007
+ ).filter((index) => index !== -1);
1008
+ for (const index of defaultDevicesIndexes) {
1009
+ const device = devices[index];
1010
+ devices.splice(index, 1);
1011
+ devices.unshift(device);
1012
+ }
1013
+ this.micDevices = devices.filter((device) => device.kind === "audioinput");
1014
+ this.speakerDevices = devices.filter(
1015
+ (device) => device.kind === "audiooutput"
1016
+ );
1017
+ Speaker2.devices = this.speakerDevices;
1018
+ this.notifyStateChange();
1019
+ };
1020
+ this.onWSOpen = () => {
1021
+ this.log("WebSocket opened");
1196
1022
  this.notifyStateChange();
1197
- if (this.params) {
1198
- this.ws?.send(JSON.stringify(this.params));
1023
+ if (this.options.params) {
1024
+ this.ws?.send(JSON.stringify(this.options.params));
1199
1025
  }
1200
1026
  };
1201
- this.ws.onmessage = (event) => {
1202
- this.log("[WS]", event.data);
1027
+ this.onWSMessage = (event) => {
1028
+ this.log("Received message:", event.data);
1203
1029
  if (event.data instanceof Blob) {
1204
- speaker.playAudio(event.data);
1030
+ if (this.isPaused || this.isUserSpeaking) return;
1031
+ Speaker2.playAudio(event.data);
1205
1032
  this._isProcessing = false;
1206
1033
  this.notifyStateChange();
1207
1034
  } else if (typeof event.data !== "string") {
1208
- console.warn(`[WS] Unknown message type: ${event.data}`);
1035
+ console.warn(`[MicdropClient] Unknown message type: ${event.data}`);
1209
1036
  } else if (event.data.startsWith("Message" /* Message */)) {
1210
1037
  const message = JSON.parse(
1211
1038
  event.data.substring("Message" /* Message */.length + 1)
1212
1039
  );
1040
+ this._isProcessing = false;
1213
1041
  this.addMessage(message);
1214
1042
  } else if (event.data === "EndCall" /* EndCall */) {
1215
1043
  setTimeout(() => {
@@ -1231,92 +1059,201 @@ var CallClient = class _CallClient extends EventEmitter6 {
1231
1059
  this._isProcessing = false;
1232
1060
  this.notifyStateChange();
1233
1061
  }
1234
- } else if (event.data === "EnableSpeakerStreaming" /* EnableSpeakerStreaming */) {
1235
- speaker.enableStreaming();
1236
1062
  }
1237
1063
  };
1238
- this.ws.onclose = (event) => {
1239
- this.log("[WS] Closed", event);
1064
+ this.onWSClose = (event) => {
1065
+ this.log("WebSocket closed", event);
1240
1066
  this.stop();
1241
- if (event.code >= 1001 && event.code <= 1011 && event.code !== 1005) {
1242
- this.emit("Error", new CallClientError());
1243
- } else if (event.code === 4401) {
1244
- this.emit(
1245
- "Error",
1246
- new CallClientError("Unauthorized" /* Unauthorized */)
1247
- );
1248
- } else if (event.code >= 4e3) {
1249
- this.emit("Error", new CallClientError());
1067
+ const error = getClientErrorFromWSCloseEvent(event);
1068
+ if (error) {
1069
+ this.setError(error);
1250
1070
  }
1251
1071
  };
1252
- this.ws.onerror = (event) => {
1253
- console.error("[CallClient] [WS] Error:", event);
1072
+ this.onWSError = (event) => {
1073
+ this.setError(new MicdropClientError("Connection" /* Connection */));
1254
1074
  this.stop();
1255
1075
  };
1076
+ this.onSpeakerStartPlaying = () => {
1077
+ this.log("Speaker started");
1078
+ if (this.options.disableInterruption && !this.isPaused) {
1079
+ this.micRecorder?.mute();
1080
+ }
1081
+ this.notifyStateChange();
1082
+ };
1083
+ this.onSpeakerStopPlaying = () => {
1084
+ this.log("Speaker stopped");
1085
+ if (this.options.disableInterruption && !this.isPaused) {
1086
+ this.micRecorder?.unmute();
1087
+ }
1088
+ this.notifyStateChange();
1089
+ };
1090
+ Speaker2.on("StartPlaying", this.onSpeakerStartPlaying);
1091
+ Speaker2.on("StopPlaying", this.onSpeakerStopPlaying);
1092
+ navigator.mediaDevices.addEventListener("devicechange", this.updateDevices);
1256
1093
  }
1257
- stopWS() {
1258
- if (!this.ws) return;
1259
- this.ws.close();
1260
- this.ws = void 0;
1094
+ get vad() {
1095
+ return this.micRecorder?.vad;
1096
+ }
1097
+ get isStarted() {
1098
+ return this.isWSStarted && this.micRecorder?.state.isStarted || false;
1099
+ }
1100
+ get isStarting() {
1101
+ return this.isWSStarting || this.micRecorder?.state.isStarting || false;
1102
+ }
1103
+ get isPaused() {
1104
+ return this._isPaused;
1105
+ }
1106
+ get isProcessing() {
1107
+ return this._isProcessing && !this.isPaused;
1108
+ }
1109
+ get isListening() {
1110
+ return this.isMicStarted && !this.isPaused && !this.isProcessing && !this.isMicMuted && !this.isUserSpeaking && !this.isAssistantSpeaking;
1111
+ }
1112
+ get isWSStarted() {
1113
+ return this.ws?.readyState === WebSocket.OPEN;
1114
+ }
1115
+ get isWSStarting() {
1116
+ return this.ws?.readyState === WebSocket.CONNECTING;
1117
+ }
1118
+ get isMicStarted() {
1119
+ return !!this.micStream;
1120
+ }
1121
+ get isMicMuted() {
1122
+ return this.micRecorder?.state.isMuted ?? false;
1123
+ }
1124
+ get isUserSpeaking() {
1125
+ return this.micRecorder?.state.isSpeaking ?? false;
1126
+ }
1127
+ get isAssistantSpeaking() {
1128
+ return Speaker2.isPlaying;
1129
+ }
1130
+ get micDeviceId() {
1131
+ return Mic2.deviceId;
1132
+ }
1133
+ get speakerDeviceId() {
1134
+ return Speaker2.deviceId;
1135
+ }
1136
+ get state() {
1137
+ return {
1138
+ isStarting: this.isStarting,
1139
+ isStarted: this.isStarted,
1140
+ isPaused: this.isPaused,
1141
+ isListening: this.isListening,
1142
+ isProcessing: this.isProcessing,
1143
+ isUserSpeaking: this.isUserSpeaking,
1144
+ isAssistantSpeaking: this.isAssistantSpeaking,
1145
+ isMicStarted: this.isMicStarted,
1146
+ isMicMuted: this.isMicMuted,
1147
+ conversation: this.conversation,
1148
+ error: this.error,
1149
+ micDeviceId: this.micDeviceId,
1150
+ speakerDeviceId: this.speakerDeviceId,
1151
+ micDevices: this.micDevices,
1152
+ speakerDevices: this.speakerDevices
1153
+ };
1154
+ }
1155
+ stopMic() {
1156
+ this.micRecorder?.stop();
1157
+ this.micRecorder = void 0;
1158
+ Mic2.stop();
1159
+ this.micStream = void 0;
1261
1160
  this.notifyStateChange();
1262
1161
  }
1263
- onSpeakerStartPlaying() {
1264
- this.log("[Speaker] Start speaking");
1265
- if (this.options?.disableInterruption && !this.isPaused) {
1266
- this.micRecorder?.mute();
1162
+ async startWS() {
1163
+ try {
1164
+ if (this.ws) {
1165
+ this.log("WebSocket is already started");
1166
+ return;
1167
+ }
1168
+ if (!this.isMicStarted) {
1169
+ throw new MicdropClientError("Mic" /* Mic */);
1170
+ }
1171
+ if (!this.options.url) {
1172
+ throw new MicdropClientError("MissingUrl" /* MissingUrl */);
1173
+ }
1174
+ this.ws = new WebSocket(this.options.url);
1175
+ this.ws.binaryType = "blob";
1176
+ this.notifyStateChange();
1177
+ this.ws.onopen = this.onWSOpen;
1178
+ this.ws.onmessage = this.onWSMessage;
1179
+ this.ws.onclose = this.onWSClose;
1180
+ this.ws.onerror = this.onWSError;
1181
+ } catch (error) {
1182
+ this.setError(
1183
+ new MicdropClientError(
1184
+ "Connection" /* Connection */,
1185
+ error?.message
1186
+ )
1187
+ );
1188
+ await this.stop();
1189
+ throw error;
1267
1190
  }
1268
- this.notifyStateChange();
1269
1191
  }
1270
- onSpeakerStopPlaying() {
1271
- this.log("[Speaker] Stop speaking");
1272
- if (this.options?.disableInterruption && !this.isPaused) {
1273
- this.micRecorder?.unmute();
1192
+ stopWS() {
1193
+ if (!this.ws) return;
1194
+ if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {
1195
+ this.ws.close();
1274
1196
  }
1197
+ this.ws = void 0;
1275
1198
  this.notifyStateChange();
1276
1199
  }
1277
1200
  addMessage(message) {
1278
1201
  this.conversation.push(message);
1279
1202
  this.notifyStateChange();
1280
1203
  }
1204
+ setError(error) {
1205
+ console.error("[MicdropClient] Error:", error);
1206
+ this.error = error;
1207
+ this.notifyStateChange();
1208
+ this.emit("Error", error);
1209
+ }
1281
1210
  notifyStateChange() {
1282
- this.emit("StateChange");
1211
+ this.emit("StateChange", this.state);
1283
1212
  }
1284
1213
  log(...message) {
1285
- if (!this.debug) return;
1286
- console.log(`[Debug ${Date.now() - this.startTime}]`, ...message);
1214
+ if (!this.options.debugLog) return;
1215
+ console.log(`[MicdropClient ${Date.now() - this.startTime}]`, ...message);
1287
1216
  }
1288
1217
  };
1289
1218
 
1219
+ // src/Micdrop.ts
1220
+ if (!window.micdropClient) {
1221
+ window.micdropClient = new MicdropClient();
1222
+ }
1223
+ var Micdrop = window.micdropClient;
1224
+
1290
1225
  // ../server/src/types.ts
1291
- var CallClientCommands = /* @__PURE__ */ ((CallClientCommands2) => {
1292
- CallClientCommands2["StartSpeaking"] = "StartSpeaking";
1293
- CallClientCommands2["StopSpeaking"] = "StopSpeaking";
1294
- CallClientCommands2["Mute"] = "Mute";
1295
- return CallClientCommands2;
1296
- })(CallClientCommands || {});
1297
- var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
1298
- CallServerCommands2["Message"] = "Message";
1299
- CallServerCommands2["CancelLastAssistantMessage"] = "CancelLastAssistantMessage";
1300
- CallServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
1301
- CallServerCommands2["SkipAnswer"] = "SkipAnswer";
1302
- CallServerCommands2["EnableSpeakerStreaming"] = "EnableSpeakerStreaming";
1303
- CallServerCommands2["EndCall"] = "EndCall";
1304
- return CallServerCommands2;
1305
- })(CallServerCommands || {});
1226
+ var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
1227
+ MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
1228
+ MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
1229
+ MicdropClientCommands2["Mute"] = "Mute";
1230
+ return MicdropClientCommands2;
1231
+ })(MicdropClientCommands || {});
1232
+ var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
1233
+ MicdropServerCommands2["Message"] = "Message";
1234
+ MicdropServerCommands2["CancelLastAssistantMessage"] = "CancelLastAssistantMessage";
1235
+ MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
1236
+ MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
1237
+ MicdropServerCommands2["EndCall"] = "EndCall";
1238
+ return MicdropServerCommands2;
1239
+ })(MicdropServerCommands || {});
1306
1240
  export {
1307
- CallClient,
1308
- CallClientCommands,
1309
- CallClientError,
1310
- CallClientErrorCode,
1311
- CallServerCommands,
1312
1241
  LocalStorageKeys,
1313
- mic as Mic,
1242
+ Mic2 as Mic,
1314
1243
  MicRecorder,
1244
+ Micdrop,
1245
+ MicdropClient,
1246
+ MicdropClientCommands,
1247
+ MicdropClientError,
1248
+ MicdropClientErrorCode,
1249
+ MicdropServerCommands,
1315
1250
  MultipleVAD,
1316
1251
  SileroVAD,
1317
- speaker as Speaker,
1252
+ Speaker2 as Speaker,
1318
1253
  VAD,
1319
1254
  VADStatus,
1320
- VolumeVAD
1255
+ VolumeVAD,
1256
+ getClientErrorFromWSCloseEvent,
1257
+ getClientErrorFromWSCloseEventCode
1321
1258
  };
1322
1259
  //# sourceMappingURL=index.mjs.map