@micdrop/client 1.8.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -636,16 +636,30 @@ var MicRecorder = class extends EventEmitter3 {
636
636
  }
637
637
  };
638
638
 
639
+ // src/audio/speaker.ts
640
+ import EventEmitter5 from "eventemitter3";
641
+
639
642
  // src/audio/player/SpeakerPlayer.ts
640
- var SpeakerPlayer = class {
643
+ import { EventEmitter as EventEmitter4 } from "eventemitter3";
644
+ var SpeakerPlayer = class extends EventEmitter4 {
641
645
  constructor(outputNode) {
646
+ super();
642
647
  this.outputNode = outputNode;
648
+ this.isPlaying = false;
643
649
  const ctx = outputNode.context;
644
650
  if (!(ctx instanceof AudioContext)) {
645
651
  throw new Error("OutputNode must be connected to an AudioContext");
646
652
  }
647
653
  this.audioContext = ctx;
648
654
  }
655
+ destroy() {
656
+ this.removeAllListeners();
657
+ }
658
+ changeIsPlaying(isPlaying) {
659
+ if (this.isPlaying == isPlaying) return;
660
+ this.isPlaying = isPlaying;
661
+ this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
662
+ }
649
663
  };
650
664
 
651
665
  // src/audio/player/SpeakerConcatPlayer.ts
@@ -669,7 +683,11 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
669
683
  this.sourceStartTime = void 0;
670
684
  this.currentSource = void 0;
671
685
  source.disconnect();
672
- this.replaceSource();
686
+ if (this.nextSource) {
687
+ this.replaceSource();
688
+ } else {
689
+ this.changeIsPlaying(false);
690
+ }
673
691
  };
674
692
  const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
675
693
  source.start(0, offset);
@@ -715,10 +733,12 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
715
733
  return destination;
716
734
  }
717
735
  addBlob(blob) {
736
+ this.changeIsPlaying(true);
718
737
  this.pendingBlobs.push(blob);
719
738
  this.convertNextBlob();
720
739
  }
721
740
  stop() {
741
+ this.changeIsPlaying(false);
722
742
  this.pendingBlobs.length = 0;
723
743
  this.sourceStartTime = void 0;
724
744
  this.prevSourceDuration = 0;
@@ -730,6 +750,7 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
730
750
  this.isConvertingBlob = false;
731
751
  }
732
752
  destroy() {
753
+ super.destroy();
733
754
  this.stop();
734
755
  }
735
756
  };
@@ -806,6 +827,7 @@ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
806
827
  this.setup();
807
828
  }
808
829
  destroy() {
830
+ super.destroy();
809
831
  if (this.sourceNode) {
810
832
  this.sourceNode.disconnect();
811
833
  this.sourceNode = void 0;
@@ -853,7 +875,11 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
853
875
  source.onended = () => {
854
876
  source.disconnect();
855
877
  this.currentSource = void 0;
856
- this.processQueue();
878
+ if (this.pendingSources.length === 0) {
879
+ this.changeIsPlaying(false);
880
+ } else {
881
+ this.processQueue();
882
+ }
857
883
  };
858
884
  source.start(0);
859
885
  }
@@ -876,6 +902,7 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
876
902
  this.convertNextBlob();
877
903
  }
878
904
  addBlob(blob) {
905
+ this.changeIsPlaying(true);
879
906
  this.pendingBlobs.push(blob);
880
907
  this.convertNextBlob();
881
908
  }
@@ -887,14 +914,17 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
887
914
  this.currentSource = void 0;
888
915
  }
889
916
  destroy() {
917
+ super.destroy();
890
918
  this.stop();
891
919
  }
892
920
  };
893
921
 
894
922
  // src/audio/speaker.ts
895
923
  var speaker;
