@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 +19 -6
- package/dist/index.d.ts +19 -6
- package/dist/index.js +92 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +92 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -504,18 +504,18 @@ var MicRecorder = class extends EventEmitter3 {
|
|
|
504
504
|
this.speakingConfirmed = true;
|
|
505
505
|
}).bind(this);
|
|
506
506
|
this.onCancelSpeaking = (async () => {
|
|
507
|
-
if (
|
|
507
|
+
if (this.state.isMuted) return;
|
|
508
508
|
try {
|
|
509
|
-
this.recorder
|
|
509
|
+
this.recorder?.stop();
|
|
510
510
|
} catch (error) {
|
|
511
511
|
console.error(error);
|
|
512
512
|
}
|
|
513
513
|
this.queuedChunks.length = 0;
|
|
514
514
|
}).bind(this);
|
|
515
515
|
this.onStopSpeaking = (async () => {
|
|
516
|
-
if (
|
|
516
|
+
if (this.state.isMuted) return;
|
|
517
517
|
try {
|
|
518
|
-
this.recorder
|
|
518
|
+
this.recorder?.stop();
|
|
519
519
|
} catch (error) {
|
|
520
520
|
console.error(error);
|
|
521
521
|
}
|
|
@@ -605,6 +605,7 @@ var MicRecorder = class extends EventEmitter3 {
|
|
|
605
605
|
await this.vad.stop();
|
|
606
606
|
}
|
|
607
607
|
async onDataAvailable(blobEvent) {
|
|
608
|
+
if (this.state.isMuted) return;
|
|
608
609
|
if (!this.speakingConfirmed) {
|
|
609
610
|
this.queuedChunks.push(blobEvent.data);
|
|
610
611
|
return;
|
|
@@ -635,16 +636,30 @@ var MicRecorder = class extends EventEmitter3 {
|
|
|
635
636
|
}
|
|
636
637
|
};
|
|
637
638
|
|
|
639
|
+
// src/audio/speaker.ts
|
|
640
|
+
import EventEmitter5 from "eventemitter3";
|
|
641
|
+
|
|
638
642
|
// src/audio/player/SpeakerPlayer.ts
|
|
639
|
-
|
|
643
|
+
import { EventEmitter as EventEmitter4 } from "eventemitter3";
|
|
644
|
+
var SpeakerPlayer = class extends EventEmitter4 {
|
|
640
645
|
constructor(outputNode) {
|
|
646
|
+
super();
|
|
641
647
|
this.outputNode = outputNode;
|
|
648
|
+
this.isPlaying = false;
|
|
642
649
|
const ctx = outputNode.context;
|
|
643
650
|
if (!(ctx instanceof AudioContext)) {
|
|
644
651
|
throw new Error("OutputNode must be connected to an AudioContext");
|
|
645
652
|
}
|
|
646
653
|
this.audioContext = ctx;
|
|
647
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
|
+
}
|
|
648
663
|
};
|
|
649
664
|
|
|
650
665
|
// src/audio/player/SpeakerConcatPlayer.ts
|
|
@@ -668,7 +683,11 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
|
|
|
668
683
|
this.sourceStartTime = void 0;
|
|
669
684
|
this.currentSource = void 0;
|
|
670
685
|
source.disconnect();
|
|
671
|
-
this.
|
|
686
|
+
if (this.nextSource) {
|
|
687
|
+
this.replaceSource();
|
|
688
|
+
} else {
|
|
689
|
+
this.changeIsPlaying(false);
|
|
690
|
+
}
|
|
672
691
|
};
|
|
673
692
|
const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
|
|
674
693
|
source.start(0, offset);
|
|
@@ -714,10 +733,12 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
|
|
|
714
733
|
return destination;
|
|
715
734
|
}
|
|
716
735
|
addBlob(blob) {
|
|
736
|
+
this.changeIsPlaying(true);
|
|
717
737
|
this.pendingBlobs.push(blob);
|
|
718
738
|
this.convertNextBlob();
|
|
719
739
|
}
|
|
720
740
|
stop() {
|
|
741
|
+
this.changeIsPlaying(false);
|
|
721
742
|
this.pendingBlobs.length = 0;
|
|
722
743
|
this.sourceStartTime = void 0;
|
|
723
744
|
this.prevSourceDuration = 0;
|
|
@@ -729,6 +750,7 @@ var SpeakerConcatPlayer = class extends SpeakerPlayer {
|
|
|
729
750
|
this.isConvertingBlob = false;
|
|
730
751
|
}
|
|
731
752
|
destroy() {
|
|
753
|
+
super.destroy();
|
|
732
754
|
this.stop();
|
|
733
755
|
}
|
|
734
756
|
};
|
|
@@ -805,6 +827,7 @@ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
|
|
|
805
827
|
this.setup();
|
|
806
828
|
}
|
|
807
829
|
destroy() {
|
|
830
|
+
super.destroy();
|
|
808
831
|
if (this.sourceNode) {
|
|
809
832
|
this.sourceNode.disconnect();
|
|
810
833
|
this.sourceNode = void 0;
|
|
@@ -852,7 +875,11 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
|
|
|
852
875
|
source.onended = () => {
|
|
853
876
|
source.disconnect();
|
|
854
877
|
this.currentSource = void 0;
|
|
855
|
-
this.
|
|
878
|
+
if (this.pendingSources.length === 0) {
|
|
879
|
+
this.changeIsPlaying(false);
|
|
880
|
+
} else {
|
|
881
|
+
this.processQueue();
|
|
882
|
+
}
|
|
856
883
|
};
|
|
857
884
|
source.start(0);
|
|
858
885
|
}
|
|
@@ -875,6 +902,7 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
|
|
|
875
902
|
this.convertNextBlob();
|
|
876
903
|
}
|
|
877
904
|
addBlob(blob) {
|
|
905
|
+
this.changeIsPlaying(true);
|
|
878
906
|
this.pendingBlobs.push(blob);
|
|
879
907
|
this.convertNextBlob();
|
|
880
908
|
}
|
|
@@ -886,14 +914,17 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
|
|
|
886
914
|
this.currentSource = void 0;
|
|
887
915
|
}
|
|
888
916
|
destroy() {
|
|
917
|
+
super.destroy();
|
|
889
918
|
this.stop();
|
|
890
919
|
}
|
|
891
920
|
};
|
|
892
921
|
|
|
893
922
|
// src/audio/speaker.ts
|
|
894
923
|
var speaker;
|
|
895
|
-
var Speaker = class {
|
|
924
|
+
var Speaker = class extends EventEmitter5 {
|
|
896
925
|
constructor() {
|
|
926
|
+
super();
|
|
927
|
+
this.isPlaying = false;
|
|
897
928
|
this.streamingEnabled = false;
|
|
898
929
|
this.analyser = new AudioAnalyser(audioContext);
|
|
899
930
|
this.audioElement = new Audio();
|
|
@@ -914,6 +945,8 @@ var Speaker = class {
|
|
|
914
945
|
this.player?.destroy();
|
|
915
946
|
this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
|
|
916
947
|
await this.player?.setup();
|
|
948
|
+
this.player.on("StartPlaying", () => this.changeIsPlaying(true));
|
|
949
|
+
this.player.on("StopPlaying", () => this.changeIsPlaying(false));
|
|
917
950
|
} catch (error) {
|
|
918
951
|
console.error("Error setting up speaker player", error);
|
|
919
952
|
}
|
|
@@ -921,6 +954,11 @@ var Speaker = class {
|
|
|
921
954
|
get isStreamingEnabled() {
|
|
922
955
|
return this.streamingEnabled;
|
|
923
956
|
}
|
|
957
|
+
changeIsPlaying(isPlaying) {
|
|
958
|
+
if (this.isPlaying == isPlaying) return;
|
|
959
|
+
this.isPlaying = isPlaying;
|
|
960
|
+
this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
|
|
961
|
+
}
|
|
924
962
|
async enableStreaming() {
|
|
925
963
|
if (this.streamingEnabled) return;
|
|
926
964
|
this.streamingEnabled = true;
|
|
@@ -980,7 +1018,7 @@ if (!window.micdropSpeaker) {
|
|
|
980
1018
|
speaker = window.micdropSpeaker;
|
|
981
1019
|
|
|
982
1020
|
// src/call/CallClient.ts
|
|
983
|
-
import
|
|
1021
|
+
import EventEmitter6 from "eventemitter3";
|
|
984
1022
|
|
|
985
1023
|
// src/call/CallClientError.ts
|
|
986
1024
|
var CallClientErrorCode = /* @__PURE__ */ ((CallClientErrorCode2) => {
|
|
@@ -997,7 +1035,7 @@ var CallClientError = class extends Error {
|
|
|
997
1035
|
};
|
|
998
1036
|
|
|
999
1037
|
// src/call/CallClient.ts
|
|
1000
|
-
var CallClient = class _CallClient extends
|
|
1038
|
+
var CallClient = class _CallClient extends EventEmitter6 {
|
|
1001
1039
|
constructor(options) {
|
|
1002
1040
|
super();
|
|
1003
1041
|
this.options = options;
|
|
@@ -1005,7 +1043,12 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1005
1043
|
this.debug = false;
|
|
1006
1044
|
this.startTime = 0;
|
|
1007
1045
|
this._isProcessing = false;
|
|
1046
|
+
this._isPaused = false;
|
|
1008
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);
|
|
1009
1052
|
}
|
|
1010
1053
|
static getInstance(options) {
|
|
1011
1054
|
if (!window.micdropCallClient) {
|
|
@@ -1019,6 +1062,15 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1019
1062
|
get isStarting() {
|
|
1020
1063
|
return this.isWSStarting || this.micRecorder?.state.isStarting || false;
|
|
1021
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
|
+
}
|
|
1022
1074
|
get isWSStarted() {
|
|
1023
1075
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
1024
1076
|
}
|
|
@@ -1031,11 +1083,11 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1031
1083
|
get isMicMuted() {
|
|
1032
1084
|
return this.micRecorder?.state.isMuted ?? false;
|
|
1033
1085
|
}
|
|
1034
|
-
get
|
|
1086
|
+
get isUserSpeaking() {
|
|
1035
1087
|
return this.micRecorder?.state.isSpeaking ?? false;
|
|
1036
1088
|
}
|
|
1037
|
-
get
|
|
1038
|
-
return
|
|
1089
|
+
get isAssistantSpeaking() {
|
|
1090
|
+
return speaker.isPlaying;
|
|
1039
1091
|
}
|
|
1040
1092
|
async start() {
|
|
1041
1093
|
this.startTime = Date.now();
|
|
@@ -1058,20 +1110,25 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1058
1110
|
}
|
|
1059
1111
|
}
|
|
1060
1112
|
pause() {
|
|
1113
|
+
if (this.isPaused) return;
|
|
1061
1114
|
this.micRecorder?.mute();
|
|
1115
|
+
this._isPaused = true;
|
|
1062
1116
|
this._isProcessing = false;
|
|
1063
1117
|
this.notifyStateChange();
|
|
1064
1118
|
speaker.pauseAudio();
|
|
1065
1119
|
this.ws?.send("Mute" /* Mute */);
|
|
1066
1120
|
}
|
|
1067
1121
|
resume() {
|
|
1068
|
-
if (this.
|
|
1069
|
-
speaker.playAudio(this.lastAudioBlob);
|
|
1070
|
-
}
|
|
1071
|
-
speaker.resumeAudio();
|
|
1122
|
+
if (!this.isPaused) return;
|
|
1072
1123
|
this.micRecorder?.unmute();
|
|
1124
|
+
this._isPaused = false;
|
|
1073
1125
|
this.notifyStateChange();
|
|
1074
1126
|
}
|
|
1127
|
+
async destroy() {
|
|
1128
|
+
this.stop();
|
|
1129
|
+
speaker.off("StartPlaying", this.onSpeakerStartPlaying);
|
|
1130
|
+
speaker.off("StopPlaying", this.onSpeakerStopPlaying);
|
|
1131
|
+
}
|
|
1075
1132
|
async startMic(deviceId, record = true) {
|
|
1076
1133
|
try {
|
|
1077
1134
|
if (!this.micRecorder) {
|
|
@@ -1080,7 +1137,7 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1080
1137
|
this.notifyStateChange();
|
|
1081
1138
|
});
|
|
1082
1139
|
this.micRecorder.on("Chunk", (blob) => {
|
|
1083
|
-
this.log(`[Mic]
|
|
1140
|
+
this.log(`[Mic] Chunk`, blob);
|
|
1084
1141
|
this.ws?.send(blob);
|
|
1085
1142
|
});
|
|
1086
1143
|
this.micRecorder.on("StartSpeaking", () => {
|
|
@@ -1093,7 +1150,9 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1093
1150
|
this.micRecorder.on("StopSpeaking", () => {
|
|
1094
1151
|
this.log("[Mic] Stop speaking");
|
|
1095
1152
|
this.ws?.send("StopSpeaking" /* StopSpeaking */);
|
|
1096
|
-
this.
|
|
1153
|
+
if (this.isWSStarted) {
|
|
1154
|
+
this._isProcessing = true;
|
|
1155
|
+
}
|
|
1097
1156
|
this.notifyStateChange();
|
|
1098
1157
|
});
|
|
1099
1158
|
}
|
|
@@ -1143,7 +1202,6 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1143
1202
|
this.log("[WS]", event.data);
|
|
1144
1203
|
if (event.data instanceof Blob) {
|
|
1145
1204
|
speaker.playAudio(event.data);
|
|
1146
|
-
this.lastAudioBlob = event.data;
|
|
1147
1205
|
this._isProcessing = false;
|
|
1148
1206
|
this.notifyStateChange();
|
|
1149
1207
|
} else if (typeof event.data !== "string") {
|
|
@@ -1202,6 +1260,20 @@ var CallClient = class _CallClient extends EventEmitter4 {
|
|
|
1202
1260
|
this.ws = void 0;
|
|
1203
1261
|
this.notifyStateChange();
|
|
1204
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
|
+
}
|
|
1205
1277
|
addMessage(message) {
|
|
1206
1278
|
this.conversation.push(message);
|
|
1207
1279
|
this.notifyStateChange();
|