@micdrop/client 1.7.2 → 1.8.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/README.md +7 -4
- package/dist/index.d.mts +42 -24
- package/dist/index.d.ts +42 -24
- package/dist/index.js +103 -67
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +99 -64
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -173,26 +173,61 @@ function createDelayedStream(stream, delay) {
|
|
|
173
173
|
|
|
174
174
|
// src/audio/vad/VAD.ts
|
|
175
175
|
import EventEmitter2 from "eventemitter3";
|
|
176
|
+
var VADStatus = /* @__PURE__ */ ((VADStatus2) => {
|
|
177
|
+
VADStatus2[VADStatus2["Silence"] = 0] = "Silence";
|
|
178
|
+
VADStatus2[VADStatus2["MaybeSpeaking"] = 1] = "MaybeSpeaking";
|
|
179
|
+
VADStatus2[VADStatus2["Speaking"] = 2] = "Speaking";
|
|
180
|
+
return VADStatus2;
|
|
181
|
+
})(VADStatus || {});
|
|
176
182
|
var VAD = class extends EventEmitter2 {
|
|
177
183
|
constructor() {
|
|
178
184
|
super(...arguments);
|
|
185
|
+
this.status = 0 /* Silence */;
|
|
179
186
|
// Max delay to detect speech, can be used to record delayed stream
|
|
180
187
|
this.delay = 100;
|
|
181
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* Emits an event (overrides the default implementation)
|
|
191
|
+
* @param event - The event to emit
|
|
192
|
+
* @param args - The arguments to emit
|
|
193
|
+
* @returns True if the event was emitted, false otherwise
|
|
194
|
+
*/
|
|
195
|
+
emit(event, ...args) {
|
|
196
|
+
switch (event) {
|
|
197
|
+
case "StartSpeaking":
|
|
198
|
+
this.setStatus(1 /* MaybeSpeaking */);
|
|
199
|
+
break;
|
|
200
|
+
case "ConfirmSpeaking":
|
|
201
|
+
this.setStatus(2 /* Speaking */);
|
|
202
|
+
break;
|
|
203
|
+
case "CancelSpeaking":
|
|
204
|
+
this.setStatus(0 /* Silence */);
|
|
205
|
+
break;
|
|
206
|
+
case "StopSpeaking":
|
|
207
|
+
this.setStatus(0 /* Silence */);
|
|
208
|
+
break;
|
|
209
|
+
default:
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
return super.emit(event, ...args);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Sets the status of the VAD
|
|
216
|
+
* @param status - The status to set
|
|
217
|
+
*/
|
|
218
|
+
setStatus(status) {
|
|
219
|
+
if (this.status === status) return;
|
|
220
|
+
this.status = status;
|
|
221
|
+
super.emit("ChangeStatus", status);
|
|
222
|
+
}
|
|
182
223
|
};
|
|
183
224
|
|
|
184
225
|
// src/audio/vad/MultipleVAD.ts
|
|
185
226
|
var MultipleVAD = class extends VAD {
|
|
186
227
|
constructor(vads) {
|
|
187
228
|
super();
|
|
188
|
-
this.startSpeakingCount = 0;
|
|
189
|
-
this.confirmSpeakingCount = 0;
|
|
190
|
-
this.lastEventName = "StopSpeaking";
|
|
191
229
|
this.vads = vads.map((vad) => getVAD(vad));
|
|
192
|
-
|
|
193
|
-
emit(eventName, ..._) {
|
|
194
|
-
this.lastEventName = eventName;
|
|
195
|
-
return super.emit(eventName);
|
|
230
|
+
this.onStatusChange = this.onStatusChange.bind(this);
|
|
196
231
|
}
|
|
197
232
|
get isStarted() {
|
|
198
233
|
return this.vads.some((vad) => vad.isStarted);
|
|
@@ -202,40 +237,45 @@ var MultipleVAD = class extends VAD {
|
|
|
202
237
|
if (vad.isStarted) {
|
|
203
238
|
continue;
|
|
204
239
|
}
|
|
205
|
-
vad.on("
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
vad
|
|
212
|
-
|
|
213
|
-
|
|
240
|
+
vad.on("ChangeStatus", this.onStatusChange);
|
|
241
|
+
await vad.start(stream);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
onStatusChange() {
|
|
245
|
+
const isAllSilence = this.vads.every(
|
|
246
|
+
(vad) => vad.status === 0 /* Silence */
|
|
247
|
+
);
|
|
248
|
+
const isAllSpeaking = this.vads.every(
|
|
249
|
+
(vad) => vad.status === 2 /* Speaking */
|
|
250
|
+
);
|
|
251
|
+
switch (this.status) {
|
|
252
|
+
case 0 /* Silence */:
|
|
253
|
+
if (isAllSilence) break;
|
|
254
|
+
this.emit("StartSpeaking");
|
|
255
|
+
if (isAllSpeaking) {
|
|
214
256
|
this.emit("ConfirmSpeaking");
|
|
215
257
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (this.lastEventName === "StartSpeaking") {
|
|
258
|
+
break;
|
|
259
|
+
case 1 /* MaybeSpeaking */:
|
|
260
|
+
if (isAllSilence) {
|
|
220
261
|
this.emit("CancelSpeaking");
|
|
262
|
+
break;
|
|
221
263
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
264
|
+
if (isAllSpeaking) {
|
|
265
|
+
this.emit("ConfirmSpeaking");
|
|
266
|
+
}
|
|
267
|
+
break;
|
|
268
|
+
case 2 /* Speaking */:
|
|
269
|
+
if (isAllSilence) {
|
|
227
270
|
this.emit("StopSpeaking");
|
|
228
271
|
}
|
|
229
|
-
|
|
230
|
-
this.confirmSpeakingCount = 0;
|
|
231
|
-
});
|
|
232
|
-
await vad.start(stream);
|
|
272
|
+
break;
|
|
233
273
|
}
|
|
234
274
|
}
|
|
235
275
|
async stop() {
|
|
236
276
|
for (const vad of this.vads) {
|
|
277
|
+
vad.off("ChangeStatus", this.onStatusChange);
|
|
237
278
|
await vad.stop();
|
|
238
|
-
vad.removeAllListeners();
|
|
239
279
|
}
|
|
240
280
|
}
|
|
241
281
|
};
|
|
@@ -548,9 +588,6 @@ var MicRecorder = class extends EventEmitter3 {
|
|
|
548
588
|
}
|
|
549
589
|
}
|
|
550
590
|
async startSpeakingDetection(stream) {
|
|
551
|
-
if (!this.vad) {
|
|
552
|
-
throw new Error("VAD not initialized");
|
|
553
|
-
}
|
|
554
591
|
if (this.vad.isStarted) {
|
|
555
592
|
this.vad.stop();
|
|
556
593
|
}
|
|
@@ -561,9 +598,6 @@ var MicRecorder = class extends EventEmitter3 {
|
|
|
561
598
|
await this.vad.start(stream);
|
|
562
599
|
}
|
|
563
600
|
async stopSpeakingDetection() {
|
|
564
|
-
if (!this.vad) {
|
|
565
|
-
throw new Error("VAD not initialized");
|
|
566
|
-
}
|
|
567
601
|
this.vad.off("StartSpeaking", this.onStartSpeaking);
|
|
568
602
|
this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
569
603
|
this.vad.off("CancelSpeaking", this.onCancelSpeaking);
|
|
@@ -945,25 +979,25 @@ if (!window.micdropSpeaker) {
|
|
|
945
979
|
}
|
|
946
980
|
speaker = window.micdropSpeaker;
|
|
947
981
|
|
|
948
|
-
// src/call/
|
|
982
|
+
// src/call/CallClient.ts
|
|
949
983
|
import EventEmitter4 from "eventemitter3";
|
|
950
984
|
|
|
951
|
-
// src/call/
|
|
952
|
-
var
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
return
|
|
957
|
-
})(
|
|
958
|
-
var
|
|
985
|
+
// src/call/CallClientError.ts
|
|
986
|
+
var CallClientErrorCode = /* @__PURE__ */ ((CallClientErrorCode2) => {
|
|
987
|
+
CallClientErrorCode2["Mic"] = "Mic";
|
|
988
|
+
CallClientErrorCode2["Unauthorized"] = "Unauthorized";
|
|
989
|
+
CallClientErrorCode2["Error"] = "Error";
|
|
990
|
+
return CallClientErrorCode2;
|
|
991
|
+
})(CallClientErrorCode || {});
|
|
992
|
+
var CallClientError = class extends Error {
|
|
959
993
|
constructor(code = "Error" /* Error */) {
|
|
960
994
|
super();
|
|
961
995
|
this.code = code;
|
|
962
996
|
}
|
|
963
997
|
};
|
|
964
998
|
|
|
965
|
-
// src/call/
|
|
966
|
-
var
|
|
999
|
+
// src/call/CallClient.ts
|
|
1000
|
+
var CallClient = class _CallClient extends EventEmitter4 {
|
|
967
1001
|
constructor(options) {
|
|
968
1002
|
super();
|
|
969
1003
|
this.options = options;
|
|
@@ -974,10 +1008,10 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
974
1008
|
this.vad = getVAD(this.options?.vad);
|
|
975
1009
|
}
|
|
976
1010
|
static getInstance(options) {
|
|
977
|
-
if (!window.
|
|
978
|
-
window.
|
|
1011
|
+
if (!window.micdropCallClient) {
|
|
1012
|
+
window.micdropCallClient = new _CallClient(options);
|
|
979
1013
|
}
|
|
980
|
-
return window.
|
|
1014
|
+
return window.micdropCallClient;
|
|
981
1015
|
}
|
|
982
1016
|
get isStarted() {
|
|
983
1017
|
return this.isWSStarted && this.micRecorder?.state.isStarted || false;
|
|
@@ -1014,13 +1048,13 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
1014
1048
|
try {
|
|
1015
1049
|
this.stopWS();
|
|
1016
1050
|
} catch (error) {
|
|
1017
|
-
console.error("[
|
|
1051
|
+
console.error("[CallClient] stop WS", error);
|
|
1018
1052
|
}
|
|
1019
1053
|
try {
|
|
1020
1054
|
this.micRecorder?.stop();
|
|
1021
1055
|
this.stopMic();
|
|
1022
1056
|
} catch (error) {
|
|
1023
|
-
console.error("[
|
|
1057
|
+
console.error("[CallClient] stop mic", error);
|
|
1024
1058
|
}
|
|
1025
1059
|
}
|
|
1026
1060
|
pause() {
|
|
@@ -1073,8 +1107,8 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
1073
1107
|
await this.micRecorder.start(this.micStream);
|
|
1074
1108
|
}
|
|
1075
1109
|
} catch (error) {
|
|
1076
|
-
console.error("[
|
|
1077
|
-
this.emit("Error", new
|
|
1110
|
+
console.error("[CallClient] startMic", error);
|
|
1111
|
+
this.emit("Error", new CallClientError("Mic" /* Mic */));
|
|
1078
1112
|
this.stop();
|
|
1079
1113
|
}
|
|
1080
1114
|
}
|
|
@@ -1087,13 +1121,13 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
1087
1121
|
}
|
|
1088
1122
|
async startWS() {
|
|
1089
1123
|
if (this.ws) {
|
|
1090
|
-
throw new Error("[
|
|
1124
|
+
throw new Error("[CallClient] startWS: WebSocket is already started");
|
|
1091
1125
|
}
|
|
1092
1126
|
if (!this.isMicStarted) {
|
|
1093
|
-
throw new Error("[
|
|
1127
|
+
throw new Error("[CallClient] startWS: Microphone is not started");
|
|
1094
1128
|
}
|
|
1095
1129
|
if (!this.url) {
|
|
1096
|
-
throw new Error("[
|
|
1130
|
+
throw new Error("[CallClient] startWS: URL is not set");
|
|
1097
1131
|
}
|
|
1098
1132
|
this.ws = new WebSocket(this.url);
|
|
1099
1133
|
this.ws.binaryType = "blob";
|
|
@@ -1147,18 +1181,18 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
1147
1181
|
this.log("[WS] Closed", event);
|
|
1148
1182
|
this.stop();
|
|
1149
1183
|
if (event.code >= 1001 && event.code <= 1011 && event.code !== 1005) {
|
|
1150
|
-
this.emit("Error", new
|
|
1184
|
+
this.emit("Error", new CallClientError());
|
|
1151
1185
|
} else if (event.code === 4401) {
|
|
1152
1186
|
this.emit(
|
|
1153
1187
|
"Error",
|
|
1154
|
-
new
|
|
1188
|
+
new CallClientError("Unauthorized" /* Unauthorized */)
|
|
1155
1189
|
);
|
|
1156
1190
|
} else if (event.code >= 4e3) {
|
|
1157
|
-
this.emit("Error", new
|
|
1191
|
+
this.emit("Error", new CallClientError());
|
|
1158
1192
|
}
|
|
1159
1193
|
};
|
|
1160
1194
|
this.ws.onerror = (event) => {
|
|
1161
|
-
console.error("[
|
|
1195
|
+
console.error("[CallClient] [WS] Error:", event);
|
|
1162
1196
|
this.stop();
|
|
1163
1197
|
};
|
|
1164
1198
|
}
|
|
@@ -1198,10 +1232,10 @@ var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
|
|
|
1198
1232
|
return CallServerCommands2;
|
|
1199
1233
|
})(CallServerCommands || {});
|
|
1200
1234
|
export {
|
|
1235
|
+
CallClient,
|
|
1201
1236
|
CallClientCommands,
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
CallHandlerErrorCode,
|
|
1237
|
+
CallClientError,
|
|
1238
|
+
CallClientErrorCode,
|
|
1205
1239
|
CallServerCommands,
|
|
1206
1240
|
LocalStorageKeys,
|
|
1207
1241
|
mic as Mic,
|
|
@@ -1210,6 +1244,7 @@ export {
|
|
|
1210
1244
|
SileroVAD,
|
|
1211
1245
|
speaker as Speaker,
|
|
1212
1246
|
VAD,
|
|
1247
|
+
VADStatus,
|
|
1213
1248
|
VolumeVAD
|
|
1214
1249
|
};
|
|
1215
1250
|
//# sourceMappingURL=index.mjs.map
|