@layercode/js-sdk 1.0.19 → 1.0.21

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.
@@ -3488,6 +3488,7 @@ registerProcessor('audio_processor', AudioProcessor);
3488
3488
  onUserAmplitudeChange: options.onUserAmplitudeChange || (() => { }),
3489
3489
  onAgentAmplitudeChange: options.onAgentAmplitudeChange || (() => { }),
3490
3490
  onStatusChange: options.onStatusChange || (() => { }),
3491
+ onUserIsSpeakingChange: options.onUserIsSpeakingChange || (() => { }),
3491
3492
  };
3492
3493
  this.AMPLITUDE_MONITORING_SAMPLE_RATE = 10;
3493
3494
  this._websocketUrl = 'wss://api.layercode.com/v1/pipelines/websocket';
@@ -3524,6 +3525,7 @@ registerProcessor('audio_processor', AudioProcessor);
3524
3525
  console.log('silero vad model timeout');
3525
3526
  // TODO: send message to server to indicate that the vad model timed out
3526
3527
  this.userIsSpeaking = true; // allow audio to be sent to the server
3528
+ this.options.onUserIsSpeakingChange(true);
3527
3529
  }, 2000);
3528
3530
  if (!this.canInterrupt) {
3529
3531
  dist.MicVAD.new({
@@ -3537,10 +3539,12 @@ registerProcessor('audio_processor', AudioProcessor);
3537
3539
  onSpeechStart: () => {
3538
3540
  if (!this.wavPlayer.isPlaying) {
3539
3541
  this.userIsSpeaking = true;
3542
+ this.options.onUserIsSpeakingChange(true);
3540
3543
  }
3541
3544
  },
3542
3545
  onVADMisfire: () => {
3543
3546
  this.userIsSpeaking = false;
3547
+ this.options.onUserIsSpeakingChange(false);
3544
3548
  },
3545
3549
  onSpeechEnd: () => {
3546
3550
  this.endUserTurn = true; // Set flag to indicate that the user turn has ended, so we can send a vad_end event to the server
@@ -3578,6 +3582,7 @@ registerProcessor('audio_processor', AudioProcessor);
3578
3582
  console.log('onSpeechStart: WavPlayer is not playing, VAD will not pause.');
3579
3583
  }
3580
3584
  this.userIsSpeaking = true;
3585
+ this.options.onUserIsSpeakingChange(true);
3581
3586
  console.log('onSpeechStart: sending vad_start');
3582
3587
  this._wsSend({
3583
3588
  type: 'vad_events',
@@ -3587,6 +3592,7 @@ registerProcessor('audio_processor', AudioProcessor);
3587
3592
  onVADMisfire: () => {
3588
3593
  // If the speech detected was for less than minSpeechFrames, this is called instead of onSpeechEnd, and we should resume the assistant audio as it was a false interruption. We include a configurable delay so the assistant isn't too quick to start speaking again.
3589
3594
  this.userIsSpeaking = false;
3595
+ this.options.onUserIsSpeakingChange(false);
3590
3596
  if (this.vadPausedPlayer) {
3591
3597
  console.log('onSpeechEnd: VAD paused the player, resuming');
3592
3598
  this.wavPlayer.play();
@@ -3597,11 +3603,7 @@ registerProcessor('audio_processor', AudioProcessor);
3597
3603
  }
3598
3604
  },
3599
3605
  onSpeechEnd: () => {
3600
- this.userIsSpeaking = false;
3601
- this._wsSend({
3602
- type: 'vad_events',
3603
- event: 'vad_end',
3604
- });
3606
+ this.endUserTurn = true; // Set flag to indicate that the user turn has ended, so we can send a vad_end event to the server
3605
3607
  },
3606
3608
  })
3607
3609
  .then((vad) => {
@@ -3722,6 +3724,7 @@ registerProcessor('audio_processor', AudioProcessor);
3722
3724
  if (this.endUserTurn) {
3723
3725
  this.endUserTurn = false;
3724
3726
  this.userIsSpeaking = false; // Reset userIsSpeaking to false so we don't send any more audio to the server
3727
+ this.options.onUserIsSpeakingChange(false);
3725
3728
  this._wsSend({
3726
3729
  type: 'vad_events',
3727
3730
  event: 'vad_end',
@@ -3872,6 +3875,25 @@ registerProcessor('audio_processor', AudioProcessor);
3872
3875
  getStream() {
3873
3876
  return this.wavRecorder.getStream();
3874
3877
  }
3878
+ /**
3879
+ * Switches the input device for the microphone and restarts recording
3880
+ * @param {string} deviceId - The deviceId of the new microphone
3881
+ */
3882
+ async setInputDevice(deviceId) {
3883
+ if (this.wavRecorder) {
3884
+ try {
3885
+ await this.wavRecorder.end();
3886
+ }
3887
+ catch (e) { }
3888
+ try {
3889
+ await this.wavRecorder.quit();
3890
+ }
3891
+ catch (e) { }
3892
+ }
3893
+ await this.wavRecorder.begin(deviceId);
3894
+ await this.wavRecorder.record(this._handleDataAvailable, 1638);
3895
+ this._setupAmplitudeMonitoring(this.wavRecorder, this.options.onUserAmplitudeChange, (amp) => (this.userAudioAmplitude = amp));
3896
+ }
3875
3897
  }
3876
3898
 
3877
3899
  return LayercodeClient;