From c3f5a63ad18c247297891536da66c3dbf84502f0 Mon Sep 17 00:00:00 2001 From: "p.souza" Date: Mon, 1 Jul 2024 10:32:50 +0200 Subject: [PATCH] fix errors on filler noises #795 795 --- lib/session/call-session.js | 38 +++++++++++++++++++++++++++++-------- lib/tasks/gather.js | 13 +++++++++++-- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/session/call-session.js b/lib/session/call-session.js index ac81dc8ff..b9c5916f0 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -85,6 +85,7 @@ class CallSession extends Emitter { language: this.application?.speech_synthesis_language, voice: this.application?.speech_synthesis_voice, }; + this.awaitCommands = false; assert(rootSpan); @@ -1816,6 +1817,7 @@ Duration=${duration} ` async _onCommand({msgid, command, call_sid, queueCommand, data}) { this.logger.info({msgid, command, queueCommand, data}, 'CallSession:_onCommand - received command'); + this.awaitCommands = false let resolution; switch (command) { case 'redirect': @@ -2504,10 +2506,26 @@ Duration=${duration} ` } } + _startFillerNoiseTimer() { + this._clearFillerNoiseTimer(); + this._fillerNoiseTimer = setTimeout(() => { + if (this.awaitCommands) { + this.logger.debug('CallSession:_awaitCommandsOrHangup - playing filler noise'); + this.ep.play(this.fillerNoise.url); + } + }, this.fillerNoise.startDelaySecs * 1000); + } + + _clearFillerNoiseTimer() { + if (this._fillerNoiseTimer) clearTimeout(this._fillerNoiseTimer); + this._fillerNoiseTimer = null; + } + _awaitCommandsOrHangup() { assert(!this.wakeupResolver); return new Promise((resolve, reject) => { this.logger.info('_awaitCommandsOrHangup - waiting...'); + this.awaitCommands = true; this.wakeupResolver = resolve; if (this._actionHookDelayProcessor) { @@ -2524,14 +2542,18 @@ Duration=${duration} ` if (this.fillerNoise?.url && this.ep?.connected && !this.ep2) { this.logger.debug('CallSession:_awaitCommandsOrHangup - playing filler noise'); this._isPlayingFillerNoise = true; - this.ep.play(this.fillerNoise.url); - this.ep.once('playback-start', (evt) => { - if (evt.file === this.fillerNoise.url && !this._isPlayingFillerNoise) { - this.logger.info('CallSession:_awaitCommandsOrHangup - filler noise started'); - this.ep.api('uuid_break', this.ep.uuid) - .catch((err) => this.logger.info(err, 'Error killing filler noise')); - } - }); + if (this.fillerNoise.startDelaySecs > 0) { + this._startFillerNoiseTimer(); + } else { + this.ep.play(this.fillerNoise.url); + this.ep.once('playback-start', (evt) => { + if (evt.file === this.fillerNoise.url && !this._isPlayingFillerNoise) { + this.logger.info('CallSession:_awaitCommandsOrHangup - filler noise started'); + this.ep.api('uuid_break', this.ep.uuid) + .catch((err) => this.logger.info(err, 'Error killing filler noise')); + } + }); + } } }); } diff --git a/lib/tasks/gather.js b/lib/tasks/gather.js index 16c7ca3e7..b45802df7 100644 --- a/lib/tasks/gather.js +++ b/lib/tasks/gather.js @@ -131,6 +131,7 @@ class TaskGather extends SttTask { ...(cs.fillerNoise || {}), ...(this.fillerNoise || {}) }; + this._fillerNoiseKill = false; this.vad = { ...(cs.vad || {}), @@ -638,10 +639,14 @@ class TaskGather extends SttTask { this._fillerNoiseOn = true; this.ep.once('playback-start', (evt) => { if (evt.file === this.fillerNoise.url && !this._fillerNoiseOn) { + this.logger.info('Filler noise already started'); + return; + /* this.logger.info({evt}, 'Gather:_startFillerNoise - race condition - kill filler noise here'); this.ep.api('uuid_break', this.ep.uuid) .catch((err) => this.logger.info(err, 'Error killing filler noise')); return; + */ } else this.logger.debug({evt}, 'Gather:_startFillerNoise - playback started'); }); } @@ -649,8 +654,11 @@ class TaskGather extends SttTask { _startFillerNoiseTimer() { this._clearFillerNoiseTimer(); this._fillerNoiseTimer = setTimeout(() => { - this.logger.debug('Gather:_startFillerNoiseTimer - playing filler noise'); - this._startFillerNoise(); + if (!this._fillerNoiseKill) { + this.logger.debug('Gather:_startFillerNoiseTimer - playing filler noise'); + this._startFillerNoise(); + } + return; }, this.fillerNoise.startDelaySecs * 1000); } @@ -672,6 +680,7 @@ class TaskGather extends SttTask { this.logger.debug('Gather:_killAudio: killing playback of any audio'); this.playComplete = true; this._fillerNoiseOn = false; // in a race, if we just started audio it may sneak through here + this._fillerNoiseKill = true; this.ep.api('uuid_break', this.ep.uuid) .catch((err) => this.logger.info(err, 'Error killing audio')); }