@micdrop/client 1.8.1 → 2.0.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 +81 -30
- package/dist/index.d.mts +192 -96
- package/dist/index.d.ts +192 -96
- package/dist/index.js +675 -664
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +665 -657
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -5
package/dist/index.js
CHANGED
|
@@ -30,20 +30,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
CallClient: () => CallClient,
|
|
34
|
-
CallClientCommands: () => CallClientCommands,
|
|
35
|
-
CallClientError: () => CallClientError,
|
|
36
|
-
CallClientErrorCode: () => CallClientErrorCode,
|
|
37
|
-
CallServerCommands: () => CallServerCommands,
|
|
38
33
|
LocalStorageKeys: () => LocalStorageKeys,
|
|
39
|
-
Mic: () =>
|
|
34
|
+
Mic: () => Mic2,
|
|
40
35
|
MicRecorder: () => MicRecorder,
|
|
36
|
+
Micdrop: () => Micdrop,
|
|
37
|
+
MicdropClient: () => MicdropClient,
|
|
38
|
+
MicdropClientCommands: () => MicdropClientCommands,
|
|
39
|
+
MicdropClientError: () => MicdropClientError,
|
|
40
|
+
MicdropClientErrorCode: () => MicdropClientErrorCode,
|
|
41
|
+
MicdropServerCommands: () => MicdropServerCommands,
|
|
41
42
|
MultipleVAD: () => MultipleVAD,
|
|
42
43
|
SileroVAD: () => SileroVAD,
|
|
43
|
-
Speaker: () =>
|
|
44
|
+
Speaker: () => Speaker2,
|
|
44
45
|
VAD: () => VAD,
|
|
45
46
|
VADStatus: () => VADStatus,
|
|
46
|
-
VolumeVAD: () => VolumeVAD
|
|
47
|
+
VolumeVAD: () => VolumeVAD,
|
|
48
|
+
getClientErrorFromWSCloseEvent: () => getClientErrorFromWSCloseEvent,
|
|
49
|
+
getClientErrorFromWSCloseEventCode: () => getClientErrorFromWSCloseEventCode
|
|
47
50
|
});
|
|
48
51
|
module.exports = __toCommonJS(index_exports);
|
|
49
52
|
|
|
@@ -133,8 +136,7 @@ function stopStream(stream) {
|
|
|
133
136
|
stream?.getTracks().forEach((track) => track.stop());
|
|
134
137
|
}
|
|
135
138
|
|
|
136
|
-
// src/audio/
|
|
137
|
-
var mic;
|
|
139
|
+
// src/audio/Mic.ts
|
|
138
140
|
var Mic = class {
|
|
139
141
|
constructor() {
|
|
140
142
|
this.analyser = new AudioAnalyser(audioContext);
|
|
@@ -165,7 +167,7 @@ var Mic = class {
|
|
|
165
167
|
deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
|
|
166
168
|
}
|
|
167
169
|
if (this.audioStream) {
|
|
168
|
-
if (this.
|
|
170
|
+
if (this.deviceId === deviceId) {
|
|
169
171
|
return this.audioStream;
|
|
170
172
|
}
|
|
171
173
|
stopStream(this.audioStream);
|
|
@@ -177,10 +179,10 @@ var Mic = class {
|
|
|
177
179
|
const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
|
|
178
180
|
if (newDeviceId) {
|
|
179
181
|
localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
|
|
180
|
-
this.
|
|
182
|
+
this.deviceId = newDeviceId;
|
|
181
183
|
} else {
|
|
182
184
|
localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
|
|
183
|
-
this.
|
|
185
|
+
this.deviceId = void 0;
|
|
184
186
|
}
|
|
185
187
|
if (this.sourceNode) this.sourceNode.disconnect();
|
|
186
188
|
this.sourceNode = audioContext.createMediaStreamSource(this.audioStream);
|
|
@@ -201,14 +203,199 @@ var Mic = class {
|
|
|
201
203
|
}
|
|
202
204
|
}
|
|
203
205
|
};
|
|
204
|
-
if (!window.micdropMic) {
|
|
205
|
-
window.micdropMic = new Mic();
|
|
206
|
-
}
|
|
207
|
-
mic = window.micdropMic;
|
|
208
206
|
|
|
209
|
-
// src/audio/
|
|
207
|
+
// src/audio/Speaker.ts
|
|
210
208
|
var import_eventemitter33 = __toESM(require("eventemitter3"));
|
|
211
209
|
|
|
210
|
+
// src/audio/stream/AudioStream.ts
|
|
211
|
+
var import_eventemitter32 = __toESM(require("eventemitter3"));
|
|
212
|
+
var AudioStream = class extends import_eventemitter32.default {
|
|
213
|
+
constructor() {
|
|
214
|
+
super();
|
|
215
|
+
this.isPlaying = false;
|
|
216
|
+
this.outputNode = audioContext.createGain();
|
|
217
|
+
}
|
|
218
|
+
setIsPlaying(isPlaying) {
|
|
219
|
+
if (this.isPlaying === isPlaying) return;
|
|
220
|
+
this.isPlaying = isPlaying;
|
|
221
|
+
this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// src/audio/stream/Pcm16AudioStream.ts
|
|
226
|
+
var SAMPLE_RATE = 16e3;
|
|
227
|
+
var CHANNELS = 1;
|
|
228
|
+
var Pcm16AudioStream = class extends AudioStream {
|
|
229
|
+
constructor() {
|
|
230
|
+
super();
|
|
231
|
+
this.audioQueue = [];
|
|
232
|
+
this.isProcessingQueue = false;
|
|
233
|
+
}
|
|
234
|
+
start() {
|
|
235
|
+
return this.outputNode;
|
|
236
|
+
}
|
|
237
|
+
async playAudio(blob) {
|
|
238
|
+
try {
|
|
239
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
240
|
+
const audioBuffer = this.createAudioBuffer(arrayBuffer);
|
|
241
|
+
if (audioBuffer) {
|
|
242
|
+
this.audioQueue.push(audioBuffer);
|
|
243
|
+
this.processQueue();
|
|
244
|
+
}
|
|
245
|
+
} catch (error) {
|
|
246
|
+
console.error("Failed to play audio chunk:", error);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
stopAudio() {
|
|
250
|
+
if (this.currentSourceNode) {
|
|
251
|
+
try {
|
|
252
|
+
this.currentSourceNode.stop();
|
|
253
|
+
this.currentSourceNode.disconnect();
|
|
254
|
+
} catch (error) {
|
|
255
|
+
}
|
|
256
|
+
this.currentSourceNode = void 0;
|
|
257
|
+
}
|
|
258
|
+
this.audioQueue = [];
|
|
259
|
+
this.isProcessingQueue = false;
|
|
260
|
+
this.setIsPlaying(false);
|
|
261
|
+
}
|
|
262
|
+
createAudioBuffer(arrayBuffer) {
|
|
263
|
+
try {
|
|
264
|
+
const int16Data = new Int16Array(arrayBuffer);
|
|
265
|
+
const numSamples = int16Data.length;
|
|
266
|
+
if (numSamples === 0) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
const audioBuffer = audioContext.createBuffer(
|
|
270
|
+
CHANNELS,
|
|
271
|
+
numSamples,
|
|
272
|
+
SAMPLE_RATE
|
|
273
|
+
);
|
|
274
|
+
const channelData = audioBuffer.getChannelData(0);
|
|
275
|
+
for (let i = 0; i < numSamples; i++) {
|
|
276
|
+
channelData[i] = int16Data[i] / 32768;
|
|
277
|
+
}
|
|
278
|
+
return audioBuffer;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.error("Error creating AudioBuffer:", error);
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
processQueue() {
|
|
285
|
+
if (this.isProcessingQueue || this.audioQueue.length === 0) {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
this.isProcessingQueue = true;
|
|
289
|
+
this.playNextBuffer();
|
|
290
|
+
}
|
|
291
|
+
playNextBuffer() {
|
|
292
|
+
if (this.audioQueue.length === 0) {
|
|
293
|
+
this.isProcessingQueue = false;
|
|
294
|
+
this.setIsPlaying(false);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const audioBuffer = this.audioQueue.shift();
|
|
298
|
+
this.currentSourceNode = audioContext.createBufferSource();
|
|
299
|
+
this.currentSourceNode.buffer = audioBuffer;
|
|
300
|
+
this.currentSourceNode.connect(this.outputNode);
|
|
301
|
+
this.currentSourceNode.onended = () => {
|
|
302
|
+
this.currentSourceNode = void 0;
|
|
303
|
+
this.playNextBuffer();
|
|
304
|
+
};
|
|
305
|
+
this.currentSourceNode.start(0);
|
|
306
|
+
this.setIsPlaying(true);
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// src/audio/Speaker.ts
|
|
311
|
+
var Speaker = class extends import_eventemitter33.default {
|
|
312
|
+
constructor() {
|
|
313
|
+
super(...arguments);
|
|
314
|
+
this.isPlaying = false;
|
|
315
|
+
this.analyser = new AudioAnalyser(audioContext);
|
|
316
|
+
// Set by MicdropClient, useful to detect default device
|
|
317
|
+
this.devices = [];
|
|
318
|
+
}
|
|
319
|
+
// Used when audiocontext sinkId is not available
|
|
320
|
+
async start() {
|
|
321
|
+
if (this.audioStream) return;
|
|
322
|
+
this.audioStream = new Pcm16AudioStream();
|
|
323
|
+
this.audioStream.on("StartPlaying", () => this.setIsPlaying(true));
|
|
324
|
+
this.audioStream.on("StopPlaying", () => this.setIsPlaying(false));
|
|
325
|
+
this.audioSourceNode = this.audioStream.start();
|
|
326
|
+
const deviceId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) ?? void 0;
|
|
327
|
+
await this.setDevice(deviceId);
|
|
328
|
+
}
|
|
329
|
+
async playAudio(blob) {
|
|
330
|
+
this.start();
|
|
331
|
+
try {
|
|
332
|
+
await this.audioStream?.playAudio(blob);
|
|
333
|
+
} catch (error) {
|
|
334
|
+
console.error("Error playing audio blob", error, blob);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
async stopAudio() {
|
|
338
|
+
if (!this.isPlaying) return;
|
|
339
|
+
this.audioStream?.stopAudio();
|
|
340
|
+
}
|
|
341
|
+
async changeDevice(deviceId) {
|
|
342
|
+
await this.setDevice(deviceId);
|
|
343
|
+
if (this.isDefaultDevice) {
|
|
344
|
+
localStorage.removeItem("micdrop.speakerDevice" /* SpeakerDevice */);
|
|
345
|
+
} else {
|
|
346
|
+
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, deviceId);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
get isDefaultDevice() {
|
|
350
|
+
return !this.deviceId || this.devices.length === 0 || this.deviceId === this.devices[0].deviceId;
|
|
351
|
+
}
|
|
352
|
+
setIsPlaying(isPlaying) {
|
|
353
|
+
if (this.isPlaying === isPlaying) return;
|
|
354
|
+
this.isPlaying = isPlaying;
|
|
355
|
+
this.emit(isPlaying ? "StartPlaying" : "StopPlaying");
|
|
356
|
+
}
|
|
357
|
+
async setDevice(deviceId) {
|
|
358
|
+
if (!this.audioSourceNode) {
|
|
359
|
+
console.error(
|
|
360
|
+
"audioSourceNode not initialized, setDevice called before start"
|
|
361
|
+
);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
try {
|
|
365
|
+
this.deviceId = deviceId;
|
|
366
|
+
const defaultDevice = this.isDefaultDevice;
|
|
367
|
+
const sinkId = !deviceId || defaultDevice ? "" : deviceId;
|
|
368
|
+
if (defaultDevice || "setSinkId" in audioContext) {
|
|
369
|
+
await audioContext.setSinkId?.(sinkId);
|
|
370
|
+
this.audioSourceNode.disconnect();
|
|
371
|
+
this.audioSourceNode.connect(audioContext.destination);
|
|
372
|
+
this.audioSourceNode.connect(this.analyser.node);
|
|
373
|
+
if (this.audioElementDestination) {
|
|
374
|
+
this.audioElementDestination.pause();
|
|
375
|
+
this.audioElementDestination.srcObject = null;
|
|
376
|
+
this.audioElementDestination = void 0;
|
|
377
|
+
}
|
|
378
|
+
} else {
|
|
379
|
+
if (!this.audioElementDestination) {
|
|
380
|
+
const mediaStreamDestination = audioContext.createMediaStreamDestination();
|
|
381
|
+
this.audioSourceNode.disconnect();
|
|
382
|
+
this.audioSourceNode.connect(mediaStreamDestination);
|
|
383
|
+
this.audioSourceNode.connect(this.analyser.node);
|
|
384
|
+
this.audioElementDestination = new Audio();
|
|
385
|
+
this.audioElementDestination.autoplay = true;
|
|
386
|
+
this.audioElementDestination.srcObject = mediaStreamDestination.stream;
|
|
387
|
+
}
|
|
388
|
+
await this.audioElementDestination.setSinkId(sinkId);
|
|
389
|
+
}
|
|
390
|
+
} catch (error) {
|
|
391
|
+
console.error(`Error setting Audio Output to ${deviceId}`, error);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
// src/audio/MicRecorder.ts
|
|
397
|
+
var import_eventemitter35 = __toESM(require("eventemitter3"));
|
|
398
|
+
|
|
212
399
|
// src/audio/utils/delayedStream.ts
|
|
213
400
|
function createDelayedStream(stream, delay) {
|
|
214
401
|
const audioSource = audioContext.createMediaStreamSource(stream);
|
|
@@ -221,16 +408,17 @@ function createDelayedStream(stream, delay) {
|
|
|
221
408
|
}
|
|
222
409
|
|
|
223
410
|
// src/audio/vad/VAD.ts
|
|
224
|
-
var
|
|
411
|
+
var import_eventemitter34 = __toESM(require("eventemitter3"));
|
|
225
412
|
var VADStatus = /* @__PURE__ */ ((VADStatus2) => {
|
|
226
413
|
VADStatus2[VADStatus2["Silence"] = 0] = "Silence";
|
|
227
414
|
VADStatus2[VADStatus2["MaybeSpeaking"] = 1] = "MaybeSpeaking";
|
|
228
415
|
VADStatus2[VADStatus2["Speaking"] = 2] = "Speaking";
|
|
229
416
|
return VADStatus2;
|
|
230
417
|
})(VADStatus || {});
|
|
231
|
-
var VAD = class extends
|
|
418
|
+
var VAD = class extends import_eventemitter34.default {
|
|
232
419
|
constructor() {
|
|
233
420
|
super(...arguments);
|
|
421
|
+
this.name = this.constructor.name;
|
|
234
422
|
this.status = 0 /* Silence */;
|
|
235
423
|
// Max delay to detect speech, can be used to record delayed stream
|
|
236
424
|
this.delay = 100;
|
|
@@ -275,8 +463,38 @@ var VAD = class extends import_eventemitter32.default {
|
|
|
275
463
|
var MultipleVAD = class extends VAD {
|
|
276
464
|
constructor(vads) {
|
|
277
465
|
super();
|
|
466
|
+
this.onStatusChange = () => {
|
|
467
|
+
const isAllSilence = this.vads.every(
|
|
468
|
+
(vad) => vad.status === 0 /* Silence */
|
|
469
|
+
);
|
|
470
|
+
const isAllSpeaking = this.vads.every(
|
|
471
|
+
(vad) => vad.status === 2 /* Speaking */
|
|
472
|
+
);
|
|
473
|
+
switch (this.status) {
|
|
474
|
+
case 0 /* Silence */:
|
|
475
|
+
if (isAllSilence) break;
|
|
476
|
+
this.emit("StartSpeaking");
|
|
477
|
+
if (isAllSpeaking) {
|
|
478
|
+
this.emit("ConfirmSpeaking");
|
|
479
|
+
}
|
|
480
|
+
break;
|
|
481
|
+
case 1 /* MaybeSpeaking */:
|
|
482
|
+
if (isAllSilence) {
|
|
483
|
+
this.emit("CancelSpeaking");
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
if (isAllSpeaking) {
|
|
487
|
+
this.emit("ConfirmSpeaking");
|
|
488
|
+
}
|
|
489
|
+
break;
|
|
490
|
+
case 2 /* Speaking */:
|
|
491
|
+
if (isAllSilence) {
|
|
492
|
+
this.emit("StopSpeaking");
|
|
493
|
+
}
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
};
|
|
278
497
|
this.vads = vads.map((vad) => getVAD(vad));
|
|
279
|
-
this.onStatusChange = this.onStatusChange.bind(this);
|
|
280
498
|
}
|
|
281
499
|
get isStarted() {
|
|
282
500
|
return this.vads.some((vad) => vad.isStarted);
|
|
@@ -290,37 +508,6 @@ var MultipleVAD = class extends VAD {
|
|
|
290
508
|
await vad.start(stream);
|
|
291
509
|
}
|
|
292
510
|
}
|
|
293
|
-
onStatusChange() {
|
|
294
|
-
const isAllSilence = this.vads.every(
|
|
295
|
-
(vad) => vad.status === 0 /* Silence */
|
|
296
|
-
);
|
|
297
|
-
const isAllSpeaking = this.vads.every(
|
|
298
|
-
(vad) => vad.status === 2 /* Speaking */
|
|
299
|
-
);
|
|
300
|
-
switch (this.status) {
|
|
301
|
-
case 0 /* Silence */:
|
|
302
|
-
if (isAllSilence) break;
|
|
303
|
-
this.emit("StartSpeaking");
|
|
304
|
-
if (isAllSpeaking) {
|
|
305
|
-
this.emit("ConfirmSpeaking");
|
|
306
|
-
}
|
|
307
|
-
break;
|
|
308
|
-
case 1 /* MaybeSpeaking */:
|
|
309
|
-
if (isAllSilence) {
|
|
310
|
-
this.emit("CancelSpeaking");
|
|
311
|
-
break;
|
|
312
|
-
}
|
|
313
|
-
if (isAllSpeaking) {
|
|
314
|
-
this.emit("ConfirmSpeaking");
|
|
315
|
-
}
|
|
316
|
-
break;
|
|
317
|
-
case 2 /* Speaking */:
|
|
318
|
-
if (isAllSilence) {
|
|
319
|
-
this.emit("StopSpeaking");
|
|
320
|
-
}
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
511
|
async stop() {
|
|
325
512
|
for (const vad of this.vads) {
|
|
326
513
|
vad.off("ChangeStatus", this.onStatusChange);
|
|
@@ -330,7 +517,7 @@ var MultipleVAD = class extends VAD {
|
|
|
330
517
|
};
|
|
331
518
|
|
|
332
519
|
// src/audio/vad/SileroVAD.ts
|
|
333
|
-
var
|
|
520
|
+
var import_vad_web = require("@ricky0123/vad-web");
|
|
334
521
|
var defaultOptions = {
|
|
335
522
|
positiveSpeechThreshold: 0.18,
|
|
336
523
|
negativeSpeechThreshold: 0.11,
|
|
@@ -359,9 +546,10 @@ var SileroVAD = class extends VAD {
|
|
|
359
546
|
}
|
|
360
547
|
async start(stream) {
|
|
361
548
|
if (this.vad) return;
|
|
362
|
-
this.vad = await
|
|
549
|
+
this.vad = await import_vad_web.MicVAD.new({
|
|
363
550
|
stream,
|
|
364
551
|
model: "v5",
|
|
552
|
+
submitUserSpeechOnPause: true,
|
|
365
553
|
onSpeechStart: () => this.emit("StartSpeaking"),
|
|
366
554
|
onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
|
|
367
555
|
onVADMisfire: () => this.emit("CancelSpeaking"),
|
|
@@ -513,8 +701,9 @@ var VolumeVAD = class extends VAD {
|
|
|
513
701
|
};
|
|
514
702
|
|
|
515
703
|
// src/audio/vad/getVAD.ts
|
|
704
|
+
var DefaultVAD = VolumeVAD;
|
|
516
705
|
function getVAD(vad) {
|
|
517
|
-
if (!vad) return new
|
|
706
|
+
if (!vad) return new DefaultVAD();
|
|
518
707
|
if (vad === "volume") return new VolumeVAD();
|
|
519
708
|
if (vad === "silero") return new SileroVAD();
|
|
520
709
|
if (Array.isArray(vad)) {
|
|
@@ -531,13 +720,74 @@ var defaultMicRecorderState = {
|
|
|
531
720
|
isMuted: false,
|
|
532
721
|
isSpeaking: false
|
|
533
722
|
};
|
|
534
|
-
var MicRecorder = class extends
|
|
723
|
+
var MicRecorder = class extends import_eventemitter35.default {
|
|
535
724
|
constructor(vadConfig) {
|
|
536
725
|
super();
|
|
537
726
|
this.audioInfo = this.getAudioInfo();
|
|
538
727
|
this.speakingConfirmed = false;
|
|
539
728
|
this.queuedChunks = [];
|
|
540
|
-
this.
|
|
729
|
+
this.start = async (stream) => {
|
|
730
|
+
if (this.state.isStarted) {
|
|
731
|
+
throw new Error("MicRecorder is already started");
|
|
732
|
+
}
|
|
733
|
+
try {
|
|
734
|
+
this.changeState({
|
|
735
|
+
isStarting: true,
|
|
736
|
+
isMuted: false,
|
|
737
|
+
isSpeaking: false
|
|
738
|
+
});
|
|
739
|
+
const delayedStream = createDelayedStream(
|
|
740
|
+
stream,
|
|
741
|
+
this.vad.delay / 1e3 + 0.05
|
|
742
|
+
);
|
|
743
|
+
this.delayedStream = delayedStream;
|
|
744
|
+
if (this.recorder) {
|
|
745
|
+
this.recorder.stop();
|
|
746
|
+
}
|
|
747
|
+
this.recorder = new window.MediaRecorder(delayedStream, {
|
|
748
|
+
mimeType: this.audioInfo.mimeType,
|
|
749
|
+
audioBitsPerSecond: 128e3
|
|
750
|
+
});
|
|
751
|
+
this.recorder.ondataavailable = this.onDataAvailable;
|
|
752
|
+
await this.startSpeakingDetection(stream);
|
|
753
|
+
this.changeState({
|
|
754
|
+
isStarting: false,
|
|
755
|
+
isStarted: true
|
|
756
|
+
});
|
|
757
|
+
} catch (err) {
|
|
758
|
+
this.stop();
|
|
759
|
+
console.error(err);
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
this.mute = () => {
|
|
763
|
+
this.changeState({ isMuted: true, isSpeaking: false });
|
|
764
|
+
this.recorder?.stop();
|
|
765
|
+
};
|
|
766
|
+
this.unmute = () => {
|
|
767
|
+
this.changeState({ isMuted: false });
|
|
768
|
+
};
|
|
769
|
+
this.stop = () => {
|
|
770
|
+
this.changeState({
|
|
771
|
+
isStarting: false,
|
|
772
|
+
isStarted: false,
|
|
773
|
+
isMuted: false,
|
|
774
|
+
isSpeaking: false
|
|
775
|
+
});
|
|
776
|
+
try {
|
|
777
|
+
this.stopSpeakingDetection();
|
|
778
|
+
if (this.recorder) {
|
|
779
|
+
this.recorder.stop();
|
|
780
|
+
this.recorder = void 0;
|
|
781
|
+
}
|
|
782
|
+
if (this.delayedStream) {
|
|
783
|
+
stopStream(this.delayedStream);
|
|
784
|
+
this.delayedStream = void 0;
|
|
785
|
+
}
|
|
786
|
+
} catch (err) {
|
|
787
|
+
console.error(err);
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
this.onStartSpeaking = async () => {
|
|
541
791
|
if (!this.recorder || this.state.isMuted) return;
|
|
542
792
|
try {
|
|
543
793
|
this.recorder.start(timeSlice);
|
|
@@ -545,14 +795,14 @@ var MicRecorder = class extends import_eventemitter33.default {
|
|
|
545
795
|
} catch (error) {
|
|
546
796
|
console.error(error);
|
|
547
797
|
}
|
|
548
|
-
}
|
|
549
|
-
this.onConfirmSpeaking =
|
|
798
|
+
};
|
|
799
|
+
this.onConfirmSpeaking = async () => {
|
|
550
800
|
if (this.state.isMuted) return;
|
|
551
801
|
this.emit("StartSpeaking");
|
|
552
802
|
this.changeState({ isSpeaking: true });
|
|
553
803
|
this.speakingConfirmed = true;
|
|
554
|
-
}
|
|
555
|
-
this.onCancelSpeaking =
|
|
804
|
+
};
|
|
805
|
+
this.onCancelSpeaking = async () => {
|
|
556
806
|
if (this.state.isMuted) return;
|
|
557
807
|
try {
|
|
558
808
|
this.recorder?.stop();
|
|
@@ -560,8 +810,8 @@ var MicRecorder = class extends import_eventemitter33.default {
|
|
|
560
810
|
console.error(error);
|
|
561
811
|
}
|
|
562
812
|
this.queuedChunks.length = 0;
|
|
563
|
-
}
|
|
564
|
-
this.onStopSpeaking =
|
|
813
|
+
};
|
|
814
|
+
this.onStopSpeaking = async () => {
|
|
565
815
|
if (this.state.isMuted) return;
|
|
566
816
|
try {
|
|
567
817
|
this.recorder?.stop();
|
|
@@ -571,70 +821,21 @@ var MicRecorder = class extends import_eventemitter33.default {
|
|
|
571
821
|
this.changeState({ isSpeaking: false });
|
|
572
822
|
this.emit("StopSpeaking");
|
|
573
823
|
this.speakingConfirmed = false;
|
|
574
|
-
}
|
|
575
|
-
this.
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
throw new Error("MicRecorder is already started");
|
|
581
|
-
}
|
|
582
|
-
try {
|
|
583
|
-
this.changeState({
|
|
584
|
-
isStarting: true,
|
|
585
|
-
isMuted: false,
|
|
586
|
-
isSpeaking: false
|
|
587
|
-
});
|
|
588
|
-
const delayedStream = createDelayedStream(
|
|
589
|
-
stream,
|
|
590
|
-
this.vad.delay / 1e3 + 0.05
|
|
591
|
-
);
|
|
592
|
-
this.delayedStream = delayedStream;
|
|
593
|
-
if (this.recorder) {
|
|
594
|
-
this.recorder.stop();
|
|
595
|
-
}
|
|
596
|
-
this.recorder = new window.MediaRecorder(delayedStream, {
|
|
597
|
-
mimeType: this.audioInfo.mimeType,
|
|
598
|
-
audioBitsPerSecond: 128e3
|
|
599
|
-
});
|
|
600
|
-
this.recorder.ondataavailable = this.onDataAvailable.bind(this);
|
|
601
|
-
await this.startSpeakingDetection(stream);
|
|
602
|
-
this.changeState({
|
|
603
|
-
isStarting: false,
|
|
604
|
-
isStarted: true
|
|
605
|
-
});
|
|
606
|
-
} catch (err) {
|
|
607
|
-
this.stop();
|
|
608
|
-
console.error(err);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
mute() {
|
|
612
|
-
this.changeState({ isMuted: true, isSpeaking: false });
|
|
613
|
-
this.recorder?.stop();
|
|
614
|
-
}
|
|
615
|
-
unmute() {
|
|
616
|
-
this.changeState({ isMuted: false });
|
|
617
|
-
}
|
|
618
|
-
stop() {
|
|
619
|
-
this.changeState({
|
|
620
|
-
isStarting: false,
|
|
621
|
-
isStarted: false,
|
|
622
|
-
isMuted: false,
|
|
623
|
-
isSpeaking: false
|
|
624
|
-
});
|
|
625
|
-
try {
|
|
626
|
-
this.stopSpeakingDetection();
|
|
627
|
-
if (this.recorder) {
|
|
628
|
-
this.recorder.stop();
|
|
629
|
-
this.recorder = void 0;
|
|
824
|
+
};
|
|
825
|
+
this.onDataAvailable = async (blobEvent) => {
|
|
826
|
+
if (this.state.isMuted) return;
|
|
827
|
+
if (!this.speakingConfirmed) {
|
|
828
|
+
this.queuedChunks.push(blobEvent.data);
|
|
829
|
+
return;
|
|
630
830
|
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
this.delayedStream = void 0;
|
|
831
|
+
for (const chunk of this.queuedChunks) {
|
|
832
|
+
this.emit("Chunk", chunk);
|
|
634
833
|
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
}
|
|
834
|
+
this.queuedChunks.length = 0;
|
|
835
|
+
this.emit("Chunk", blobEvent.data);
|
|
836
|
+
};
|
|
837
|
+
this.state = defaultMicRecorderState;
|
|
838
|
+
this.vad = getVAD(vadConfig);
|
|
638
839
|
}
|
|
639
840
|
async startSpeakingDetection(stream) {
|
|
640
841
|
if (this.vad.isStarted) {
|
|
@@ -653,18 +854,6 @@ var MicRecorder = class extends import_eventemitter33.default {
|
|
|
653
854
|
this.vad.off("StopSpeaking", this.onStopSpeaking);
|
|
654
855
|
await this.vad.stop();
|
|
655
856
|
}
|
|
656
|
-
async onDataAvailable(blobEvent) {
|
|
657
|
-
if (this.state.isMuted) return;
|
|
658
|
-
if (!this.speakingConfirmed) {
|
|
659
|
-
this.queuedChunks.push(blobEvent.data);
|
|
660
|
-
return;
|
|
661
|
-
}
|
|
662
|
-
for (const chunk of this.queuedChunks) {
|
|
663
|
-
this.emit("Chunk", chunk);
|
|
664
|
-
}
|
|
665
|
-
this.queuedChunks.length = 0;
|
|
666
|
-
this.emit("Chunk", blobEvent.data);
|
|
667
|
-
}
|
|
668
857
|
changeState(state) {
|
|
669
858
|
const hasChanged = Object.keys(state).some(
|
|
670
859
|
(key) => this.state[key] !== state[key]
|
|
@@ -685,523 +874,222 @@ var MicRecorder = class extends import_eventemitter33.default {
|
|
|
685
874
|
}
|
|
686
875
|
};
|
|
687
876
|
|
|
688
|
-
// src/audio/
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
if (!(ctx instanceof AudioContext)) {
|
|
694
|
-
throw new Error("OutputNode must be connected to an AudioContext");
|
|
695
|
-
}
|
|
696
|
-
this.audioContext = ctx;
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
|
|
700
|
-
// src/audio/player/SpeakerConcatPlayer.ts
|
|
701
|
-
var SpeakerConcatPlayer = class extends SpeakerPlayer {
|
|
702
|
-
constructor() {
|
|
703
|
-
super(...arguments);
|
|
704
|
-
this.pendingBlobs = [];
|
|
705
|
-
this.prevSourceDuration = 0;
|
|
706
|
-
this.isConvertingBlob = false;
|
|
707
|
-
}
|
|
708
|
-
async setup() {
|
|
709
|
-
}
|
|
710
|
-
replaceSource() {
|
|
711
|
-
if (!this.nextSource) return;
|
|
712
|
-
const time = performance.now() / 1e3;
|
|
713
|
-
const source = this.nextSource;
|
|
714
|
-
source.connect(this.outputNode);
|
|
715
|
-
source.onended = () => {
|
|
716
|
-
if (source !== this.currentSource) return;
|
|
717
|
-
this.prevSourceDuration = source.buffer?.duration ?? 0;
|
|
718
|
-
this.sourceStartTime = void 0;
|
|
719
|
-
this.currentSource = void 0;
|
|
720
|
-
source.disconnect();
|
|
721
|
-
this.replaceSource();
|
|
722
|
-
};
|
|
723
|
-
const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
|
|
724
|
-
source.start(0, offset);
|
|
725
|
-
if (!this.sourceStartTime) {
|
|
726
|
-
this.sourceStartTime = time;
|
|
727
|
-
}
|
|
728
|
-
this.currentSource?.stop();
|
|
729
|
-
this.currentSource?.disconnect();
|
|
730
|
-
this.currentSource = source;
|
|
731
|
-
this.nextSource = void 0;
|
|
732
|
-
}
|
|
733
|
-
async convertNextBlob() {
|
|
734
|
-
if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
|
|
735
|
-
this.isConvertingBlob = true;
|
|
736
|
-
const blob = this.pendingBlobs.shift();
|
|
737
|
-
try {
|
|
738
|
-
const arrayBuffer = await blob.arrayBuffer();
|
|
739
|
-
this.currentBuffer = this.currentBuffer ? this.concatArrayBuffers(this.currentBuffer, arrayBuffer) : arrayBuffer;
|
|
740
|
-
const audioBuffer = await this.audioContext.decodeAudioData(
|
|
741
|
-
// Copy the buffer to avoid mutating the original
|
|
742
|
-
this.copyArrayBuffer(this.currentBuffer)
|
|
743
|
-
);
|
|
744
|
-
const source = this.audioContext.createBufferSource();
|
|
745
|
-
source.buffer = audioBuffer;
|
|
746
|
-
this.nextSource = source;
|
|
747
|
-
this.replaceSource();
|
|
748
|
-
} catch (error) {
|
|
749
|
-
console.error("Error converting blob", error);
|
|
750
|
-
} finally {
|
|
751
|
-
this.isConvertingBlob = false;
|
|
752
|
-
}
|
|
753
|
-
this.convertNextBlob();
|
|
754
|
-
}
|
|
755
|
-
concatArrayBuffers(buffer1, buffer2) {
|
|
756
|
-
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
|
|
757
|
-
tmp.set(new Uint8Array(buffer1), 0);
|
|
758
|
-
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
|
|
759
|
-
return tmp.buffer;
|
|
760
|
-
}
|
|
761
|
-
copyArrayBuffer(buffer) {
|
|
762
|
-
const destination = new ArrayBuffer(buffer.byteLength);
|
|
763
|
-
new Uint8Array(destination).set(new Uint8Array(buffer));
|
|
764
|
-
return destination;
|
|
765
|
-
}
|
|
766
|
-
addBlob(blob) {
|
|
767
|
-
this.pendingBlobs.push(blob);
|
|
768
|
-
this.convertNextBlob();
|
|
769
|
-
}
|
|
770
|
-
stop() {
|
|
771
|
-
this.pendingBlobs.length = 0;
|
|
772
|
-
this.sourceStartTime = void 0;
|
|
773
|
-
this.prevSourceDuration = 0;
|
|
774
|
-
this.currentSource?.stop();
|
|
775
|
-
this.currentSource?.disconnect();
|
|
776
|
-
this.currentSource = void 0;
|
|
777
|
-
this.nextSource = void 0;
|
|
778
|
-
this.currentBuffer = void 0;
|
|
779
|
-
this.isConvertingBlob = false;
|
|
780
|
-
}
|
|
781
|
-
destroy() {
|
|
782
|
-
this.stop();
|
|
783
|
-
}
|
|
784
|
-
};
|
|
785
|
-
|
|
786
|
-
// src/audio/player/SpeakerMediaSourcePlayer.ts
|
|
787
|
-
var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
|
|
788
|
-
constructor() {
|
|
789
|
-
super(...arguments);
|
|
790
|
-
this.isProcessingQueue = false;
|
|
791
|
-
this.pendingBlobs = [];
|
|
792
|
-
}
|
|
793
|
-
async setup() {
|
|
794
|
-
if (this.audioElement || this.mediaSource) return;
|
|
795
|
-
this.audioElement = new Audio();
|
|
796
|
-
this.audioElement.autoplay = true;
|
|
797
|
-
this.audioElement.muted = true;
|
|
798
|
-
this.mediaSource = new MediaSource();
|
|
799
|
-
if (!this.mediaSource) {
|
|
800
|
-
throw new Error("Failed to create MediaSource");
|
|
801
|
-
}
|
|
802
|
-
this.audioElement.src = URL.createObjectURL(this.mediaSource);
|
|
803
|
-
await new Promise((resolve) => {
|
|
804
|
-
this.mediaSource?.addEventListener("sourceopen", () => {
|
|
805
|
-
const sourceBuffer = this.mediaSource?.addSourceBuffer("audio/mpeg");
|
|
806
|
-
if (!sourceBuffer) throw new Error("Failed to create SourceBuffer");
|
|
807
|
-
this.sourceBuffer = sourceBuffer;
|
|
808
|
-
sourceBuffer.addEventListener("updateend", () => this.processQueue());
|
|
809
|
-
this.processQueue();
|
|
810
|
-
resolve();
|
|
811
|
-
});
|
|
812
|
-
});
|
|
813
|
-
await new Promise((resolve) => {
|
|
814
|
-
const onTimeUpdate = () => {
|
|
815
|
-
this.audioElement?.removeEventListener("timeupdate", onTimeUpdate);
|
|
816
|
-
if (!this.audioElement) {
|
|
817
|
-
throw new Error("Audio element or audio context not found");
|
|
818
|
-
}
|
|
819
|
-
const mediaStream = this.audioElement.captureStream();
|
|
820
|
-
if (mediaStream.getAudioTracks().length === 0) return;
|
|
821
|
-
this.mediaStream = mediaStream;
|
|
822
|
-
this.sourceNode = this.audioContext.createMediaStreamSource(mediaStream);
|
|
823
|
-
this.sourceNode.connect(this.outputNode);
|
|
824
|
-
this.audioElement.currentTime = 0;
|
|
825
|
-
resolve();
|
|
826
|
-
};
|
|
827
|
-
this.audioElement?.addEventListener("timeupdate", onTimeUpdate);
|
|
828
|
-
});
|
|
829
|
-
}
|
|
830
|
-
async processQueue() {
|
|
831
|
-
if (!this.sourceBuffer || !this.mediaSource || this.pendingBlobs.length === 0 || this.isProcessingQueue || this.sourceBuffer.updating) {
|
|
832
|
-
return;
|
|
833
|
-
}
|
|
834
|
-
this.isProcessingQueue = true;
|
|
835
|
-
const blob = this.pendingBlobs.shift();
|
|
836
|
-
try {
|
|
837
|
-
const arrayBuffer = await blob.arrayBuffer();
|
|
838
|
-
if (this.isProcessingQueue) {
|
|
839
|
-
this.sourceBuffer.appendBuffer(arrayBuffer);
|
|
840
|
-
}
|
|
841
|
-
} catch (error) {
|
|
842
|
-
console.error("Error appending buffer", error);
|
|
843
|
-
} finally {
|
|
844
|
-
this.isProcessingQueue = false;
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
addBlob(blob) {
|
|
848
|
-
this.pendingBlobs.push(blob);
|
|
849
|
-
if (this.sourceBuffer && !this.sourceBuffer.updating && !this.isProcessingQueue) {
|
|
850
|
-
this.processQueue();
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
stop() {
|
|
854
|
-
this.destroy();
|
|
855
|
-
this.setup();
|
|
856
|
-
}
|
|
857
|
-
destroy() {
|
|
858
|
-
if (this.sourceNode) {
|
|
859
|
-
this.sourceNode.disconnect();
|
|
860
|
-
this.sourceNode = void 0;
|
|
861
|
-
}
|
|
862
|
-
this.pendingBlobs.length = 0;
|
|
863
|
-
if (this.mediaSource) {
|
|
864
|
-
try {
|
|
865
|
-
if (this.mediaSource.readyState === "open") {
|
|
866
|
-
this.mediaSource.endOfStream();
|
|
867
|
-
}
|
|
868
|
-
} catch (error) {
|
|
869
|
-
console.error("Error cleaning up media source", error);
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
if (this.audioElement) {
|
|
873
|
-
this.audioElement.pause();
|
|
874
|
-
this.audioElement.src = "";
|
|
875
|
-
this.audioElement = void 0;
|
|
876
|
-
}
|
|
877
|
-
if (this.mediaStream) {
|
|
878
|
-
stopStream(this.mediaStream);
|
|
879
|
-
this.mediaStream = void 0;
|
|
880
|
-
}
|
|
881
|
-
this.mediaSource = void 0;
|
|
882
|
-
this.sourceBuffer = void 0;
|
|
883
|
-
}
|
|
884
|
-
};
|
|
885
|
-
SpeakerMediaSourcePlayer.isCompatible = typeof MediaSource !== "undefined" && MediaSource.isTypeSupported("audio/mpeg");
|
|
886
|
-
|
|
887
|
-
// src/audio/player/SpeakerSequencePlayer.ts
|
|
888
|
-
var SpeakerSequencePlayer = class extends SpeakerPlayer {
|
|
889
|
-
constructor() {
|
|
890
|
-
super(...arguments);
|
|
891
|
-
this.pendingBlobs = [];
|
|
892
|
-
this.pendingSources = [];
|
|
893
|
-
this.isConvertingBlob = false;
|
|
894
|
-
}
|
|
895
|
-
async setup() {
|
|
896
|
-
}
|
|
897
|
-
processQueue() {
|
|
898
|
-
if (this.pendingSources.length === 0 || this.currentSource) return;
|
|
899
|
-
const source = this.pendingSources.shift();
|
|
900
|
-
this.currentSource = source;
|
|
901
|
-
source.connect(this.outputNode);
|
|
902
|
-
source.onended = () => {
|
|
903
|
-
source.disconnect();
|
|
904
|
-
this.currentSource = void 0;
|
|
905
|
-
this.processQueue();
|
|
906
|
-
};
|
|
907
|
-
source.start(0);
|
|
908
|
-
}
|
|
909
|
-
async convertNextBlob() {
|
|
910
|
-
if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
|
|
911
|
-
this.isConvertingBlob = true;
|
|
912
|
-
const blob = this.pendingBlobs.shift();
|
|
913
|
-
try {
|
|
914
|
-
const arrayBuffer = await blob.arrayBuffer();
|
|
915
|
-
const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
|
|
916
|
-
const source = this.audioContext.createBufferSource();
|
|
917
|
-
source.buffer = audioBuffer;
|
|
918
|
-
this.pendingSources.push(source);
|
|
919
|
-
this.processQueue();
|
|
920
|
-
} catch (error) {
|
|
921
|
-
console.error("Error converting blob", error);
|
|
922
|
-
} finally {
|
|
923
|
-
this.isConvertingBlob = false;
|
|
924
|
-
}
|
|
925
|
-
this.convertNextBlob();
|
|
926
|
-
}
|
|
927
|
-
addBlob(blob) {
|
|
928
|
-
this.pendingBlobs.push(blob);
|
|
929
|
-
this.convertNextBlob();
|
|
930
|
-
}
|
|
931
|
-
stop() {
|
|
932
|
-
this.pendingBlobs.length = 0;
|
|
933
|
-
this.pendingSources.length = 0;
|
|
934
|
-
this.currentSource?.stop();
|
|
935
|
-
this.currentSource?.disconnect();
|
|
936
|
-
this.currentSource = void 0;
|
|
937
|
-
}
|
|
938
|
-
destroy() {
|
|
939
|
-
this.stop();
|
|
940
|
-
}
|
|
941
|
-
};
|
|
942
|
-
|
|
943
|
-
// src/audio/speaker.ts
|
|
944
|
-
var speaker;
|
|
945
|
-
var Speaker = class {
|
|
946
|
-
constructor() {
|
|
947
|
-
this.streamingEnabled = false;
|
|
948
|
-
this.analyser = new AudioAnalyser(audioContext);
|
|
949
|
-
this.audioElement = new Audio();
|
|
950
|
-
this.audioElement.autoplay = true;
|
|
951
|
-
const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
|
|
952
|
-
if (speakerId) {
|
|
953
|
-
this.setSinkId(speakerId).catch((error) => {
|
|
954
|
-
console.error("Failed to set sink ID:", error);
|
|
955
|
-
});
|
|
956
|
-
}
|
|
957
|
-
this.setupSpeakerPlayer();
|
|
958
|
-
const destinationNode = audioContext.createMediaStreamDestination();
|
|
959
|
-
this.analyser.node.connect(destinationNode);
|
|
960
|
-
this.audioElement.srcObject = destinationNode.stream;
|
|
961
|
-
}
|
|
962
|
-
async setupSpeakerPlayer() {
|
|
963
|
-
try {
|
|
964
|
-
this.player?.destroy();
|
|
965
|
-
this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
|
|
966
|
-
await this.player?.setup();
|
|
967
|
-
} catch (error) {
|
|
968
|
-
console.error("Error setting up speaker player", error);
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
get isStreamingEnabled() {
|
|
972
|
-
return this.streamingEnabled;
|
|
973
|
-
}
|
|
974
|
-
async enableStreaming() {
|
|
975
|
-
if (this.streamingEnabled) return;
|
|
976
|
-
this.streamingEnabled = true;
|
|
977
|
-
await this.setupSpeakerPlayer();
|
|
978
|
-
}
|
|
979
|
-
async disableStreaming() {
|
|
980
|
-
if (!this.streamingEnabled) return;
|
|
981
|
-
this.streamingEnabled = false;
|
|
982
|
-
await this.setupSpeakerPlayer();
|
|
983
|
-
}
|
|
984
|
-
canChangeDevice() {
|
|
985
|
-
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
986
|
-
}
|
|
987
|
-
async setSinkId(speakerId) {
|
|
988
|
-
if (speakerId && this.canChangeDevice() && this.audioElement) {
|
|
989
|
-
return this.audioElement.setSinkId(speakerId);
|
|
990
|
-
}
|
|
991
|
-
}
|
|
992
|
-
async changeDevice(speakerId) {
|
|
993
|
-
if (!this.canChangeDevice()) return;
|
|
994
|
-
try {
|
|
995
|
-
await this.setSinkId(speakerId);
|
|
996
|
-
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
997
|
-
} catch (error) {
|
|
998
|
-
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
pauseAudio() {
|
|
1002
|
-
if (this.audioElement) {
|
|
1003
|
-
this.audioElement.pause();
|
|
1004
|
-
}
|
|
1005
|
-
}
|
|
1006
|
-
resumeAudio() {
|
|
1007
|
-
if (this.audioElement) {
|
|
1008
|
-
this.audioElement.play();
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
async playAudio(blob) {
|
|
1012
|
-
if (!this.audioElement || !this.player) return;
|
|
1013
|
-
try {
|
|
1014
|
-
this.player.addBlob(blob);
|
|
1015
|
-
if (this.audioElement.paused) {
|
|
1016
|
-
this.audioElement.play();
|
|
1017
|
-
}
|
|
1018
|
-
} catch (error) {
|
|
1019
|
-
console.error("Error playing audio blob", error, blob);
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
1022
|
-
stopAudio() {
|
|
1023
|
-
this.audioElement?.pause();
|
|
1024
|
-
this.player?.stop();
|
|
1025
|
-
}
|
|
1026
|
-
};
|
|
877
|
+
// src/audio/index.ts
|
|
878
|
+
if (!window.micdropMic) {
|
|
879
|
+
window.micdropMic = new Mic();
|
|
880
|
+
}
|
|
881
|
+
var Mic2 = window.micdropMic;
|
|
1027
882
|
if (!window.micdropSpeaker) {
|
|
1028
883
|
window.micdropSpeaker = new Speaker();
|
|
1029
884
|
}
|
|
1030
|
-
|
|
885
|
+
var Speaker2 = window.micdropSpeaker;
|
|
1031
886
|
|
|
1032
|
-
// src/
|
|
1033
|
-
var
|
|
887
|
+
// src/client/MicdropClient.ts
|
|
888
|
+
var import_eventemitter36 = __toESM(require("eventemitter3"));
|
|
1034
889
|
|
|
1035
|
-
// src/
|
|
1036
|
-
var
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
890
|
+
// src/client/MicdropClientError.ts
|
|
891
|
+
var MicdropClientErrorCode = /* @__PURE__ */ ((MicdropClientErrorCode2) => {
|
|
892
|
+
MicdropClientErrorCode2["MissingUrl"] = "MissingUrl";
|
|
893
|
+
MicdropClientErrorCode2["Mic"] = "Mic";
|
|
894
|
+
MicdropClientErrorCode2["Connection"] = "Connection";
|
|
895
|
+
MicdropClientErrorCode2["InternalServer"] = "InternalServer";
|
|
896
|
+
MicdropClientErrorCode2["BadRequest"] = "BadRequest";
|
|
897
|
+
MicdropClientErrorCode2["Unauthorized"] = "Unauthorized";
|
|
898
|
+
MicdropClientErrorCode2["NotFound"] = "NotFound";
|
|
899
|
+
MicdropClientErrorCode2["Unknown"] = "Unknown";
|
|
900
|
+
return MicdropClientErrorCode2;
|
|
901
|
+
})(MicdropClientErrorCode || {});
|
|
902
|
+
var MicdropClientError = class extends Error {
|
|
903
|
+
constructor(code = "Unknown" /* Unknown */, message) {
|
|
904
|
+
super(message);
|
|
905
|
+
this.name = `MicdropClientError:${code}`;
|
|
1045
906
|
this.code = code;
|
|
1046
907
|
}
|
|
1047
908
|
};
|
|
909
|
+
function getClientErrorFromWSCloseEvent(event) {
|
|
910
|
+
const code = getClientErrorFromWSCloseEventCode(event.code);
|
|
911
|
+
return code && new MicdropClientError(code, event.reason);
|
|
912
|
+
}
|
|
913
|
+
function getClientErrorFromWSCloseEventCode(code) {
|
|
914
|
+
if (code === 1011) {
|
|
915
|
+
return "InternalServer" /* InternalServer */;
|
|
916
|
+
} else if (code >= 1001 && code < 1011 && code !== 1005) {
|
|
917
|
+
return "Connection" /* Connection */;
|
|
918
|
+
} else if (code === 4401) {
|
|
919
|
+
return "Unauthorized" /* Unauthorized */;
|
|
920
|
+
} else if (code === 4404) {
|
|
921
|
+
return "NotFound" /* NotFound */;
|
|
922
|
+
} else if (code === 4400) {
|
|
923
|
+
return "BadRequest" /* BadRequest */;
|
|
924
|
+
} else if (code >= 4e3) {
|
|
925
|
+
return "Unknown" /* Unknown */;
|
|
926
|
+
} else {
|
|
927
|
+
return void 0;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
1048
930
|
|
|
1049
|
-
// src/
|
|
1050
|
-
var
|
|
1051
|
-
constructor(options) {
|
|
931
|
+
// src/client/MicdropClient.ts
|
|
932
|
+
var MicdropClient = class extends import_eventemitter36.default {
|
|
933
|
+
constructor(options = {}) {
|
|
1052
934
|
super();
|
|
1053
935
|
this.options = options;
|
|
1054
936
|
this.conversation = [];
|
|
1055
|
-
this.
|
|
937
|
+
this.speakerDevices = [];
|
|
938
|
+
this.micDevices = [];
|
|
1056
939
|
this.startTime = 0;
|
|
1057
940
|
this._isProcessing = false;
|
|
1058
|
-
this.
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
return this.ws?.readyState === WebSocket.CONNECTING;
|
|
1077
|
-
}
|
|
1078
|
-
get isMicStarted() {
|
|
1079
|
-
return !!this.micStream;
|
|
1080
|
-
}
|
|
1081
|
-
get isMicMuted() {
|
|
1082
|
-
return this.micRecorder?.state.isMuted ?? false;
|
|
1083
|
-
}
|
|
1084
|
-
get isMicSpeaking() {
|
|
1085
|
-
return this.micRecorder?.state.isSpeaking ?? false;
|
|
1086
|
-
}
|
|
1087
|
-
get isProcessing() {
|
|
1088
|
-
return this._isProcessing;
|
|
1089
|
-
}
|
|
1090
|
-
async start() {
|
|
1091
|
-
this.startTime = Date.now();
|
|
1092
|
-
this.conversation = [];
|
|
1093
|
-
this._isProcessing = true;
|
|
1094
|
-
await this.startMic(void 0, true);
|
|
1095
|
-
await this.startWS();
|
|
1096
|
-
}
|
|
1097
|
-
async stop() {
|
|
1098
|
-
try {
|
|
1099
|
-
this.stopWS();
|
|
1100
|
-
} catch (error) {
|
|
1101
|
-
console.error("[CallClient] stop WS", error);
|
|
1102
|
-
}
|
|
1103
|
-
try {
|
|
1104
|
-
this.micRecorder?.stop();
|
|
1105
|
-
this.stopMic();
|
|
1106
|
-
} catch (error) {
|
|
1107
|
-
console.error("[CallClient] stop mic", error);
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
pause() {
|
|
1111
|
-
this.micRecorder?.mute();
|
|
1112
|
-
this._isProcessing = false;
|
|
1113
|
-
this.notifyStateChange();
|
|
1114
|
-
speaker.pauseAudio();
|
|
1115
|
-
this.ws?.send("Mute" /* Mute */);
|
|
1116
|
-
}
|
|
1117
|
-
resume() {
|
|
1118
|
-
if (this.lastAudioBlob) {
|
|
1119
|
-
speaker.playAudio(this.lastAudioBlob);
|
|
1120
|
-
}
|
|
1121
|
-
speaker.resumeAudio();
|
|
1122
|
-
this.micRecorder?.unmute();
|
|
1123
|
-
this.notifyStateChange();
|
|
1124
|
-
}
|
|
1125
|
-
async startMic(deviceId, record = true) {
|
|
1126
|
-
try {
|
|
1127
|
-
if (!this.micRecorder) {
|
|
1128
|
-
this.micRecorder = new MicRecorder(this.vad);
|
|
1129
|
-
this.micRecorder.on("StateChange", () => {
|
|
1130
|
-
this.notifyStateChange();
|
|
1131
|
-
});
|
|
1132
|
-
this.micRecorder.on("Chunk", (blob) => {
|
|
1133
|
-
this.log(`[Mic] Received chunk`, blob);
|
|
1134
|
-
this.ws?.send(blob);
|
|
1135
|
-
});
|
|
1136
|
-
this.micRecorder.on("StartSpeaking", () => {
|
|
1137
|
-
this.log("[Mic] Start speaking");
|
|
1138
|
-
this.ws?.send("StartSpeaking" /* StartSpeaking */);
|
|
1139
|
-
this._isProcessing = false;
|
|
1140
|
-
this.notifyStateChange();
|
|
1141
|
-
speaker.stopAudio();
|
|
1142
|
-
});
|
|
1143
|
-
this.micRecorder.on("StopSpeaking", () => {
|
|
1144
|
-
this.log("[Mic] Stop speaking");
|
|
1145
|
-
this.ws?.send("StopSpeaking" /* StopSpeaking */);
|
|
1146
|
-
this._isProcessing = true;
|
|
1147
|
-
this.notifyStateChange();
|
|
1148
|
-
});
|
|
941
|
+
this._isPaused = false;
|
|
942
|
+
this.start = async (options) => {
|
|
943
|
+
this.error = void 0;
|
|
944
|
+
this.options = { ...this.options, ...options };
|
|
945
|
+
this.startTime = Date.now();
|
|
946
|
+
this.conversation = [];
|
|
947
|
+
this._isProcessing = true;
|
|
948
|
+
this._isPaused = false;
|
|
949
|
+
await this.startMic({ record: true });
|
|
950
|
+
await this.startWS();
|
|
951
|
+
};
|
|
952
|
+
this.stop = async () => {
|
|
953
|
+
this._isProcessing = false;
|
|
954
|
+
this._isPaused = false;
|
|
955
|
+
try {
|
|
956
|
+
this.stopWS();
|
|
957
|
+
} catch (error) {
|
|
958
|
+
console.error("[MicdropClient] Error stopping WebSocket", error);
|
|
1149
959
|
}
|
|
1150
|
-
|
|
1151
|
-
if (isRecorderStarted) {
|
|
960
|
+
try {
|
|
1152
961
|
this.micRecorder?.stop();
|
|
962
|
+
this.stopMic();
|
|
963
|
+
} catch (error) {
|
|
964
|
+
console.error("[MicdropClient] Error stopping microphone", error);
|
|
1153
965
|
}
|
|
1154
|
-
|
|
966
|
+
Speaker2.stopAudio();
|
|
967
|
+
};
|
|
968
|
+
this.pause = () => {
|
|
969
|
+
if (this.isPaused) return;
|
|
970
|
+
this.micRecorder?.mute();
|
|
971
|
+
this._isPaused = true;
|
|
972
|
+
this._isProcessing = false;
|
|
973
|
+
this.notifyStateChange();
|
|
974
|
+
Speaker2.stopAudio();
|
|
975
|
+
this.ws?.send("Mute" /* Mute */);
|
|
976
|
+
};
|
|
977
|
+
this.resume = () => {
|
|
978
|
+
if (!this.isPaused) return;
|
|
979
|
+
this.micRecorder?.unmute();
|
|
980
|
+
this._isPaused = false;
|
|
1155
981
|
this.notifyStateChange();
|
|
1156
|
-
|
|
1157
|
-
|
|
982
|
+
};
|
|
983
|
+
this.startMic = async ({
|
|
984
|
+
vad,
|
|
985
|
+
deviceId,
|
|
986
|
+
record = true
|
|
987
|
+
}) => {
|
|
988
|
+
this.error = void 0;
|
|
989
|
+
if (vad) {
|
|
990
|
+
this.options.vad = vad;
|
|
1158
991
|
}
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
992
|
+
try {
|
|
993
|
+
if (!this.micRecorder) {
|
|
994
|
+
this.micRecorder = new MicRecorder(this.options.vad);
|
|
995
|
+
this.micRecorder.on("StateChange", () => {
|
|
996
|
+
this.notifyStateChange();
|
|
997
|
+
});
|
|
998
|
+
this.micRecorder.on("Chunk", (blob) => {
|
|
999
|
+
this.log(`[MicdropClient] User audio chunk`, blob);
|
|
1000
|
+
this.ws?.send(blob);
|
|
1001
|
+
});
|
|
1002
|
+
this.micRecorder.on("StartSpeaking", () => {
|
|
1003
|
+
this.log("User start speaking");
|
|
1004
|
+
this.ws?.send("StartSpeaking" /* StartSpeaking */);
|
|
1005
|
+
this._isProcessing = false;
|
|
1006
|
+
this.notifyStateChange();
|
|
1007
|
+
Speaker2.stopAudio();
|
|
1008
|
+
});
|
|
1009
|
+
this.micRecorder.on("StopSpeaking", () => {
|
|
1010
|
+
this.log("User stop speaking");
|
|
1011
|
+
this.ws?.send("StopSpeaking" /* StopSpeaking */);
|
|
1012
|
+
if (this.isWSStarted) {
|
|
1013
|
+
this._isProcessing = true;
|
|
1014
|
+
}
|
|
1015
|
+
this.notifyStateChange();
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
const isRecorderStarted = this.micRecorder.state.isStarted;
|
|
1019
|
+
if (isRecorderStarted) {
|
|
1020
|
+
this.micRecorder?.stop();
|
|
1021
|
+
}
|
|
1022
|
+
const micStream = await Mic2.start(deviceId);
|
|
1023
|
+
this.micStream = micStream;
|
|
1024
|
+
if (isRecorderStarted || record) {
|
|
1025
|
+
await this.micRecorder.start(micStream);
|
|
1026
|
+
}
|
|
1027
|
+
await this.updateDevices();
|
|
1028
|
+
await Speaker2.start();
|
|
1029
|
+
this.notifyStateChange();
|
|
1030
|
+
} catch (error) {
|
|
1031
|
+
this.setError(
|
|
1032
|
+
new MicdropClientError(
|
|
1033
|
+
"Mic" /* Mic */,
|
|
1034
|
+
error?.message
|
|
1035
|
+
)
|
|
1036
|
+
);
|
|
1037
|
+
await this.stop();
|
|
1038
|
+
throw error;
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
this.changeMicDevice = async (deviceId) => {
|
|
1042
|
+
await this.startMic({ deviceId });
|
|
1043
|
+
};
|
|
1044
|
+
this.changeSpeakerDevice = async (deviceId) => {
|
|
1045
|
+
await Speaker2.changeDevice(deviceId);
|
|
1187
1046
|
this.notifyStateChange();
|
|
1188
|
-
|
|
1189
|
-
|
|
1047
|
+
};
|
|
1048
|
+
this.updateDevices = async () => {
|
|
1049
|
+
const rawDevices = await navigator.mediaDevices.enumerateDevices();
|
|
1050
|
+
if (rawDevices.length === 0) return;
|
|
1051
|
+
const defaultDevices = rawDevices.filter(
|
|
1052
|
+
(device) => device.deviceId === "default"
|
|
1053
|
+
);
|
|
1054
|
+
const devices = rawDevices.filter((device) => device.deviceId !== "default");
|
|
1055
|
+
const defaultDevicesIndexes = defaultDevices.map(
|
|
1056
|
+
(device) => devices.findIndex(
|
|
1057
|
+
(d) => d.groupId === device.groupId && d.kind === device.kind
|
|
1058
|
+
)
|
|
1059
|
+
).filter((index) => index !== -1);
|
|
1060
|
+
for (const index of defaultDevicesIndexes) {
|
|
1061
|
+
const device = devices[index];
|
|
1062
|
+
devices.splice(index, 1);
|
|
1063
|
+
devices.unshift(device);
|
|
1190
1064
|
}
|
|
1065
|
+
this.micDevices = devices.filter((device) => device.kind === "audioinput");
|
|
1066
|
+
this.speakerDevices = devices.filter(
|
|
1067
|
+
(device) => device.kind === "audiooutput"
|
|
1068
|
+
);
|
|
1069
|
+
Speaker2.devices = this.speakerDevices;
|
|
1070
|
+
this.notifyStateChange();
|
|
1191
1071
|
};
|
|
1192
|
-
this.
|
|
1193
|
-
this.log("
|
|
1072
|
+
this.onWSOpen = () => {
|
|
1073
|
+
this.log("WebSocket opened");
|
|
1074
|
+
this.notifyStateChange();
|
|
1075
|
+
if (this.options.params) {
|
|
1076
|
+
this.ws?.send(JSON.stringify(this.options.params));
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
this.onWSMessage = (event) => {
|
|
1080
|
+
this.log("Received message:", event.data);
|
|
1194
1081
|
if (event.data instanceof Blob) {
|
|
1195
|
-
|
|
1196
|
-
|
|
1082
|
+
if (this.isPaused || this.isUserSpeaking) return;
|
|
1083
|
+
Speaker2.playAudio(event.data);
|
|
1197
1084
|
this._isProcessing = false;
|
|
1198
1085
|
this.notifyStateChange();
|
|
1199
1086
|
} else if (typeof event.data !== "string") {
|
|
1200
|
-
console.warn(`[
|
|
1087
|
+
console.warn(`[MicdropClient] Unknown message type: ${event.data}`);
|
|
1201
1088
|
} else if (event.data.startsWith("Message" /* Message */)) {
|
|
1202
1089
|
const message = JSON.parse(
|
|
1203
1090
|
event.data.substring("Message" /* Message */.length + 1)
|
|
1204
1091
|
);
|
|
1092
|
+
this._isProcessing = false;
|
|
1205
1093
|
this.addMessage(message);
|
|
1206
1094
|
} else if (event.data === "EndCall" /* EndCall */) {
|
|
1207
1095
|
setTimeout(() => {
|
|
@@ -1223,32 +1111,141 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
|
|
|
1223
1111
|
this._isProcessing = false;
|
|
1224
1112
|
this.notifyStateChange();
|
|
1225
1113
|
}
|
|
1226
|
-
} else if (event.data === "EnableSpeakerStreaming" /* EnableSpeakerStreaming */) {
|
|
1227
|
-
speaker.enableStreaming();
|
|
1228
1114
|
}
|
|
1229
1115
|
};
|
|
1230
|
-
this.
|
|
1231
|
-
this.log("
|
|
1116
|
+
this.onWSClose = (event) => {
|
|
1117
|
+
this.log("WebSocket closed", event);
|
|
1232
1118
|
this.stop();
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
this.emit(
|
|
1237
|
-
"Error",
|
|
1238
|
-
new CallClientError("Unauthorized" /* Unauthorized */)
|
|
1239
|
-
);
|
|
1240
|
-
} else if (event.code >= 4e3) {
|
|
1241
|
-
this.emit("Error", new CallClientError());
|
|
1119
|
+
const error = getClientErrorFromWSCloseEvent(event);
|
|
1120
|
+
if (error) {
|
|
1121
|
+
this.setError(error);
|
|
1242
1122
|
}
|
|
1243
1123
|
};
|
|
1244
|
-
this.
|
|
1245
|
-
|
|
1124
|
+
this.onWSError = (event) => {
|
|
1125
|
+
this.setError(new MicdropClientError("Connection" /* Connection */));
|
|
1246
1126
|
this.stop();
|
|
1247
1127
|
};
|
|
1128
|
+
this.onSpeakerStartPlaying = () => {
|
|
1129
|
+
this.log("Speaker started");
|
|
1130
|
+
if (this.options.disableInterruption && !this.isPaused) {
|
|
1131
|
+
this.micRecorder?.mute();
|
|
1132
|
+
}
|
|
1133
|
+
this.notifyStateChange();
|
|
1134
|
+
};
|
|
1135
|
+
this.onSpeakerStopPlaying = () => {
|
|
1136
|
+
this.log("Speaker stopped");
|
|
1137
|
+
if (this.options.disableInterruption && !this.isPaused) {
|
|
1138
|
+
this.micRecorder?.unmute();
|
|
1139
|
+
}
|
|
1140
|
+
this.notifyStateChange();
|
|
1141
|
+
};
|
|
1142
|
+
Speaker2.on("StartPlaying", this.onSpeakerStartPlaying);
|
|
1143
|
+
Speaker2.on("StopPlaying", this.onSpeakerStopPlaying);
|
|
1144
|
+
navigator.mediaDevices.addEventListener("devicechange", this.updateDevices);
|
|
1145
|
+
}
|
|
1146
|
+
get vad() {
|
|
1147
|
+
return this.micRecorder?.vad;
|
|
1148
|
+
}
|
|
1149
|
+
get isStarted() {
|
|
1150
|
+
return this.isWSStarted && this.micRecorder?.state.isStarted || false;
|
|
1151
|
+
}
|
|
1152
|
+
get isStarting() {
|
|
1153
|
+
return this.isWSStarting || this.micRecorder?.state.isStarting || false;
|
|
1154
|
+
}
|
|
1155
|
+
get isPaused() {
|
|
1156
|
+
return this._isPaused;
|
|
1157
|
+
}
|
|
1158
|
+
get isProcessing() {
|
|
1159
|
+
return this._isProcessing && !this.isPaused;
|
|
1160
|
+
}
|
|
1161
|
+
get isListening() {
|
|
1162
|
+
return this.isMicStarted && !this.isPaused && !this.isProcessing && !this.isMicMuted && !this.isUserSpeaking && !this.isAssistantSpeaking;
|
|
1163
|
+
}
|
|
1164
|
+
get isWSStarted() {
|
|
1165
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
1166
|
+
}
|
|
1167
|
+
get isWSStarting() {
|
|
1168
|
+
return this.ws?.readyState === WebSocket.CONNECTING;
|
|
1169
|
+
}
|
|
1170
|
+
get isMicStarted() {
|
|
1171
|
+
return !!this.micStream;
|
|
1172
|
+
}
|
|
1173
|
+
get isMicMuted() {
|
|
1174
|
+
return this.micRecorder?.state.isMuted ?? false;
|
|
1175
|
+
}
|
|
1176
|
+
get isUserSpeaking() {
|
|
1177
|
+
return this.micRecorder?.state.isSpeaking ?? false;
|
|
1178
|
+
}
|
|
1179
|
+
get isAssistantSpeaking() {
|
|
1180
|
+
return Speaker2.isPlaying;
|
|
1181
|
+
}
|
|
1182
|
+
get micDeviceId() {
|
|
1183
|
+
return Mic2.deviceId;
|
|
1184
|
+
}
|
|
1185
|
+
get speakerDeviceId() {
|
|
1186
|
+
return Speaker2.deviceId;
|
|
1187
|
+
}
|
|
1188
|
+
get state() {
|
|
1189
|
+
return {
|
|
1190
|
+
isStarting: this.isStarting,
|
|
1191
|
+
isStarted: this.isStarted,
|
|
1192
|
+
isPaused: this.isPaused,
|
|
1193
|
+
isListening: this.isListening,
|
|
1194
|
+
isProcessing: this.isProcessing,
|
|
1195
|
+
isUserSpeaking: this.isUserSpeaking,
|
|
1196
|
+
isAssistantSpeaking: this.isAssistantSpeaking,
|
|
1197
|
+
isMicStarted: this.isMicStarted,
|
|
1198
|
+
isMicMuted: this.isMicMuted,
|
|
1199
|
+
conversation: this.conversation,
|
|
1200
|
+
error: this.error,
|
|
1201
|
+
micDeviceId: this.micDeviceId,
|
|
1202
|
+
speakerDeviceId: this.speakerDeviceId,
|
|
1203
|
+
micDevices: this.micDevices,
|
|
1204
|
+
speakerDevices: this.speakerDevices
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
stopMic() {
|
|
1208
|
+
this.micRecorder?.stop();
|
|
1209
|
+
this.micRecorder = void 0;
|
|
1210
|
+
Mic2.stop();
|
|
1211
|
+
this.micStream = void 0;
|
|
1212
|
+
this.notifyStateChange();
|
|
1213
|
+
}
|
|
1214
|
+
async startWS() {
|
|
1215
|
+
try {
|
|
1216
|
+
if (this.ws) {
|
|
1217
|
+
this.log("WebSocket is already started");
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
if (!this.isMicStarted) {
|
|
1221
|
+
throw new MicdropClientError("Mic" /* Mic */);
|
|
1222
|
+
}
|
|
1223
|
+
if (!this.options.url) {
|
|
1224
|
+
throw new MicdropClientError("MissingUrl" /* MissingUrl */);
|
|
1225
|
+
}
|
|
1226
|
+
this.ws = new WebSocket(this.options.url);
|
|
1227
|
+
this.ws.binaryType = "blob";
|
|
1228
|
+
this.notifyStateChange();
|
|
1229
|
+
this.ws.onopen = this.onWSOpen;
|
|
1230
|
+
this.ws.onmessage = this.onWSMessage;
|
|
1231
|
+
this.ws.onclose = this.onWSClose;
|
|
1232
|
+
this.ws.onerror = this.onWSError;
|
|
1233
|
+
} catch (error) {
|
|
1234
|
+
this.setError(
|
|
1235
|
+
new MicdropClientError(
|
|
1236
|
+
"Connection" /* Connection */,
|
|
1237
|
+
error?.message
|
|
1238
|
+
)
|
|
1239
|
+
);
|
|
1240
|
+
await this.stop();
|
|
1241
|
+
throw error;
|
|
1242
|
+
}
|
|
1248
1243
|
}
|
|
1249
1244
|
stopWS() {
|
|
1250
1245
|
if (!this.ws) return;
|
|
1251
|
-
this.ws.
|
|
1246
|
+
if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {
|
|
1247
|
+
this.ws.close();
|
|
1248
|
+
}
|
|
1252
1249
|
this.ws = void 0;
|
|
1253
1250
|
this.notifyStateChange();
|
|
1254
1251
|
}
|
|
@@ -1256,46 +1253,60 @@ var CallClient = class _CallClient extends import_eventemitter34.default {
|
|
|
1256
1253
|
this.conversation.push(message);
|
|
1257
1254
|
this.notifyStateChange();
|
|
1258
1255
|
}
|
|
1256
|
+
setError(error) {
|
|
1257
|
+
console.error("[MicdropClient] Error:", error);
|
|
1258
|
+
this.error = error;
|
|
1259
|
+
this.notifyStateChange();
|
|
1260
|
+
this.emit("Error", error);
|
|
1261
|
+
}
|
|
1259
1262
|
notifyStateChange() {
|
|
1260
|
-
this.emit("StateChange");
|
|
1263
|
+
this.emit("StateChange", this.state);
|
|
1261
1264
|
}
|
|
1262
1265
|
log(...message) {
|
|
1263
|
-
if (!this.
|
|
1264
|
-
console.log(`[
|
|
1266
|
+
if (!this.options.debugLog) return;
|
|
1267
|
+
console.log(`[MicdropClient ${Date.now() - this.startTime}]`, ...message);
|
|
1265
1268
|
}
|
|
1266
1269
|
};
|
|
1267
1270
|
|
|
1271
|
+
// src/Micdrop.ts
|
|
1272
|
+
if (!window.micdropClient) {
|
|
1273
|
+
window.micdropClient = new MicdropClient();
|
|
1274
|
+
}
|
|
1275
|
+
var Micdrop = window.micdropClient;
|
|
1276
|
+
|
|
1268
1277
|
// ../server/src/types.ts
|
|
1269
|
-
var
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
return
|
|
1274
|
-
})(
|
|
1275
|
-
var
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
})(CallServerCommands || {});
|
|
1278
|
+
var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
|
|
1279
|
+
MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
|
|
1280
|
+
MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
|
|
1281
|
+
MicdropClientCommands2["Mute"] = "Mute";
|
|
1282
|
+
return MicdropClientCommands2;
|
|
1283
|
+
})(MicdropClientCommands || {});
|
|
1284
|
+
var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
|
|
1285
|
+
MicdropServerCommands2["Message"] = "Message";
|
|
1286
|
+
MicdropServerCommands2["CancelLastAssistantMessage"] = "CancelLastAssistantMessage";
|
|
1287
|
+
MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
|
|
1288
|
+
MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
|
|
1289
|
+
MicdropServerCommands2["EndCall"] = "EndCall";
|
|
1290
|
+
return MicdropServerCommands2;
|
|
1291
|
+
})(MicdropServerCommands || {});
|
|
1284
1292
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1285
1293
|
0 && (module.exports = {
|
|
1286
|
-
CallClient,
|
|
1287
|
-
CallClientCommands,
|
|
1288
|
-
CallClientError,
|
|
1289
|
-
CallClientErrorCode,
|
|
1290
|
-
CallServerCommands,
|
|
1291
1294
|
LocalStorageKeys,
|
|
1292
1295
|
Mic,
|
|
1293
1296
|
MicRecorder,
|
|
1297
|
+
Micdrop,
|
|
1298
|
+
MicdropClient,
|
|
1299
|
+
MicdropClientCommands,
|
|
1300
|
+
MicdropClientError,
|
|
1301
|
+
MicdropClientErrorCode,
|
|
1302
|
+
MicdropServerCommands,
|
|
1294
1303
|
MultipleVAD,
|
|
1295
1304
|
SileroVAD,
|
|
1296
1305
|
Speaker,
|
|
1297
1306
|
VAD,
|
|
1298
1307
|
VADStatus,
|
|
1299
|
-
VolumeVAD
|
|
1308
|
+
VolumeVAD,
|
|
1309
|
+
getClientErrorFromWSCloseEvent,
|
|
1310
|
+
getClientErrorFromWSCloseEventCode
|
|
1300
1311
|
});
|
|
1301
1312
|
//# sourceMappingURL=index.js.map
|