@micdrop/client 1.8.0 → 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.d.mts CHANGED
@@ -18,10 +18,10 @@ declare class AudioAnalyser extends EventEmitter<AudioAnalyserEvents> {
18
18
 
19
19
  declare let mic: Mic;
20
20
  declare class Mic {
21
+ analyser: AudioAnalyser;
21
22
  private currentDeviceId;
22
23
  private audioStream;
23
24
  private sourceNode;
24
- analyser: AudioAnalyser;
25
25
  constructor();
26
26
  private getMicConstraints;
27
27
  /**
@@ -128,14 +128,20 @@ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
128
128
  }
129
129
 
130
130
  declare let speaker: Speaker;
131
- declare class Speaker {
131
+ interface SpeakerEvents {
132
+ StartPlaying: void;
133
+ StopPlaying: void;
134
+ }
135
+ declare class Speaker extends EventEmitter<SpeakerEvents> {
136
+ isPlaying: boolean;
137
+ analyser: AudioAnalyser;
132
138
  private audioElement?;
133
139
  private player?;
134
140
  private streamingEnabled;
135
- analyser: AudioAnalyser;
136
141
  constructor();
137
142
  private setupSpeakerPlayer;
138
143
  get isStreamingEnabled(): boolean;
144
+ protected changeIsPlaying(isPlaying: boolean): void;
139
145
  enableStreaming(): Promise<void>;
140
146
  disableStreaming(): Promise<void>;
141
147
  canChangeDevice(): boolean;
@@ -236,6 +242,7 @@ interface CallClientEvents {
236
242
  }
237
243
  interface CallClientOptions {
238
244
  vad?: VADConfig;
245
+ disableInterruption?: boolean;
239
246
  }
240
247
  declare global {
241
248
  interface Window {
@@ -254,25 +261,31 @@ declare class CallClient<Params extends {}, Options extends CallClientOptions =
254
261
  private ws?;
255
262
  private micStream?;
256
263
  private startTime;
257
- private lastAudioBlob?;
258
264
  private _isProcessing;
265
+ private _isPaused;
259
266
  private constructor();
260
267
  get isStarted(): boolean;
261
268
  get isStarting(): boolean;
269
+ get isPaused(): boolean;
270
+ get isProcessing(): boolean;
271
+ get isListening(): boolean;
262
272
  get isWSStarted(): boolean;
263
273
  get isWSStarting(): boolean;
264
274
  get isMicStarted(): boolean;
265
275
  get isMicMuted(): boolean;
266
- get isMicSpeaking(): boolean;
267
- get isProcessing(): boolean;
276
+ get isUserSpeaking(): boolean;
277
+ get isAssistantSpeaking(): boolean;
268
278
  start(): Promise<void>;
269
279
  stop(): Promise<void>;
270
280
  pause(): void;
271
281
  resume(): void;
282
+ destroy(): Promise<void>;
272
283
  startMic(deviceId?: string, record?: boolean): Promise<void>;
273
284
  private stopMic;
274
285
  private startWS;
275
286
  private stopWS;
287
+ private onSpeakerStartPlaying;
288
+ private onSpeakerStopPlaying;
276
289
  private addMessage;
277
290
  private notifyStateChange;
278
291
  private log;
package/dist/index.d.ts CHANGED
@@ -18,10 +18,10 @@ declare class AudioAnalyser extends EventEmitter<AudioAnalyserEvents> {
18
18
 
19
19
  declare let mic: Mic;
20
20
  declare class Mic {
21
+ analyser: AudioAnalyser;
21
22
  private currentDeviceId;
22
23
  private audioStream;
23
24
  private sourceNode;
24
- analyser: AudioAnalyser;
25
25
  constructor();
26
26
  private getMicConstraints;
27
27
  /**
@@ -128,14 +128,20 @@ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
128
128
  }
129
129
 
130
130
  declare let speaker: Speaker;
131
- declare class Speaker {
131
+ interface SpeakerEvents {
132
+ StartPlaying: void;
133
+ StopPlaying: void;
134
+ }
135
+ declare class Speaker extends EventEmitter<SpeakerEvents> {
136
+ isPlaying: boolean;
137
+ analyser: AudioAnalyser;
132
138
  private audioElement?;
133
139
  private player?;
134
140
  private streamingEnabled;
135
- analyser: AudioAnalyser;
136
141
  constructor();
137
142
  private setupSpeakerPlayer;
138
143
  get isStreamingEnabled(): boolean;
144
+ protected changeIsPlaying(isPlaying: boolean): void;
139
145
  enableStreaming(): Promise<void>;
140
146
  disableStreaming(): Promise<void>;
141
147
  canChangeDevice(): boolean;
@@ -236,6 +242,7 @@ interface CallClientEvents {
236
242
  }
237
243
  interface CallClientOptions {
238
244
  vad?: VADConfig;
245
+ disableInterruption?: boolean;
239
246
  }
240
247
  declare global {
241
248
  interface Window {
@@ -254,25 +261,31 @@ declare class CallClient<Params extends {}, Options extends CallClientOptions =
254
261
  private ws?;
255
262
  private micStream?;
256
263
  private startTime;
257
- private lastAudioBlob?;
258
264
  private _isProcessing;
265
+ private _isPaused;
259
266
  private constructor();
260
267
  get isStarted(): boolean;
261
268
  get isStarting(): boolean;
269
+ get isPaused(): boolean;
270
+ get isProcessing(): boolean;
271
+ get isListening(): boolean;
262
272
  get isWSStarted(): boolean;
263
273
  get isWSStarting(): boolean;
264
274
  get isMicStarted(): boolean;
265
275
  get isMicMuted(): boolean;
266
- get isMicSpeaking(): boolean;
267
- get isProcessing(): boolean;
276
+ get isUserSpeaking(): boolean;
277
+ get isAssistantSpeaking(): boolean;
268
278
  start(): Promise<void>;
269
279
  stop(): Promise<void>;
270
280
  pause(): void;
271
281
  resume(): void;
282
+ destroy(): Promise<void>;
272
283
  startMic(deviceId?: string, record?: boolean): Promise<void>;
273
284
  private stopMic;
274
285
  private startWS;
275
286
  private stopWS;
287
+ private onSpeakerStartPlaying;
288
+ private onSpeakerStopPlaying;
276
289
  private addMessage;
277
290
  private notifyStateChange;
278
291
  private log;
package/dist/index.js CHANGED
@@ -553,18 +553,18 @@ var MicRecorder = class extends import_eventemitter33.default {
553
553
  this.speakingConfirmed = true;
554
554
  }).bind(this);
555
555
  this.onCancelSpeaking = (async () => {
556
- if (!this.recorder) return;
556
+ if (this.state.isMuted) return;
557
557
  try {
558
- this.recorder.stop();
558
+ this.recorder?.stop();
559
559
  } catch (error) {
560
560
  console.error(error);
561
561
  }
562
562
  this.queuedChunks.length = 0;
563
563
  }).bind(this);
564
564
  this.onStopSpeaking = (async () => {
565
- if (!this.recorder) return;
565
+ if (this.state.isMuted) return;
566
566
  try {
567
- this.recorder.stop();
567
+ this.recorder?.stop();
568
568
  } catch (error) {
569
569
  console.error(error);
570
570
  }
@@ -654,6 +654,7 @@ var MicRecorder = class extends import_eventemitter33.default {
654
654
  await this.vad.stop();
655
655
  }
656
656
  async onDataAvailable(blobEvent) {
657
+ if (this.state.isMuted) return;
657
658
  if (!this.speakingConfirmed) {
658
659
  this.queuedChunks.push(blobEvent.data);
659
660
  return;
@@ -684,16 +685,30 @@ var MicRecorder = class extends import_eventemitter33.default {
684
685
  }
685
686
  };
686
687
 
688
+ // src/audio/speaker.ts
689
+ var import_eventemitter35 = __toESM(require("eventemitter3"));
690
+
687
691
  // src/audio/player/SpeakerPlayer.ts
688
- var SpeakerPlayer = class {
692
+ var import_eventemitter34 = require("eventemitter3");
693
+ var SpeakerPlayer = class extends import_eventemitter34.EventEmitter {
689
694
  constructor(outputNode) {
695
+ super();
690
696
  this.outputNode = outputNode;
697
+ this.isPlaying = false;
691
698
  const ctx = outputNode.context;
692
699
  if (!(ctx instanceof AudioContext)) {
693
700
  throw new Error("OutputNode must be connected to an AudioContext");
694
701
  }
695
702
  this.audioContext = ctx;
696
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
+ }
697
712
  };
698
713
 
699
714
  // src/audio/player/SpeakerConcatPlayer.ts
@@ -717,7 +732,11 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
717
732
  this.sourceStartTime = void 0;
718
733
  this.currentSource = void 0;
719
734
  source.disconnect();
720
- this.replaceSource();
735
+ if (this.nextSource) {
736
+ this.replaceSource();
737
+ } else {
738
+ this.changeIsPlaying(false);
739
+ }
721
740
  };
722
741
  const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
723
742
  source.start(0, offset);
@@ -763,10 +782,12 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
763
782
  return destination;
764
783
  }
765
784
  addBlob(blob) {
785
+ this.changeIsPlaying(true);
766
786
  this.pendingBlobs.push(blob);
767
787
  this.convertNextBlob();
768
788
  }
769
789
  stop() {
790
+ this.changeIsPlaying(false);
770
791
  this.pendingBlobs.length = 0;
771
792
  this.sourceStartTime = void 0;
772
793
  this.prevSourceDuration = 0;
@@ -778,6 +799,7 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
778
799
  this.isConvertingBlob = false;
779
800
  }
780
801
  destroy() {
802
+ super.destroy();
781
803
  this.stop();
782
804
  }
783
805
  };
@@ -854,6 +876,7 @@ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
854
876
  this.setup();
855
877
  }
856
878
  destroy() {
879
+ super.destroy();
857
880
  if (this.sourceNode) {
858
881
  this.sourceNode.disconnect();
859
882
  this.sourceNode = void 0;
@@ -901,7 +924,11 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
901
924
  source.onended = () => {
902
925
  source.disconnect();
903
926
  this.currentSource = void 0;
904
- this.processQueue();
927
+ if (this.pendingSources.length === 0) {
928
+ this.changeIsPlaying(false);
929
+ } else {
930
+ this.processQueue();
931
+ }
905
932
  };
906
933
  source.start(0);
907
934
  }
@@ -924,6 +951,7 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
924
951
  this.convertNextBlob();
925
952
  }
926
953
  addBlob(blob) {
954
+ this.changeIsPlaying(true);
927
955
  this.pendingBlobs.push(blob);
928
956
  this.convertNextBlob();
929
957
  }
@@ -935,14 +963,17 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
935
963
  this.currentSource = void 0;
936
964
  }
937
965
  destroy() {
966
+ super.destroy();
938
967
  this.stop();
939
968
  }
940
969
  };
941
970
 
942
971
  // src/audio/speaker.ts
943
972
  var speaker;
944
- var Speaker = class {
973
+ var Speaker = class extends import_eventemitter35.default {
945
974
  constructor() {
975
+ super();
976
+ this.isPlaying = false;
946
977
  this.streamingEnabled = false;
947
978
  this.analyser = new AudioAnalyser(audioContext);
948
979
  this.audioElement = new Audio();
@@ -963,6 +994,8 @@ var Speaker = class {
963
994
  this.player?.destroy();
964
995
  this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
965
996
  await this.player?.setup();
997
+ this.player.on("StartPlaying", () => this.changeIsPlaying(true));
998
+ this.player.on("StopPlaying", () => this.changeIsPlaying(false));
966
999
  } catch (error) {
967
1000
  console.error("Error setting up speaker player", error);
968
1001
  }
@@ -970,6 +1003,11 @@ var Speaker = class {
970
1003
  get isStreamingEnabled() {
971
1004
  return this.streamingEnabled;
972
1005
  }
1006
+ changeIsPlaying(isPlaying) {
1007
+ if (this.isPlaying == isPlaying) return;
1008
+ this.isPlaying = isPlaying;
1009
+ this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
1010
+ }
973
1011
  async enableStreaming() {
974
1012
  if (this.streamingEnabled) return;
975
1013
  this.streamingEnabled = true;
@@ -1029,7 +1067,7 @@ if (!window.micdropSpeaker) {
1029
1067
  speaker = window.micdropSpeaker;
1030
1068
 
1031
1069
  // src/call/CallClient.ts
1032
- var import_eventemitter34 = __toESM(require("eventemitter3"));
1070
+ var import_eventemitter36 = __toESM(require("eventemitter3"));
1033
1071
 
1034
1072
  // src/call/CallClientError.ts
1035
1073
  var CallClientErrorCode = /* @__PURE__ */ ((CallClientErrorCode2) => {
@@ -1046,7 +1084,7 @@ var CallClientError = class extends Error {
1046
1084
  };
1047
1085
 
1048
1086
  // src/call/CallClient.ts
1049
- var CallClient = class _CallClient extends import_eventemitter34.default {
1087
+ var CallClient = class _CallClient extends import_eventemitter36.default {
1050
1088
  constructor(options) {
1051
1089
  super();
1052
1090
  this.options = options;
@@ -1054,7 +1092,12 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1054
1092
  this.debug = false;
1055
1093
  this.startTime = 0;
1056
1094
  this._isProcessing = false;
1095
+ this._isPaused = false;
1057
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);
1058
1101
  }
1059
1102
  static getInstance(options) {
1060
1103
  if (!window.micdropCallClient) {
@@ -1068,6 +1111,15 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1068
1111
  get isStarting() {
1069
1112
  return this.isWSStarting || this.micRecorder?.state.isStarting || false;
1070
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
+ }
1071
1123
  get isWSStarted() {
1072
1124
  return this.ws?.readyState === WebSocket.OPEN;
1073
1125
  }
@@ -1080,11 +1132,11 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1080
1132
  get isMicMuted() {
1081
1133
  return this.micRecorder?.state.isMuted ?? false;
1082
1134
  }
1083
- get isMicSpeaking() {
1135
+ get isUserSpeaking() {
1084
1136
  return this.micRecorder?.state.isSpeaking ?? false;
1085
1137
  }
1086
- get isProcessing() {
1087
- return this._isProcessing;
1138
+ get isAssistantSpeaking() {
1139
+ return speaker.isPlaying;
1088
1140
  }
1089
1141
  async start() {
1090
1142
  this.startTime = Date.now();
@@ -1107,20 +1159,25 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1107
1159
  }
1108
1160
  }
1109
1161
  pause() {
1162
+ if (this.isPaused) return;
1110
1163
  this.micRecorder?.mute();
1164
+ this._isPaused = true;
1111
1165
  this._isProcessing = false;
1112
1166
  this.notifyStateChange();
1113
1167
  speaker.pauseAudio();
1114
1168
  this.ws?.send("Mute" /* Mute */);
1115
1169
  }
1116
1170
  resume() {
1117
- if (this.lastAudioBlob) {
1118
- speaker.playAudio(this.lastAudioBlob);
1119
- }
1120
- speaker.resumeAudio();
1171
+ if (!this.isPaused) return;
1121
1172
  this.micRecorder?.unmute();
1173
+ this._isPaused = false;
1122
1174
  this.notifyStateChange();
1123
1175
  }
1176
+ async destroy() {
1177
+ this.stop();
1178
+ speaker.off("StartPlaying", this.onSpeakerStartPlaying);
1179
+ speaker.off("StopPlaying", this.onSpeakerStopPlaying);
1180
+ }
1124
1181
  async startMic(deviceId, record = true) {
1125
1182
  try {
1126
1183
  if (!this.micRecorder) {
@@ -1129,7 +1186,7 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1129
1186
  this.notifyStateChange();
1130
1187
  });
1131
1188
  this.micRecorder.on("Chunk", (blob) => {
1132
- this.log(`[Mic] Received chunk`, blob);
1189
+ this.log(`[Mic] Chunk`, blob);
1133
1190
  this.ws?.send(blob);
1134
1191
  });
1135
1192
  this.micRecorder.on("StartSpeaking", () => {
@@ -1142,7 +1199,9 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1142
1199
  this.micRecorder.on("StopSpeaking", () => {
1143
1200
  this.log("[Mic] Stop speaking");
1144
1201
  this.ws?.send("StopSpeaking" /* StopSpeaking */);
1145
- this._isProcessing = true;
1202
+ if (this.isWSStarted) {
1203
+ this._isProcessing = true;
1204
+ }
1146
1205
  this.notifyStateChange();
1147
1206
  });
1148
1207
  }
@@ -1192,7 +1251,6 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1192
1251
  this.log("[WS]", event.data);
1193
1252
  if (event.data instanceof Blob) {
1194
1253
  speaker.playAudio(event.data);
1195
- this.lastAudioBlob = event.data;
1196
1254
  this._isProcessing = false;
1197
1255
  this.notifyStateChange();
1198
1256
  } else if (typeof event.data !== "string") {
@@ -1251,6 +1309,20 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
1251
1309
  this.ws = void 0;
1252
1310
  this.notifyStateChange();
1253
1311
  }
1312
+ onSpeakerStartPlaying() {
1313
+ this.log("[Speaker] Start speaking");
1314
+ if (this.options?.disableInterruption && !this.isPaused) {
1315
+ this.micRecorder?.mute();
1316
+ }
1317
+ this.notifyStateChange();
1318
+ }
1319
+ onSpeakerStopPlaying() {
1320
+ this.log("[Speaker] Stop speaking");
1321
+ if (this.options?.disableInterruption && !this.isPaused) {
1322
+ this.micRecorder?.unmute();
1323
+ }
1324
+ this.notifyStateChange();
1325
+ }
1254
1326
  addMessage(message) {
1255
1327
  this.conversation.push(message);
1256
1328
  this.notifyStateChange();