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