mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-10-03 23:33:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fa68bc712 | ||
|
|
d914b26cac | ||
|
|
7c7be6bbb1 | ||
|
|
46127bb763 | ||
|
|
17305325ff | ||
|
|
0e883cf82a | ||
|
|
2f3a766713 | ||
|
|
b429645ec2 | ||
|
|
0becf33bab | ||
|
|
3d3875741c | ||
|
|
472acc4fbd | ||
|
|
73762e2d61 | ||
|
|
f7417acf58 | ||
|
|
54117ba251 |
@@ -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,
|
||||
|
||||
+46
-12
@@ -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,14 @@ async function synthAudio(client, logger, stats, {
|
||||
|
||||
const synthPolly = async(logger, {credentials, stats, language, voice, engine, text}) => {
|
||||
try {
|
||||
const polly = new PollyClient(credentials);
|
||||
const {region, accessKeyId, secretAccessKey} = credentials;
|
||||
const polly = new PollyClient({
|
||||
region,
|
||||
credentials: {
|
||||
accessKeyId,
|
||||
secretAccessKey
|
||||
}
|
||||
});
|
||||
const opts = {
|
||||
Engine: engine,
|
||||
OutputFormat: 'mp3',
|
||||
@@ -440,30 +448,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
@@ -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 = {
|
||||
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@jambonz/speech-utils",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.5",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@jambonz/speech-utils",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.5",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-polly": "^3.276.0",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@jambonz/speech-utils",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.6",
|
||||
"description": "TTS-related speech utilities for jambonz",
|
||||
"main": "index.js",
|
||||
"author": "Dave Horton",
|
||||
|
||||
+30
-1
@@ -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();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user