Merge pull request #126 from jambonz/feat/revamp-playback-id

use synth key as playback id
This commit is contained in:
Dave Horton
2025-08-20 18:02:35 -04:00
committed by GitHub

View File

@@ -53,9 +53,6 @@ const EXPIRES = JAMBONES_TTS_CACHE_DURATION_MINS;
const OpenAI = require('openai'); const OpenAI = require('openai');
const getAwsAuthToken = require('./get-aws-sts-token'); const getAwsAuthToken = require('./get-aws-sts-token');
/* counter for playback id */
let playbackIdCounter = 0;
const trimTrailingSilence = (buffer) => { const trimTrailingSilence = (buffer) => {
assert.ok(buffer instanceof Buffer, 'trimTrailingSilence - argument is not a Buffer'); assert.ok(buffer instanceof Buffer, 'trimTrailingSilence - argument is not a Buffer');
@@ -207,74 +204,74 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
const startAt = process.hrtime(); const startAt = process.hrtime();
switch (vendor) { switch (vendor) {
case 'google': case 'google':
audioData = await synthGoogle(logger, {credentials, stats, language, voice, gender, text}); audioData = await synthGoogle(logger, {credentials, stats, language, voice, gender, key, text});
break; break;
case 'aws': case 'aws':
case 'polly': case 'polly':
vendorLabel = 'aws'; vendorLabel = 'aws';
audioData = await synthPolly(createHash, retrieveHash, logger, audioData = await synthPolly(createHash, retrieveHash, logger,
{credentials, stats, language, voice, text, engine, renderForCaching, disableTtsStreaming}); {credentials, stats, language, voice, key, text, engine, renderForCaching, disableTtsStreaming});
break; break;
case 'azure': case 'azure':
case 'microsoft': case 'microsoft':
vendorLabel = 'microsoft'; vendorLabel = 'microsoft';
audioData = await synthMicrosoft(logger, {credentials, stats, language, voice, text, deploymentId, audioData = await synthMicrosoft(logger, {credentials, stats, language, voice, key, text, deploymentId,
renderForCaching, disableTtsStreaming}); renderForCaching, disableTtsStreaming});
break; break;
case 'nuance': case 'nuance':
model = model || 'enhanced'; model = model || 'enhanced';
audioData = await synthNuance(client, logger, {credentials, stats, voice, model, text}); audioData = await synthNuance(client, logger, {credentials, stats, voice, model, key, text});
break; break;
case 'nvidia': case 'nvidia':
audioData = await synthNvidia(client, logger, {credentials, stats, language, voice, model, text, audioData = await synthNvidia(client, logger, {credentials, stats, language, voice, model, key, text,
renderForCaching, disableTtsStreaming}); renderForCaching, disableTtsStreaming});
break; break;
case 'ibm': case 'ibm':
audioData = await synthIbm(logger, {credentials, stats, voice, text}); audioData = await synthIbm(logger, {credentials, stats, voice, key, text});
break; break;
case 'wellsaid': case 'wellsaid':
audioData = await synthWellSaid(logger, {credentials, stats, language, voice, text}); audioData = await synthWellSaid(logger, {credentials, stats, language, voice, key, text});
break; break;
case 'elevenlabs': case 'elevenlabs':
audioData = await synthElevenlabs(logger, { audioData = await synthElevenlabs(logger, {
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming}); credentials, options, stats, language, voice, key, text, renderForCaching, disableTtsStreaming});
break; break;
case 'playht': case 'playht':
audioData = await synthPlayHT(client, logger, { audioData = await synthPlayHT(client, logger, {
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming}); credentials, options, stats, language, voice, key, text, renderForCaching, disableTtsStreaming});
break; break;
case 'cartesia': case 'cartesia':
audioData = await synthCartesia(logger, { audioData = await synthCartesia(logger, {
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming}); credentials, options, stats, language, voice, key, text, renderForCaching, disableTtsStreaming});
break; break;
case 'inworld': case 'inworld':
audioData = await synthInworld(logger, { audioData = await synthInworld(logger, {
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming}); credentials, options, stats, language, voice, key, text, renderForCaching, disableTtsStreaming});
break; break;
case 'rimelabs': case 'rimelabs':
audioData = await synthRimelabs(logger, { audioData = await synthRimelabs(logger, {
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming}); credentials, options, stats, language, voice, key, text, renderForCaching, disableTtsStreaming});
break; break;
case 'whisper': case 'whisper':
audioData = await synthWhisper(logger, { audioData = await synthWhisper(logger, {
credentials, stats, voice, text, instructions, renderForCaching, disableTtsStreaming}); credentials, stats, voice, key, text, instructions, renderForCaching, disableTtsStreaming});
break; break;
case 'verbio': case 'verbio':
audioData = await synthVerbio(client, logger, { audioData = await synthVerbio(client, logger, {
credentials, stats, voice, text, renderForCaching, disableTtsStreaming}); credentials, stats, voice, key, text, renderForCaching, disableTtsStreaming});
if (audioData?.filePath) return audioData; if (audioData?.filePath) return audioData;
break; break;
case 'deepgram': case 'deepgram':
audioData = await synthDeepgram(logger, {credentials, stats, model, text, audioData = await synthDeepgram(logger, {credentials, stats, model, key, text,
renderForCaching, disableTtsStreaming}); renderForCaching, disableTtsStreaming});
break; break;
case 'resemble': case 'resemble':
audioData = await synthResemble(logger, { audioData = await synthResemble(logger, {
credentials, stats, voice, text, options, renderForCaching, disableTtsStreaming}); credentials, stats, voice, key, text, options, renderForCaching, disableTtsStreaming});
break; break;
case vendor.startsWith('custom') ? vendor : 'cant_match_value': case vendor.startsWith('custom') ? vendor : 'cant_match_value':
audioData = await synthCustomVendor(logger, audioData = await synthCustomVendor(logger,
{credentials, stats, language, voice, text}); {credentials, stats, language, voice, key, text});
break; break;
default: default:
assert(`synthAudio: unsupported speech vendor ${vendor}`); assert(`synthAudio: unsupported speech vendor ${vendor}`);
@@ -309,13 +306,13 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
} }
const synthPolly = async(createHash, retrieveHash, logger, const synthPolly = async(createHash, retrieveHash, logger,
{credentials, stats, language, voice, engine, text, renderForCaching, disableTtsStreaming}) => { {credentials, stats, language, voice, engine, key, text, renderForCaching, disableTtsStreaming}) => {
const {region, accessKeyId, secretAccessKey, roleArn} = credentials; const {region, accessKeyId, secretAccessKey, roleArn} = credentials;
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `language=${language}`; params += `language=${language}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += ',write_cache_file=1'; params += ',write_cache_file=1';
params += ',vendor=aws'; params += ',vendor=aws';
if (accessKeyId && secretAccessKey) { if (accessKeyId && secretAccessKey) {
@@ -563,6 +560,7 @@ const synthMicrosoft = async(logger, {
stats, stats,
language, language,
voice, voice,
key,
text, text,
renderForCaching, renderForCaching,
disableTtsStreaming disableTtsStreaming
@@ -594,7 +592,7 @@ const synthMicrosoft = async(logger, {
!renderForCaching && !disableTtsStreaming) { !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${apiKey}`; params += `api_key=${apiKey}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += `,language=${language}`; params += `,language=${language}`;
params += ',vendor=microsoft'; params += ',vendor=microsoft';
params += `,voice=${voice}`; params += `,voice=${voice}`;
@@ -771,13 +769,13 @@ const synthNuance = async(client, logger, {credentials, stats, voice, model, tex
}; };
const synthNvidia = async(client, logger, { const synthNvidia = async(client, logger, {
credentials, stats, language, voice, model, text, renderForCaching, disableTtsStreaming credentials, stats, language, voice, model, key, text, renderForCaching, disableTtsStreaming
}) => { }) => {
const {riva_server_uri} = credentials; const {riva_server_uri} = credentials;
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = ''; let params = '';
params += `{riva_server_uri=${riva_server_uri}`; params += `{riva_server_uri=${riva_server_uri}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += `,voice=${voice}`; params += `,voice=${voice}`;
params += `,language=${language}`; params += `,language=${language}`;
params += ',write_cache_file=1'; params += ',write_cache_file=1';
@@ -851,7 +849,7 @@ const synthCustomVendor = async(logger, {credentials, stats, language, voice, te
}; };
const synthElevenlabs = async(logger, { const synthElevenlabs = async(logger, {
credentials, options, stats, voice, text, renderForCaching, disableTtsStreaming credentials, options, stats, voice, key, text, renderForCaching, disableTtsStreaming
}) => { }) => {
const {api_key, model_id, options: credOpts} = credentials; const {api_key, model_id, options: credOpts} = credentials;
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}'); const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
@@ -860,7 +858,7 @@ const synthElevenlabs = async(logger, {
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += ',vendor=elevenlabs'; params += ',vendor=elevenlabs';
params += `,voice=${voice}`; params += `,voice=${voice}`;
params += `,model_id=${model_id}`; params += `,model_id=${model_id}`;
@@ -915,7 +913,7 @@ const synthElevenlabs = async(logger, {
}; };
const synthPlayHT = async(client, logger, { const synthPlayHT = async(client, logger, {
credentials, options, stats, voice, language, text, renderForCaching, disableTtsStreaming credentials, options, stats, voice, language, key, text, renderForCaching, disableTtsStreaming
}) => { }) => {
const {api_key, user_id, voice_engine, playht_tts_uri, options: credOpts} = credentials; const {api_key, user_id, voice_engine, playht_tts_uri, options: credOpts} = credentials;
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}'); const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
@@ -951,7 +949,7 @@ const synthPlayHT = async(client, logger, {
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += `,user_id=${user_id}`; params += `,user_id=${user_id}`;
params += ',vendor=playht'; params += ',vendor=playht';
params += `,voice=${voice}`; params += `,voice=${voice}`;
@@ -1010,7 +1008,7 @@ const synthPlayHT = async(client, logger, {
}; };
const synthInworld = async(logger, { const synthInworld = async(logger, {
credentials, options, stats, voice, text, renderForCaching, disableTtsStreaming credentials, options, stats, voice, key, text, renderForCaching, disableTtsStreaming
}) => { }) => {
const {api_key, model_id, options: credOpts} = credentials; const {api_key, model_id, options: credOpts} = credentials;
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}'); const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
@@ -1019,7 +1017,7 @@ const synthInworld = async(logger, {
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += `,model_id=${model_id}`; params += `,model_id=${model_id}`;
params += ',vendor=inworld'; params += ',vendor=inworld';
params += `,voice=${voice}`; params += `,voice=${voice}`;
@@ -1075,7 +1073,7 @@ const synthInworld = async(logger, {
}; };
const synthRimelabs = async(logger, { const synthRimelabs = async(logger, {
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming credentials, options, stats, language, voice, key, text, renderForCaching, disableTtsStreaming
}) => { }) => {
const {api_key, model_id, options: credOpts} = credentials; const {api_key, model_id, options: credOpts} = credentials;
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}'); const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
@@ -1084,7 +1082,7 @@ const synthRimelabs = async(logger, {
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += `,model_id=${model_id}`; params += `,model_id=${model_id}`;
params += ',vendor=rimelabs'; params += ',vendor=rimelabs';
params += `,language=${language}`; params += `,language=${language}`;
@@ -1132,7 +1130,9 @@ const synthRimelabs = async(logger, {
throw err; throw err;
} }
}; };
const synthVerbio = async(client, logger, {credentials, stats, voice, text, renderForCaching, disableTtsStreaming}) => { const synthVerbio = async(client, logger, {
credentials, stats, voice, key, text, renderForCaching, disableTtsStreaming
}) => {
//https://doc.speechcenter.verbio.com/#tag/Text-To-Speech-REST-API //https://doc.speechcenter.verbio.com/#tag/Text-To-Speech-REST-API
if (text.length > 2000) { if (text.length > 2000) {
throw new Error('Verbio cannot synthesize for the text length larger than 2000 characters'); throw new Error('Verbio cannot synthesize for the text length larger than 2000 characters');
@@ -1141,7 +1141,7 @@ const synthVerbio = async(client, logger, {credentials, stats, voice, text, rend
if (!process.env.JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!process.env.JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `access_token=${token.access_token}`; params += `access_token=${token.access_token}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += ',vendor=verbio'; params += ',vendor=verbio';
params += `,voice=${voice}`; params += `,voice=${voice}`;
params += ',write_cache_file=1'; params += ',write_cache_file=1';
@@ -1178,14 +1178,14 @@ const synthVerbio = async(client, logger, {credentials, stats, voice, text, rend
} }
}; };
const synthWhisper = async(logger, {credentials, stats, voice, text, instructions, const synthWhisper = async(logger, {credentials, stats, voice, key, text, instructions,
renderForCaching, disableTtsStreaming}) => { renderForCaching, disableTtsStreaming}) => {
const {api_key, model_id, baseURL, timeout, speed} = credentials; const {api_key, model_id, baseURL, timeout, speed} = credentials;
/* if the env is set to stream then bag out, unless we are specifically rendering to generate a cache file */ /* if the env is set to stream then bag out, unless we are specifically rendering to generate a cache file */
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += `,model_id=${model_id}`; params += `,model_id=${model_id}`;
params += ',vendor=whisper'; params += ',vendor=whisper';
params += `,voice=${voice}`; params += `,voice=${voice}`;
@@ -1227,12 +1227,12 @@ const synthWhisper = async(logger, {credentials, stats, voice, text, instruction
} }
}; };
const synthDeepgram = async(logger, {credentials, stats, model, text, renderForCaching, disableTtsStreaming}) => { const synthDeepgram = async(logger, {credentials, stats, model, key, text, renderForCaching, disableTtsStreaming}) => {
const {api_key, deepgram_tts_uri} = credentials; const {api_key, deepgram_tts_uri} = credentials;
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += ',vendor=deepgram'; params += ',vendor=deepgram';
params += `,voice=${model}`; params += `,voice=${model}`;
params += ',write_cache_file=1'; params += ',write_cache_file=1';
@@ -1268,7 +1268,7 @@ const synthDeepgram = async(logger, {credentials, stats, model, text, renderForC
}; };
const synthCartesia = async(logger, { const synthCartesia = async(logger, {
credentials, options, stats, voice, language, text, renderForCaching, disableTtsStreaming credentials, options, stats, voice, language, key, text, renderForCaching, disableTtsStreaming
}) => { }) => {
const {api_key, model_id, embedding, options: credOpts} = credentials; const {api_key, model_id, embedding, options: credOpts} = credentials;
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}'); const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
@@ -1276,7 +1276,7 @@ const synthCartesia = async(logger, {
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += `,model_id=${model_id}`; params += `,model_id=${model_id}`;
params += ',vendor=cartesia'; params += ',vendor=cartesia';
params += `,voice=${voice}`; params += `,voice=${voice}`;
@@ -1339,7 +1339,7 @@ const synthCartesia = async(logger, {
}; };
const synthResemble = async(logger, { const synthResemble = async(logger, {
credentials, options, stats, voice, text, renderForCaching, disableTtsStreaming credentials, options, stats, voice, key, text, renderForCaching, disableTtsStreaming
}) => { }) => {
const {api_key, resemble_tts_uri, resemble_tts_use_tls} = credentials; const {api_key, resemble_tts_uri, resemble_tts_use_tls} = credentials;
const {project_uuid, use_hd} = options || {}; const {project_uuid, use_hd} = options || {};
@@ -1348,7 +1348,7 @@ const synthResemble = async(logger, {
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) { if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
let params = '{'; let params = '{';
params += `api_key=${api_key}`; params += `api_key=${api_key}`;
params += `,playback_id=${playbackIdCounter++}`; params += `,playback_id=${key}`;
params += ',vendor=resemble'; params += ',vendor=resemble';
params += `,voice=${voice}`; params += `,voice=${voice}`;
params += ',write_cache_file=1'; params += ',write_cache_file=1';