@layercode/js-sdk 2.3.3 → 2.5.0
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.
|
@@ -511,12 +511,37 @@ registerProcessor('stream_processor', StreamProcessor);
|
|
|
511
511
|
this.sampleRate = sampleRate;
|
|
512
512
|
this.context = null;
|
|
513
513
|
this.stream = null;
|
|
514
|
+
this.gainNode = null;
|
|
514
515
|
this.analyser = null;
|
|
515
516
|
this.trackSampleOffsets = {};
|
|
516
517
|
this.interruptedTrackIds = {};
|
|
517
518
|
this.finishedPlayingCallback = finishedPlayingCallback;
|
|
518
519
|
this.isPlaying = false;
|
|
519
520
|
this.amplitudeMonitorRaf = undefined;
|
|
521
|
+
this.muted = false;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
_ensureGainNode() {
|
|
525
|
+
if (!this.context) {
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
if (!this.gainNode) {
|
|
529
|
+
this.gainNode = this.context.createGain();
|
|
530
|
+
this._applyGain();
|
|
531
|
+
}
|
|
532
|
+
return this.gainNode;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
_applyGain() {
|
|
536
|
+
if (!this.gainNode || !this.context) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const target = this.muted ? 0 : 1;
|
|
540
|
+
try {
|
|
541
|
+
this.gainNode.gain.setTargetAtTime(target, this.context.currentTime, 0.01);
|
|
542
|
+
} catch {
|
|
543
|
+
this.gainNode.gain.value = target;
|
|
544
|
+
}
|
|
520
545
|
}
|
|
521
546
|
|
|
522
547
|
/**
|
|
@@ -556,6 +581,7 @@ registerProcessor('stream_processor', StreamProcessor);
|
|
|
556
581
|
analyser.fftSize = 8192;
|
|
557
582
|
analyser.smoothingTimeConstant = 0.1;
|
|
558
583
|
this.analyser = analyser;
|
|
584
|
+
this._ensureGainNode();
|
|
559
585
|
this.isPlaying = true;
|
|
560
586
|
return true;
|
|
561
587
|
}
|
|
@@ -625,12 +651,19 @@ registerProcessor('stream_processor', StreamProcessor);
|
|
|
625
651
|
*/
|
|
626
652
|
_start() {
|
|
627
653
|
const streamNode = new AudioWorkletNode(this.context, "stream_processor");
|
|
628
|
-
|
|
654
|
+
const gainNode = this._ensureGainNode();
|
|
655
|
+
if (!gainNode) {
|
|
656
|
+
throw new Error("GainNode not initialized");
|
|
657
|
+
}
|
|
629
658
|
streamNode.port.onmessage = (e) => {
|
|
630
659
|
const { event } = e.data;
|
|
631
660
|
if (event === "stop") {
|
|
632
661
|
streamNode.disconnect();
|
|
662
|
+
gainNode.disconnect();
|
|
633
663
|
this.stream = null;
|
|
664
|
+
if (this.analyser) {
|
|
665
|
+
this.analyser.disconnect();
|
|
666
|
+
}
|
|
634
667
|
this.isPlaying = false;
|
|
635
668
|
this.finishedPlayingCallback();
|
|
636
669
|
} else if (event === "offset") {
|
|
@@ -639,8 +672,15 @@ registerProcessor('stream_processor', StreamProcessor);
|
|
|
639
672
|
this.trackSampleOffsets[requestId] = { trackId, offset, currentTime };
|
|
640
673
|
}
|
|
641
674
|
};
|
|
642
|
-
this.analyser
|
|
643
|
-
|
|
675
|
+
if (this.analyser) {
|
|
676
|
+
this.analyser.disconnect();
|
|
677
|
+
streamNode.connect(gainNode);
|
|
678
|
+
gainNode.connect(this.analyser);
|
|
679
|
+
this.analyser.connect(this.context.destination);
|
|
680
|
+
} else {
|
|
681
|
+
streamNode.connect(gainNode);
|
|
682
|
+
gainNode.connect(this.context.destination);
|
|
683
|
+
}
|
|
644
684
|
this.stream = streamNode;
|
|
645
685
|
this.isPlaying = true;
|
|
646
686
|
return true;
|
|
@@ -770,6 +810,16 @@ registerProcessor('stream_processor', StreamProcessor);
|
|
|
770
810
|
}
|
|
771
811
|
}
|
|
772
812
|
|
|
813
|
+
mute() {
|
|
814
|
+
this.muted = true;
|
|
815
|
+
this._applyGain();
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
unmute() {
|
|
819
|
+
this.muted = false;
|
|
820
|
+
this._applyGain();
|
|
821
|
+
}
|
|
822
|
+
|
|
773
823
|
/**
|
|
774
824
|
* Disconnects the audio context and cleans up resources
|
|
775
825
|
* @returns {void}
|
|
@@ -781,6 +831,11 @@ registerProcessor('stream_processor', StreamProcessor);
|
|
|
781
831
|
this.stream = null;
|
|
782
832
|
}
|
|
783
833
|
|
|
834
|
+
if (this.gainNode) {
|
|
835
|
+
this.gainNode.disconnect();
|
|
836
|
+
this.gainNode = null;
|
|
837
|
+
}
|
|
838
|
+
|
|
784
839
|
if (this.analyser) {
|
|
785
840
|
this.analyser.disconnect();
|
|
786
841
|
}
|
|
@@ -3631,7 +3686,7 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3631
3686
|
* @param {Object} options - Configuration options
|
|
3632
3687
|
*/
|
|
3633
3688
|
constructor(options) {
|
|
3634
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
3689
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
|
|
3635
3690
|
this.deviceId = null;
|
|
3636
3691
|
this.options = {
|
|
3637
3692
|
agentId: options.agentId,
|
|
@@ -3642,22 +3697,25 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3642
3697
|
vadResumeDelay: (_c = options.vadResumeDelay) !== null && _c !== void 0 ? _c : 500,
|
|
3643
3698
|
audioInput: (_d = options.audioInput) !== null && _d !== void 0 ? _d : true,
|
|
3644
3699
|
audioInputChanged: (_e = options.audioInputChanged) !== null && _e !== void 0 ? _e : NOOP,
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3700
|
+
audioOutput: (_f = options.audioOutput) !== null && _f !== void 0 ? _f : true,
|
|
3701
|
+
audioOutputChanged: (_g = options.audioOutputChanged) !== null && _g !== void 0 ? _g : NOOP,
|
|
3702
|
+
onConnect: (_h = options.onConnect) !== null && _h !== void 0 ? _h : NOOP,
|
|
3703
|
+
onDisconnect: (_j = options.onDisconnect) !== null && _j !== void 0 ? _j : NOOP,
|
|
3704
|
+
onError: (_k = options.onError) !== null && _k !== void 0 ? _k : NOOP,
|
|
3705
|
+
onDeviceSwitched: (_l = options.onDeviceSwitched) !== null && _l !== void 0 ? _l : NOOP,
|
|
3706
|
+
onDevicesChanged: (_m = options.onDevicesChanged) !== null && _m !== void 0 ? _m : NOOP,
|
|
3707
|
+
onDataMessage: (_o = options.onDataMessage) !== null && _o !== void 0 ? _o : NOOP,
|
|
3708
|
+
onMessage: (_p = options.onMessage) !== null && _p !== void 0 ? _p : NOOP,
|
|
3709
|
+
onUserAmplitudeChange: (_q = options.onUserAmplitudeChange) !== null && _q !== void 0 ? _q : NOOP,
|
|
3710
|
+
onAgentAmplitudeChange: (_r = options.onAgentAmplitudeChange) !== null && _r !== void 0 ? _r : NOOP,
|
|
3711
|
+
onStatusChange: (_s = options.onStatusChange) !== null && _s !== void 0 ? _s : NOOP,
|
|
3712
|
+
onUserIsSpeakingChange: (_t = options.onUserIsSpeakingChange) !== null && _t !== void 0 ? _t : NOOP,
|
|
3713
|
+
onAgentSpeakingChange: (_u = options.onAgentSpeakingChange) !== null && _u !== void 0 ? _u : NOOP,
|
|
3714
|
+
onMuteStateChange: (_v = options.onMuteStateChange) !== null && _v !== void 0 ? _v : NOOP,
|
|
3715
|
+
enableAmplitudeMonitoring: (_w = options.enableAmplitudeMonitoring) !== null && _w !== void 0 ? _w : true,
|
|
3659
3716
|
};
|
|
3660
|
-
this.audioInput = (
|
|
3717
|
+
this.audioInput = (_x = options.audioInput) !== null && _x !== void 0 ? _x : true;
|
|
3718
|
+
this.audioOutput = (_y = options.audioOutput) !== null && _y !== void 0 ? _y : true;
|
|
3661
3719
|
this._emitAudioInput();
|
|
3662
3720
|
this.AMPLITUDE_MONITORING_SAMPLE_RATE = 2;
|
|
3663
3721
|
this._websocketUrl = DEFAULT_WS_URL;
|
|
@@ -3848,6 +3906,9 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3848
3906
|
await this._clientInterruptAgentReplay();
|
|
3849
3907
|
this._wsSend({ type: 'client.response.text', content: text });
|
|
3850
3908
|
}
|
|
3909
|
+
async sendClientResponseData(data) {
|
|
3910
|
+
this._wsSend({ type: 'client.response.data', data: data });
|
|
3911
|
+
}
|
|
3851
3912
|
/**
|
|
3852
3913
|
* Handles incoming WebSocket messages
|
|
3853
3914
|
* @param {MessageEvent} event - The WebSocket message event
|
|
@@ -4082,10 +4143,25 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
4082
4143
|
}
|
|
4083
4144
|
}
|
|
4084
4145
|
}
|
|
4146
|
+
async setAudioOutput(state) {
|
|
4147
|
+
if (this.audioOutput !== state) {
|
|
4148
|
+
this.audioOutput = state;
|
|
4149
|
+
this._emitAudioOutput();
|
|
4150
|
+
if (state) {
|
|
4151
|
+
this.wavPlayer.unmute();
|
|
4152
|
+
}
|
|
4153
|
+
else {
|
|
4154
|
+
this.wavPlayer.mute();
|
|
4155
|
+
}
|
|
4156
|
+
}
|
|
4157
|
+
}
|
|
4085
4158
|
/** Emitters for audio flags */
|
|
4086
4159
|
_emitAudioInput() {
|
|
4087
4160
|
this.options.audioInputChanged(this.audioInput);
|
|
4088
4161
|
}
|
|
4162
|
+
_emitAudioOutput() {
|
|
4163
|
+
this.options.audioOutputChanged(this.audioOutput);
|
|
4164
|
+
}
|
|
4089
4165
|
get audioInputEnabled() {
|
|
4090
4166
|
return this.audioInput;
|
|
4091
4167
|
}
|
|
@@ -4211,6 +4287,12 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
4211
4287
|
if (!this.options.enableAmplitudeMonitoring) {
|
|
4212
4288
|
this.agentAudioAmplitude = 0;
|
|
4213
4289
|
}
|
|
4290
|
+
if (this.audioOutput) {
|
|
4291
|
+
this.wavPlayer.unmute();
|
|
4292
|
+
}
|
|
4293
|
+
else {
|
|
4294
|
+
this.wavPlayer.mute();
|
|
4295
|
+
}
|
|
4214
4296
|
}
|
|
4215
4297
|
async connectToAudioInput() {
|
|
4216
4298
|
if (!this.audioInput) {
|