mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
initial changes for major tts revamp
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const Emitter = require('events');
|
||||
const fs = require('fs');
|
||||
const {CallDirection, TaskPreconditions, CallStatus, TaskName} = require('../utils/constants');
|
||||
const moment = require('moment');
|
||||
const assert = require('assert');
|
||||
@@ -40,6 +41,8 @@ class CallSession extends Emitter {
|
||||
this.stackIdx = 0;
|
||||
this.callGone = false;
|
||||
|
||||
this.tmpFiles = new Set();
|
||||
|
||||
sessionTracker.add(this.callSid, this);
|
||||
}
|
||||
|
||||
@@ -99,6 +102,12 @@ class CallSession extends Emitter {
|
||||
get speechSynthesisVoice() {
|
||||
return this.application.speech_synthesis_voice;
|
||||
}
|
||||
/**
|
||||
* default language to use for speech synthesis if not provided in the app
|
||||
*/
|
||||
get speechSynthesisLanguage() {
|
||||
return this.application.speech_synthesis_language;
|
||||
}
|
||||
|
||||
/**
|
||||
* default vendor to use for speech recognition if not provided in the app
|
||||
@@ -172,6 +181,10 @@ class CallSession extends Emitter {
|
||||
sessionTracker.remove(this.callSid);
|
||||
}
|
||||
|
||||
trackTmpFile(path) {
|
||||
this.tmpFiles.add(path);
|
||||
}
|
||||
|
||||
normalizeUrl(url, method, auth) {
|
||||
const hook = {
|
||||
url,
|
||||
@@ -492,6 +505,17 @@ class CallSession extends Emitter {
|
||||
this.logger.error(err, 'CallSession:_clearResources error');
|
||||
}
|
||||
}
|
||||
|
||||
// remove any temporary tts files that were created (audio is still cached in redis)
|
||||
for (const path of this.tmpFiles) {
|
||||
fs.unlink(path, (err) => {
|
||||
if (err) {
|
||||
return this.logger.error(err, `CallSession:_clearResources Error deleting tmp file ${path}`);
|
||||
}
|
||||
this.logger.debug(`CallSession:_clearResources successfully deleted ${path}`);
|
||||
});
|
||||
}
|
||||
this.tmpFiles.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,10 @@ function makeTask(logger, obj, parent) {
|
||||
const TaskHangup = require('./hangup');
|
||||
return new TaskHangup(logger, data, parent);
|
||||
case TaskName.Say:
|
||||
if (data.vendor === 'google' && !data.language) {
|
||||
const TaskSayLegacy = require('./say-legacy');
|
||||
return new TaskSayLegacy(logger, data, parent);
|
||||
}
|
||||
const TaskSay = require('./say');
|
||||
return new TaskSay(logger, data, parent);
|
||||
case TaskName.Play:
|
||||
|
||||
53
lib/tasks/say-legacy.js
Normal file
53
lib/tasks/say-legacy.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const Task = require('./task');
|
||||
const {TaskName, TaskPreconditions} = require('../utils/constants');
|
||||
|
||||
class TaskSayLegacy extends Task {
|
||||
constructor(logger, opts, parentTask) {
|
||||
super(logger, opts);
|
||||
this.preconditions = TaskPreconditions.Endpoint;
|
||||
|
||||
this.text = this.data.text;
|
||||
this.loop = this.data.loop || 1;
|
||||
this.earlyMedia = this.data.earlyMedia === true || (parentTask && parentTask.earlyMedia);
|
||||
if (this.data.synthesizer) {
|
||||
this.voice = this.data.synthesizer.voice;
|
||||
switch (this.data.synthesizer.vendor) {
|
||||
case 'google':
|
||||
this.ttsEngine = 'google_tts';
|
||||
break;
|
||||
default:
|
||||
throw new Error(`unsupported tts vendor ${this.data.synthesizer.vendor}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get name() { return TaskName.SayLegacy; }
|
||||
|
||||
async exec(cs, ep) {
|
||||
super.exec(cs);
|
||||
this.ep = ep;
|
||||
try {
|
||||
while (!this.killed && this.loop--) {
|
||||
this.logger.debug(`TaskSayLegacy: remaining loops ${this.loop}`);
|
||||
await ep.speak({
|
||||
ttsEngine: 'google_tts',
|
||||
voice: this.voice || this.callSession.speechSynthesisVoice,
|
||||
text: this.text
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.info(err, 'TaskSayLegacy:exec error');
|
||||
}
|
||||
this.emit('playDone');
|
||||
}
|
||||
|
||||
async kill() {
|
||||
super.kill();
|
||||
if (this.ep.connected) {
|
||||
this.logger.debug('TaskSayLegacy:kill - killing audio');
|
||||
await this.ep.api('uuid_break', this.ep.uuid).catch((err) => this.logger.info(err, 'Error killing audio'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TaskSayLegacy;
|
||||
@@ -9,31 +9,32 @@ class TaskSay extends Task {
|
||||
this.text = this.data.text;
|
||||
this.loop = this.data.loop || 1;
|
||||
this.earlyMedia = this.data.earlyMedia === true || (parentTask && parentTask.earlyMedia);
|
||||
if (this.data.synthesizer) {
|
||||
this.voice = this.data.synthesizer.voice;
|
||||
switch (this.data.synthesizer.vendor) {
|
||||
case 'google':
|
||||
this.ttsEngine = 'google_tts';
|
||||
break;
|
||||
default:
|
||||
throw new Error(`unsupported tts vendor ${this.data.synthesizer.vendor}`);
|
||||
}
|
||||
}
|
||||
this.synthesizer = this.data.synthesizer || {};
|
||||
}
|
||||
|
||||
get name() { return TaskName.Say; }
|
||||
|
||||
async exec(cs, ep) {
|
||||
const {srf} = cs;
|
||||
const {synthAudio} = srf.locals.dbHelpers;
|
||||
super.exec(cs);
|
||||
this.ep = ep;
|
||||
try {
|
||||
let filepath;
|
||||
const opts = Object.assign({
|
||||
text: this.text,
|
||||
vendor: cs.speechSynthesisVendor,
|
||||
language: cs.speechSynthesisLanguage,
|
||||
voice: cs.speechSynthesisVoice
|
||||
}, this.synthesizer);
|
||||
|
||||
while (!this.killed && this.loop--) {
|
||||
this.logger.debug(`TaskSay: remaining loops ${this.loop}`);
|
||||
await ep.speak({
|
||||
ttsEngine: 'google_tts',
|
||||
voice: this.voice || this.callSession.speechSynthesisVoice,
|
||||
text: this.text
|
||||
});
|
||||
if (!filepath) {
|
||||
filepath = await synthAudio(opts);
|
||||
cs.trackTmpFile(filepath);
|
||||
}
|
||||
await ep.play(filepath);
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.info(err, 'TaskSay:exec error');
|
||||
|
||||
@@ -187,12 +187,14 @@
|
||||
"properties": {
|
||||
"vendor": {
|
||||
"type": "string",
|
||||
"enum": ["google"]
|
||||
"enum": ["google", "aws", "polly"]
|
||||
},
|
||||
"language": "string",
|
||||
"voice": "string"
|
||||
},
|
||||
"required": [
|
||||
"vendor"
|
||||
"vendor",
|
||||
"language"
|
||||
]
|
||||
},
|
||||
"recognizer": {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"SipNotify": "sip:notify",
|
||||
"SipRedirect": "sip:redirect",
|
||||
"Say": "say",
|
||||
"SayLegacy": "say:legacy",
|
||||
"Tag": "tag",
|
||||
"Transcribe": "transcribe"
|
||||
},
|
||||
|
||||
@@ -105,7 +105,8 @@ function installSrfLocals(srf, logger) {
|
||||
updateCallStatus,
|
||||
retrieveCall,
|
||||
listCalls,
|
||||
deleteCall
|
||||
deleteCall,
|
||||
synthAudio
|
||||
} = require('jambonz-realtimedb-helpers')({
|
||||
host: process.env.JAMBONES_REDIS_HOST,
|
||||
port: process.env.JAMBONES_REDIS_PORT || 6379
|
||||
@@ -119,7 +120,8 @@ function installSrfLocals(srf, logger) {
|
||||
updateCallStatus,
|
||||
retrieveCall,
|
||||
listCalls,
|
||||
deleteCall
|
||||
deleteCall,
|
||||
synthAudio
|
||||
},
|
||||
parentLogger: logger,
|
||||
ipv4: localIp,
|
||||
|
||||
Reference in New Issue
Block a user