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