Compare commits

...
12 Commits
Author SHA1 Message Date
Dave Horton 7c7be6bbb1 bugfix: AWS polly 2023-03-20 15:33:00 -04:00
Dave Horton 46127bb763 bump version 2023-03-15 08:52:57 -04:00
Dave Horton 17305325ff Merge pull request #13 from jambonz/feat/aws-v3
fix: custom tts support multiple audio types
2023-03-15 08:51:25 -04:00
Quan HL 0e883cf82a fix: custom tts support multiple audio types 2023-03-15 13:43:35 +07:00
Dave Horton 2f3a766713 Merge pull request #12 from jambonz/#10
fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit
2023-03-09 19:45:28 -05:00
Quan HL b429645ec2 fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit 2023-03-10 06:41:22 +07:00
Quan HL 0becf33bab fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit 2023-03-10 06:41:07 +07:00
Quan HL 3d3875741c fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit 2023-03-10 06:31:17 +07:00
Dave Horton 472acc4fbd bump version 2023-03-09 16:21:01 -05:00
Dave Horton 73762e2d61 Merge pull request #11 from jambonz/#9
feat: add account_sid to tts hash key and confiurable TTL for cached file
2023-03-09 16:20:05 -05:00
Quan HL f7417acf58 fix: review comments 2023-03-09 22:11:48 +07:00
Quan HL 54117ba251 feat: add account_sid to tts hash key and confiurable TTL for cached file 2023-03-09 21:42:10 +07:00
6 changed files with 82 additions and 20 deletions
+8 -2
View File
@@ -11,7 +11,8 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
* @param {string} opts.text - text or ssml to synthesize
* @returns {object} result - {error, purgedCount}
*/
async function purgeTtsCache(client, logger, {all, vendor, language, voice, deploymentId, engine, text} = {all: true}) {
async function purgeTtsCache(client, logger, {all, account_sid, vendor,
language, voice, deploymentId, engine, text} = {all: true}) {
logger = logger || noopLogger;
let purgedCount = 0, error;
@@ -21,8 +22,13 @@ async function purgeTtsCache(client, logger, {all, vendor, language, voice, depl
const keys = await client.keysAsync('tts:*');
purgedCount = await client.delAsync(keys);
} else {
} else if (account_sid && !vendor && !language && !voice && !engine && !text) {
const keys = await client.keysAsync(`tts:${account_sid}:*`);
purgedCount = await client.delAsync(keys);
}
else {
const key = makeSynthKey({
account_sid,
vendor,
language: language || '',
voice: voice || deploymentId,
+39 -12
View File
@@ -32,7 +32,7 @@ const {
const {SynthesizeSpeechRequest} = require('../stubs/riva/proto/riva_tts_pb');
const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
const debug = require('debug')('jambonz:realtimedb-helpers');
const EXPIRES = 3600 * 24; // cache tts for 24 hours
const EXPIRES = (process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 24 * 60) * 60; // cache tts for 24 hours
const TMP_FOLDER = '/tmp';
/**
@@ -52,7 +52,7 @@ const TMP_FOLDER = '/tmp';
* @returns object containing filepath to an mp3 file in the /tmp folder containing
* the synthesized audio, and a variable indicating whether it was served from cache
*/
async function synthAudio(client, logger, stats, {
async function synthAudio(client, logger, stats, { account_sid,
vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId, disableTtsCache
}) {
let audioBuffer;
@@ -95,8 +95,8 @@ async function synthAudio(client, logger, stats, {
} else if (vendor.startsWith('custom')) {
assert.ok(credentials.custom_tts_url, `synthAudio requires custom_tts_url in credentials when ${vendor} is used`);
}
const key = makeSynthKey({
account_sid,
vendor,
language: language || '',
voice: voice || deploymentId,
@@ -155,7 +155,8 @@ async function synthAudio(client, logger, stats, {
audioBuffer = await synthWellSaid(logger, {credentials, stats, language, voice, text, filePath});
break;
case vendor.startsWith('custom') ? vendor : 'cant_match_value':
audioBuffer = await synthCustomVendor(logger, {credentials, stats, language, voice, text});
({ audioBuffer, filePath } = await synthCustomVendor(logger,
{credentials, stats, language, voice, text, filePath}));
break;
default:
assert(`synthAudio: unsupported speech vendor ${vendor}`);
@@ -183,7 +184,7 @@ async function synthAudio(client, logger, stats, {
const synthPolly = async(logger, {credentials, stats, language, voice, engine, text}) => {
try {
const polly = new PollyClient(credentials);
const polly = new PollyClient({credentials});
const opts = {
Engine: engine,
OutputFormat: 'mp3',
@@ -440,30 +441,56 @@ const synthNvidia = async(client, logger, {credentials, stats, language, voice,
};
// CustomVendor accept only mp3
const synthCustomVendor = async(logger, {credentials, stats, language, voice, text}) => {
const synthCustomVendor = async(logger, {credentials, stats, language, voice, text, filePath}) => {
const {vendor, auth_token, custom_tts_url} = credentials;
try {
const post = bent('POST', 'buffer', {
const post = bent('POST', {
'Authorization': `Bearer ${auth_token}`,
'Accept': 'audio/mpeg',
'Content-Type': 'application/json'
});
const mp3 = await post(custom_tts_url, {
const response = await post(custom_tts_url, {
language,
format: 'audio/mpeg',
voice,
type: text.startsWith('<speak>') ? 'ssml' : 'text',
text
});
return mp3;
const regex = /\.[^\.]*$/g;
const mime = response.headers['content-type'];
const buffer = await response.arrayBuffer();
return {
audioBuffer: buffer,
filePath: filePath.replace(regex, getFileExtFromMime(mime))
};
} catch (err) {
logger.info({err}, `Vendor ${vendor} returned error`);
throw err;
}
};
const getFileExtFromMime = (mime) => {
switch (mime) {
case 'audio/wav':
case 'audio/x-wav':
return '.wav';
case /audio\/l16.*rate=8000/.test(mime) ? mime : 'cant match value':
return '.r8';
case /audio\/l16.*rate=16000/.test(mime) ? mime : 'cant match value':
return '.r16';
case /audio\/l16.*rate=24000/.test(mime) ? mime : 'cant match value':
return '.r24';
case /audio\/l16.*rate=32000/.test(mime) ? mime : 'cant match value':
return '.r32';
case /audio\/l16.*rate=48000/.test(mime) ? mime : 'cant match value':
return '.r48';
case 'audio/mpeg':
case 'audio/mp3':
return '.mp3';
default:
return '.wav';
}
};
module.exports = synthAudio;
+2 -2
View File
@@ -16,10 +16,10 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
*/
//const nuanceClientMap = new Map();
function makeSynthKey({vendor, language, voice, engine = '', text}) {
function makeSynthKey({account_sid = '', vendor, language, voice, engine = '', text}) {
const hash = crypto.createHash('sha1');
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
return `tts:${hash.digest('hex')}`;
return `tts${account_sid ? (':' + account_sid) : ''}:${hash.digest('hex')}`;
}
const noopLogger = {
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "@jambonz/speech-utils",
"version": "0.0.1",
"version": "0.0.4",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@jambonz/speech-utils",
"version": "0.0.1",
"version": "0.0.4",
"license": "MIT",
"dependencies": {
"@aws-sdk/client-polly": "^3.276.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@jambonz/speech-utils",
"version": "0.0.1",
"version": "0.0.4",
"description": "TTS-related speech utilities for jambonz",
"main": "index.js",
"author": "Dave Horton",
+30 -1
View File
@@ -348,7 +348,6 @@ test('Custom Vendor speech synth tests', async(t) => {
let obj = await getJSON(`http://127.0.0.1:3100/lastRequest/somethingnew`);
t.ok(obj.headers.Authorization == 'Bearer some_jwt_token', 'Custom Vendor Authentication Header is correct');
t.ok(obj.body.language == 'en-US', 'Custom Vendor Language is correct');
t.ok(obj.body.format == 'audio/mpeg', 'Custom Vendor format is correct');
t.ok(obj.body.voice == 'English-US.Female-1', 'Custom Vendor voice is correct');
t.ok(obj.body.type == 'text', 'Custom Vendor type is correct');
t.ok(obj.body.text == 'This is a test. This is only a test', 'Custom Vendor text is correct');
@@ -427,5 +426,35 @@ test('TTS Cache tests', async(t) => {
t.end(err);
}
try {
// clear cache
await purgeTtsCache();
// save some random tts keys to cache
const minRecords = 8;
const account_sid = "12412512_cabc_5aff"
const account_sid2 = "22412512_cabc_5aff"
for (const i in Array(minRecords).fill(0)) {
await client.setAsync(makeSynthKey({account_sid, vendor: i, language: i, voice: i, engine: i, text: i}), i);
}
for (const i in Array(minRecords).fill(0)) {
await client.setAsync(makeSynthKey({account_sid: account_sid2, vendor: i, language: i, voice: i, engine: i, text: i}), i);
}
const {purgedCount} = await purgeTtsCache({account_sid});
t.equal(purgedCount, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid}`);
let cached = (await client.keysAsync('tts:*')).length;
t.equal(cached, minRecords, `successfully purged all tts records from cache for account_sid:${account_sid}`);
const {purgedCount: purgedCount2} = await purgeTtsCache({account_sid: account_sid2});
t.equal(purgedCount2, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid2}`);
cached = (await client.keysAsync('tts:*')).length;
t.equal(cached, 0, `successfully purged all tts records from cache`);
} catch (err) {
console.error(JSON.stringify(err));
t.end(err);
}
client.quit();
});