@micdrop/client 1.9.0 → 2.0.1

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