From 4e4ce0914ec8d63e7b4dc85d7f60af956c3aee91 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Tue, 16 Jun 2020 14:39:46 -0400 Subject: [PATCH] bugfix: say text continued to play after task was killed --- lib/tasks/say.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/tasks/say.js b/lib/tasks/say.js index 18f24f54..20f69e19 100644 --- a/lib/tasks/say.js +++ b/lib/tasks/say.js @@ -20,24 +20,27 @@ class TaskSay extends Task { await super.exec(cs); this.ep = ep; try { - const filepath = []; + // synthesize all of the text elements + const filepath = (await Promise.all(this.text.map(async(text) => { + const fp = await synthAudio({ + text, + vendor: cs.speechSynthesisVendor, + language: cs.speechSynthesisLanguage, + voice: cs.speechSynthesisVoice, + salt: cs.callSid + }).catch((err) => this.logger.error(err, 'Error synthesizing text')); + if (fp) cs.trackTmpFile(fp); + return fp; + }))) + .filter((fp) => fp && fp.length); + + this.logger.debug({filepath}, 'synthesized files for tts'); + while (!this.killed && this.loop-- && this.ep.connected) { let segment = 0; do { - if (filepath.length <= segment) { - const opts = Object.assign({ - text: this.text[segment], - vendor: cs.speechSynthesisVendor, - language: cs.speechSynthesisLanguage, - voice: cs.speechSynthesisVoice, - salt: cs.callSid - }, this.synthesizer); - const path = await synthAudio(opts); - filepath.push(path); - cs.trackTmpFile(path); - } await ep.play(filepath[segment]); - } while (++segment < this.text.length); + } while (!this.killed && ++segment < filepath.length); } } catch (err) { this.logger.info(err, 'TaskSay:exec error');