Compare commits

..
4 Commits
Author SHA1 Message Date
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 46 additions and 10 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 * @param {string} opts.text - text or ssml to synthesize
* @returns {object} result - {error, purgedCount} * @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; logger = logger || noopLogger;
let purgedCount = 0, error; let purgedCount = 0, error;
@@ -21,8 +22,13 @@ async function purgeTtsCache(client, logger, {all, vendor, language, voice, depl
const keys = await client.keysAsync('tts:*'); const keys = await client.keysAsync('tts:*');
purgedCount = await client.delAsync(keys); 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({ const key = makeSynthKey({
account_sid,
vendor, vendor,
language: language || '', language: language || '',
voice: voice || deploymentId, voice: voice || deploymentId,
+3 -3
View File
@@ -32,7 +32,7 @@ const {
const {SynthesizeSpeechRequest} = require('../stubs/riva/proto/riva_tts_pb'); const {SynthesizeSpeechRequest} = require('../stubs/riva/proto/riva_tts_pb');
const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb'); const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
const debug = require('debug')('jambonz:realtimedb-helpers'); 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 || 3600 * 24; // cache tts for 24 hours
const TMP_FOLDER = '/tmp'; 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 * @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 * 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 vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId, disableTtsCache
}) { }) {
let audioBuffer; let audioBuffer;
@@ -95,8 +95,8 @@ async function synthAudio(client, logger, stats, {
} else if (vendor.startsWith('custom')) { } else if (vendor.startsWith('custom')) {
assert.ok(credentials.custom_tts_url, `synthAudio requires custom_tts_url in credentials when ${vendor} is used`); assert.ok(credentials.custom_tts_url, `synthAudio requires custom_tts_url in credentials when ${vendor} is used`);
} }
const key = makeSynthKey({ const key = makeSynthKey({
account_sid,
vendor, vendor,
language: language || '', language: language || '',
voice: voice || deploymentId, voice: voice || deploymentId,
+2 -2
View File
@@ -16,10 +16,10 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
*/ */
//const nuanceClientMap = new Map(); //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'); const hash = crypto.createHash('sha1');
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`); hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
return `tts:${hash.digest('hex')}`; return `tts${account_sid ? (':' + account_sid) : ''}:${hash.digest('hex')}`;
} }
const noopLogger = { const noopLogger = {
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "@jambonz/speech-utils", "name": "@jambonz/speech-utils",
"version": "0.0.1", "version": "0.0.2",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@jambonz/speech-utils", "name": "@jambonz/speech-utils",
"version": "0.0.1", "version": "0.0.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@aws-sdk/client-polly": "^3.276.0", "@aws-sdk/client-polly": "^3.276.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@jambonz/speech-utils", "name": "@jambonz/speech-utils",
"version": "0.0.1", "version": "0.0.2",
"description": "TTS-related speech utilities for jambonz", "description": "TTS-related speech utilities for jambonz",
"main": "index.js", "main": "index.js",
"author": "Dave Horton", "author": "Dave Horton",
+30
View File
@@ -427,5 +427,35 @@ test('TTS Cache tests', async(t) => {
t.end(err); 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(); client.quit();
}); });