mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-01-25 02:08:26 +00:00
support precache audio with tts stream enabled
This commit is contained in:
@@ -86,7 +86,7 @@ const trimTrailingSilence = (buffer) => {
|
||||
*/
|
||||
async function synthAudio(client, createHash, retrieveHash, logger, stats, { account_sid,
|
||||
vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId,
|
||||
disableTtsCache, renderForCaching, disableTtsStreaming, options
|
||||
disableTtsCache, renderForCaching, disableTtsStreaming, options, preCache = false
|
||||
}) {
|
||||
let audioBuffer;
|
||||
let servedFromCache = false;
|
||||
@@ -157,20 +157,40 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const preCachekey = makeSynthKey({
|
||||
account_sid,
|
||||
vendor,
|
||||
language: language || '',
|
||||
voice: voice || deploymentId,
|
||||
engine,
|
||||
text,
|
||||
preCache: true
|
||||
});
|
||||
|
||||
const key = makeSynthKey({
|
||||
account_sid,
|
||||
vendor,
|
||||
language: language || '',
|
||||
voice: voice || deploymentId,
|
||||
engine,
|
||||
text
|
||||
text,
|
||||
preCache
|
||||
});
|
||||
let filePath;
|
||||
filePath = makeFilePath(vendor, key, salt);
|
||||
filePath = makeFilePath({vendor, key, salt, preCache});
|
||||
debug(`synth key is ${key}`);
|
||||
let cached;
|
||||
if (!disableTtsCache) {
|
||||
cached = await client.get(key);
|
||||
// 1. check if precache audio
|
||||
// 2. if there is no precache, check key
|
||||
cached = await client.get(preCachekey);
|
||||
if (cached) {
|
||||
// Precache audio is available update filpath with precache file extension.
|
||||
filePath = makeFilePath({vendor, key, salt, preCache: true});
|
||||
} else {
|
||||
cached = await client.get(key);
|
||||
}
|
||||
}
|
||||
if (cached) {
|
||||
// found in cache - extend the expiry and use it
|
||||
|
||||
16
lib/utils.js
16
lib/utils.js
@@ -16,29 +16,31 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||
*/
|
||||
//const nuanceClientMap = new Map();
|
||||
|
||||
function makeSynthKey({account_sid = '', vendor, language, voice, engine = '', text}) {
|
||||
function makeSynthKey({
|
||||
account_sid = '', vendor, language, voice, engine = '', text,
|
||||
preCache = false}) {
|
||||
const hash = crypto.createHash('sha1');
|
||||
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
|
||||
const hexHashKey = hash.digest('hex');
|
||||
const accountKey = account_sid ? `:${account_sid}` : '';
|
||||
const namespace = vendor.startsWith('custom') ? vendor : getFileExtension(vendor);
|
||||
const namespace = vendor.startsWith('custom') ? vendor : getFileExtension({vendor, preCache});
|
||||
const key = `tts${accountKey}:${namespace}:${hexHashKey}`;
|
||||
return key;
|
||||
}
|
||||
|
||||
function makeFilePath(vendor, key, salt = '') {
|
||||
const extension = getFileExtension(vendor);
|
||||
function makeFilePath({vendor, key, salt = '', preCache = false}) {
|
||||
const extension = getFileExtension({vendor, preCache});
|
||||
return `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt}`)}.${extension}`;
|
||||
}
|
||||
|
||||
function getFileExtension(vendor) {
|
||||
function getFileExtension({vendor, preCache = false}) {
|
||||
const mp3Extension = 'mp3';
|
||||
const r8Extension = 'r8';
|
||||
|
||||
switch (vendor) {
|
||||
case 'azure':
|
||||
case 'microsoft':
|
||||
if (!JAMBONES_DISABLE_TTS_STREAMING || JAMBONES_TTS_TRIM_SILENCE) {
|
||||
if (!preCache && !JAMBONES_DISABLE_TTS_STREAMING || JAMBONES_TTS_TRIM_SILENCE) {
|
||||
return r8Extension;
|
||||
} else {
|
||||
return mp3Extension;
|
||||
@@ -46,7 +48,7 @@ function getFileExtension(vendor) {
|
||||
case 'deepgram':
|
||||
case 'elevenlabs':
|
||||
case 'rimlabs':
|
||||
if (!JAMBONES_DISABLE_TTS_STREAMING) {
|
||||
if (!preCache && !JAMBONES_DISABLE_TTS_STREAMING) {
|
||||
return r8Extension;
|
||||
} else {
|
||||
return mp3Extension;
|
||||
|
||||
Reference in New Issue
Block a user