@micdrop/client 2.0.14 → 2.1.1
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 +34 -16
- package/dist/index.d.ts +34 -16
- package/dist/index.js +152 -140
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +151 -140
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -35,6 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
VAD: () => VAD,
|
|
36
36
|
VADStatus: () => VADStatus,
|
|
37
37
|
VolumeVAD: () => VolumeVAD,
|
|
38
|
+
equalVADConfig: () => equalVADConfig,
|
|
38
39
|
getClientErrorFromWSCloseEvent: () => getClientErrorFromWSCloseEvent,
|
|
39
40
|
getClientErrorFromWSCloseEventCode: () => getClientErrorFromWSCloseEventCode,
|
|
40
41
|
getVAD: () => getVAD
|
|
@@ -490,7 +491,10 @@ var MultipleVAD = class extends VAD {
|
|
|
490
491
|
get isStarted() {
|
|
491
492
|
return this.vads.some((vad) => vad.isStarted);
|
|
492
493
|
}
|
|
493
|
-
|
|
494
|
+
get isPaused() {
|
|
495
|
+
return this.vads.every((vad) => vad.isPaused);
|
|
496
|
+
}
|
|
497
|
+
async start(stream) {
|
|
494
498
|
for (const vad of this.vads) {
|
|
495
499
|
if (vad.isStarted) {
|
|
496
500
|
continue;
|
|
@@ -505,6 +509,16 @@ var MultipleVAD = class extends VAD {
|
|
|
505
509
|
await vad.stop();
|
|
506
510
|
}
|
|
507
511
|
}
|
|
512
|
+
async pause() {
|
|
513
|
+
for (const vad of this.vads) {
|
|
514
|
+
await vad.pause();
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
async resume() {
|
|
518
|
+
for (const vad of this.vads) {
|
|
519
|
+
await vad.resume();
|
|
520
|
+
}
|
|
521
|
+
}
|
|
508
522
|
};
|
|
509
523
|
|
|
510
524
|
// src/audio/vad/SileroVAD.ts
|
|
@@ -519,6 +533,7 @@ var SileroVAD = class extends VAD {
|
|
|
519
533
|
constructor(options) {
|
|
520
534
|
super();
|
|
521
535
|
this.options = defaultOptions;
|
|
536
|
+
this._isPaused = false;
|
|
522
537
|
if (options) {
|
|
523
538
|
this.setOptions(options);
|
|
524
539
|
} else {
|
|
@@ -535,6 +550,9 @@ var SileroVAD = class extends VAD {
|
|
|
535
550
|
get isStarted() {
|
|
536
551
|
return !!this.vad;
|
|
537
552
|
}
|
|
553
|
+
get isPaused() {
|
|
554
|
+
return this._isPaused;
|
|
555
|
+
}
|
|
538
556
|
async start(stream) {
|
|
539
557
|
if (this.vad) return;
|
|
540
558
|
this.vad = await import_vad_web.MicVAD.new({
|
|
@@ -554,6 +572,16 @@ var SileroVAD = class extends VAD {
|
|
|
554
572
|
this.vad.destroy();
|
|
555
573
|
this.vad = void 0;
|
|
556
574
|
}
|
|
575
|
+
async pause() {
|
|
576
|
+
if (!this.vad || this._isPaused) return;
|
|
577
|
+
this._isPaused = true;
|
|
578
|
+
this.vad.pause();
|
|
579
|
+
}
|
|
580
|
+
async resume() {
|
|
581
|
+
if (!this.vad || !this._isPaused) return;
|
|
582
|
+
this._isPaused = false;
|
|
583
|
+
this.vad.start();
|
|
584
|
+
}
|
|
557
585
|
setOptions(options) {
|
|
558
586
|
this.options = { ...this.options, ...options };
|
|
559
587
|
this.vad?.setOptions(this.options);
|
|
@@ -576,6 +604,7 @@ var VolumeVAD = class extends VAD {
|
|
|
576
604
|
constructor(options) {
|
|
577
605
|
super();
|
|
578
606
|
this.options = defaultOptions2;
|
|
607
|
+
this._isPaused = false;
|
|
579
608
|
this.running = false;
|
|
580
609
|
this.speaking = false;
|
|
581
610
|
this.attemptSpeaking = false;
|
|
@@ -607,8 +636,11 @@ var VolumeVAD = class extends VAD {
|
|
|
607
636
|
get isStarted() {
|
|
608
637
|
return this.running;
|
|
609
638
|
}
|
|
639
|
+
get isPaused() {
|
|
640
|
+
return this._isPaused;
|
|
641
|
+
}
|
|
610
642
|
async start(stream) {
|
|
611
|
-
if (this.running) return;
|
|
643
|
+
if (this.running || this._isPaused) return;
|
|
612
644
|
this.analyser = audioContext.createAnalyser();
|
|
613
645
|
this.sourceNode = audioContext.createMediaStreamSource(stream);
|
|
614
646
|
this.analyser.fftSize = 512;
|
|
@@ -656,10 +688,17 @@ var VolumeVAD = class extends VAD {
|
|
|
656
688
|
this.speakingHistory.push(Number(currentVolume > this.options.threshold));
|
|
657
689
|
setTimeout(() => this.startLoop(), this.delay);
|
|
658
690
|
}
|
|
691
|
+
resetHistory() {
|
|
692
|
+
this.speaking = false;
|
|
693
|
+
this.attemptSpeaking = false;
|
|
694
|
+
for (let i = 0; i < this.speakingHistory.length; i++) {
|
|
695
|
+
this.speakingHistory[i] = 0;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
659
698
|
async stop() {
|
|
660
699
|
if (!this.running) return;
|
|
661
700
|
this.running = false;
|
|
662
|
-
this.
|
|
701
|
+
this.resetHistory();
|
|
663
702
|
if (this.sourceNode) {
|
|
664
703
|
this.sourceNode.disconnect();
|
|
665
704
|
this.sourceNode = void 0;
|
|
@@ -670,6 +709,26 @@ var VolumeVAD = class extends VAD {
|
|
|
670
709
|
}
|
|
671
710
|
this.fftBins = void 0;
|
|
672
711
|
}
|
|
712
|
+
async pause() {
|
|
713
|
+
if (!this.running || this._isPaused) return;
|
|
714
|
+
this._isPaused = true;
|
|
715
|
+
this.running = false;
|
|
716
|
+
this.resetHistory();
|
|
717
|
+
switch (this.status) {
|
|
718
|
+
case 2 /* Speaking */:
|
|
719
|
+
this.emit("StopSpeaking");
|
|
720
|
+
break;
|
|
721
|
+
case 1 /* MaybeSpeaking */:
|
|
722
|
+
this.emit("CancelSpeaking");
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
async resume() {
|
|
727
|
+
if (!this._isPaused) return;
|
|
728
|
+
this._isPaused = false;
|
|
729
|
+
this.running = true;
|
|
730
|
+
this.startLoop();
|
|
731
|
+
}
|
|
673
732
|
setOptions(options) {
|
|
674
733
|
if (typeof options.history === "number" && options.history < 3) {
|
|
675
734
|
throw new Error("History must be at least 3");
|
|
@@ -702,23 +761,41 @@ function getVAD(vad) {
|
|
|
702
761
|
}
|
|
703
762
|
return vad;
|
|
704
763
|
}
|
|
764
|
+
function equalVADConfig(a, b) {
|
|
765
|
+
if (a === b) return true;
|
|
766
|
+
if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
|
|
767
|
+
return a.every((item, index) => equalVADConfig(item, b[index]));
|
|
768
|
+
}
|
|
769
|
+
return false;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// raw:./pcm-processor-worklet
|
|
773
|
+
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";
|
|
774
|
+
|
|
775
|
+
// src/audio/pcm-processor.ts
|
|
776
|
+
async function initPcmProcessor() {
|
|
777
|
+
if (window.micdropPcmProcessor) return;
|
|
778
|
+
window.micdropPcmProcessor = true;
|
|
779
|
+
const workletBlob = new Blob([pcm_processor_worklet_default], {
|
|
780
|
+
type: "application/javascript"
|
|
781
|
+
});
|
|
782
|
+
const workletUrl = URL.createObjectURL(workletBlob);
|
|
783
|
+
await audioContext.audioWorklet.addModule(workletUrl);
|
|
784
|
+
}
|
|
705
785
|
|
|
706
786
|
// src/audio/MicRecorder.ts
|
|
707
787
|
var SAMPLE_RATE2 = 16e3;
|
|
708
|
-
var BUFFER_SIZE = 4096;
|
|
709
|
-
var CHUNK_INTERVAL_MS = 100;
|
|
710
788
|
var defaultMicRecorderState = {
|
|
711
789
|
isStarting: false,
|
|
712
790
|
isStarted: false,
|
|
713
|
-
isMuted: false,
|
|
714
791
|
isSpeaking: false
|
|
715
792
|
};
|
|
716
793
|
var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
717
794
|
constructor(vadConfig) {
|
|
718
795
|
super();
|
|
796
|
+
this.vadConfig = vadConfig;
|
|
719
797
|
this.speakingConfirmed = false;
|
|
720
798
|
this.queuedChunks = [];
|
|
721
|
-
this.audioBuffer = [];
|
|
722
799
|
this.start = async (stream) => {
|
|
723
800
|
if (this.state.isStarted) {
|
|
724
801
|
throw new Error("MicRecorder is already started");
|
|
@@ -726,24 +803,22 @@ var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
|
726
803
|
try {
|
|
727
804
|
this.changeState({
|
|
728
805
|
isStarting: true,
|
|
729
|
-
isMuted: false,
|
|
730
806
|
isSpeaking: false
|
|
731
807
|
});
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
);
|
|
808
|
+
this.stream = stream;
|
|
809
|
+
this.speakingConfirmed = false;
|
|
810
|
+
await initPcmProcessor();
|
|
811
|
+
this.workletNode = new AudioWorkletNode(audioContext, "pcm-processor");
|
|
812
|
+
this.workletNode.port.onmessage = this.onWorkletMessage;
|
|
813
|
+
this.workletNode.port.postMessage({
|
|
814
|
+
type: "configure",
|
|
815
|
+
sourceSampleRate: audioContext.sampleRate,
|
|
816
|
+
targetSampleRate: SAMPLE_RATE2
|
|
817
|
+
});
|
|
818
|
+
const delayedStream = createDelayedStream(stream, this.vad.delay / 1e3);
|
|
736
819
|
this.delayedStream = delayedStream;
|
|
737
|
-
this.
|
|
738
|
-
this.source
|
|
739
|
-
this.processor = this.audioContext.createScriptProcessor(
|
|
740
|
-
BUFFER_SIZE,
|
|
741
|
-
1,
|
|
742
|
-
1
|
|
743
|
-
);
|
|
744
|
-
this.processor.onaudioprocess = this.onAudioProcess;
|
|
745
|
-
this.source.connect(this.processor);
|
|
746
|
-
this.processor.connect(this.audioContext.destination);
|
|
820
|
+
this.source = audioContext.createMediaStreamSource(delayedStream);
|
|
821
|
+
this.source.connect(this.workletNode);
|
|
747
822
|
await this.vad.start(stream);
|
|
748
823
|
this.vad.on("StartSpeaking", this.onStartSpeaking);
|
|
749
824
|
this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
@@ -758,52 +833,27 @@ var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
|
758
833
|
console.error(err);
|
|
759
834
|
}
|
|
760
835
|
};
|
|
761
|
-
this.mute = () => {
|
|
762
|
-
if (this.state.isMuted) return;
|
|
763
|
-
if (this.state.isSpeaking) {
|
|
764
|
-
this.onStopSpeaking();
|
|
765
|
-
}
|
|
766
|
-
this.changeState({ isMuted: true });
|
|
767
|
-
};
|
|
768
|
-
this.unmute = () => {
|
|
769
|
-
if (!this.state.isMuted) return;
|
|
770
|
-
this.changeState({ isMuted: false });
|
|
771
|
-
if (this.vad.status === 1 /* MaybeSpeaking */) {
|
|
772
|
-
this.onStartSpeaking();
|
|
773
|
-
} else if (this.vad.status === 2 /* Speaking */) {
|
|
774
|
-
this.onStartSpeaking();
|
|
775
|
-
this.onConfirmSpeaking();
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
836
|
this.stop = () => {
|
|
779
837
|
this.changeState({
|
|
780
838
|
isStarting: false,
|
|
781
839
|
isStarted: false,
|
|
782
|
-
isMuted: false,
|
|
783
840
|
isSpeaking: false
|
|
784
841
|
});
|
|
842
|
+
this.stream = void 0;
|
|
785
843
|
try {
|
|
786
844
|
this.vad.stop();
|
|
787
845
|
this.vad.off("StartSpeaking", this.onStartSpeaking);
|
|
788
846
|
this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
789
847
|
this.vad.off("CancelSpeaking", this.onCancelSpeaking);
|
|
790
848
|
this.vad.off("StopSpeaking", this.onStopSpeaking);
|
|
791
|
-
if (this.
|
|
792
|
-
|
|
793
|
-
this.
|
|
794
|
-
}
|
|
795
|
-
if (this.processor) {
|
|
796
|
-
this.processor.disconnect();
|
|
797
|
-
this.processor = void 0;
|
|
849
|
+
if (this.workletNode) {
|
|
850
|
+
this.workletNode.disconnect();
|
|
851
|
+
this.workletNode = void 0;
|
|
798
852
|
}
|
|
799
853
|
if (this.source) {
|
|
800
854
|
this.source.disconnect();
|
|
801
855
|
this.source = void 0;
|
|
802
856
|
}
|
|
803
|
-
if (this.audioContext) {
|
|
804
|
-
this.audioContext.close();
|
|
805
|
-
this.audioContext = void 0;
|
|
806
|
-
}
|
|
807
857
|
if (this.delayedStream) {
|
|
808
858
|
stopStream(this.delayedStream);
|
|
809
859
|
this.delayedStream = void 0;
|
|
@@ -812,92 +862,52 @@ var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
|
812
862
|
console.error(err);
|
|
813
863
|
}
|
|
814
864
|
};
|
|
815
|
-
this.
|
|
816
|
-
if (this.
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
this.
|
|
820
|
-
this.speakingConfirmed = false;
|
|
821
|
-
} catch (error) {
|
|
822
|
-
console.error(error);
|
|
865
|
+
this.changeVad = (vadConfig) => {
|
|
866
|
+
if (equalVADConfig(vadConfig, this.vadConfig)) return;
|
|
867
|
+
const stream = this.stream;
|
|
868
|
+
if (stream) {
|
|
869
|
+
this.stop();
|
|
823
870
|
}
|
|
871
|
+
this.vadConfig = vadConfig;
|
|
872
|
+
this.vad = getVAD(vadConfig);
|
|
873
|
+
if (stream) {
|
|
874
|
+
this.start(stream);
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
this.onStartSpeaking = async () => {
|
|
878
|
+
this.speakingConfirmed = false;
|
|
879
|
+
this.queuedChunks.length = 0;
|
|
880
|
+
this.workletNode?.port.postMessage({ type: "start" });
|
|
824
881
|
};
|
|
825
882
|
this.onConfirmSpeaking = async () => {
|
|
826
|
-
if (this.state.isMuted) return;
|
|
827
|
-
this.emit("StartSpeaking");
|
|
828
|
-
this.changeState({ isSpeaking: true });
|
|
829
883
|
this.speakingConfirmed = true;
|
|
884
|
+
this.changeState({ isSpeaking: true });
|
|
885
|
+
this.emit("StartSpeaking");
|
|
830
886
|
};
|
|
831
887
|
this.onCancelSpeaking = async () => {
|
|
832
|
-
if (this.state.isMuted) return;
|
|
833
|
-
try {
|
|
834
|
-
this.stopChunking();
|
|
835
|
-
} catch (error) {
|
|
836
|
-
console.error(error);
|
|
837
|
-
}
|
|
838
888
|
this.queuedChunks.length = 0;
|
|
889
|
+
this.workletNode?.port.postMessage({ type: "stop" });
|
|
839
890
|
};
|
|
840
891
|
this.onStopSpeaking = async () => {
|
|
841
|
-
|
|
842
|
-
try {
|
|
843
|
-
this.stopChunking();
|
|
844
|
-
} catch (error) {
|
|
845
|
-
console.error(error);
|
|
846
|
-
}
|
|
892
|
+
this.speakingConfirmed = false;
|
|
847
893
|
this.changeState({ isSpeaking: false });
|
|
848
894
|
this.emit("StopSpeaking");
|
|
849
|
-
this.
|
|
850
|
-
};
|
|
851
|
-
this.onAudioProcess = (audioProcessingEvent) => {
|
|
852
|
-
if (this.state.isMuted) return;
|
|
853
|
-
const inputBuffer = audioProcessingEvent.inputBuffer;
|
|
854
|
-
const inputData = inputBuffer.getChannelData(0);
|
|
855
|
-
this.audioBuffer.push(new Float32Array(inputData));
|
|
895
|
+
this.workletNode?.port.postMessage({ type: "stop" });
|
|
856
896
|
};
|
|
857
|
-
this.
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
if (this.audioBuffer.length === 0) return;
|
|
871
|
-
const totalSamples = this.audioBuffer.reduce(
|
|
872
|
-
(sum, buffer) => sum + buffer.length,
|
|
873
|
-
0
|
|
874
|
-
);
|
|
875
|
-
if (totalSamples === 0) return;
|
|
876
|
-
const mergedBuffer = new Float32Array(totalSamples);
|
|
877
|
-
let offset = 0;
|
|
878
|
-
for (const buffer of this.audioBuffer) {
|
|
879
|
-
mergedBuffer.set(buffer, offset);
|
|
880
|
-
offset += buffer.length;
|
|
881
|
-
}
|
|
882
|
-
const pcmData = this.floatToPCM16(mergedBuffer);
|
|
883
|
-
this.audioBuffer.length = 0;
|
|
884
|
-
if (!this.speakingConfirmed) {
|
|
885
|
-
this.queuedChunks.push(pcmData);
|
|
886
|
-
return;
|
|
887
|
-
}
|
|
888
|
-
for (const chunk of this.queuedChunks) {
|
|
889
|
-
this.emit("Chunk", chunk);
|
|
890
|
-
}
|
|
891
|
-
this.queuedChunks.length = 0;
|
|
892
|
-
this.emit("Chunk", pcmData);
|
|
893
|
-
};
|
|
894
|
-
this.floatToPCM16 = (floatData) => {
|
|
895
|
-
const pcmData = new Int16Array(floatData.length);
|
|
896
|
-
for (let i = 0; i < floatData.length; i++) {
|
|
897
|
-
const sample = Math.max(-1, Math.min(1, floatData[i]));
|
|
898
|
-
pcmData[i] = sample < 0 ? sample * 32768 : sample * 32767;
|
|
897
|
+
this.onWorkletMessage = (event) => {
|
|
898
|
+
const { type, data } = event.data;
|
|
899
|
+
if (type === "chunk") {
|
|
900
|
+
const pcmData = data;
|
|
901
|
+
if (!this.speakingConfirmed) {
|
|
902
|
+
this.queuedChunks.push(pcmData);
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
for (const chunk of this.queuedChunks) {
|
|
906
|
+
this.emit("Chunk", chunk);
|
|
907
|
+
}
|
|
908
|
+
this.queuedChunks.length = 0;
|
|
909
|
+
this.emit("Chunk", pcmData);
|
|
899
910
|
}
|
|
900
|
-
return pcmData;
|
|
901
911
|
};
|
|
902
912
|
this.state = defaultMicRecorderState;
|
|
903
913
|
this.vad = getVAD(vadConfig);
|
|
@@ -1003,7 +1013,9 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1003
1013
|
this._isProcessing = true;
|
|
1004
1014
|
this._isMuted = false;
|
|
1005
1015
|
this._isPaused = false;
|
|
1006
|
-
|
|
1016
|
+
if (!this.micRecorder || options?.vad) {
|
|
1017
|
+
await this.startMic({ vad: options?.vad });
|
|
1018
|
+
}
|
|
1007
1019
|
await this.startWS();
|
|
1008
1020
|
};
|
|
1009
1021
|
this.stop = async () => {
|
|
@@ -1024,20 +1036,20 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1024
1036
|
Speaker2.stopAudio();
|
|
1025
1037
|
};
|
|
1026
1038
|
this.mute = () => {
|
|
1027
|
-
this.
|
|
1039
|
+
this.vad?.pause();
|
|
1028
1040
|
this._isMuted = true;
|
|
1029
1041
|
this.notifyStateChange();
|
|
1030
1042
|
};
|
|
1031
1043
|
this.unmute = () => {
|
|
1032
1044
|
if (!this.isPaused && !(this.options.disableInterruption && Speaker2.isPlaying)) {
|
|
1033
|
-
this.
|
|
1045
|
+
this.vad?.resume();
|
|
1034
1046
|
}
|
|
1035
1047
|
this._isMuted = false;
|
|
1036
1048
|
this.notifyStateChange();
|
|
1037
1049
|
};
|
|
1038
1050
|
this.pause = () => {
|
|
1039
1051
|
if (this.isPaused) return;
|
|
1040
|
-
this.
|
|
1052
|
+
this.vad?.pause();
|
|
1041
1053
|
this._isPaused = true;
|
|
1042
1054
|
this._isProcessing = false;
|
|
1043
1055
|
this.notifyStateChange();
|
|
@@ -1047,7 +1059,7 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1047
1059
|
this.resume = () => {
|
|
1048
1060
|
if (!this.isPaused) return;
|
|
1049
1061
|
if (!this.isMuted) {
|
|
1050
|
-
this.
|
|
1062
|
+
this.vad?.resume();
|
|
1051
1063
|
}
|
|
1052
1064
|
this._isPaused = false;
|
|
1053
1065
|
this.notifyStateChange();
|
|
@@ -1059,7 +1071,12 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1059
1071
|
this.options.vad = options.vad;
|
|
1060
1072
|
}
|
|
1061
1073
|
try {
|
|
1062
|
-
if (
|
|
1074
|
+
if (this.micRecorder) {
|
|
1075
|
+
this.micRecorder.stop();
|
|
1076
|
+
if (options.vad) {
|
|
1077
|
+
this.micRecorder.changeVad(options.vad);
|
|
1078
|
+
}
|
|
1079
|
+
} else {
|
|
1063
1080
|
this.micRecorder = new MicRecorder(this.options.vad);
|
|
1064
1081
|
this.micRecorder.on("StateChange", () => {
|
|
1065
1082
|
this.notifyStateChange();
|
|
@@ -1084,15 +1101,9 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1084
1101
|
this.notifyStateChange();
|
|
1085
1102
|
});
|
|
1086
1103
|
}
|
|
1087
|
-
const isRecorderStarted = this.micRecorder.state.isStarted;
|
|
1088
|
-
if (isRecorderStarted) {
|
|
1089
|
-
this.micRecorder?.stop();
|
|
1090
|
-
}
|
|
1091
1104
|
const micStream = await Mic2.start(options.deviceId);
|
|
1092
1105
|
this.micStream = micStream;
|
|
1093
|
-
|
|
1094
|
-
await this.micRecorder.start(micStream);
|
|
1095
|
-
}
|
|
1106
|
+
await this.micRecorder.start(micStream);
|
|
1096
1107
|
await this.updateDevices();
|
|
1097
1108
|
await Speaker2.start();
|
|
1098
1109
|
this.notifyStateChange();
|
|
@@ -1213,14 +1224,14 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1213
1224
|
this.onSpeakerStartPlaying = () => {
|
|
1214
1225
|
this.log("Speaker started");
|
|
1215
1226
|
if (this.options.disableInterruption) {
|
|
1216
|
-
this.
|
|
1227
|
+
this.vad?.pause();
|
|
1217
1228
|
}
|
|
1218
1229
|
this.notifyStateChange();
|
|
1219
1230
|
};
|
|
1220
1231
|
this.onSpeakerStopPlaying = () => {
|
|
1221
1232
|
this.log("Speaker stopped");
|
|
1222
1233
|
if (this.options.disableInterruption && !this.isMuted) {
|
|
1223
|
-
this.
|
|
1234
|
+
this.vad?.resume();
|
|
1224
1235
|
}
|
|
1225
1236
|
this.notifyStateChange();
|
|
1226
1237
|
};
|
|
@@ -1259,7 +1270,7 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1259
1270
|
return !!this.micStream;
|
|
1260
1271
|
}
|
|
1261
1272
|
get isMicMuted() {
|
|
1262
|
-
return this.
|
|
1273
|
+
return this.vad?.isPaused ?? false;
|
|
1263
1274
|
}
|
|
1264
1275
|
get isUserSpeaking() {
|
|
1265
1276
|
return this.micRecorder?.state.isSpeaking ?? false;
|
|
@@ -1381,6 +1392,7 @@ var Micdrop = window.micdropClient;
|
|
|
1381
1392
|
VAD,
|
|
1382
1393
|
VADStatus,
|
|
1383
1394
|
VolumeVAD,
|
|
1395
|
+
equalVADConfig,
|
|
1384
1396
|
getClientErrorFromWSCloseEvent,
|
|
1385
1397
|
getClientErrorFromWSCloseEventCode,
|
|
1386
1398
|
getVAD
|