mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
const Task = require('./task');
|
|
const {TaskName, TaskPreconditions} = require('../utils/constants');
|
|
|
|
class TaskSay extends Task {
|
|
constructor(logger, opts, parentTask) {
|
|
super(logger, opts);
|
|
this.preconditions = TaskPreconditions.Endpoint;
|
|
|
|
this.text = Array.isArray(this.data.text) ? this.data.text : [this.data.text];
|
|
this.loop = this.data.loop || 1;
|
|
this.earlyMedia = this.data.earlyMedia === true || (parentTask && parentTask.earlyMedia);
|
|
this.synthesizer = this.data.synthesizer || {};
|
|
}
|
|
|
|
get name() { return TaskName.Say; }
|
|
|
|
async exec(cs, ep) {
|
|
const {srf} = cs;
|
|
const {synthAudio} = srf.locals.dbHelpers;
|
|
await super.exec(cs);
|
|
this.ep = ep;
|
|
try {
|
|
const filepath = [];
|
|
while (!this.killed && this.loop--) {
|
|
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);
|
|
}
|
|
} catch (err) {
|
|
this.logger.info(err, 'TaskSay:exec error');
|
|
}
|
|
this.emit('playDone');
|
|
}
|
|
|
|
async kill(cs) {
|
|
super.kill(cs);
|
|
if (this.ep.connected) {
|
|
this.logger.debug('TaskSay:kill - killing audio');
|
|
await this.ep.api('uuid_break', this.ep.uuid).catch((err) => this.logger.info(err, 'Error killing audio'));
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = TaskSay;
|