@micdrop/client 2.1.0 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +32 -8
- package/dist/index.d.ts +32 -8
- package/dist/index.js +129 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +128 -59
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -344,9 +344,6 @@ var Speaker = class extends EventEmitter3 {
|
|
|
344
344
|
// src/audio/MicRecorder.ts
|
|
345
345
|
import { EventEmitter as EventEmitter5 } from "eventemitter3";
|
|
346
346
|
|
|
347
|
-
// raw:./pcm-processor-worklet
|
|
348
|
-
var pcm_processor_worklet_default = "const INTERVAL_MS = 100\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.audioBuffer = []\n this.isRecording = false\n this.sourceSampleRate = 16000\n this.targetSampleRate = 16000\n this.resamplingRatio = 1\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n\n // Listen for messages\n this.port.onmessage = this.handleMessage.bind(this)\n }\n\n handleMessage(event) {\n if (event.data.type === 'configure') {\n this.sourceSampleRate = event.data.sourceSampleRate\n this.targetSampleRate = event.data.targetSampleRate\n this.resamplingRatio = this.sourceSampleRate / this.targetSampleRate\n // Recalculate chunk interval for the source sample rate\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n } else if (event.data.type === 'start') {\n this.isRecording = true\n } else if (event.data.type === 'stop') {\n this.isRecording = false\n // Clear any remaining audio buffer when stopping\n this.audioBuffer.length = 0\n }\n }\n\n process(inputs) {\n const input = inputs[0]\n\n if (input && input.length > 0 && this.isRecording) {\n const inputChannel = input[0]\n\n // Store audio data in buffer\n this.audioBuffer.push(new Float32Array(inputChannel))\n\n // Check if we have enough data for a chunk\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n\n if (totalSamples >= this.CHUNK_INTERVAL_SAMPLES) {\n this.processChunk()\n }\n }\n\n return true\n }\n\n processChunk() {\n if (this.audioBuffer.length === 0) return\n\n // Calculate total samples\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n if (totalSamples === 0) return\n\n // Merge all buffers\n const mergedBuffer = new Float32Array(totalSamples)\n let offset = 0\n for (const buffer of this.audioBuffer) {\n mergedBuffer.set(buffer, offset)\n offset += buffer.length\n }\n\n // Resample to target sample rate if necessary\n const resampledBuffer = this.resample(mergedBuffer)\n\n // Convert float32 to int16 PCM\n const pcmData = this.floatToPCM16(resampledBuffer)\n\n // Clear the buffer\n this.audioBuffer.length = 0\n\n // Send PCM chunk to main thread\n this.port.postMessage({\n type: 'chunk',\n data: pcmData,\n })\n }\n\n resample(inputBuffer) {\n // If no resampling needed, return original buffer\n if (this.resamplingRatio === 1) {\n return inputBuffer\n }\n\n // Calculate output length\n const outputLength = Math.round(inputBuffer.length / this.resamplingRatio)\n const outputBuffer = new Float32Array(outputLength)\n\n // Simple linear interpolation resampling\n for (let i = 0; i < outputLength; i++) {\n const srcIndex = i * this.resamplingRatio\n const srcIndexFloor = Math.floor(srcIndex)\n const srcIndexCeil = Math.min(srcIndexFloor + 1, inputBuffer.length - 1)\n const fraction = srcIndex - srcIndexFloor\n\n // Linear interpolation\n outputBuffer[i] =\n inputBuffer[srcIndexFloor] * (1 - fraction) +\n inputBuffer[srcIndexCeil] * fraction\n }\n\n return outputBuffer\n }\n\n floatToPCM16(floatData) {\n const pcmData = new Int16Array(floatData.length)\n for (let i = 0; i < floatData.length; i++) {\n const sample = Math.max(-1, Math.min(1, floatData[i]))\n pcmData[i] = sample < 0 ? sample * 0x8000 : sample * 0x7fff\n }\n return pcmData\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n";
|
|
349
|
-
|
|
350
347
|
// src/audio/utils/delayedStream.ts
|
|
351
348
|
function createDelayedStream(stream, delay) {
|
|
352
349
|
const audioSource = audioContext.createMediaStreamSource(stream);
|
|
@@ -450,7 +447,10 @@ var MultipleVAD = class extends VAD {
|
|
|
450
447
|
get isStarted() {
|
|
451
448
|
return this.vads.some((vad) => vad.isStarted);
|
|
452
449
|
}
|
|
453
|
-
|
|
450
|
+
get isPaused() {
|
|
451
|
+
return this.vads.every((vad) => vad.isPaused);
|
|
452
|
+
}
|
|
453
|
+
async start(stream) {
|
|
454
454
|
for (const vad of this.vads) {
|
|
455
455
|
if (vad.isStarted) {
|
|
456
456
|
continue;
|
|
@@ -465,6 +465,16 @@ var MultipleVAD = class extends VAD {
|
|
|
465
465
|
await vad.stop();
|
|
466
466
|
}
|
|
467
467
|
}
|
|
468
|
+
async pause() {
|
|
469
|
+
for (const vad of this.vads) {
|
|
470
|
+
await vad.pause();
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
async resume() {
|
|
474
|
+
for (const vad of this.vads) {
|
|
475
|
+
await vad.resume();
|
|
476
|
+
}
|
|
477
|
+
}
|
|
468
478
|
};
|
|
469
479
|
|
|
470
480
|
// src/audio/vad/SileroVAD.ts
|
|
@@ -479,6 +489,7 @@ var SileroVAD = class extends VAD {
|
|
|
479
489
|
constructor(options) {
|
|
480
490
|
super();
|
|
481
491
|
this.options = defaultOptions;
|
|
492
|
+
this._isPaused = false;
|
|
482
493
|
if (options) {
|
|
483
494
|
this.setOptions(options);
|
|
484
495
|
} else {
|
|
@@ -495,6 +506,9 @@ var SileroVAD = class extends VAD {
|
|
|
495
506
|
get isStarted() {
|
|
496
507
|
return !!this.vad;
|
|
497
508
|
}
|
|
509
|
+
get isPaused() {
|
|
510
|
+
return this._isPaused;
|
|
511
|
+
}
|
|
498
512
|
async start(stream) {
|
|
499
513
|
if (this.vad) return;
|
|
500
514
|
this.vad = await MicVAD.new({
|
|
@@ -514,6 +528,16 @@ var SileroVAD = class extends VAD {
|
|
|
514
528
|
this.vad.destroy();
|
|
515
529
|
this.vad = void 0;
|
|
516
530
|
}
|
|
531
|
+
async pause() {
|
|
532
|
+
if (!this.vad || this._isPaused) return;
|
|
533
|
+
this._isPaused = true;
|
|
534
|
+
this.vad.pause();
|
|
535
|
+
}
|
|
536
|
+
async resume() {
|
|
537
|
+
if (!this.vad || !this._isPaused) return;
|
|
538
|
+
this._isPaused = false;
|
|
539
|
+
this.vad.start();
|
|
540
|
+
}
|
|
517
541
|
setOptions(options) {
|
|
518
542
|
this.options = { ...this.options, ...options };
|
|
519
543
|
this.vad?.setOptions(this.options);
|
|
@@ -536,6 +560,7 @@ var VolumeVAD = class extends VAD {
|
|
|
536
560
|
constructor(options) {
|
|
537
561
|
super();
|
|
538
562
|
this.options = defaultOptions2;
|
|
563
|
+
this._isPaused = false;
|
|
539
564
|
this.running = false;
|
|
540
565
|
this.speaking = false;
|
|
541
566
|
this.attemptSpeaking = false;
|
|
@@ -567,8 +592,11 @@ var VolumeVAD = class extends VAD {
|
|
|
567
592
|
get isStarted() {
|
|
568
593
|
return this.running;
|
|
569
594
|
}
|
|
595
|
+
get isPaused() {
|
|
596
|
+
return this._isPaused;
|
|
597
|
+
}
|
|
570
598
|
async start(stream) {
|
|
571
|
-
if (this.running) return;
|
|
599
|
+
if (this.running || this._isPaused) return;
|
|
572
600
|
this.analyser = audioContext.createAnalyser();
|
|
573
601
|
this.sourceNode = audioContext.createMediaStreamSource(stream);
|
|
574
602
|
this.analyser.fftSize = 512;
|
|
@@ -616,10 +644,25 @@ var VolumeVAD = class extends VAD {
|
|
|
616
644
|
this.speakingHistory.push(Number(currentVolume > this.options.threshold));
|
|
617
645
|
setTimeout(() => this.startLoop(), this.delay);
|
|
618
646
|
}
|
|
647
|
+
resetHistory() {
|
|
648
|
+
this.speaking = false;
|
|
649
|
+
this.attemptSpeaking = false;
|
|
650
|
+
for (let i = 0; i < this.speakingHistory.length; i++) {
|
|
651
|
+
this.speakingHistory[i] = 0;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
619
654
|
async stop() {
|
|
620
655
|
if (!this.running) return;
|
|
621
656
|
this.running = false;
|
|
622
|
-
this.
|
|
657
|
+
this.resetHistory();
|
|
658
|
+
switch (this.status) {
|
|
659
|
+
case 2 /* Speaking */:
|
|
660
|
+
this.emit("StopSpeaking");
|
|
661
|
+
break;
|
|
662
|
+
case 1 /* MaybeSpeaking */:
|
|
663
|
+
this.emit("CancelSpeaking");
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
623
666
|
if (this.sourceNode) {
|
|
624
667
|
this.sourceNode.disconnect();
|
|
625
668
|
this.sourceNode = void 0;
|
|
@@ -630,6 +673,26 @@ var VolumeVAD = class extends VAD {
|
|
|
630
673
|
}
|
|
631
674
|
this.fftBins = void 0;
|
|
632
675
|
}
|
|
676
|
+
async pause() {
|
|
677
|
+
if (!this.running || this._isPaused) return;
|
|
678
|
+
this._isPaused = true;
|
|
679
|
+
this.running = false;
|
|
680
|
+
this.resetHistory();
|
|
681
|
+
switch (this.status) {
|
|
682
|
+
case 2 /* Speaking */:
|
|
683
|
+
this.emit("StopSpeaking");
|
|
684
|
+
break;
|
|
685
|
+
case 1 /* MaybeSpeaking */:
|
|
686
|
+
this.emit("CancelSpeaking");
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
async resume() {
|
|
691
|
+
if (!this._isPaused) return;
|
|
692
|
+
this._isPaused = false;
|
|
693
|
+
this.running = true;
|
|
694
|
+
this.startLoop();
|
|
695
|
+
}
|
|
633
696
|
setOptions(options) {
|
|
634
697
|
if (typeof options.history === "number" && options.history < 3) {
|
|
635
698
|
throw new Error("History must be at least 3");
|
|
@@ -662,18 +725,39 @@ function getVAD(vad) {
|
|
|
662
725
|
}
|
|
663
726
|
return vad;
|
|
664
727
|
}
|
|
728
|
+
function equalVADConfig(a, b) {
|
|
729
|
+
if (a === b) return true;
|
|
730
|
+
if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
|
|
731
|
+
return a.every((item, index) => equalVADConfig(item, b[index]));
|
|
732
|
+
}
|
|
733
|
+
return false;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// raw:./pcm-processor-worklet
|
|
737
|
+
var pcm_processor_worklet_default = "const INTERVAL_MS = 100\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.audioBuffer = []\n this.isRecording = false\n this.sourceSampleRate = 16000\n this.targetSampleRate = 16000\n this.resamplingRatio = 1\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n\n // Listen for messages\n this.port.onmessage = this.handleMessage.bind(this)\n }\n\n handleMessage(event) {\n if (event.data.type === 'configure') {\n this.sourceSampleRate = event.data.sourceSampleRate\n this.targetSampleRate = event.data.targetSampleRate\n this.resamplingRatio = this.sourceSampleRate / this.targetSampleRate\n // Recalculate chunk interval for the source sample rate\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n } else if (event.data.type === 'start') {\n this.isRecording = true\n } else if (event.data.type === 'stop') {\n this.isRecording = false\n // Clear any remaining audio buffer when stopping\n this.audioBuffer.length = 0\n }\n }\n\n process(inputs) {\n const input = inputs[0]\n\n if (input && input.length > 0 && this.isRecording) {\n const inputChannel = input[0]\n\n // Store audio data in buffer\n this.audioBuffer.push(new Float32Array(inputChannel))\n\n // Check if we have enough data for a chunk\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n\n if (totalSamples >= this.CHUNK_INTERVAL_SAMPLES) {\n this.processChunk()\n }\n }\n\n return true\n }\n\n processChunk() {\n if (this.audioBuffer.length === 0) return\n\n // Calculate total samples\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n if (totalSamples === 0) return\n\n // Merge all buffers\n const mergedBuffer = new Float32Array(totalSamples)\n let offset = 0\n for (const buffer of this.audioBuffer) {\n mergedBuffer.set(buffer, offset)\n offset += buffer.length\n }\n\n // Resample to target sample rate if necessary\n const resampledBuffer = this.resample(mergedBuffer)\n\n // Convert float32 to int16 PCM\n const pcmData = this.floatToPCM16(resampledBuffer)\n\n // Clear the buffer\n this.audioBuffer.length = 0\n\n // Send PCM chunk to main thread\n this.port.postMessage({\n type: 'chunk',\n data: pcmData,\n })\n }\n\n resample(inputBuffer) {\n // If no resampling needed, return original buffer\n if (this.resamplingRatio === 1) {\n return inputBuffer\n }\n\n // Calculate output length\n const outputLength = Math.round(inputBuffer.length / this.resamplingRatio)\n const outputBuffer = new Float32Array(outputLength)\n\n // Simple linear interpolation resampling\n for (let i = 0; i < outputLength; i++) {\n const srcIndex = i * this.resamplingRatio\n const srcIndexFloor = Math.floor(srcIndex)\n const srcIndexCeil = Math.min(srcIndexFloor + 1, inputBuffer.length - 1)\n const fraction = srcIndex - srcIndexFloor\n\n // Linear interpolation\n outputBuffer[i] =\n inputBuffer[srcIndexFloor] * (1 - fraction) +\n inputBuffer[srcIndexCeil] * fraction\n }\n\n return outputBuffer\n }\n\n floatToPCM16(floatData) {\n const pcmData = new Int16Array(floatData.length)\n for (let i = 0; i < floatData.length; i++) {\n const sample = Math.max(-1, Math.min(1, floatData[i]))\n pcmData[i] = sample < 0 ? sample * 0x8000 : sample * 0x7fff\n }\n return pcmData\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n";
|
|
738
|
+
|
|
739
|
+
// src/audio/pcm-processor.ts
|
|
740
|
+
async function initPcmProcessor() {
|
|
741
|
+
if (window.micdropPcmProcessor) return;
|
|
742
|
+
window.micdropPcmProcessor = true;
|
|
743
|
+
const workletBlob = new Blob([pcm_processor_worklet_default], {
|
|
744
|
+
type: "application/javascript"
|
|
745
|
+
});
|
|
746
|
+
const workletUrl = URL.createObjectURL(workletBlob);
|
|
747
|
+
await audioContext.audioWorklet.addModule(workletUrl);
|
|
748
|
+
}
|
|
665
749
|
|
|
666
750
|
// src/audio/MicRecorder.ts
|
|
667
751
|
var SAMPLE_RATE2 = 16e3;
|
|
668
752
|
var defaultMicRecorderState = {
|
|
669
753
|
isStarting: false,
|
|
670
754
|
isStarted: false,
|
|
671
|
-
isMuted: false,
|
|
672
755
|
isSpeaking: false
|
|
673
756
|
};
|
|
674
757
|
var MicRecorder = class extends EventEmitter5 {
|
|
675
758
|
constructor(vadConfig) {
|
|
676
759
|
super();
|
|
760
|
+
this.vadConfig = vadConfig;
|
|
677
761
|
this.speakingConfirmed = false;
|
|
678
762
|
this.queuedChunks = [];
|
|
679
763
|
this.start = async (stream) => {
|
|
@@ -683,25 +767,22 @@ var MicRecorder = class extends EventEmitter5 {
|
|
|
683
767
|
try {
|
|
684
768
|
this.changeState({
|
|
685
769
|
isStarting: true,
|
|
686
|
-
isMuted: false,
|
|
687
770
|
isSpeaking: false
|
|
688
771
|
});
|
|
772
|
+
this.stream = stream;
|
|
689
773
|
this.speakingConfirmed = false;
|
|
690
|
-
|
|
691
|
-
stream,
|
|
692
|
-
this.vad.delay / 1e3 + 0.05
|
|
693
|
-
);
|
|
694
|
-
this.delayedStream = delayedStream;
|
|
695
|
-
await audioContext.audioWorklet.addModule(this.workletUrl);
|
|
696
|
-
this.source = audioContext.createMediaStreamSource(delayedStream);
|
|
774
|
+
await initPcmProcessor();
|
|
697
775
|
this.workletNode = new AudioWorkletNode(audioContext, "pcm-processor");
|
|
698
776
|
this.workletNode.port.onmessage = this.onWorkletMessage;
|
|
699
|
-
this.source.connect(this.workletNode);
|
|
700
777
|
this.workletNode.port.postMessage({
|
|
701
778
|
type: "configure",
|
|
702
779
|
sourceSampleRate: audioContext.sampleRate,
|
|
703
780
|
targetSampleRate: SAMPLE_RATE2
|
|
704
781
|
});
|
|
782
|
+
const delayedStream = createDelayedStream(stream, this.vad.delay / 1e3);
|
|
783
|
+
this.delayedStream = delayedStream;
|
|
784
|
+
this.source = audioContext.createMediaStreamSource(delayedStream);
|
|
785
|
+
this.source.connect(this.workletNode);
|
|
705
786
|
await this.vad.start(stream);
|
|
706
787
|
this.vad.on("StartSpeaking", this.onStartSpeaking);
|
|
707
788
|
this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
@@ -716,30 +797,13 @@ var MicRecorder = class extends EventEmitter5 {
|
|
|
716
797
|
console.error(err);
|
|
717
798
|
}
|
|
718
799
|
};
|
|
719
|
-
this.mute = () => {
|
|
720
|
-
if (this.state.isMuted) return;
|
|
721
|
-
if (this.state.isSpeaking) {
|
|
722
|
-
this.onStopSpeaking();
|
|
723
|
-
}
|
|
724
|
-
this.changeState({ isMuted: true });
|
|
725
|
-
};
|
|
726
|
-
this.unmute = () => {
|
|
727
|
-
if (!this.state.isMuted) return;
|
|
728
|
-
this.changeState({ isMuted: false });
|
|
729
|
-
if (this.vad.status === 1 /* MaybeSpeaking */) {
|
|
730
|
-
this.onStartSpeaking();
|
|
731
|
-
} else if (this.vad.status === 2 /* Speaking */) {
|
|
732
|
-
this.onStartSpeaking();
|
|
733
|
-
this.onConfirmSpeaking();
|
|
734
|
-
}
|
|
735
|
-
};
|
|
736
800
|
this.stop = () => {
|
|
737
801
|
this.changeState({
|
|
738
802
|
isStarting: false,
|
|
739
803
|
isStarted: false,
|
|
740
|
-
isMuted: false,
|
|
741
804
|
isSpeaking: false
|
|
742
805
|
});
|
|
806
|
+
this.stream = void 0;
|
|
743
807
|
try {
|
|
744
808
|
this.vad.stop();
|
|
745
809
|
this.vad.off("StartSpeaking", this.onStartSpeaking);
|
|
@@ -762,32 +826,39 @@ var MicRecorder = class extends EventEmitter5 {
|
|
|
762
826
|
console.error(err);
|
|
763
827
|
}
|
|
764
828
|
};
|
|
829
|
+
this.changeVad = (vadConfig) => {
|
|
830
|
+
if (equalVADConfig(vadConfig, this.vadConfig)) return;
|
|
831
|
+
const stream = this.stream;
|
|
832
|
+
if (stream) {
|
|
833
|
+
this.stop();
|
|
834
|
+
}
|
|
835
|
+
this.vadConfig = vadConfig;
|
|
836
|
+
this.vad = getVAD(vadConfig);
|
|
837
|
+
if (stream) {
|
|
838
|
+
this.start(stream);
|
|
839
|
+
}
|
|
840
|
+
};
|
|
765
841
|
this.onStartSpeaking = async () => {
|
|
766
|
-
if (this.state.isMuted) return;
|
|
767
842
|
this.speakingConfirmed = false;
|
|
768
843
|
this.queuedChunks.length = 0;
|
|
769
844
|
this.workletNode?.port.postMessage({ type: "start" });
|
|
770
845
|
};
|
|
771
846
|
this.onConfirmSpeaking = async () => {
|
|
772
|
-
if (this.state.isMuted) return;
|
|
773
847
|
this.speakingConfirmed = true;
|
|
774
848
|
this.changeState({ isSpeaking: true });
|
|
775
849
|
this.emit("StartSpeaking");
|
|
776
850
|
};
|
|
777
851
|
this.onCancelSpeaking = async () => {
|
|
778
|
-
if (this.state.isMuted) return;
|
|
779
852
|
this.queuedChunks.length = 0;
|
|
780
853
|
this.workletNode?.port.postMessage({ type: "stop" });
|
|
781
854
|
};
|
|
782
855
|
this.onStopSpeaking = async () => {
|
|
783
|
-
if (this.state.isMuted) return;
|
|
784
856
|
this.speakingConfirmed = false;
|
|
785
857
|
this.changeState({ isSpeaking: false });
|
|
786
858
|
this.emit("StopSpeaking");
|
|
787
859
|
this.workletNode?.port.postMessage({ type: "stop" });
|
|
788
860
|
};
|
|
789
861
|
this.onWorkletMessage = (event) => {
|
|
790
|
-
if (this.state.isMuted) return;
|
|
791
862
|
const { type, data } = event.data;
|
|
792
863
|
if (type === "chunk") {
|
|
793
864
|
const pcmData = data;
|
|
@@ -803,10 +874,6 @@ var MicRecorder = class extends EventEmitter5 {
|
|
|
803
874
|
}
|
|
804
875
|
};
|
|
805
876
|
this.state = defaultMicRecorderState;
|
|
806
|
-
const workletBlob = new Blob([pcm_processor_worklet_default], {
|
|
807
|
-
type: "application/javascript"
|
|
808
|
-
});
|
|
809
|
-
this.workletUrl = URL.createObjectURL(workletBlob);
|
|
810
877
|
this.vad = getVAD(vadConfig);
|
|
811
878
|
}
|
|
812
879
|
changeState(state) {
|
|
@@ -910,7 +977,9 @@ var MicdropClient = class extends EventEmitter6 {
|
|
|
910
977
|
this._isProcessing = true;
|
|
911
978
|
this._isMuted = false;
|
|
912
979
|
this._isPaused = false;
|
|
913
|
-
|
|
980
|
+
if (!this.micRecorder || options?.vad) {
|
|
981
|
+
await this.startMic({ vad: options?.vad });
|
|
982
|
+
}
|
|
914
983
|
await this.startWS();
|
|
915
984
|
};
|
|
916
985
|
this.stop = async () => {
|
|
@@ -931,20 +1000,20 @@ var MicdropClient = class extends EventEmitter6 {
|
|
|
931
1000
|
Speaker2.stopAudio();
|
|
932
1001
|
};
|
|
933
1002
|
this.mute = () => {
|
|
934
|
-
this.
|
|
1003
|
+
this.vad?.pause();
|
|
935
1004
|
this._isMuted = true;
|
|
936
1005
|
this.notifyStateChange();
|
|
937
1006
|
};
|
|
938
1007
|
this.unmute = () => {
|
|
939
1008
|
if (!this.isPaused && !(this.options.disableInterruption && Speaker2.isPlaying)) {
|
|
940
|
-
this.
|
|
1009
|
+
this.vad?.resume();
|
|
941
1010
|
}
|
|
942
1011
|
this._isMuted = false;
|
|
943
1012
|
this.notifyStateChange();
|
|
944
1013
|
};
|
|
945
1014
|
this.pause = () => {
|
|
946
1015
|
if (this.isPaused) return;
|
|
947
|
-
this.
|
|
1016
|
+
this.vad?.pause();
|
|
948
1017
|
this._isPaused = true;
|
|
949
1018
|
this._isProcessing = false;
|
|
950
1019
|
this.notifyStateChange();
|
|
@@ -954,7 +1023,7 @@ var MicdropClient = class extends EventEmitter6 {
|
|
|
954
1023
|
this.resume = () => {
|
|
955
1024
|
if (!this.isPaused) return;
|
|
956
1025
|
if (!this.isMuted) {
|
|
957
|
-
this.
|
|
1026
|
+
this.vad?.resume();
|
|
958
1027
|
}
|
|
959
1028
|
this._isPaused = false;
|
|
960
1029
|
this.notifyStateChange();
|
|
@@ -966,7 +1035,12 @@ var MicdropClient = class extends EventEmitter6 {
|
|
|
966
1035
|
this.options.vad = options.vad;
|
|
967
1036
|
}
|
|
968
1037
|
try {
|
|
969
|
-
if (
|
|
1038
|
+
if (this.micRecorder) {
|
|
1039
|
+
this.micRecorder.stop();
|
|
1040
|
+
if (options.vad) {
|
|
1041
|
+
this.micRecorder.changeVad(options.vad);
|
|
1042
|
+
}
|
|
1043
|
+
} else {
|
|
970
1044
|
this.micRecorder = new MicRecorder(this.options.vad);
|
|
971
1045
|
this.micRecorder.on("StateChange", () => {
|
|
972
1046
|
this.notifyStateChange();
|
|
@@ -991,15 +1065,9 @@ var MicdropClient = class extends EventEmitter6 {
|
|
|
991
1065
|
this.notifyStateChange();
|
|
992
1066
|
});
|
|
993
1067
|
}
|
|
994
|
-
const isRecorderStarted = this.micRecorder.state.isStarted;
|
|
995
|
-
if (isRecorderStarted) {
|
|
996
|
-
this.micRecorder?.stop();
|
|
997
|
-
}
|
|
998
1068
|
const micStream = await Mic2.start(options.deviceId);
|
|
999
1069
|
this.micStream = micStream;
|
|
1000
|
-
|
|
1001
|
-
await this.micRecorder.start(micStream);
|
|
1002
|
-
}
|
|
1070
|
+
await this.micRecorder.start(micStream);
|
|
1003
1071
|
await this.updateDevices();
|
|
1004
1072
|
await Speaker2.start();
|
|
1005
1073
|
this.notifyStateChange();
|
|
@@ -1120,14 +1188,14 @@ var MicdropClient = class extends EventEmitter6 {
|
|
|
1120
1188
|
this.onSpeakerStartPlaying = () => {
|
|
1121
1189
|
this.log("Speaker started");
|
|
1122
1190
|
if (this.options.disableInterruption) {
|
|
1123
|
-
this.
|
|
1191
|
+
this.vad?.pause();
|
|
1124
1192
|
}
|
|
1125
1193
|
this.notifyStateChange();
|
|
1126
1194
|
};
|
|
1127
1195
|
this.onSpeakerStopPlaying = () => {
|
|
1128
1196
|
this.log("Speaker stopped");
|
|
1129
1197
|
if (this.options.disableInterruption && !this.isMuted) {
|
|
1130
|
-
this.
|
|
1198
|
+
this.vad?.resume();
|
|
1131
1199
|
}
|
|
1132
1200
|
this.notifyStateChange();
|
|
1133
1201
|
};
|
|
@@ -1166,7 +1234,7 @@ var MicdropClient = class extends EventEmitter6 {
|
|
|
1166
1234
|
return !!this.micStream;
|
|
1167
1235
|
}
|
|
1168
1236
|
get isMicMuted() {
|
|
1169
|
-
return this.
|
|
1237
|
+
return this.vad?.isPaused ?? false;
|
|
1170
1238
|
}
|
|
1171
1239
|
get isUserSpeaking() {
|
|
1172
1240
|
return this.micRecorder?.state.isSpeaking ?? false;
|
|
@@ -1287,6 +1355,7 @@ export {
|
|
|
1287
1355
|
VAD,
|
|
1288
1356
|
VADStatus,
|
|
1289
1357
|
VolumeVAD,
|
|
1358
|
+
equalVADConfig,
|
|
1290
1359
|
getClientErrorFromWSCloseEvent,
|
|
1291
1360
|
getClientErrorFromWSCloseEventCode,
|
|
1292
1361
|
getVAD
|