@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.
@@ -3482,6 +3482,7 @@ class LayercodeClient {
3482
3482
  onUserAmplitudeChange: options.onUserAmplitudeChange || (() => { }),
3483
3483
  onAgentAmplitudeChange: options.onAgentAmplitudeChange || (() => { }),
3484
3484
  onStatusChange: options.onStatusChange || (() => { }),
3485
+ onUserIsSpeakingChange: options.onUserIsSpeakingChange || (() => { }),
3485
3486
  };
3486
3487
  this.AMPLITUDE_MONITORING_SAMPLE_RATE = 10;
3487
3488
  this._websocketUrl = 'wss://api.layercode.com/v1/pipelines/websocket';
@@ -3518,6 +3519,7 @@ class LayercodeClient {
3518
3519
  console.log('silero vad model timeout');
3519
3520
  // TODO: send message to server to indicate that the vad model timed out
3520
3521
  this.userIsSpeaking = true; // allow audio to be sent to the server
3522
+ this.options.onUserIsSpeakingChange(true);
3521
3523
  }, 2000);
3522
3524
  if (!this.canInterrupt) {
3523
3525
  dist.MicVAD.new({
@@ -3531,10 +3533,12 @@ class LayercodeClient {
3531
3533
  onSpeechStart: () => {
3532
3534
  if (!this.wavPlayer.isPlaying) {
3533
3535
  this.userIsSpeaking = true;
3536
+ this.options.onUserIsSpeakingChange(true);
3534
3537
  }
3535
3538
  },
3536
3539
  onVADMisfire: () => {
3537
3540
  this.userIsSpeaking = false;
3541
+ this.options.onUserIsSpeakingChange(false);
3538
3542
  },
3539
3543
  onSpeechEnd: () => {
3540
3544
  this.endUserTurn = true; // Set flag to indicate that the user turn has ended, so we can send a vad_end event to the server
@@ -3572,6 +3576,7 @@ class LayercodeClient {
3572
3576
  console.log('onSpeechStart: WavPlayer is not playing, VAD will not pause.');
3573
3577
  }
3574
3578
  this.userIsSpeaking = true;
3579
+ this.options.onUserIsSpeakingChange(true);
3575
3580
  console.log('onSpeechStart: sending vad_start');
3576
3581
  this._wsSend({
3577
3582
  type: 'vad_events',
@@ -3581,6 +3586,7 @@ class LayercodeClient {
3581
3586
  onVADMisfire: () => {
3582
3587
  // 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.
3583
3588
  this.userIsSpeaking = false;
3589
+ this.options.onUserIsSpeakingChange(false);
3584
3590
  if (this.vadPausedPlayer) {
3585
3591
  console.log('onSpeechEnd: VAD paused the player, resuming');
3586
3592
  this.wavPlayer.play();
@@ -3591,11 +3597,7 @@ class LayercodeClient {
3591
3597
  }
3592
3598
  },
3593
3599
  onSpeechEnd: () => {
3594
- this.userIsSpeaking = false;
3595
- this._wsSend({
3596
- type: 'vad_events',
3597
- event: 'vad_end',
3598
- });
3600
+ this.endUserTurn = true; // Set flag to indicate that the user turn has ended, so we can send a vad_end event to the server
3599
3601
  },
3600
3602
  })
3601
3603
  .then((vad) => {
@@ -3716,6 +3718,7 @@ class LayercodeClient {
3716
3718
  if (this.endUserTurn) {
3717
3719
  this.endUserTurn = false;
3718
3720
  this.userIsSpeaking = false; // Reset userIsSpeaking to false so we don't send any more audio to the server
3721
+ this.options.onUserIsSpeakingChange(false);
3719
3722
  this._wsSend({
3720
3723
  type: 'vad_events',
3721
3724
  event: 'vad_end',
@@ -3866,6 +3869,25 @@ class LayercodeClient {
3866
3869
  getStream() {
3867
3870
  return this.wavRecorder.getStream();
3868
3871
  }
3872
+ /**
3873
+ * Switches the input device for the microphone and restarts recording
3874
+ * @param {string} deviceId - The deviceId of the new microphone
3875
+ */
3876
+ async setInputDevice(deviceId) {
3877
+ if (this.wavRecorder) {
3878
+ try {
3879
+ await this.wavRecorder.end();
3880
+ }
3881
+ catch (e) { }
3882
+ try {
3883
+ await this.wavRecorder.quit();
3884
+ }
3885
+ catch (e) { }
3886
+ }
3887
+ await this.wavRecorder.begin(deviceId);
3888
+ await this.wavRecorder.record(this._handleDataAvailable, 1638);
3889
+ this._setupAmplitudeMonitoring(this.wavRecorder, this.options.onUserAmplitudeChange, (amp) => (this.userAudioAmplitude = amp));
3890
+ }
3869
3891
  }
3870
3892
 
3871
3893
  export { LayercodeClient as default };