@micdrop/client 2.1.0 → 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
@@ -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
- async start(stream, threshold) {
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,17 @@ 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.speaking = false;
657
+ this.resetHistory();
623
658
  if (this.sourceNode) {
624
659
  this.sourceNode.disconnect();
625
660
  this.sourceNode = void 0;
@@ -630,6 +665,26 @@ var VolumeVAD = class extends VAD {
630
665
  }
631
666
  this.fftBins = void 0;
632
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
+ }
633
688
  setOptions(options) {
634
689
  if (typeof options.history === "number" && options.history < 3) {
635
690
  throw new Error("History must be at least 3");
@@ -662,18 +717,39 @@ function getVAD(vad) {
662
717
  }
663
718
  return vad;
664
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
+ }
665
741
 
666
742
  // src/audio/MicRecorder.ts
667
743
  var SAMPLE_RATE2 = 16e3;
668
744
  var defaultMicRecorderState = {
669
745
  isStarting: false,
670
746
  isStarted: false,
671
- isMuted: false,
672
747
  isSpeaking: false
673
748
  };
674
749
  var MicRecorder = class extends EventEmitter5 {
675
750
  constructor(vadConfig) {
676
751
  super();
752
+ this.vadConfig = vadConfig;
677
753
  this.speakingConfirmed = false;
678
754
  this.queuedChunks = [];
679
755
  this.start = async (stream) => {
@@ -683,25 +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
  });
764
+ this.stream = stream;
689
765
  this.speakingConfirmed = false;
690
- const delayedStream = createDelayedStream(
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);
766
+ await initPcmProcessor();
697
767
  this.workletNode = new AudioWorkletNode(audioContext, "pcm-processor");
698
768
  this.workletNode.port.onmessage = this.onWorkletMessage;
699
- this.source.connect(this.workletNode);
700
769
  this.workletNode.port.postMessage({
701
770
  type: "configure",
702
771
  sourceSampleRate: audioContext.sampleRate,
703
772
  targetSampleRate: SAMPLE_RATE2
704
773
  });
774
+ const delayedStream = createDelayedStream(stream, this.vad.delay / 1e3);
775
+ this.delayedStream = delayedStream;
776
+ this.source = audioContext.createMediaStreamSource(delayedStream);
777
+ this.source.connect(this.workletNode);
705
778
  await this.vad.start(stream);
706
779
  this.vad.on("StartSpeaking", this.onStartSpeaking);
707
780
  this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
@@ -716,30 +789,13 @@ var MicRecorder = class extends EventEmitter5 {
716
789
  console.error(err);
717
790
  }
718
791
  };
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
792
  this.stop = () => {
737
793
  this.changeState({
738
794
  isStarting: false,
739
795
  isStarted: false,
740
- isMuted: false,
741
796
  isSpeaking: false
742
797
  });
798
+ this.stream = void 0;
743
799
  try {
744
800
  this.vad.stop();
745
801
  this.vad.off("StartSpeaking", this.onStartSpeaking);
@@ -762,32 +818,39 @@ var MicRecorder = class extends EventEmitter5 {
762
818
  console.error(err);
763
819
  }
764
820
  };
821
+ this.changeVad = (vadConfig) => {
822
+ if (equalVADConfig(vadConfig, this.vadConfig)) return;
823
+ const stream = this.stream;
824
+ if (stream) {
825
+ this.stop();
826
+ }
827
+ this.vadConfig = vadConfig;
828
+ this.vad = getVAD(vadConfig);
829
+ if (stream) {
830
+ this.start(stream);
831
+ }
832
+ };
765
833
  this.onStartSpeaking = async () => {
766
- if (this.state.isMuted) return;
767
834
  this.speakingConfirmed = false;
768
835
  this.queuedChunks.length = 0;
769
836
  this.workletNode?.port.postMessage({ type: "start" });
770
837
  };
771
838
  this.onConfirmSpeaking = async () => {
772
- if (this.state.isMuted) return;
773
839
  this.speakingConfirmed = true;
774
840
  this.changeState({ isSpeaking: true });
775
841
  this.emit("StartSpeaking");
776
842
  };
777
843
  this.onCancelSpeaking = async () => {
778
- if (this.state.isMuted) return;
779
844
  this.queuedChunks.length = 0;
780
845
  this.workletNode?.port.postMessage({ type: "stop" });
781
846
  };
782
847
  this.onStopSpeaking = async () => {
783
- if (this.state.isMuted) return;
784
848
  this.speakingConfirmed = false;
785
849
  this.changeState({ isSpeaking: false });
786
850
  this.emit("StopSpeaking");
787
851
  this.workletNode?.port.postMessage({ type: "stop" });
788
852
  };
789
853
  this.onWorkletMessage = (event) => {
790
- if (this.state.isMuted) return;
791
854
  const { type, data } = event.data;
792
855
  if (type === "chunk") {
793
856
  const pcmData = data;
@@ -803,10 +866,6 @@ var MicRecorder = class extends EventEmitter5 {
803
866
  }
804
867
  };
805
868
  this.state = defaultMicRecorderState;
806
- const workletBlob = new Blob([pcm_processor_worklet_default], {
807
- type: "application/javascript"
808
- });
809
- this.workletUrl = URL.createObjectURL(workletBlob);
810
869
  this.vad = getVAD(vadConfig);
811
870
  }
812
871
  changeState(state) {
@@ -910,7 +969,9 @@ var MicdropClient = class extends EventEmitter6 {
910
969
  this._isProcessing = true;
911
970
  this._isMuted = false;
912
971
  this._isPaused = false;
913
- await this.startMic({ record: true });
972
+ if (!this.micRecorder || options?.vad) {
973
+ await this.startMic({ vad: options?.vad });
974
+ }
914
975
  await this.startWS();
915
976
  };
916
977
  this.stop = async () => {
@@ -931,20 +992,20 @@ var MicdropClient = class extends EventEmitter6 {
931
992
  Speaker2.stopAudio();
932
993
  };
933
994
  this.mute = () => {
934
- this.micRecorder?.mute();
995
+ this.vad?.pause();
935
996
  this._isMuted = true;
936
997
  this.notifyStateChange();
937
998
  };
938
999
  this.unmute = () => {
939
1000
  if (!this.isPaused && !(this.options.disableInterruption && Speaker2.isPlaying)) {
940
- this.micRecorder?.unmute();
1001
+ this.vad?.resume();
941
1002
  }
942
1003
  this._isMuted = false;
943
1004
  this.notifyStateChange();
944
1005
  };
945
1006
  this.pause = () => {
946
1007
  if (this.isPaused) return;
947
- this.micRecorder?.mute();
1008
+ this.vad?.pause();
948
1009
  this._isPaused = true;
949
1010
  this._isProcessing = false;
950
1011
  this.notifyStateChange();
@@ -954,7 +1015,7 @@ var MicdropClient = class extends EventEmitter6 {
954
1015
  this.resume = () => {
955
1016
  if (!this.isPaused) return;
956
1017
  if (!this.isMuted) {
957
- this.micRecorder?.unmute();
1018
+ this.vad?.resume();
958
1019
  }
959
1020
  this._isPaused = false;
960
1021
  this.notifyStateChange();
@@ -966,7 +1027,12 @@ var MicdropClient = class extends EventEmitter6 {
966
1027
  this.options.vad = options.vad;
967
1028
  }
968
1029
  try {
969
- if (!this.micRecorder) {
1030
+ if (this.micRecorder) {
1031
+ this.micRecorder.stop();
1032
+ if (options.vad) {
1033
+ this.micRecorder.changeVad(options.vad);
1034
+ }
1035
+ } else {
970
1036
  this.micRecorder = new MicRecorder(this.options.vad);
971
1037
  this.micRecorder.on("StateChange", () => {
972
1038
  this.notifyStateChange();
@@ -991,15 +1057,9 @@ var MicdropClient = class extends EventEmitter6 {
991
1057
  this.notifyStateChange();
992
1058
  });
993
1059
  }
994
- const isRecorderStarted = this.micRecorder.state.isStarted;
995
- if (isRecorderStarted) {
996
- this.micRecorder?.stop();
997
- }
998
1060
  const micStream = await Mic2.start(options.deviceId);
999
1061
  this.micStream = micStream;
1000
- if (isRecorderStarted || options.record) {
1001
- await this.micRecorder.start(micStream);
1002
- }
1062
+ await this.micRecorder.start(micStream);
1003
1063
  await this.updateDevices();
1004
1064
  await Speaker2.start();
1005
1065
  this.notifyStateChange();
@@ -1120,14 +1180,14 @@ var MicdropClient = class extends EventEmitter6 {
1120
1180
  this.onSpeakerStartPlaying = () => {
1121
1181
  this.log("Speaker started");
1122
1182
  if (this.options.disableInterruption) {
1123
- this.micRecorder?.mute();
1183
+ this.vad?.pause();
1124
1184
  }
1125
1185
  this.notifyStateChange();
1126
1186
  };
1127
1187
  this.onSpeakerStopPlaying = () => {
1128
1188
  this.log("Speaker stopped");
1129
1189
  if (this.options.disableInterruption && !this.isMuted) {
1130
- this.micRecorder?.unmute();
1190
+ this.vad?.resume();
1131
1191
  }
1132
1192
  this.notifyStateChange();
1133
1193
  };
@@ -1166,7 +1226,7 @@ var MicdropClient = class extends EventEmitter6 {
1166
1226
  return !!this.micStream;
1167
1227
  }
1168
1228
  get isMicMuted() {
1169
- return this.micRecorder?.state.isMuted ?? false;
1229
+ return this.vad?.isPaused ?? false;
1170
1230
  }
1171
1231
  get isUserSpeaking() {
1172
1232
  return this.micRecorder?.state.isSpeaking ?? false;
@@ -1287,6 +1347,7 @@ export {
1287
1347
  VAD,
1288
1348
  VADStatus,
1289
1349
  VolumeVAD,
1350
+ equalVADConfig,
1290
1351
  getClientErrorFromWSCloseEvent,
1291
1352
  getClientErrorFromWSCloseEventCode,
1292
1353
  getVAD