@micdrop/client 3.2.0 → 3.3.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.d.mts +78 -4
- package/dist/index.d.ts +78 -4
- package/dist/index.js +170 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +166 -49
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -20,7 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
CONFIRM_MARGIN: () => CONFIRM_MARGIN,
|
|
23
24
|
DEFAULT_TURN_MAX_WAIT: () => DEFAULT_TURN_MAX_WAIT,
|
|
25
|
+
ECHO_TAIL: () => ECHO_TAIL,
|
|
24
26
|
Mic: () => Mic,
|
|
25
27
|
MicController: () => MicController,
|
|
26
28
|
MicDriver: () => MicDriver,
|
|
@@ -46,10 +48,12 @@ __export(index_exports, {
|
|
|
46
48
|
VolumeMeter: () => VolumeMeter,
|
|
47
49
|
VolumeVAD: () => VolumeVAD,
|
|
48
50
|
concatFloat32: () => concatFloat32,
|
|
51
|
+
currentTurn: () => currentTurn,
|
|
49
52
|
equalVADConfig: () => equalVADConfig,
|
|
50
53
|
floatToPcm16: () => floatToPcm16,
|
|
51
54
|
getClientErrorFromWSCloseEvent: () => getClientErrorFromWSCloseEvent,
|
|
52
55
|
getClientErrorFromWSCloseEventCode: () => getClientErrorFromWSCloseEventCode,
|
|
56
|
+
getTurnClassification: () => getTurnClassification,
|
|
53
57
|
getVAD: () => getVAD,
|
|
54
58
|
getVolume: () => getVolume,
|
|
55
59
|
isRecoverableError: () => isRecoverableError,
|
|
@@ -287,6 +291,10 @@ var MicController = class extends import_eventemitter3.EventEmitter {
|
|
|
287
291
|
get deviceId() {
|
|
288
292
|
return this.driver?.deviceId;
|
|
289
293
|
}
|
|
294
|
+
/** Whether echo cancellation is on, undefined when nobody knows */
|
|
295
|
+
get echoCancellation() {
|
|
296
|
+
return this.driver?.echoCancellation;
|
|
297
|
+
}
|
|
290
298
|
/**
|
|
291
299
|
* Asks for the microphone permission and starts capturing
|
|
292
300
|
* @param deviceId - Input device to record from, defaults to the system one
|
|
@@ -554,18 +562,19 @@ var MultipleVAD = class extends VAD {
|
|
|
554
562
|
constructor(vads) {
|
|
555
563
|
super();
|
|
556
564
|
this.name = "MultipleVAD";
|
|
565
|
+
// Every VAD has to hear the speech, but only one has to be sure of it. A
|
|
566
|
+
// short word like "No" is over before the volume VAD gathers the loud
|
|
567
|
+
// reports it needs to confirm, while Silero confirms it right away.
|
|
557
568
|
this.onStatusChange = () => {
|
|
558
569
|
const isAllSilence = this.vads.every(
|
|
559
570
|
(vad) => vad.status === 0 /* Silence */
|
|
560
571
|
);
|
|
561
|
-
const
|
|
562
|
-
(vad) => vad.status === 2 /* Speaking */
|
|
563
|
-
);
|
|
572
|
+
const isConfirmed = this.vads.every((vad) => vad.status !== 0 /* Silence */) && this.vads.some((vad) => vad.status === 2 /* Speaking */);
|
|
564
573
|
switch (this.status) {
|
|
565
574
|
case 0 /* Silence */:
|
|
566
575
|
if (isAllSilence) break;
|
|
567
576
|
this.emit("StartSpeaking");
|
|
568
|
-
if (
|
|
577
|
+
if (isConfirmed) {
|
|
569
578
|
this.emit("ConfirmSpeaking");
|
|
570
579
|
}
|
|
571
580
|
break;
|
|
@@ -574,7 +583,7 @@ var MultipleVAD = class extends VAD {
|
|
|
574
583
|
this.emit("CancelSpeaking");
|
|
575
584
|
break;
|
|
576
585
|
}
|
|
577
|
-
if (
|
|
586
|
+
if (isConfirmed) {
|
|
578
587
|
this.emit("ConfirmSpeaking");
|
|
579
588
|
}
|
|
580
589
|
break;
|
|
@@ -586,6 +595,7 @@ var MultipleVAD = class extends VAD {
|
|
|
586
595
|
}
|
|
587
596
|
};
|
|
588
597
|
this.vads = vads.map((vad) => getVAD(vad));
|
|
598
|
+
this.delay = Math.max(...this.vads.map((vad) => vad.delay));
|
|
589
599
|
}
|
|
590
600
|
get isStarted() {
|
|
591
601
|
return this.vads.some((vad) => vad.isStarted);
|
|
@@ -594,18 +604,19 @@ var MultipleVAD = class extends VAD {
|
|
|
594
604
|
return this.vads.every((vad) => vad.isPaused);
|
|
595
605
|
}
|
|
596
606
|
async start(mic) {
|
|
607
|
+
this.setStatus(0 /* Silence */);
|
|
597
608
|
for (const vad of this.vads) {
|
|
598
|
-
|
|
599
|
-
continue;
|
|
600
|
-
}
|
|
609
|
+
vad.off("ChangeStatus", this.onStatusChange);
|
|
601
610
|
vad.on("ChangeStatus", this.onStatusChange);
|
|
602
|
-
|
|
611
|
+
if (!vad.isStarted) {
|
|
612
|
+
await vad.start(mic);
|
|
613
|
+
}
|
|
603
614
|
}
|
|
604
615
|
}
|
|
605
616
|
async stop() {
|
|
606
617
|
for (const vad of this.vads) {
|
|
607
|
-
vad.off("ChangeStatus", this.onStatusChange);
|
|
608
618
|
await vad.stop();
|
|
619
|
+
vad.off("ChangeStatus", this.onStatusChange);
|
|
609
620
|
}
|
|
610
621
|
}
|
|
611
622
|
async pause() {
|
|
@@ -639,10 +650,11 @@ var SileroVAD = class extends VAD {
|
|
|
639
650
|
super();
|
|
640
651
|
this.name = "SileroVAD";
|
|
641
652
|
this.options = defaultOptions;
|
|
642
|
-
// The model
|
|
643
|
-
//
|
|
644
|
-
// and may lag behind the audio
|
|
645
|
-
|
|
653
|
+
// The model scores a window as speech a few windows after the voice began:
|
|
654
|
+
// a soft consonant or a breathy onset stays under the threshold until the
|
|
655
|
+
// vowel comes. Scoring also runs asynchronously and may lag behind the audio
|
|
656
|
+
// on a busy device, so the reserve covers about a third of a second.
|
|
657
|
+
this.delay = 10 * MS_PER_FRAME;
|
|
646
658
|
this._isPaused = false;
|
|
647
659
|
this.buffer = [];
|
|
648
660
|
this.bufferLength = 0;
|
|
@@ -651,6 +663,9 @@ var SileroVAD = class extends VAD {
|
|
|
651
663
|
this.speechFrames = 0;
|
|
652
664
|
this.silenceFrames = 0;
|
|
653
665
|
this.processing = false;
|
|
666
|
+
// Bumped whenever the state is reset, so a window scored before that is
|
|
667
|
+
// dropped rather than reopening a turn
|
|
668
|
+
this.round = 0;
|
|
654
669
|
this.onFrames = (frames, sampleRate) => {
|
|
655
670
|
if (this._isPaused || !this.model) return;
|
|
656
671
|
if (sampleRate !== this.bufferRate) {
|
|
@@ -688,6 +703,7 @@ var SileroVAD = class extends VAD {
|
|
|
688
703
|
}
|
|
689
704
|
this.model = await loadModel();
|
|
690
705
|
this.resetState();
|
|
706
|
+
this.setStatus(0 /* Silence */);
|
|
691
707
|
this.mic = mic;
|
|
692
708
|
mic.on("Frames", this.onFrames);
|
|
693
709
|
}
|
|
@@ -734,9 +750,11 @@ var SileroVAD = class extends VAD {
|
|
|
734
750
|
this.buffer = rest.length ? [rest] : [];
|
|
735
751
|
this.bufferLength = rest.length;
|
|
736
752
|
const frame = resample(window, this.bufferRate, SILERO_SAMPLE_RATE);
|
|
753
|
+
const round = this.round;
|
|
737
754
|
const probability = await this.model.process(
|
|
738
755
|
frame.length === SILERO_FRAME_SAMPLES ? frame : fit(frame, SILERO_FRAME_SAMPLES)
|
|
739
756
|
);
|
|
757
|
+
if (round !== this.round) break;
|
|
740
758
|
this.onProbability(probability);
|
|
741
759
|
}
|
|
742
760
|
} catch (error) {
|
|
@@ -777,6 +795,7 @@ var SileroVAD = class extends VAD {
|
|
|
777
795
|
this.emit(confirmed ? "StopSpeaking" : "CancelSpeaking");
|
|
778
796
|
}
|
|
779
797
|
resetState() {
|
|
798
|
+
this.round++;
|
|
780
799
|
this.model?.reset();
|
|
781
800
|
this.buffer = [];
|
|
782
801
|
this.bufferLength = 0;
|
|
@@ -804,9 +823,11 @@ var VolumeVAD = class extends VAD {
|
|
|
804
823
|
this.options = defaultOptions2;
|
|
805
824
|
// The microphone reports a level about every 100 ms, and two consecutive loud
|
|
806
825
|
// ones are needed before a turn opens. Speech can therefore begin up to two
|
|
807
|
-
// reports before it is noticed
|
|
808
|
-
//
|
|
809
|
-
|
|
826
|
+
// reports before it is noticed. A quiet onset (a soft consonant, a word
|
|
827
|
+
// spoken low) stays under the threshold for a while too, so the reserve adds
|
|
828
|
+
// a margin: without it the first syllable of a sentence never reaches the
|
|
829
|
+
// server.
|
|
830
|
+
this.delay = 500;
|
|
810
831
|
this.running = false;
|
|
811
832
|
this._isPaused = false;
|
|
812
833
|
this.speaking = false;
|
|
@@ -949,6 +970,8 @@ function equalVADConfig(a, b) {
|
|
|
949
970
|
var SAMPLE_RATE = 16e3;
|
|
950
971
|
var CHUNK_INTERVAL = 100;
|
|
951
972
|
var DEFAULT_TURN_MAX_WAIT = 4e3;
|
|
973
|
+
var CONFIRM_MARGIN = 300;
|
|
974
|
+
var ECHO_TAIL = 300;
|
|
952
975
|
var defaultMicRecorderState = {
|
|
953
976
|
isStarting: false,
|
|
954
977
|
isStarted: false,
|
|
@@ -967,7 +990,9 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
967
990
|
this.reserveLength = 0;
|
|
968
991
|
this.buffer = [];
|
|
969
992
|
this.bufferLength = 0;
|
|
970
|
-
|
|
993
|
+
// Samples left before the echo of the speaker has died out
|
|
994
|
+
this.echoSamples = 0;
|
|
995
|
+
this.speakerPlaying = false;
|
|
971
996
|
// The turn is open between the first confirmed word and the moment the
|
|
972
997
|
// server is told to answer, which can span several pauses
|
|
973
998
|
this.turnOpen = false;
|
|
@@ -1043,7 +1068,30 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1043
1068
|
this.start(mic);
|
|
1044
1069
|
}
|
|
1045
1070
|
};
|
|
1071
|
+
/**
|
|
1072
|
+
* Leaves out what the microphone hears while the speaker plays, so the
|
|
1073
|
+
* assistant is never sent back to the server as if the user had said it.
|
|
1074
|
+
*
|
|
1075
|
+
* Only for calls the user may not interrupt: an interruption relies on echo
|
|
1076
|
+
* cancellation, and needs every word the user says over the assistant.
|
|
1077
|
+
* @param playing - True while the assistant is heard
|
|
1078
|
+
*/
|
|
1079
|
+
this.setSpeakerPlaying = (playing) => {
|
|
1080
|
+
if (playing === this.speakerPlaying) return;
|
|
1081
|
+
this.speakerPlaying = playing;
|
|
1082
|
+
if (!playing) {
|
|
1083
|
+
this.echoSamples = Math.round(ECHO_TAIL / 1e3 * this.sourceSampleRate);
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1046
1086
|
this.onFrames = (frames, sampleRate) => {
|
|
1087
|
+
if (this.speakerPlaying || this.echoSamples > 0) {
|
|
1088
|
+
this.echoSamples = Math.max(0, this.echoSamples - frames.length);
|
|
1089
|
+
this.reserve = [];
|
|
1090
|
+
this.reserveLength = 0;
|
|
1091
|
+
this.buffer = [];
|
|
1092
|
+
this.bufferLength = 0;
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1047
1095
|
if (this.turnListening) {
|
|
1048
1096
|
this.turnDetector?.push(frames, sampleRate);
|
|
1049
1097
|
}
|
|
@@ -1053,28 +1101,31 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1053
1101
|
this.reserve = [];
|
|
1054
1102
|
this.reserveLength = 0;
|
|
1055
1103
|
}
|
|
1056
|
-
if (this.isRecording) {
|
|
1057
|
-
this.buffer.push(frames);
|
|
1058
|
-
this.bufferLength += frames.length;
|
|
1059
|
-
this.flushChunks();
|
|
1060
|
-
return;
|
|
1061
|
-
}
|
|
1062
1104
|
this.reserve.push(frames);
|
|
1063
1105
|
this.reserveLength += frames.length;
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
this.reserveLength
|
|
1067
|
-
this.
|
|
1106
|
+
this.reserveLength = trimFrames(
|
|
1107
|
+
this.reserve,
|
|
1108
|
+
this.reserveLength,
|
|
1109
|
+
Math.round(this.vad.delay / 1e3 * sampleRate)
|
|
1110
|
+
);
|
|
1111
|
+
if (!this.isRecording) return;
|
|
1112
|
+
this.buffer.push(frames);
|
|
1113
|
+
this.bufferLength += frames.length;
|
|
1114
|
+
if (this.speakingConfirmed) {
|
|
1115
|
+
this.flushChunks();
|
|
1116
|
+
return;
|
|
1068
1117
|
}
|
|
1118
|
+
this.bufferLength = trimFrames(
|
|
1119
|
+
this.buffer,
|
|
1120
|
+
this.bufferLength,
|
|
1121
|
+
Math.round((this.vad.delay + CONFIRM_MARGIN) / 1e3 * sampleRate)
|
|
1122
|
+
);
|
|
1069
1123
|
};
|
|
1070
1124
|
this.onStartSpeaking = () => {
|
|
1071
1125
|
this.speakingConfirmed = false;
|
|
1072
|
-
this.queuedChunks.length = 0;
|
|
1073
1126
|
this.isRecording = true;
|
|
1074
|
-
this.buffer = this.reserve;
|
|
1127
|
+
this.buffer = [...this.reserve];
|
|
1075
1128
|
this.bufferLength = this.reserveLength;
|
|
1076
|
-
this.reserve = [];
|
|
1077
|
-
this.reserveLength = 0;
|
|
1078
1129
|
if (this.turnDetector && !this.turnListening) {
|
|
1079
1130
|
this.turnDetector.reset();
|
|
1080
1131
|
this.turnListening = true;
|
|
@@ -1082,7 +1133,6 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1082
1133
|
this.turnDetector.push(frames, this.sourceSampleRate);
|
|
1083
1134
|
}
|
|
1084
1135
|
}
|
|
1085
|
-
this.flushChunks();
|
|
1086
1136
|
};
|
|
1087
1137
|
this.onConfirmSpeaking = () => {
|
|
1088
1138
|
this.speakingConfirmed = true;
|
|
@@ -1092,10 +1142,7 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1092
1142
|
this.turnOpen = true;
|
|
1093
1143
|
this.emit("StartSpeaking");
|
|
1094
1144
|
}
|
|
1095
|
-
|
|
1096
|
-
this.emit("Chunk", chunk);
|
|
1097
|
-
}
|
|
1098
|
-
this.queuedChunks.length = 0;
|
|
1145
|
+
this.flushChunks();
|
|
1099
1146
|
};
|
|
1100
1147
|
this.onCancelSpeaking = () => {
|
|
1101
1148
|
this.stopRecording();
|
|
@@ -1162,7 +1209,6 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1162
1209
|
stopRecording() {
|
|
1163
1210
|
this.isRecording = false;
|
|
1164
1211
|
this.speakingConfirmed = false;
|
|
1165
|
-
this.queuedChunks.length = 0;
|
|
1166
1212
|
this.buffer = [];
|
|
1167
1213
|
this.bufferLength = 0;
|
|
1168
1214
|
}
|
|
@@ -1174,7 +1220,7 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1174
1220
|
const chunkLength = Math.round(
|
|
1175
1221
|
CHUNK_INTERVAL / 1e3 * this.sourceSampleRate
|
|
1176
1222
|
);
|
|
1177
|
-
if (this.bufferLength === 0) return;
|
|
1223
|
+
if (!this.speakingConfirmed || this.bufferLength === 0) return;
|
|
1178
1224
|
if (this.bufferLength < chunkLength && !final) return;
|
|
1179
1225
|
const merged = concatFloat32(this.buffer);
|
|
1180
1226
|
let offset = 0;
|
|
@@ -1196,10 +1242,6 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1196
1242
|
const pcm = floatToPcm16(
|
|
1197
1243
|
resample(samples, this.sourceSampleRate, SAMPLE_RATE)
|
|
1198
1244
|
);
|
|
1199
|
-
if (!this.speakingConfirmed) {
|
|
1200
|
-
this.queuedChunks.push(pcm);
|
|
1201
|
-
return;
|
|
1202
|
-
}
|
|
1203
1245
|
this.emit("Chunk", pcm);
|
|
1204
1246
|
}
|
|
1205
1247
|
resetBuffers() {
|
|
@@ -1211,7 +1253,6 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1211
1253
|
this.reserveLength = 0;
|
|
1212
1254
|
this.buffer = [];
|
|
1213
1255
|
this.bufferLength = 0;
|
|
1214
|
-
this.queuedChunks.length = 0;
|
|
1215
1256
|
}
|
|
1216
1257
|
changeState(state) {
|
|
1217
1258
|
const hasChanged = Object.keys(state).some(
|
|
@@ -1222,6 +1263,13 @@ var MicRecorder = class extends import_eventemitter34.EventEmitter {
|
|
|
1222
1263
|
this.emit("StateChange", this.state);
|
|
1223
1264
|
}
|
|
1224
1265
|
};
|
|
1266
|
+
function trimFrames(frames, length, max) {
|
|
1267
|
+
while (frames.length > 1 && length - frames[0].length >= max) {
|
|
1268
|
+
length -= frames[0].length;
|
|
1269
|
+
frames.shift();
|
|
1270
|
+
}
|
|
1271
|
+
return length;
|
|
1272
|
+
}
|
|
1225
1273
|
|
|
1226
1274
|
// src/audio/Pcm16AudioStream.ts
|
|
1227
1275
|
var import_eventemitter35 = require("eventemitter3");
|
|
@@ -1382,6 +1430,13 @@ var Pcm16AudioStream = class extends import_eventemitter35.EventEmitter {
|
|
|
1382
1430
|
// src/audio/types.ts
|
|
1383
1431
|
var import_eventemitter36 = require("eventemitter3");
|
|
1384
1432
|
var MicDriver = class extends import_eventemitter36.EventEmitter {
|
|
1433
|
+
/**
|
|
1434
|
+
* Whether the system removes the speaker from what the microphone hears.
|
|
1435
|
+
* False when it is known to be off, undefined when the system does not say.
|
|
1436
|
+
*/
|
|
1437
|
+
get echoCancellation() {
|
|
1438
|
+
return void 0;
|
|
1439
|
+
}
|
|
1385
1440
|
};
|
|
1386
1441
|
var SpeakerDriver = class extends import_eventemitter36.EventEmitter {
|
|
1387
1442
|
};
|
|
@@ -1414,8 +1469,30 @@ var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
|
1414
1469
|
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
1415
1470
|
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
1416
1471
|
MicdropServerCommands2["ToolCall"] = "ToolCall";
|
|
1472
|
+
MicdropServerCommands2["Classification"] = "Classification";
|
|
1417
1473
|
return MicdropServerCommands2;
|
|
1418
1474
|
})(MicdropServerCommands || {});
|
|
1475
|
+
function currentTurn(conversation) {
|
|
1476
|
+
let start = conversation.length;
|
|
1477
|
+
while (start > 0) {
|
|
1478
|
+
const item = conversation[start - 1];
|
|
1479
|
+
if (item.role !== "user" || item.metadata?.classification) break;
|
|
1480
|
+
start--;
|
|
1481
|
+
}
|
|
1482
|
+
const messages = conversation.slice(start);
|
|
1483
|
+
return {
|
|
1484
|
+
transcript: messages.map((message) => message.content).join(" "),
|
|
1485
|
+
messages,
|
|
1486
|
+
before: conversation.slice(0, start)
|
|
1487
|
+
};
|
|
1488
|
+
}
|
|
1489
|
+
function getTurnClassification(conversation) {
|
|
1490
|
+
for (let index = conversation.length - 1; index >= 0; index--) {
|
|
1491
|
+
const item = conversation[index];
|
|
1492
|
+
if (item.role === "user") return item.metadata?.classification;
|
|
1493
|
+
}
|
|
1494
|
+
return void 0;
|
|
1495
|
+
}
|
|
1419
1496
|
|
|
1420
1497
|
// src/client/MicdropClientError.ts
|
|
1421
1498
|
var MicdropClientErrorCode = /* @__PURE__ */ ((MicdropClientErrorCode2) => {
|
|
@@ -1481,6 +1558,9 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1481
1558
|
this._isMuted = false;
|
|
1482
1559
|
this._isPaused = false;
|
|
1483
1560
|
this._isReconnecting = false;
|
|
1561
|
+
// From start() until the connection opens, the microphone included: opening
|
|
1562
|
+
// it can take a while, for the permission prompt or the browser itself
|
|
1563
|
+
this._isStartingCall = false;
|
|
1484
1564
|
this.reconnectAttempt = 0;
|
|
1485
1565
|
this.start = async (options) => {
|
|
1486
1566
|
this.error = void 0;
|
|
@@ -1492,18 +1572,25 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1492
1572
|
this._isMuted = false;
|
|
1493
1573
|
this._isPaused = false;
|
|
1494
1574
|
this._isReconnecting = false;
|
|
1575
|
+
this._isStartingCall = true;
|
|
1495
1576
|
this.reconnectAttempt = 0;
|
|
1496
1577
|
if (this.reconnectTimer) {
|
|
1497
1578
|
clearTimeout(this.reconnectTimer);
|
|
1498
1579
|
this.reconnectTimer = void 0;
|
|
1499
1580
|
}
|
|
1581
|
+
this.notifyStateChange();
|
|
1500
1582
|
if (!this.micRecorder || options?.vad) {
|
|
1501
1583
|
await this.startMic({ vad: options?.vad });
|
|
1502
1584
|
}
|
|
1503
1585
|
await this.startWS();
|
|
1586
|
+
if (this.isWSStarted && this._isStartingCall) {
|
|
1587
|
+
this._isStartingCall = false;
|
|
1588
|
+
this.notifyStateChange();
|
|
1589
|
+
}
|
|
1504
1590
|
};
|
|
1505
1591
|
this.stop = async () => {
|
|
1506
1592
|
this.partialAssistantMessage = "";
|
|
1593
|
+
this._isStartingCall = false;
|
|
1507
1594
|
this._isProcessing = false;
|
|
1508
1595
|
this._isMuted = false;
|
|
1509
1596
|
this._isPaused = false;
|
|
@@ -1530,7 +1617,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1530
1617
|
this.notifyStateChange();
|
|
1531
1618
|
};
|
|
1532
1619
|
this.unmute = () => {
|
|
1533
|
-
if (!this.isPaused && !(this.
|
|
1620
|
+
if (!this.isPaused && !(this.isInterruptionDisabled && Speaker.isPlaying)) {
|
|
1534
1621
|
this.vad?.resume();
|
|
1535
1622
|
}
|
|
1536
1623
|
this._isMuted = false;
|
|
@@ -1571,6 +1658,9 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1571
1658
|
this.options.turnDetector,
|
|
1572
1659
|
this.options.turnMaxWait
|
|
1573
1660
|
);
|
|
1661
|
+
this.micRecorder.setSpeakerPlaying(
|
|
1662
|
+
this.isInterruptionDisabled && Speaker.isPlaying
|
|
1663
|
+
);
|
|
1574
1664
|
this.micRecorder.on("StateChange", () => {
|
|
1575
1665
|
this.notifyStateChange();
|
|
1576
1666
|
});
|
|
@@ -1602,6 +1692,9 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1602
1692
|
} else {
|
|
1603
1693
|
storage.removeItem("micdrop.micDevice" /* MicDevice */);
|
|
1604
1694
|
}
|
|
1695
|
+
if (Mic.echoCancellation === false && !this.options.disableInterruption) {
|
|
1696
|
+
this.log("No echo cancellation, interruptions are disabled");
|
|
1697
|
+
}
|
|
1605
1698
|
await this.micRecorder.start(Mic);
|
|
1606
1699
|
await Speaker.start();
|
|
1607
1700
|
await this.updateDevices();
|
|
@@ -1662,6 +1755,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1662
1755
|
this.connectionTimer = void 0;
|
|
1663
1756
|
}
|
|
1664
1757
|
this._isReconnecting = false;
|
|
1758
|
+
this._isStartingCall = false;
|
|
1665
1759
|
this.reconnectAttempt = 0;
|
|
1666
1760
|
this.error = void 0;
|
|
1667
1761
|
this.notifyStateChange();
|
|
@@ -1722,6 +1816,19 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1722
1816
|
} catch (error) {
|
|
1723
1817
|
console.error("[MicdropClient] Error parsing tool call:", data, error);
|
|
1724
1818
|
}
|
|
1819
|
+
} else if (data.startsWith("Classification" /* Classification */)) {
|
|
1820
|
+
try {
|
|
1821
|
+
const classification = JSON.parse(
|
|
1822
|
+
data.substring("Classification" /* Classification */.length + 1)
|
|
1823
|
+
);
|
|
1824
|
+
this.emit("Classification", classification);
|
|
1825
|
+
} catch (error) {
|
|
1826
|
+
console.error(
|
|
1827
|
+
"[MicdropClient] Error parsing classification:",
|
|
1828
|
+
data,
|
|
1829
|
+
error
|
|
1830
|
+
);
|
|
1831
|
+
}
|
|
1725
1832
|
}
|
|
1726
1833
|
};
|
|
1727
1834
|
this.onWSClose = (event) => {
|
|
@@ -1745,20 +1852,22 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1745
1852
|
};
|
|
1746
1853
|
this.onSpeakerStartPlaying = () => {
|
|
1747
1854
|
this.log("Speaker started");
|
|
1748
|
-
if (this.
|
|
1855
|
+
if (this.isInterruptionDisabled) {
|
|
1856
|
+
this.micRecorder?.setSpeakerPlaying(true);
|
|
1749
1857
|
this.vad?.pause();
|
|
1750
1858
|
}
|
|
1751
1859
|
this.notifyStateChange();
|
|
1752
1860
|
};
|
|
1753
1861
|
this.onSpeakerStopPlaying = () => {
|
|
1754
1862
|
this.log("Speaker stopped");
|
|
1755
|
-
|
|
1863
|
+
this.micRecorder?.setSpeakerPlaying(false);
|
|
1864
|
+
if (this.isInterruptionDisabled) {
|
|
1756
1865
|
setTimeout(() => {
|
|
1757
1866
|
if (!this.isMuted) {
|
|
1758
1867
|
this.vad?.resume();
|
|
1759
1868
|
this.notifyStateChange();
|
|
1760
1869
|
}
|
|
1761
|
-
},
|
|
1870
|
+
}, ECHO_TAIL);
|
|
1762
1871
|
} else {
|
|
1763
1872
|
this.notifyStateChange();
|
|
1764
1873
|
}
|
|
@@ -1774,7 +1883,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1774
1883
|
return (this.isWSStarted || this._isReconnecting) && (this.micRecorder?.state.isStarted || false);
|
|
1775
1884
|
}
|
|
1776
1885
|
get isStarting() {
|
|
1777
|
-
return (this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
|
|
1886
|
+
return (this._isStartingCall || this.isWSStarting || this.micRecorder?.state.isStarting || false) && !this._isReconnecting;
|
|
1778
1887
|
}
|
|
1779
1888
|
get isReconnecting() {
|
|
1780
1889
|
return this._isReconnecting;
|
|
@@ -1809,6 +1918,14 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
|
|
|
1809
1918
|
get isAssistantSpeaking() {
|
|
1810
1919
|
return Speaker.isPlaying;
|
|
1811
1920
|
}
|
|
1921
|
+
/**
|
|
1922
|
+
* Whether the user is kept from speaking over the assistant. Without echo
|
|
1923
|
+
* cancellation the microphone hears the assistant, which would interrupt
|
|
1924
|
+
* itself and end up in the transcript.
|
|
1925
|
+
*/
|
|
1926
|
+
get isInterruptionDisabled() {
|
|
1927
|
+
return !!this.options.disableInterruption || Mic.echoCancellation === false;
|
|
1928
|
+
}
|
|
1812
1929
|
get micDeviceId() {
|
|
1813
1930
|
return Mic.deviceId;
|
|
1814
1931
|
}
|
|
@@ -1960,7 +2077,9 @@ if (!globalScope2.micdropClient) {
|
|
|
1960
2077
|
var Micdrop = globalScope2.micdropClient;
|
|
1961
2078
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1962
2079
|
0 && (module.exports = {
|
|
2080
|
+
CONFIRM_MARGIN,
|
|
1963
2081
|
DEFAULT_TURN_MAX_WAIT,
|
|
2082
|
+
ECHO_TAIL,
|
|
1964
2083
|
Mic,
|
|
1965
2084
|
MicController,
|
|
1966
2085
|
MicDriver,
|
|
@@ -1986,10 +2105,12 @@ var Micdrop = globalScope2.micdropClient;
|
|
|
1986
2105
|
VolumeMeter,
|
|
1987
2106
|
VolumeVAD,
|
|
1988
2107
|
concatFloat32,
|
|
2108
|
+
currentTurn,
|
|
1989
2109
|
equalVADConfig,
|
|
1990
2110
|
floatToPcm16,
|
|
1991
2111
|
getClientErrorFromWSCloseEvent,
|
|
1992
2112
|
getClientErrorFromWSCloseEventCode,
|
|
2113
|
+
getTurnClassification,
|
|
1993
2114
|
getVAD,
|
|
1994
2115
|
getVolume,
|
|
1995
2116
|
isRecoverableError,
|