@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.mjs CHANGED
@@ -447,7 +447,10 @@ var MultipleVAD = class extends VAD {
447
447
  get isStarted() {
448
448
  return this.vads.some((vad) => vad.isStarted);
449
449
  }
450
- async start(stream, threshold) {
450
+ get isPaused() {
451
+ return this.vads.every((vad) => vad.isPaused);
452
+ }
453
+ async start(stream) {
451
454
  for (const vad of this.vads) {
452
455
  if (vad.isStarted) {
453
456
  continue;
@@ -462,6 +465,16 @@ var MultipleVAD = class extends VAD {
462
465
  await vad.stop();
463
466
  }
464
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
+ }
465
478
  };
466
479
 
467
480
  // src/audio/vad/SileroVAD.ts
@@ -476,6 +489,7 @@ var SileroVAD = class extends VAD {
476
489
  constructor(options) {
477
490
  super();
478
491
  this.options = defaultOptions;
492
+ this._isPaused = false;
479
493
  if (options) {
480
494
  this.setOptions(options);
481
495
  } else {
@@ -492,6 +506,9 @@ var SileroVAD = class extends VAD {
492
506
  get isStarted() {
493
507
  return !!this.vad;
494
508
  }
509
+ get isPaused() {
510
+ return this._isPaused;
511
+ }
495
512
  async start(stream) {
496
513
  if (this.vad) return;
497
514
  this.vad = await MicVAD.new({
@@ -511,6 +528,16 @@ var SileroVAD = class extends VAD {
511
528
  this.vad.destroy();
512
529
  this.vad = void 0;
513
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
+ }
514
541
  setOptions(options) {
515
542
  this.options = { ...this.options, ...options };
516
543
  this.vad?.setOptions(this.options);
@@ -533,6 +560,7 @@ var VolumeVAD = class extends VAD {
533
560
  constructor(options) {
534
561
  super();
535
562
  this.options = defaultOptions2;
563
+ this._isPaused = false;
536
564
  this.running = false;
537
565
  this.speaking = false;
538
566
  this.attemptSpeaking = false;
@@ -564,8 +592,11 @@ var VolumeVAD = class extends VAD {
564
592
  get isStarted() {
565
593
  return this.running;
566
594
  }
595
+ get isPaused() {
596
+ return this._isPaused;
597
+ }
567
598
  async start(stream) {
568
- if (this.running) return;
599
+ if (this.running || this._isPaused) return;
569
600
  this.analyser = audioContext.createAnalyser();
570
601
  this.sourceNode = audioContext.createMediaStreamSource(stream);
571
602
  this.analyser.fftSize = 512;
@@ -613,10 +644,17 @@ var VolumeVAD = class extends VAD {
613
644
  this.speakingHistory.push(Number(currentVolume > this.options.threshold));
614
645
  setTimeout(() => this.startLoop(), this.delay);
615
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
+ }
616
654
  async stop() {
617
655
  if (!this.running) return;
618
656
  this.running = false;
619
- this.speaking = false;
657
+ this.resetHistory();
620
658
  if (this.sourceNode) {
621
659
  this.sourceNode.disconnect();
622
660
  this.sourceNode = void 0;
@@ -627,6 +665,26 @@ var VolumeVAD = class extends VAD {
627
665
  }
628
666
  this.fftBins = void 0;
629
667
  }
668
+ async pause() {
669
+ if (!this.running || this._isPaused) return;
670
+ this._isPaused = true;
671
+ this.running = false;
672
+ this.resetHistory();
673
+ switch (this.status) {
674
+ case 2 /* Speaking */:
675
+ this.emit("StopSpeaking");
676
+ break;
677
+ case 1 /* MaybeSpeaking */:
678
+ this.emit("CancelSpeaking");
679
+ break;
680
+ }
681
+ }
682
+ async resume() {
683
+ if (!this._isPaused) return;
684
+ this._isPaused = false;
685
+ this.running = true;
686
+ this.startLoop();
687
+ }
630
688
  setOptions(options) {
631
689
  if (typeof options.history === "number" && options.history < 3) {
632
690
  throw new Error("History must be at least 3");
@@ -659,23 +717,41 @@ function getVAD(vad) {
659
717
  }
660
718
  return vad;
661
719
  }
720
+ function equalVADConfig(a, b) {
721
+ if (a === b) return true;
722
+ if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
723
+ return a.every((item, index) => equalVADConfig(item, b[index]));
724
+ }
725
+ return false;
726
+ }
727
+
728
+ // raw:./pcm-processor-worklet
729
+ 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";
730
+
731
+ // src/audio/pcm-processor.ts
732
+ async function initPcmProcessor() {
733
+ if (window.micdropPcmProcessor) return;
734
+ window.micdropPcmProcessor = true;
735
+ const workletBlob = new Blob([pcm_processor_worklet_default], {
736
+ type: "application/javascript"
737
+ });
738
+ const workletUrl = URL.createObjectURL(workletBlob);
739
+ await audioContext.audioWorklet.addModule(workletUrl);
740
+ }
662
741
 
663
742
  // src/audio/MicRecorder.ts
664
743
  var SAMPLE_RATE2 = 16e3;
665
- var BUFFER_SIZE = 4096;
666
- var CHUNK_INTERVAL_MS = 100;
667
744
  var defaultMicRecorderState = {
668
745
  isStarting: false,
669
746
  isStarted: false,
670
- isMuted: false,
671
747
  isSpeaking: false
672
748
  };
673
749
  var MicRecorder = class extends EventEmitter5 {
674
750
  constructor(vadConfig) {
675
751
  super();
752
+ this.vadConfig = vadConfig;
676
753
  this.speakingConfirmed = false;
677
754
  this.queuedChunks = [];
678
- this.audioBuffer = [];
679
755
  this.start = async (stream) => {
680
756
  if (this.state.isStarted) {
681
757
  throw new Error("MicRecorder is already started");
@@ -683,24 +759,22 @@ var MicRecorder = class extends EventEmitter5 {
683
759
  try {
684
760
  this.changeState({
685
761
  isStarting: true,
686
- isMuted: false,
687
762
  isSpeaking: false
688
763
  });
689
- const delayedStream = createDelayedStream(
690
- stream,
691
- this.vad.delay / 1e3 + 0.05
692
- );
764
+ this.stream = stream;
765
+ this.speakingConfirmed = false;
766
+ await initPcmProcessor();
767
+ this.workletNode = new AudioWorkletNode(audioContext, "pcm-processor");
768
+ this.workletNode.port.onmessage = this.onWorkletMessage;
769
+ this.workletNode.port.postMessage({
770
+ type: "configure",
771
+ sourceSampleRate: audioContext.sampleRate,
772
+ targetSampleRate: SAMPLE_RATE2
773
+ });
774
+ const delayedStream = createDelayedStream(stream, this.vad.delay / 1e3);
693
775
  this.delayedStream = delayedStream;
694
- this.audioContext = new AudioContext({ sampleRate: SAMPLE_RATE2 });
695
- this.source = this.audioContext.createMediaStreamSource(delayedStream);
696
- this.processor = this.audioContext.createScriptProcessor(
697
- BUFFER_SIZE,
698
- 1,
699
- 1
700
- );
701
- this.processor.onaudioprocess = this.onAudioProcess;
702
- this.source.connect(this.processor);
703
- this.processor.connect(this.audioContext.destination);
776
+ this.source = audioContext.createMediaStreamSource(delayedStream);
777
+ this.source.connect(this.workletNode);
704
778
  await this.vad.start(stream);
705
779
  this.vad.on("StartSpeaking", this.onStartSpeaking);
706
780
  this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
@@ -715,52 +789,27 @@ var MicRecorder = class extends EventEmitter5 {
715
789
  console.error(err);
716
790
  }
717
791
  };
718
- this.mute = () => {
719
- if (this.state.isMuted) return;
720
- if (this.state.isSpeaking) {
721
- this.onStopSpeaking();
722
- }
723
- this.changeState({ isMuted: true });
724
- };
725
- this.unmute = () => {
726
- if (!this.state.isMuted) return;
727
- this.changeState({ isMuted: false });
728
- if (this.vad.status === 1 /* MaybeSpeaking */) {
729
- this.onStartSpeaking();
730
- } else if (this.vad.status === 2 /* Speaking */) {
731
- this.onStartSpeaking();
732
- this.onConfirmSpeaking();
733
- }
734
- };
735
792
  this.stop = () => {
736
793
  this.changeState({
737
794
  isStarting: false,
738
795
  isStarted: false,
739
- isMuted: false,
740
796
  isSpeaking: false
741
797
  });
798
+ this.stream = void 0;
742
799
  try {
743
800
  this.vad.stop();
744
801
  this.vad.off("StartSpeaking", this.onStartSpeaking);
745
802
  this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
746
803
  this.vad.off("CancelSpeaking", this.onCancelSpeaking);
747
804
  this.vad.off("StopSpeaking", this.onStopSpeaking);
748
- if (this.chunkTimer) {
749
- clearInterval(this.chunkTimer);
750
- this.chunkTimer = void 0;
751
- }
752
- if (this.processor) {
753
- this.processor.disconnect();
754
- this.processor = void 0;
805
+ if (this.workletNode) {
806
+ this.workletNode.disconnect();
807
+ this.workletNode = void 0;
755
808
  }
756
809
  if (this.source) {
757
810
  this.source.disconnect();
758
811
  this.source = void 0;
759
812
  }
760
- if (this.audioContext) {
761
- this.audioContext.close();
762
- this.audioContext = void 0;
763
- }
764
813
  if (this.delayedStream) {
765
814
  stopStream(this.delayedStream);
766
815
  this.delayedStream = void 0;
@@ -769,92 +818,52 @@ var MicRecorder = class extends EventEmitter5 {
769
818
  console.error(err);
770
819
  }
771
820
  };
772
- this.onStartSpeaking = async () => {
773
- if (this.state.isMuted) return;
774
- try {
775
- this.startChunking();
776
- this.queuedChunks.length = 0;
777
- this.speakingConfirmed = false;
778
- } catch (error) {
779
- console.error(error);
821
+ this.changeVad = (vadConfig) => {
822
+ if (equalVADConfig(vadConfig, this.vadConfig)) return;
823
+ const stream = this.stream;
824
+ if (stream) {
825
+ this.stop();
780
826
  }
827
+ this.vadConfig = vadConfig;
828
+ this.vad = getVAD(vadConfig);
829
+ if (stream) {
830
+ this.start(stream);
831
+ }
832
+ };
833
+ this.onStartSpeaking = async () => {
834
+ this.speakingConfirmed = false;
835
+ this.queuedChunks.length = 0;
836
+ this.workletNode?.port.postMessage({ type: "start" });
781
837
  };
782
838
  this.onConfirmSpeaking = async () => {
783
- if (this.state.isMuted) return;
784
- this.emit("StartSpeaking");
785
- this.changeState({ isSpeaking: true });
786
839
  this.speakingConfirmed = true;
840
+ this.changeState({ isSpeaking: true });
841
+ this.emit("StartSpeaking");
787
842
  };
788
843
  this.onCancelSpeaking = async () => {
789
- if (this.state.isMuted) return;
790
- try {
791
- this.stopChunking();
792
- } catch (error) {
793
- console.error(error);
794
- }
795
844
  this.queuedChunks.length = 0;
845
+ this.workletNode?.port.postMessage({ type: "stop" });
796
846
  };
797
847
  this.onStopSpeaking = async () => {
798
- if (this.state.isMuted) return;
799
- try {
800
- this.stopChunking();
801
- } catch (error) {
802
- console.error(error);
803
- }
848
+ this.speakingConfirmed = false;
804
849
  this.changeState({ isSpeaking: false });
805
850
  this.emit("StopSpeaking");
806
- this.speakingConfirmed = false;
807
- };
808
- this.onAudioProcess = (audioProcessingEvent) => {
809
- if (this.state.isMuted) return;
810
- const inputBuffer = audioProcessingEvent.inputBuffer;
811
- const inputData = inputBuffer.getChannelData(0);
812
- this.audioBuffer.push(new Float32Array(inputData));
851
+ this.workletNode?.port.postMessage({ type: "stop" });
813
852
  };
814
- this.startChunking = () => {
815
- if (this.chunkTimer) return;
816
- this.chunkTimer = setInterval(() => {
817
- this.processAudioChunk();
818
- }, CHUNK_INTERVAL_MS);
819
- };
820
- this.stopChunking = () => {
821
- if (this.chunkTimer) {
822
- clearInterval(this.chunkTimer);
823
- this.chunkTimer = void 0;
824
- }
825
- };
826
- this.processAudioChunk = () => {
827
- if (this.audioBuffer.length === 0) return;
828
- const totalSamples = this.audioBuffer.reduce(
829
- (sum, buffer) => sum + buffer.length,
830
- 0
831
- );
832
- if (totalSamples === 0) return;
833
- const mergedBuffer = new Float32Array(totalSamples);
834
- let offset = 0;
835
- for (const buffer of this.audioBuffer) {
836
- mergedBuffer.set(buffer, offset);
837
- offset += buffer.length;
838
- }
839
- const pcmData = this.floatToPCM16(mergedBuffer);
840
- this.audioBuffer.length = 0;
841
- if (!this.speakingConfirmed) {
842
- this.queuedChunks.push(pcmData);
843
- return;
844
- }
845
- for (const chunk of this.queuedChunks) {
846
- this.emit("Chunk", chunk);
847
- }
848
- this.queuedChunks.length = 0;
849
- this.emit("Chunk", pcmData);
850
- };
851
- this.floatToPCM16 = (floatData) => {
852
- const pcmData = new Int16Array(floatData.length);
853
- for (let i = 0; i < floatData.length; i++) {
854
- const sample = Math.max(-1, Math.min(1, floatData[i]));
855
- pcmData[i] = sample < 0 ? sample * 32768 : sample * 32767;
853
+ this.onWorkletMessage = (event) => {
854
+ const { type, data } = event.data;
855
+ if (type === "chunk") {
856
+ const pcmData = data;
857
+ if (!this.speakingConfirmed) {
858
+ this.queuedChunks.push(pcmData);
859
+ return;
860
+ }
861
+ for (const chunk of this.queuedChunks) {
862
+ this.emit("Chunk", chunk);
863
+ }
864
+ this.queuedChunks.length = 0;
865
+ this.emit("Chunk", pcmData);
856
866
  }
857
- return pcmData;
858
867
  };
859
868
  this.state = defaultMicRecorderState;
860
869
  this.vad = getVAD(vadConfig);
@@ -960,7 +969,9 @@ var MicdropClient = class extends EventEmitter6 {
960
969
  this._isProcessing = true;
961
970
  this._isMuted = false;
962
971
  this._isPaused = false;
963
- await this.startMic({ record: true });
972
+ if (!this.micRecorder || options?.vad) {
973
+ await this.startMic({ vad: options?.vad });
974
+ }
964
975
  await this.startWS();
965
976
  };
966
977
  this.stop = async () => {
@@ -981,20 +992,20 @@ var MicdropClient = class extends EventEmitter6 {
981
992
  Speaker2.stopAudio();
982
993
  };
983
994
  this.mute = () => {
984
- this.micRecorder?.mute();
995
+ this.vad?.pause();
985
996
  this._isMuted = true;
986
997
  this.notifyStateChange();
987
998
  };
988
999
  this.unmute = () => {
989
1000
  if (!this.isPaused && !(this.options.disableInterruption && Speaker2.isPlaying)) {
990
- this.micRecorder?.unmute();
1001
+ this.vad?.resume();
991
1002
  }
992
1003
  this._isMuted = false;
993
1004
  this.notifyStateChange();
994
1005
  };
995
1006
  this.pause = () => {
996
1007
  if (this.isPaused) return;
997
- this.micRecorder?.mute();
1008
+ this.vad?.pause();
998
1009
  this._isPaused = true;
999
1010
  this._isProcessing = false;
1000
1011
  this.notifyStateChange();
@@ -1004,7 +1015,7 @@ var MicdropClient = class extends EventEmitter6 {
1004
1015
  this.resume = () => {
1005
1016
  if (!this.isPaused) return;
1006
1017
  if (!this.isMuted) {
1007
- this.micRecorder?.unmute();
1018
+ this.vad?.resume();
1008
1019
  }
1009
1020
  this._isPaused = false;
1010
1021
  this.notifyStateChange();
@@ -1016,7 +1027,12 @@ var MicdropClient = class extends EventEmitter6 {
1016
1027
  this.options.vad = options.vad;
1017
1028
  }
1018
1029
  try {
1019
- if (!this.micRecorder) {
1030
+ if (this.micRecorder) {
1031
+ this.micRecorder.stop();
1032
+ if (options.vad) {
1033
+ this.micRecorder.changeVad(options.vad);
1034
+ }
1035
+ } else {
1020
1036
  this.micRecorder = new MicRecorder(this.options.vad);
1021
1037
  this.micRecorder.on("StateChange", () => {
1022
1038
  this.notifyStateChange();
@@ -1041,15 +1057,9 @@ var MicdropClient = class extends EventEmitter6 {
1041
1057
  this.notifyStateChange();
1042
1058
  });
1043
1059
  }
1044
- const isRecorderStarted = this.micRecorder.state.isStarted;
1045
- if (isRecorderStarted) {
1046
- this.micRecorder?.stop();
1047
- }
1048
1060
  const micStream = await Mic2.start(options.deviceId);
1049
1061
  this.micStream = micStream;
1050
- if (isRecorderStarted || options.record) {
1051
- await this.micRecorder.start(micStream);
1052
- }
1062
+ await this.micRecorder.start(micStream);
1053
1063
  await this.updateDevices();
1054
1064
  await Speaker2.start();
1055
1065
  this.notifyStateChange();
@@ -1170,14 +1180,14 @@ var MicdropClient = class extends EventEmitter6 {
1170
1180
  this.onSpeakerStartPlaying = () => {
1171
1181
  this.log("Speaker started");
1172
1182
  if (this.options.disableInterruption) {
1173
- this.micRecorder?.mute();
1183
+ this.vad?.pause();
1174
1184
  }
1175
1185
  this.notifyStateChange();
1176
1186
  };
1177
1187
  this.onSpeakerStopPlaying = () => {
1178
1188
  this.log("Speaker stopped");
1179
1189
  if (this.options.disableInterruption && !this.isMuted) {
1180
- this.micRecorder?.unmute();
1190
+ this.vad?.resume();
1181
1191
  }
1182
1192
  this.notifyStateChange();
1183
1193
  };
@@ -1216,7 +1226,7 @@ var MicdropClient = class extends EventEmitter6 {
1216
1226
  return !!this.micStream;
1217
1227
  }
1218
1228
  get isMicMuted() {
1219
- return this.micRecorder?.state.isMuted ?? false;
1229
+ return this.vad?.isPaused ?? false;
1220
1230
  }
1221
1231
  get isUserSpeaking() {
1222
1232
  return this.micRecorder?.state.isSpeaking ?? false;
@@ -1337,6 +1347,7 @@ export {
1337
1347
  VAD,
1338
1348
  VADStatus,
1339
1349
  VolumeVAD,
1350
+ equalVADConfig,
1340
1351
  getClientErrorFromWSCloseEvent,
1341
1352
  getClientErrorFromWSCloseEventCode,
1342
1353
  getVAD