@micdrop/client 1.0.2 → 1.2.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 +23 -41
- package/dist/index.d.mts +130 -16
- package/dist/index.d.ts +130 -16
- package/dist/index.js +391 -96
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +387 -95
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
|
@@ -14,14 +14,12 @@ var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
|
|
|
14
14
|
|
|
15
15
|
// src/audio/MicRecorder.ts
|
|
16
16
|
import EventEmitter2 from "eventemitter3";
|
|
17
|
-
import hark from "hark";
|
|
18
17
|
|
|
19
18
|
// src/audio/microphone.ts
|
|
20
19
|
var microphone_exports = {};
|
|
21
20
|
__export(microphone_exports, {
|
|
22
|
-
|
|
21
|
+
audioContext: () => audioContext,
|
|
23
22
|
defaultMicThreshold: () => defaultMicThreshold,
|
|
24
|
-
getMicConstraints: () => getMicConstraints,
|
|
25
23
|
micAnalyser: () => micAnalyser,
|
|
26
24
|
startMicrophone: () => startMicrophone,
|
|
27
25
|
stopMicrophone: () => stopMicrophone
|
|
@@ -138,13 +136,15 @@ function stopMicrophone() {
|
|
|
138
136
|
audioStream = void 0;
|
|
139
137
|
}
|
|
140
138
|
}
|
|
141
|
-
|
|
139
|
+
|
|
140
|
+
// src/audio/createDelayedStream.ts
|
|
141
|
+
function createDelayedStream(stream, delay) {
|
|
142
142
|
if (!audioContext) {
|
|
143
143
|
throw new Error("AudioContext not initialized");
|
|
144
144
|
}
|
|
145
145
|
const audioSource = audioContext.createMediaStreamSource(stream);
|
|
146
|
-
const delayNode = audioContext.createDelay();
|
|
147
|
-
delayNode.delayTime.value =
|
|
146
|
+
const delayNode = audioContext.createDelay(delay);
|
|
147
|
+
delayNode.delayTime.value = delay;
|
|
148
148
|
const audioDestination = audioContext.createMediaStreamDestination();
|
|
149
149
|
audioSource.connect(delayNode);
|
|
150
150
|
delayNode.connect(audioDestination);
|
|
@@ -152,7 +152,7 @@ function createDelayedStream(stream) {
|
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
// src/audio/MicRecorder.ts
|
|
155
|
-
var timeSlice =
|
|
155
|
+
var timeSlice = 100;
|
|
156
156
|
var defaultMicRecorderState = {
|
|
157
157
|
isStarting: false,
|
|
158
158
|
isStarted: false,
|
|
@@ -161,22 +161,36 @@ var defaultMicRecorderState = {
|
|
|
161
161
|
threshold: defaultMicThreshold
|
|
162
162
|
};
|
|
163
163
|
var MicRecorder = class extends EventEmitter2 {
|
|
164
|
-
constructor() {
|
|
164
|
+
constructor(vad) {
|
|
165
165
|
super();
|
|
166
166
|
this.audioInfo = this.getAudioInfo();
|
|
167
|
+
this.speakingConfirmed = false;
|
|
168
|
+
this.queuedChunks = [];
|
|
167
169
|
this.onStartSpeaking = (async () => {
|
|
168
170
|
if (!this.recorder || this.state.isMuted) return;
|
|
169
171
|
this.recorder.start(timeSlice);
|
|
172
|
+
this.queuedChunks.length = 0;
|
|
173
|
+
}).bind(this);
|
|
174
|
+
this.onConfirmSpeaking = (async () => {
|
|
170
175
|
this.emit("StartSpeaking");
|
|
171
176
|
this.changeState({ isSpeaking: true });
|
|
177
|
+
this.speakingConfirmed = true;
|
|
178
|
+
}).bind(this);
|
|
179
|
+
this.onCancelSpeaking = (async () => {
|
|
180
|
+
if (!this.recorder) return;
|
|
181
|
+
this.recorder.stop();
|
|
182
|
+
this.queuedChunks.length = 0;
|
|
172
183
|
}).bind(this);
|
|
173
184
|
this.onStopSpeaking = (async () => {
|
|
174
|
-
if (!this.recorder
|
|
185
|
+
if (!this.recorder) return;
|
|
175
186
|
this.recorder.stop();
|
|
176
187
|
this.changeState({ isSpeaking: false });
|
|
188
|
+
this.emit("StopSpeaking");
|
|
189
|
+
this.speakingConfirmed = false;
|
|
177
190
|
}).bind(this);
|
|
178
191
|
const threshold = parseFloat(localStorage.getItem("micdrop.micThreshold" /* MicThreshold */) || "0") || defaultMicThreshold;
|
|
179
192
|
this.state = { ...defaultMicRecorderState, threshold };
|
|
193
|
+
this.vad = vad;
|
|
180
194
|
}
|
|
181
195
|
async start(stream) {
|
|
182
196
|
if (this.state.isStarted) {
|
|
@@ -188,15 +202,19 @@ var MicRecorder = class extends EventEmitter2 {
|
|
|
188
202
|
isMuted: false,
|
|
189
203
|
isSpeaking: false
|
|
190
204
|
});
|
|
191
|
-
const delayedStream = createDelayedStream(
|
|
205
|
+
const delayedStream = createDelayedStream(
|
|
206
|
+
stream,
|
|
207
|
+
this.vad.delay / 1e3 + 0.05
|
|
208
|
+
);
|
|
192
209
|
this.delayedStream = delayedStream;
|
|
193
|
-
if (
|
|
194
|
-
this.recorder
|
|
195
|
-
mimeType: this.audioInfo.mimeType,
|
|
196
|
-
audioBitsPerSecond: 128e3
|
|
197
|
-
});
|
|
198
|
-
this.recorder.ondataavailable = this.onDataAvailable.bind(this);
|
|
210
|
+
if (this.recorder) {
|
|
211
|
+
this.recorder.stop();
|
|
199
212
|
}
|
|
213
|
+
this.recorder = new window.MediaRecorder(delayedStream, {
|
|
214
|
+
mimeType: this.audioInfo.mimeType,
|
|
215
|
+
audioBitsPerSecond: 128e3
|
|
216
|
+
});
|
|
217
|
+
this.recorder.ondataavailable = this.onDataAvailable.bind(this);
|
|
200
218
|
await this.startSpeakingDetection(stream);
|
|
201
219
|
this.changeState({
|
|
202
220
|
isStarting: false,
|
|
@@ -238,30 +256,35 @@ var MicRecorder = class extends EventEmitter2 {
|
|
|
238
256
|
setThreshold(threshold) {
|
|
239
257
|
this.changeState({ threshold });
|
|
240
258
|
localStorage.setItem("micdrop.micThreshold" /* MicThreshold */, threshold.toString());
|
|
241
|
-
this.
|
|
259
|
+
this.vad.setThreshold(threshold);
|
|
242
260
|
}
|
|
243
261
|
async startSpeakingDetection(stream) {
|
|
244
|
-
if (this.
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
this.
|
|
251
|
-
this.
|
|
252
|
-
}
|
|
253
|
-
stopSpeakingDetection() {
|
|
254
|
-
|
|
255
|
-
this.
|
|
256
|
-
this.
|
|
257
|
-
this.
|
|
258
|
-
this.
|
|
262
|
+
if (this.vad.isStarted) {
|
|
263
|
+
this.vad.stop();
|
|
264
|
+
}
|
|
265
|
+
this.vad.on("StartSpeaking", this.onStartSpeaking);
|
|
266
|
+
this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
267
|
+
this.vad.on("CancelSpeaking", this.onCancelSpeaking);
|
|
268
|
+
this.vad.on("StopSpeaking", this.onStopSpeaking);
|
|
269
|
+
await this.vad.start(stream, this.state.threshold);
|
|
270
|
+
}
|
|
271
|
+
async stopSpeakingDetection() {
|
|
272
|
+
this.vad.off("StartSpeaking", this.onStartSpeaking);
|
|
273
|
+
this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
274
|
+
this.vad.off("CancelSpeaking", this.onCancelSpeaking);
|
|
275
|
+
this.vad.off("StopSpeaking", this.onStopSpeaking);
|
|
276
|
+
await this.vad.stop();
|
|
259
277
|
}
|
|
260
278
|
async onDataAvailable(blobEvent) {
|
|
261
|
-
this.
|
|
262
|
-
|
|
263
|
-
|
|
279
|
+
if (!this.speakingConfirmed) {
|
|
280
|
+
this.queuedChunks.push(blobEvent.data);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
for (const chunk of this.queuedChunks) {
|
|
284
|
+
this.emit("Chunk", chunk);
|
|
264
285
|
}
|
|
286
|
+
this.queuedChunks.length = 0;
|
|
287
|
+
this.emit("Chunk", blobEvent.data);
|
|
265
288
|
}
|
|
266
289
|
changeState(state) {
|
|
267
290
|
const hasChanged = Object.keys(state).some(
|
|
@@ -288,9 +311,11 @@ var speaker_exports = {};
|
|
|
288
311
|
__export(speaker_exports, {
|
|
289
312
|
canChangeSpeakerDevice: () => canChangeSpeakerDevice,
|
|
290
313
|
changeSpeakerDevice: () => changeSpeakerDevice,
|
|
291
|
-
|
|
314
|
+
pauseAudio: () => pauseAudio,
|
|
315
|
+
playAudio: () => playAudio,
|
|
316
|
+
resumeAudio: () => resumeAudio,
|
|
292
317
|
speakerAnalyser: () => speakerAnalyser,
|
|
293
|
-
|
|
318
|
+
stopAudio: () => stopAudio
|
|
294
319
|
});
|
|
295
320
|
|
|
296
321
|
// src/audio/unlock.ts
|
|
@@ -324,88 +349,344 @@ function unlockSafari(audioContext3) {
|
|
|
324
349
|
// src/audio/speaker.ts
|
|
325
350
|
var audioElement;
|
|
326
351
|
var audioContext2;
|
|
327
|
-
var
|
|
352
|
+
var mediaSource;
|
|
353
|
+
var sourceBuffer;
|
|
354
|
+
var currentMediaStream;
|
|
355
|
+
var isProcessingQueue = false;
|
|
356
|
+
var pendingBlobs = [];
|
|
328
357
|
var speakerAnalyser;
|
|
329
358
|
function init2() {
|
|
330
359
|
if (audioContext2) return;
|
|
331
360
|
audioContext2 = new (window.AudioContext || window.webkitAudioContext)();
|
|
332
361
|
speakerAnalyser = new AudioAnalyser(audioContext2);
|
|
333
|
-
speakerAnalyser.node.connect(audioContext2.destination);
|
|
334
362
|
unlock(audioContext2);
|
|
335
363
|
}
|
|
336
364
|
init2();
|
|
337
|
-
function
|
|
338
|
-
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
339
|
-
}
|
|
340
|
-
function setSinkId(speakerId) {
|
|
341
|
-
if (speakerId && canChangeSpeakerDevice()) {
|
|
342
|
-
return audioElement?.setSinkId(speakerId);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
async function attachAudioElement(speakerId) {
|
|
365
|
+
async function setupAudioElement(speakerId) {
|
|
346
366
|
if (!audioContext2 || !speakerAnalyser) return;
|
|
347
367
|
if (!speakerId) {
|
|
348
368
|
speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) || void 0;
|
|
349
369
|
}
|
|
350
|
-
if (!
|
|
351
|
-
|
|
352
|
-
|
|
370
|
+
if (!audioElement) {
|
|
371
|
+
audioElement = new Audio();
|
|
372
|
+
audioElement.autoplay = true;
|
|
373
|
+
mediaSource = new MediaSource();
|
|
374
|
+
if (!mediaSource) {
|
|
375
|
+
throw new Error("Failed to create MediaSource");
|
|
376
|
+
}
|
|
377
|
+
audioElement.src = URL.createObjectURL(mediaSource);
|
|
378
|
+
const ms = mediaSource;
|
|
379
|
+
ms.addEventListener("sourceopen", () => {
|
|
380
|
+
sourceBuffer = ms.addSourceBuffer("audio/mpeg");
|
|
381
|
+
sourceBuffer.addEventListener("updateend", () => {
|
|
382
|
+
processQueue();
|
|
383
|
+
});
|
|
384
|
+
if (pendingBlobs.length > 0) {
|
|
385
|
+
processQueue();
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
audioElement.addEventListener("timeupdate", function onTimeUpdate() {
|
|
389
|
+
if (!audioContext2 || !speakerAnalyser || !audioElement) return;
|
|
390
|
+
try {
|
|
391
|
+
const mediaStream = audioElement.captureStream();
|
|
392
|
+
if (mediaStream.getAudioTracks().length === 0) return;
|
|
393
|
+
currentMediaStream = mediaStream;
|
|
394
|
+
const sourceNode2 = audioContext2.createMediaStreamSource(mediaStream);
|
|
395
|
+
sourceNode2.connect(speakerAnalyser.node);
|
|
396
|
+
audioElement.removeEventListener("timeupdate", onTimeUpdate);
|
|
397
|
+
} catch (error) {
|
|
398
|
+
console.error("Error creating media stream source:", error);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
if (speakerId) {
|
|
403
|
+
try {
|
|
404
|
+
await setSinkId(speakerId);
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return audioElement;
|
|
410
|
+
}
|
|
411
|
+
async function processQueue() {
|
|
412
|
+
if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
|
|
413
|
+
if (isProcessingQueue || sourceBuffer.updating) return;
|
|
414
|
+
isProcessingQueue = true;
|
|
415
|
+
try {
|
|
416
|
+
await playNextBlob();
|
|
417
|
+
} finally {
|
|
418
|
+
isProcessingQueue = false;
|
|
419
|
+
if (pendingBlobs.length > 0 && !sourceBuffer.updating) {
|
|
420
|
+
processQueue();
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
async function playNextBlob() {
|
|
425
|
+
if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
|
|
426
|
+
if (sourceBuffer.updating) return;
|
|
427
|
+
if (mediaSource.readyState !== "open") {
|
|
428
|
+
console.warn("MediaSource is no longer open, resetting audio pipeline");
|
|
429
|
+
stopAudio();
|
|
430
|
+
await setupAudioElement();
|
|
353
431
|
return;
|
|
354
432
|
}
|
|
355
|
-
|
|
433
|
+
const blob = pendingBlobs.shift();
|
|
356
434
|
try {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
435
|
+
if (sourceBuffer && Array.from(mediaSource.sourceBuffers).includes(sourceBuffer)) {
|
|
436
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
437
|
+
sourceBuffer.appendBuffer(arrayBuffer);
|
|
438
|
+
} else {
|
|
439
|
+
console.warn("SourceBuffer is no longer valid, resetting audio pipeline");
|
|
440
|
+
stopAudio();
|
|
441
|
+
await setupAudioElement();
|
|
442
|
+
}
|
|
363
443
|
} catch (error) {
|
|
364
|
-
console.error(
|
|
365
|
-
|
|
444
|
+
console.error("Error appending buffer", error);
|
|
445
|
+
if (error instanceof Error && error.name === "InvalidStateError") {
|
|
446
|
+
console.warn("Resetting audio pipeline due to invalid state");
|
|
447
|
+
stopAudio();
|
|
448
|
+
await setupAudioElement();
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function canChangeSpeakerDevice() {
|
|
453
|
+
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
454
|
+
}
|
|
455
|
+
async function setSinkId(speakerId) {
|
|
456
|
+
if (speakerId && canChangeSpeakerDevice() && audioElement) {
|
|
457
|
+
return audioElement.setSinkId(speakerId);
|
|
366
458
|
}
|
|
367
459
|
}
|
|
368
460
|
async function changeSpeakerDevice(speakerId) {
|
|
369
461
|
if (!canChangeSpeakerDevice()) return;
|
|
370
462
|
try {
|
|
371
|
-
await
|
|
463
|
+
await setupAudioElement(speakerId);
|
|
372
464
|
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
373
465
|
} catch (error) {
|
|
374
466
|
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
375
467
|
}
|
|
376
468
|
}
|
|
377
|
-
|
|
469
|
+
function pauseAudio() {
|
|
470
|
+
if (audioElement) {
|
|
471
|
+
audioElement.pause();
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
function resumeAudio() {
|
|
475
|
+
if (audioElement) {
|
|
476
|
+
audioElement.play();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
async function playAudio(blob) {
|
|
378
480
|
if (!audioContext2 || !speakerAnalyser) return;
|
|
481
|
+
pendingBlobs.push(blob);
|
|
482
|
+
if (!audioElement) {
|
|
483
|
+
await setupAudioElement();
|
|
484
|
+
}
|
|
485
|
+
if (!audioElement) return;
|
|
379
486
|
try {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
};
|
|
387
|
-
sourceNode2.buffer = await audioContext2.decodeAudioData(
|
|
388
|
-
await blob.arrayBuffer()
|
|
389
|
-
);
|
|
390
|
-
sourceNode2.connect(speakerAnalyser.node);
|
|
391
|
-
sourceNode2.start();
|
|
487
|
+
if (audioElement.paused) {
|
|
488
|
+
audioElement.play();
|
|
489
|
+
}
|
|
490
|
+
if (sourceBuffer && !sourceBuffer.updating && !isProcessingQueue) {
|
|
491
|
+
processQueue();
|
|
492
|
+
}
|
|
392
493
|
} catch (error) {
|
|
393
494
|
console.error("Error playing audio blob", error, blob);
|
|
394
495
|
}
|
|
395
496
|
}
|
|
396
|
-
function
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
497
|
+
function stopAudio() {
|
|
498
|
+
pendingBlobs.length = 0;
|
|
499
|
+
if (mediaSource) {
|
|
500
|
+
try {
|
|
501
|
+
if (mediaSource.readyState === "open") {
|
|
502
|
+
mediaSource.endOfStream();
|
|
503
|
+
}
|
|
504
|
+
if (sourceBuffer && mediaSource.readyState !== "closed") {
|
|
505
|
+
mediaSource.removeSourceBuffer(sourceBuffer);
|
|
506
|
+
}
|
|
507
|
+
} catch (error) {
|
|
508
|
+
console.error("Error cleaning up media source", error);
|
|
509
|
+
}
|
|
510
|
+
mediaSource = void 0;
|
|
511
|
+
sourceBuffer = void 0;
|
|
512
|
+
}
|
|
513
|
+
if (audioElement) {
|
|
514
|
+
audioElement.pause();
|
|
515
|
+
audioElement.src = "";
|
|
516
|
+
audioElement = void 0;
|
|
517
|
+
}
|
|
518
|
+
if (currentMediaStream) {
|
|
519
|
+
currentMediaStream.getTracks().forEach((track) => track.stop());
|
|
520
|
+
currentMediaStream = void 0;
|
|
402
521
|
}
|
|
403
|
-
audioBlobSourceNode.disconnect();
|
|
404
|
-
audioBlobSourceNode = void 0;
|
|
405
522
|
}
|
|
406
523
|
|
|
407
|
-
// src/
|
|
524
|
+
// src/audio/vad/SileroVAD.ts
|
|
525
|
+
import { MicVAD } from "@ricky0123/vad-web/src";
|
|
526
|
+
|
|
527
|
+
// src/audio/vad/VAD.ts
|
|
408
528
|
import EventEmitter3 from "eventemitter3";
|
|
529
|
+
var VAD = class extends EventEmitter3 {
|
|
530
|
+
/**
|
|
531
|
+
* Constructor for the VAD class
|
|
532
|
+
* @param threshold - The threshold to use for the VAD
|
|
533
|
+
*/
|
|
534
|
+
constructor(threshold = 0) {
|
|
535
|
+
super();
|
|
536
|
+
// Max delay to detect speech, can be used to record delayed stream
|
|
537
|
+
this.delay = 100;
|
|
538
|
+
this.threshold = threshold;
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
// src/audio/vad/SileroVAD.ts
|
|
543
|
+
var SileroVAD = class extends VAD {
|
|
544
|
+
get isStarted() {
|
|
545
|
+
return !!this.vad;
|
|
546
|
+
}
|
|
547
|
+
async start(stream, threshold) {
|
|
548
|
+
if (this.vad) return;
|
|
549
|
+
if (threshold !== void 0) {
|
|
550
|
+
this.threshold = threshold;
|
|
551
|
+
}
|
|
552
|
+
const speechThreshold = Math.min(
|
|
553
|
+
Math.max((this.threshold + 100) / 100, 0),
|
|
554
|
+
1
|
|
555
|
+
);
|
|
556
|
+
this.vad = await MicVAD.new({
|
|
557
|
+
stream,
|
|
558
|
+
onSpeechStart: () => this.emit("StartSpeaking"),
|
|
559
|
+
onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
|
|
560
|
+
onVADMisfire: () => this.emit("CancelSpeaking"),
|
|
561
|
+
onSpeechEnd: () => this.emit("StopSpeaking"),
|
|
562
|
+
positiveSpeechThreshold: speechThreshold,
|
|
563
|
+
negativeSpeechThreshold: speechThreshold * 0.7,
|
|
564
|
+
// Adjust negative threshold proportionally
|
|
565
|
+
redemptionFrames: 5
|
|
566
|
+
});
|
|
567
|
+
this.vad.start();
|
|
568
|
+
}
|
|
569
|
+
async stop() {
|
|
570
|
+
if (!this.vad) return;
|
|
571
|
+
this.vad.pause();
|
|
572
|
+
this.vad = void 0;
|
|
573
|
+
}
|
|
574
|
+
async setThreshold(threshold) {
|
|
575
|
+
if (threshold === this.threshold) return;
|
|
576
|
+
if (this.vad) {
|
|
577
|
+
const stream = this.vad["stream"];
|
|
578
|
+
await this.stop();
|
|
579
|
+
await this.start(stream, threshold);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
// src/audio/vad/VolumeVAD.ts
|
|
585
|
+
var VolumeVAD = class extends VAD {
|
|
586
|
+
constructor() {
|
|
587
|
+
super();
|
|
588
|
+
this.running = false;
|
|
589
|
+
this.speaking = false;
|
|
590
|
+
this.attemptSpeaking = false;
|
|
591
|
+
this.history = 10;
|
|
592
|
+
this.speakingHistory = [];
|
|
593
|
+
for (let i = 0; i < this.history; i++) {
|
|
594
|
+
this.speakingHistory.push(0);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
getMaxVolume() {
|
|
598
|
+
if (!this.analyser || !this.fftBins) return -Infinity;
|
|
599
|
+
let maxVolume = -Infinity;
|
|
600
|
+
this.analyser.getFloatFrequencyData(this.fftBins);
|
|
601
|
+
for (let i = 4; i < this.fftBins.length; i++) {
|
|
602
|
+
if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
|
|
603
|
+
maxVolume = this.fftBins[i];
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return maxVolume;
|
|
607
|
+
}
|
|
608
|
+
get isStarted() {
|
|
609
|
+
return this.running;
|
|
610
|
+
}
|
|
611
|
+
async start(stream, threshold) {
|
|
612
|
+
if (this.running) return;
|
|
613
|
+
if (threshold !== void 0) {
|
|
614
|
+
this.threshold = threshold;
|
|
615
|
+
}
|
|
616
|
+
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
617
|
+
this.analyser = this.audioContext.createAnalyser();
|
|
618
|
+
this.sourceNode = this.audioContext.createMediaStreamSource(stream);
|
|
619
|
+
this.analyser.fftSize = 512;
|
|
620
|
+
this.analyser.smoothingTimeConstant = 0.1;
|
|
621
|
+
this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
|
|
622
|
+
this.sourceNode.connect(this.analyser);
|
|
623
|
+
this.running = true;
|
|
624
|
+
this.startLoop();
|
|
625
|
+
}
|
|
626
|
+
startLoop() {
|
|
627
|
+
if (!this.running) return;
|
|
628
|
+
const currentVolume = this.getMaxVolume();
|
|
629
|
+
if (currentVolume > this.threshold) {
|
|
630
|
+
if (!this.speaking) {
|
|
631
|
+
let recentHistory = 0;
|
|
632
|
+
for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
|
|
633
|
+
recentHistory += this.speakingHistory[i];
|
|
634
|
+
}
|
|
635
|
+
if (recentHistory >= 2) {
|
|
636
|
+
this.speaking = true;
|
|
637
|
+
this.attemptSpeaking = false;
|
|
638
|
+
this.emit("ConfirmSpeaking");
|
|
639
|
+
} else if (recentHistory === 1) {
|
|
640
|
+
this.attemptSpeaking = true;
|
|
641
|
+
this.emit("StartSpeaking");
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
} else if (this.speaking) {
|
|
645
|
+
let totalHistory = 0;
|
|
646
|
+
for (let i = 0; i < this.speakingHistory.length; i++) {
|
|
647
|
+
totalHistory += this.speakingHistory[i];
|
|
648
|
+
}
|
|
649
|
+
if (totalHistory === 0) {
|
|
650
|
+
this.speaking = false;
|
|
651
|
+
this.emit("StopSpeaking");
|
|
652
|
+
}
|
|
653
|
+
} else if (this.attemptSpeaking) {
|
|
654
|
+
let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
|
|
655
|
+
if (lastHistory === 0) {
|
|
656
|
+
this.attemptSpeaking = false;
|
|
657
|
+
this.emit("CancelSpeaking");
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
this.speakingHistory.shift();
|
|
661
|
+
this.speakingHistory.push(Number(currentVolume > this.threshold));
|
|
662
|
+
setTimeout(() => this.startLoop(), this.delay);
|
|
663
|
+
}
|
|
664
|
+
async stop() {
|
|
665
|
+
if (!this.running) return;
|
|
666
|
+
this.running = false;
|
|
667
|
+
this.speaking = false;
|
|
668
|
+
if (this.sourceNode) {
|
|
669
|
+
this.sourceNode.disconnect();
|
|
670
|
+
this.sourceNode = void 0;
|
|
671
|
+
}
|
|
672
|
+
if (this.analyser) {
|
|
673
|
+
this.analyser.disconnect();
|
|
674
|
+
this.analyser = void 0;
|
|
675
|
+
}
|
|
676
|
+
if (this.audioContext) {
|
|
677
|
+
await this.audioContext.close();
|
|
678
|
+
this.audioContext = void 0;
|
|
679
|
+
}
|
|
680
|
+
this.fftBins = void 0;
|
|
681
|
+
}
|
|
682
|
+
async setThreshold(threshold) {
|
|
683
|
+
if (threshold === this.threshold) return;
|
|
684
|
+
this.threshold = threshold;
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
// src/call/CallHandler.ts
|
|
689
|
+
import EventEmitter4 from "eventemitter3";
|
|
409
690
|
|
|
410
691
|
// src/call/CallHandlerError.ts
|
|
411
692
|
var CallHandlerErrorCode = /* @__PURE__ */ ((CallHandlerErrorCode2) => {
|
|
@@ -422,36 +703,42 @@ var CallHandlerError = class extends Error {
|
|
|
422
703
|
};
|
|
423
704
|
|
|
424
705
|
// src/call/CallHandler.ts
|
|
425
|
-
var CallHandler = class _CallHandler extends
|
|
426
|
-
constructor() {
|
|
706
|
+
var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
707
|
+
constructor(options) {
|
|
427
708
|
super();
|
|
428
|
-
this.micRecorder = new MicRecorder();
|
|
429
709
|
this.conversation = [];
|
|
430
710
|
this.debug = false;
|
|
431
711
|
this.startTime = 0;
|
|
712
|
+
this.micRecorder = new MicRecorder(this.getVAD(options?.vad));
|
|
432
713
|
this.micRecorder.on("StateChange", () => {
|
|
433
714
|
this.notifyStateChange();
|
|
434
715
|
});
|
|
435
716
|
this.micRecorder.on("Chunk", (blob) => {
|
|
436
|
-
this.log(`[Mic]
|
|
717
|
+
this.log(`[Mic] Received chunk`, blob);
|
|
437
718
|
this.ws?.send(blob);
|
|
438
719
|
});
|
|
439
720
|
this.micRecorder.on("StartSpeaking", () => {
|
|
440
|
-
this.log("[Mic]
|
|
721
|
+
this.log("[Mic] Start speaking");
|
|
441
722
|
this.ws?.send("startSpeaking" /* StartSpeaking */);
|
|
442
|
-
|
|
723
|
+
stopAudio();
|
|
443
724
|
});
|
|
444
725
|
this.micRecorder.on("StopSpeaking", () => {
|
|
445
|
-
this.log("[Mic]
|
|
726
|
+
this.log("[Mic] Stop speaking");
|
|
446
727
|
this.ws?.send("stopSpeaking" /* StopSpeaking */);
|
|
447
728
|
});
|
|
448
729
|
}
|
|
449
|
-
static getInstance() {
|
|
730
|
+
static getInstance(options) {
|
|
450
731
|
if (!_CallHandler.instance) {
|
|
451
|
-
_CallHandler.instance = new _CallHandler();
|
|
732
|
+
_CallHandler.instance = new _CallHandler(options);
|
|
452
733
|
}
|
|
453
734
|
return _CallHandler.instance;
|
|
454
735
|
}
|
|
736
|
+
getVAD(vad) {
|
|
737
|
+
if (!vad) return new VolumeVAD();
|
|
738
|
+
if (vad === "volume") return new VolumeVAD();
|
|
739
|
+
if (vad === "silero") return new SileroVAD();
|
|
740
|
+
return vad;
|
|
741
|
+
}
|
|
455
742
|
get isStarted() {
|
|
456
743
|
return this.isWSStarted && this.micRecorder.state.isStarted;
|
|
457
744
|
}
|
|
@@ -489,11 +776,13 @@ var CallHandler = class _CallHandler extends EventEmitter3 {
|
|
|
489
776
|
pause() {
|
|
490
777
|
this.micRecorder.mute();
|
|
491
778
|
this.notifyStateChange();
|
|
779
|
+
pauseAudio();
|
|
492
780
|
}
|
|
493
781
|
resume() {
|
|
494
782
|
if (this.lastAudioBlob) {
|
|
495
|
-
|
|
783
|
+
playAudio(this.lastAudioBlob);
|
|
496
784
|
}
|
|
785
|
+
resumeAudio();
|
|
497
786
|
this.micRecorder.unmute();
|
|
498
787
|
this.notifyStateChange();
|
|
499
788
|
}
|
|
@@ -542,7 +831,7 @@ var CallHandler = class _CallHandler extends EventEmitter3 {
|
|
|
542
831
|
this.ws.onmessage = (event) => {
|
|
543
832
|
this.log("[WS]", event.data);
|
|
544
833
|
if (event.data instanceof Blob) {
|
|
545
|
-
|
|
834
|
+
playAudio(event.data);
|
|
546
835
|
this.lastAudioBlob = event.data;
|
|
547
836
|
} else if (typeof event.data !== "string") {
|
|
548
837
|
console.warn(`[WS] Unknown message type: ${event.data}`);
|
|
@@ -626,6 +915,9 @@ export {
|
|
|
626
915
|
LocalStorageKeys,
|
|
627
916
|
microphone_exports as Mic,
|
|
628
917
|
MicRecorder,
|
|
629
|
-
|
|
918
|
+
SileroVAD,
|
|
919
|
+
speaker_exports as Speaker,
|
|
920
|
+
VAD,
|
|
921
|
+
VolumeVAD
|
|
630
922
|
};
|
|
631
923
|
//# sourceMappingURL=index.mjs.map
|