896
- var Speaker = class {
924
+ var Speaker = class extends EventEmitter5 {
897
925
  constructor() {
926
+ super();
927
+ this.isPlaying = false;
898
928
  this.streamingEnabled = false;
899
929
  this.analyser = new AudioAnalyser(audioContext);
900
930
  this.audioElement = new Audio();
@@ -915,6 +945,8 @@ var Speaker = class {
915
945
  this.player?.destroy();
916
946
  this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
917
947
  await this.player?.setup();
948
+ this.player.on("StartPlaying", () => this.changeIsPlaying(true));
949
+ this.player.on("StopPlaying", () => this.changeIsPlaying(false));
918
950
  } catch (error) {
919
951
  console.error("Error setting up speaker player", error);
920
952
  }
@@ -922,6 +954,11 @@ var Speaker = class {
922
954
  get isStreamingEnabled() {
923
955
  return this.streamingEnabled;
924
956
  }
957
+ changeIsPlaying(isPlaying) {
958
+ if (this.isPlaying == isPlaying) return;
959
+ this.isPlaying = isPlaying;
960
+ this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
961
+ }
925
962
  async enableStreaming() {
926
963
  if (this.streamingEnabled) return;
927
964
  this.streamingEnabled = true;
@@ -981,7 +1018,7 @@ if (!window.micdropSpeaker) {
981
1018
  speaker = window.micdropSpeaker;
982
1019
 
983
1020
  // src/call/CallClient.ts
984
- import EventEmitter4 from "eventemitter3";
1021
+ import EventEmitter6 from "eventemitter3";
985
1022
 
986
1023
  // src/call/CallClientError.ts
987
1024
  var CallClientErrorCode = /* @__PURE__ */ ((CallClientErrorCode2) => {
@@ -998,7 +1035,7 @@ var CallClientError = class extends Error {
998
1035
  };
999
1036
 
1000
1037
  // src/call/CallClient.ts
1001
- var CallClient = class _CallClient extends EventEmitter4 {
1038
+ var CallClient = class _CallClient extends EventEmitter6 {
1002
1039
  constructor(options) {
1003
1040
  super();
1004
1041
  this.options = options;
@@ -1006,7 +1043,12 @@ var CallClient = class _CallClient extends EventEmitter4 {
1006
1043
  this.debug = false;
1007
1044
  this.startTime = 0;
1008
1045
  this._isProcessing = false;
1046
+ this._isPaused = false;
1009
1047
  this.vad = getVAD(this.options?.vad);
1048
+ this.onSpeakerStartPlaying = this.onSpeakerStartPlaying.bind(this);
1049
+ this.onSpeakerStopPlaying = this.onSpeakerStopPlaying.bind(this);
1050
+ speaker.on("StartPlaying", this.onSpeakerStartPlaying);
1051
+ speaker.on("StopPlaying", this.onSpeakerStopPlaying);
1010
1052
  }
1011
1053
  static getInstance(options) {
1012
1054
  if (!window.micdropCallClient) {
@@ -1020,6 +1062,15 @@ var CallClient = class _CallClient extends EventEmitter4 {
1020
1062
  get isStarting() {
1021
1063
  return this.isWSStarting || this.micRecorder?.state.isStarting || false;
1022
1064
  }
1065
+ get isPaused() {
1066
+ return this._isPaused;
1067
+ }
1068
+ get isProcessing() {
1069
+ return this._isProcessing && !this.isPaused;
1070
+ }
1071
+ get isListening() {
1072
+ return this.isMicStarted && !this.isPaused && !this.isProcessing && !this.isMicMuted && !this.isUserSpeaking && !this.isAssistantSpeaking;
1073
+ }
1023
1074
  get isWSStarted() {
1024
1075
  return this.ws?.readyState === WebSocket.OPEN;
1025
1076
  }
@@ -1032,11 +1083,11 @@ var CallClient = class _CallClient extends EventEmitter4 {
1032
1083
  get isMicMuted() {
1033
1084
  return this.micRecorder?.state.isMuted ?? false;
1034
1085
  }
1035
- get isMicSpeaking() {
1086
+ get isUserSpeaking() {
1036
1087
  return this.micRecorder?.state.isSpeaking ?? false;
1037
1088
  }
1038
- get isProcessing() {
1039
- return this._isProcessing;
1089
+ get isAssistantSpeaking() {
1090
+ return speaker.isPlaying;
1040
1091
  }
1041
1092
  async start() {
1042
1093
  this.startTime = Date.now();
@@ -1059,20 +1110,25 @@ var CallClient = class _CallClient extends EventEmitter4 {
1059
1110
  }
1060
1111
  }
1061
1112
  pause() {
1113
+ if (this.isPaused) return;
1062
1114
  this.micRecorder?.mute();
1115
+ this._isPaused = true;
1063
1116
  this._isProcessing = false;
1064
1117
  this.notifyStateChange();
1065
1118
  speaker.pauseAudio();
1066
1119
  this.ws?.send("Mute" /* Mute */);
1067
1120
  }
1068
1121
  resume() {
1069
- if (this.lastAudioBlob) {
1070
- speaker.playAudio(this.lastAudioBlob);
1071
- }
1072
- speaker.resumeAudio();
1122
+ if (!this.isPaused) return;
1073
1123
  this.micRecorder?.unmute();
1124
+ this._isPaused = false;
1074
1125
  this.notifyStateChange();
1075
1126
  }
1127
+ async destroy() {
1128
+ this.stop();
1129
+ speaker.off("StartPlaying", this.onSpeakerStartPlaying);
1130
+ speaker.off("StopPlaying", this.onSpeakerStopPlaying);
1131
+ }
1076
1132
  async startMic(deviceId, record = true) {
1077
1133
  try {
1078
1134
  if (!this.micRecorder) {
@@ -1081,7 +1137,7 @@ var CallClient = class _CallClient extends EventEmitter4 {
1081
1137
  this.notifyStateChange();
1082
1138
  });
1083
1139
  this.micRecorder.on("Chunk", (blob) => {
1084
- this.log(`[Mic] Received chunk`, blob);
1140
+ this.log(`[Mic] Chunk`, blob);
1085
1141
  this.ws?.send(blob);
1086
1142
  });
1087
1143
  this.micRecorder.on("StartSpeaking", () => {
@@ -1094,7 +1150,9 @@ var CallClient = class _CallClient extends EventEmitter4 {
1094
1150
  this.micRecorder.on("StopSpeaking", () => {
1095
1151
  this.log("[Mic] Stop speaking");
1096
1152
  this.ws?.send("StopSpeaking" /* StopSpeaking */);
1097
- this._isProcessing = true;
1153
+ if (this.isWSStarted) {
1154
+ this._isProcessing = true;
1155
+ }
1098
1156
  this.notifyStateChange();
1099
1157
  });
1100
1158
  }
@@ -1144,7 +1202,6 @@ var CallClient = class _CallClient extends EventEmitter4 {
1144
1202
  this.log("[WS]", event.data);
1145
1203
  if (event.data instanceof Blob) {
1146
1204
  speaker.playAudio(event.data);
1147
- this.lastAudioBlob = event.data;
1148
1205
  this._isProcessing = false;
1149
1206
  this.notifyStateChange();
1150
1207
  } else if (typeof event.data !== "string") {
@@ -1203,6 +1260,20 @@ var CallClient = class _CallClient extends EventEmitter4 {
1203
1260
  this.ws = void 0;
1204
1261
  this.notifyStateChange();
1205
1262
  }
1263
+ onSpeakerStartPlaying() {
1264
+ this.log("[Speaker] Start speaking");
1265
+ if (this.options?.disableInterruption && !this.isPaused) {
1266
+ this.micRecorder?.mute();
1267
+ }
1268
+ this.notifyStateChange();
1269
+ }
1270
+ onSpeakerStopPlaying() {
1271
+ this.log("[Speaker] Stop speaking");
1272
+ if (this.options?.disableInterruption && !this.isPaused) {
1273
+ this.micRecorder?.unmute();
1274
+ }
1275
+ this.notifyStateChange();
1276
+ }
1206
1277
  addMessage(message) {
1207
1278
  this.conversation.push(message);
1208
1279
  this.notifyStateChange();