mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-10-03 23:33:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
305695d068 | ||
|
|
1477752d40 | ||
|
|
fbedfe947f | ||
|
|
ab88facd52 | ||
|
|
a2be64da89 | ||
|
|
c5e0f256e6 | ||
|
|
d0378751ba | ||
|
|
a7e391fcb2 | ||
|
|
10095de25d | ||
|
|
89007ba7cc | ||
|
|
29edccd5bf | ||
|
|
ca9538030f | ||
|
|
417d58080e | ||
|
|
62fec6f5e4 | ||
|
|
490d23a703 | ||
|
|
b91e6cc145 | ||
|
|
0e33358254 | ||
|
|
6b2b35acfb | ||
|
|
ded60cb7aa | ||
|
|
460ca70ea7 | ||
|
|
0fbbbb8053 | ||
|
|
0ea7082da2 | ||
|
|
5f7e7458bb | ||
|
|
f6714fb9e1 | ||
|
|
c04ef29f7c | ||
|
|
8154944252 | ||
|
|
16fe8dce01 | ||
|
|
11a955500d | ||
|
|
c122129b55 | ||
|
|
41b26b966b | ||
|
|
aa09c15b20 | ||
|
|
32d5f12638 | ||
|
|
4e336822a0 | ||
|
|
b898d794b0 | ||
|
|
fb754ca101 | ||
|
|
8d8195be9a | ||
|
|
eb4e1a773f | ||
|
|
6fecb8755d | ||
|
|
fdb56cbc77 | ||
|
|
5328a60de8 | ||
|
|
ea1ab301d5 | ||
|
|
0a4994c5a3 | ||
|
|
cb71460189 | ||
|
|
f08efa49b1 | ||
|
|
80eda37ef6 | ||
|
|
7768e58b19 | ||
|
|
6efc99ad83 | ||
|
|
127d01a8c1 | ||
|
|
1900b26d8b | ||
|
|
aa25a6f6d9 | ||
|
|
8dfe17b600 | ||
|
|
5463e9f56e | ||
|
|
2eb36af650 | ||
|
|
b186cbc4f2 | ||
|
|
b1b9c182a9 | ||
|
|
15c77626fc | ||
|
|
76126ec0b3 | ||
|
|
55431ee511 | ||
|
|
1c76f74b4a | ||
|
|
62ad8abb8e | ||
|
|
92734aaedb | ||
|
|
fe4ccfe7d7 | ||
|
|
ab05976032 | ||
|
|
babd8abc51 | ||
|
|
91f85e16b2 | ||
|
|
8a0c5e3dbd | ||
|
|
d40e012138 | ||
|
|
49b23f5240 | ||
|
|
043642ea5f | ||
|
|
91ea9820f9 | ||
|
|
fad0f57b13 | ||
|
|
2608e149a8 | ||
|
|
d21289acd6 | ||
|
|
a7b19e1d8a | ||
|
|
09c7aa9a5f | ||
|
|
f311c21327 | ||
|
|
535d0191da | ||
|
|
c00d4f9be4 | ||
|
|
db135ee5ad | ||
|
|
5eac4c2ad8 | ||
|
|
e23a1a6d09 | ||
|
|
3a78300a08 | ||
|
|
1d7390e7ae | ||
|
|
3c0940f657 | ||
|
|
6fb6195b16 | ||
|
|
2ff8587601 | ||
|
|
925bd26a70 | ||
|
|
ddea485f5f | ||
|
|
49de25feb8 | ||
|
|
08b55b8d79 | ||
|
|
59bca302b9 | ||
|
|
0d98f73c43 | ||
|
|
36670e0080 | ||
|
|
61672f9868 | ||
|
|
ddeca8eb99 | ||
|
|
6e8271b2f6 | ||
|
|
74a4938eb6 | ||
|
|
cb50f603cd | ||
|
|
9a524f00bc | ||
|
|
545df0b770 | ||
|
|
9409405769 | ||
|
|
7dc3bbdb01 | ||
|
|
f8f9de2645 | ||
|
|
467d7ede26 | ||
|
|
fc211ab2e7 | ||
|
|
536f8aab31 | ||
|
|
69b3fdffbe | ||
|
|
53fe72d89e | ||
|
|
e79c15c5da | ||
|
|
a4a427e174 | ||
|
|
b697bc5268 | ||
|
|
d35d7f0aec | ||
|
|
ed1c564fa2 | ||
|
|
7189d471c1 | ||
|
|
04080cc5ec | ||
|
|
3e5ab4af27 | ||
|
|
f06dddd2f7 | ||
|
|
7b4a71f55d | ||
|
|
d552b65618 | ||
|
|
f701b50244 | ||
|
|
199e502fbe | ||
|
|
6769779189 | ||
|
|
4b43c3986c | ||
|
|
e1292772e6 | ||
|
|
37fb045431 | ||
|
|
c328df67a2 | ||
|
|
877cba7065 | ||
|
|
35a178b468 | ||
|
|
7327190471 | ||
|
|
e5b5e3d0c6 | ||
|
|
3760be088b | ||
|
|
5cb52eb8ec | ||
|
|
8a390a8edf | ||
|
|
d96ab5cdf3 | ||
|
|
3bf74671c6 |
@@ -11,7 +11,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: lts/*
|
node-version: '20'
|
||||||
- run: npm install
|
- run: npm install
|
||||||
- name: Install Docker Compose
|
- name: Install Docker Compose
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -3,8 +3,29 @@ const {noopLogger, makeSynthKey} = require('./utils');
|
|||||||
const {JAMBONES_TTS_CACHE_DURATION_MINS} = require('./config');
|
const {JAMBONES_TTS_CACHE_DURATION_MINS} = require('./config');
|
||||||
const EXPIRES = JAMBONES_TTS_CACHE_DURATION_MINS;
|
const EXPIRES = JAMBONES_TTS_CACHE_DURATION_MINS;
|
||||||
|
|
||||||
|
function getExtensionAndSampleRate(path) {
|
||||||
|
const match = path.match(/\.([^.]*)$/);
|
||||||
|
if (!match) {
|
||||||
|
//default should be wav file.
|
||||||
|
return ['wav', 8000];
|
||||||
|
}
|
||||||
|
|
||||||
|
const extension = match[1];
|
||||||
|
const sampleRateMap = {
|
||||||
|
r8: 8000,
|
||||||
|
r16: 16000,
|
||||||
|
r24: 24000,
|
||||||
|
r44: 44100,
|
||||||
|
r48: 48000,
|
||||||
|
r96: 96000,
|
||||||
|
};
|
||||||
|
|
||||||
|
const sampleRate = sampleRateMap[extension] || 8000;
|
||||||
|
return [extension, sampleRate];
|
||||||
|
}
|
||||||
|
|
||||||
async function addFileToCache(client, logger, path,
|
async function addFileToCache(client, logger, path,
|
||||||
{account_sid, vendor, language, voice, deploymentId, engine, text}) {
|
{account_sid, vendor, language, voice, deploymentId, engine, model, text, instructions}) {
|
||||||
let key;
|
let key;
|
||||||
logger = logger || noopLogger;
|
logger = logger || noopLogger;
|
||||||
|
|
||||||
@@ -15,10 +36,19 @@ async function addFileToCache(client, logger, path,
|
|||||||
language: language || '',
|
language: language || '',
|
||||||
voice: voice || deploymentId,
|
voice: voice || deploymentId,
|
||||||
engine,
|
engine,
|
||||||
|
model,
|
||||||
text,
|
text,
|
||||||
|
instructions
|
||||||
});
|
});
|
||||||
|
const [extension, sampleRate] = getExtensionAndSampleRate(path);
|
||||||
const audioBuffer = await fs.readFile(path);
|
const audioBuffer = await fs.readFile(path);
|
||||||
await client.setex(key, EXPIRES, audioBuffer.toString('base64'));
|
await client.setex(key, EXPIRES, JSON.stringify(
|
||||||
|
{
|
||||||
|
audioContent: audioBuffer.toString('base64'),
|
||||||
|
extension,
|
||||||
|
sampleRate
|
||||||
|
}
|
||||||
|
));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err, 'addFileToCache: Error');
|
logger.error(err, 'addFileToCache: Error');
|
||||||
return;
|
return;
|
||||||
|
|||||||
+4
-2
@@ -2,13 +2,14 @@ const JAMBONES_TTS_TRIM_SILENCE = process.env.JAMBONES_TTS_TRIM_SILENCE;
|
|||||||
const JAMBONES_DISABLE_TTS_STREAMING = process.env.JAMBONES_DISABLE_TTS_STREAMING;
|
const JAMBONES_DISABLE_TTS_STREAMING = process.env.JAMBONES_DISABLE_TTS_STREAMING;
|
||||||
const JAMBONES_DISABLE_AZURE_TTS_STREAMING = process.env.JAMBONES_DISABLE_AZURE_TTS_STREAMING;
|
const JAMBONES_DISABLE_AZURE_TTS_STREAMING = process.env.JAMBONES_DISABLE_AZURE_TTS_STREAMING;
|
||||||
const JAMBONES_EAGERLY_PRE_CACHE_AUDIO = process.env.JAMBONES_EAGERLY_PRE_CACHE_AUDIO;
|
const JAMBONES_EAGERLY_PRE_CACHE_AUDIO = process.env.JAMBONES_EAGERLY_PRE_CACHE_AUDIO;
|
||||||
|
const JAMBONES_AZURE_ENABLE_SSML = process.env.JAMBONES_AZURE_ENABLE_SSML;
|
||||||
|
|
||||||
const JAMBONES_HTTP_PROXY_IP = process.env.JAMBONES_HTTP_PROXY_IP;
|
const JAMBONES_HTTP_PROXY_IP = process.env.JAMBONES_HTTP_PROXY_IP;
|
||||||
const JAMBONES_HTTP_PROXY_PORT = process.env.JAMBONES_HTTP_PROXY_PORT;
|
const JAMBONES_HTTP_PROXY_PORT = process.env.JAMBONES_HTTP_PROXY_PORT;
|
||||||
const JAMBONES_TTS_CACHE_DURATION_MINS =
|
const JAMBONES_TTS_CACHE_DURATION_MINS =
|
||||||
(parseInt(process.env.JAMBONES_TTS_CACHE_DURATION_MINS) || 4 * 60) * 60; // cache tts for 4 hours
|
(parseInt(process.env.JAMBONES_TTS_CACHE_DURATION_MINS) || 4 * 60) * 60; // cache tts for 4 hours
|
||||||
|
|
||||||
const TMP_FOLDER = '/tmp';
|
const TMP_FOLDER = process.env.JAMBONES_TMP_FOLDER || '/tmp';
|
||||||
|
|
||||||
const HTTP_TIMEOUT = 5000;
|
const HTTP_TIMEOUT = 5000;
|
||||||
|
|
||||||
@@ -21,5 +22,6 @@ module.exports = {
|
|||||||
JAMBONES_TTS_CACHE_DURATION_MINS,
|
JAMBONES_TTS_CACHE_DURATION_MINS,
|
||||||
JAMBONES_EAGERLY_PRE_CACHE_AUDIO,
|
JAMBONES_EAGERLY_PRE_CACHE_AUDIO,
|
||||||
TMP_FOLDER,
|
TMP_FOLDER,
|
||||||
HTTP_TIMEOUT
|
HTTP_TIMEOUT,
|
||||||
|
JAMBONES_AZURE_ENABLE_SSML
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,22 +7,23 @@ const CACHE_EXPIRY = process.env.AWS_STS_SESSION_RESET_EXPIRY || (EXPIRY - 600);
|
|||||||
|
|
||||||
async function getAwsAuthToken(
|
async function getAwsAuthToken(
|
||||||
logger, createHash, retrieveHash,
|
logger, createHash, retrieveHash,
|
||||||
{accessKeyId, secretAccessKey, region, roleArn}) {
|
{speech_credential_sid, accessKeyId, secretAccessKey, region, roleArn}) {
|
||||||
logger = logger || noopLogger;
|
logger = logger || noopLogger;
|
||||||
try {
|
try {
|
||||||
const key = makeAwsKey(roleArn || accessKeyId);
|
// if incase instance profile is used, speech_credential_sid will be used as key to lookup cache
|
||||||
|
const key = makeAwsKey(roleArn || accessKeyId || speech_credential_sid);
|
||||||
const obj = await retrieveHash(key);
|
const obj = await retrieveHash(key);
|
||||||
if (obj) return {...obj, servedFromCache: true};
|
if (obj) return {...obj, servedFromCache: true};
|
||||||
|
/* access token not found in cache, so generate it using STS */
|
||||||
let data;
|
let data;
|
||||||
|
let expiry = CACHE_EXPIRY;
|
||||||
if (roleArn) {
|
if (roleArn) {
|
||||||
const stsClient = new STSClient({ region });
|
const stsClient = new STSClient({ region });
|
||||||
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
||||||
const command = new AssumeRoleCommand(roleToAssume);
|
const command = new AssumeRoleCommand(roleToAssume);
|
||||||
|
|
||||||
data = await stsClient.send(command);
|
data = await stsClient.send(command);
|
||||||
} else {
|
} else if (accessKeyId) {
|
||||||
/* access token not found in cache, so generate it using STS */
|
|
||||||
const stsClient = new STSClient({
|
const stsClient = new STSClient({
|
||||||
region,
|
region,
|
||||||
credentials: {
|
credentials: {
|
||||||
@@ -32,6 +33,26 @@ async function getAwsAuthToken(
|
|||||||
});
|
});
|
||||||
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
||||||
data = await stsClient.send(command);
|
data = await stsClient.send(command);
|
||||||
|
} else {
|
||||||
|
// instance profile is used.
|
||||||
|
const stsClient = new STSClient({ region });
|
||||||
|
const cred = await stsClient.config.credentials();
|
||||||
|
// method in the AWS SDK automatically fetches credentials using the default credential
|
||||||
|
// provider chain. If the credentials come from an instance profile or an environment
|
||||||
|
// variable, their expiration is controlled by AWS and not explicitly by our code.
|
||||||
|
if (cred && cred.expiration) {
|
||||||
|
const currentTime = new Date();
|
||||||
|
const expiryTime = new Date(cred.expiration);
|
||||||
|
const remainingTimeInSeconds = Math.round((expiryTime - currentTime) / 1000);
|
||||||
|
expiry = remainingTimeInSeconds;
|
||||||
|
}
|
||||||
|
data = {
|
||||||
|
Credentials: {
|
||||||
|
AccessKeyId: cred.accessKeyId,
|
||||||
|
SecretAccessKey: cred.secretAccessKey,
|
||||||
|
SessionToken: cred.sessionToken
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const credentials = {
|
const credentials = {
|
||||||
@@ -40,9 +61,11 @@ async function getAwsAuthToken(
|
|||||||
sessionToken: data.Credentials.SessionToken,
|
sessionToken: data.Credentials.SessionToken,
|
||||||
securityToken: data.Credentials.SessionToken
|
securityToken: data.Credentials.SessionToken
|
||||||
};
|
};
|
||||||
|
// Only cache if expiry is good
|
||||||
createHash(key, credentials, CACHE_EXPIRY)
|
if (expiry > 0) {
|
||||||
|
createHash(key, credentials, expiry)
|
||||||
.catch((err) => logger.error(err, `Error saving hash for key ${key}`));
|
.catch((err) => logger.error(err, `Error saving hash for key ${key}`));
|
||||||
|
}
|
||||||
|
|
||||||
return {...credentials, servedFromCache: false};
|
return {...credentials, servedFromCache: false};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
|
|||||||
* @returns {object} result - {error, purgedCount}
|
* @returns {object} result - {error, purgedCount}
|
||||||
*/
|
*/
|
||||||
async function purgeTtsCache(client, logger, {all, account_sid, vendor,
|
async function purgeTtsCache(client, logger, {all, account_sid, vendor,
|
||||||
language, voice, deploymentId, engine, text} = {all: true}) {
|
language, voice, deploymentId, engine, model, text, instructions} = {all: true}) {
|
||||||
logger = logger || noopLogger;
|
logger = logger || noopLogger;
|
||||||
|
|
||||||
let purgedCount = 0, error;
|
let purgedCount = 0, error;
|
||||||
@@ -33,7 +33,9 @@ async function purgeTtsCache(client, logger, {all, account_sid, vendor,
|
|||||||
language: language || '',
|
language: language || '',
|
||||||
voice: voice || deploymentId,
|
voice: voice || deploymentId,
|
||||||
engine,
|
engine,
|
||||||
|
model,
|
||||||
text,
|
text,
|
||||||
|
instructions
|
||||||
});
|
});
|
||||||
purgedCount = await client.del(key);
|
purgedCount = await client.del(key);
|
||||||
if (purgedCount === 0) error = 'Specified item not found';
|
if (purgedCount === 0) error = 'Specified item not found';
|
||||||
|
|||||||
+638
-210
File diff suppressed because it is too large
Load Diff
+13
-53
@@ -6,7 +6,7 @@ const pool = new Pool('https://auth.crt.nuance.com');
|
|||||||
const NUANCE_AUTH_ENDPOINT = 'tts.api.nuance.com:443';
|
const NUANCE_AUTH_ENDPOINT = 'tts.api.nuance.com:443';
|
||||||
const grpc = require('@grpc/grpc-js');
|
const grpc = require('@grpc/grpc-js');
|
||||||
const formurlencoded = require('form-urlencoded');
|
const formurlencoded = require('form-urlencoded');
|
||||||
const { JAMBONES_DISABLE_TTS_STREAMING, JAMBONES_TTS_TRIM_SILENCE, TMP_FOLDER, HTTP_TIMEOUT } = require('./config');
|
const { TMP_FOLDER, HTTP_TIMEOUT } = require('./config');
|
||||||
|
|
||||||
const debug = require('debug')('jambonz:realtimedb-helpers');
|
const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||||
/**
|
/**
|
||||||
@@ -17,67 +17,27 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
|
|||||||
//const nuanceClientMap = new Map();
|
//const nuanceClientMap = new Map();
|
||||||
|
|
||||||
function makeSynthKey({
|
function makeSynthKey({
|
||||||
account_sid = '', vendor, language, voice, engine = '', text,
|
account_sid = '',
|
||||||
renderForCaching = false}) {
|
vendor,
|
||||||
|
language,
|
||||||
|
voice,
|
||||||
|
engine = '',
|
||||||
|
model = '',
|
||||||
|
text,
|
||||||
|
instructions = '',
|
||||||
|
}) {
|
||||||
const hash = crypto.createHash('sha1');
|
const hash = crypto.createHash('sha1');
|
||||||
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
|
hash.update(`${language}:${vendor}:${voice}:${engine}:${model}:${text}:${instructions}`);
|
||||||
const hexHashKey = hash.digest('hex');
|
const hexHashKey = hash.digest('hex');
|
||||||
const accountKey = account_sid ? `:${account_sid}` : '';
|
const accountKey = account_sid ? `:${account_sid}` : '';
|
||||||
const namespace = vendor.startsWith('custom') ? vendor : getFileExtension({vendor, voice, renderForCaching});
|
const key = `tts${accountKey}:${hexHashKey}`;
|
||||||
const key = `tts${accountKey}:${namespace}:${hexHashKey}`;
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeFilePath({vendor, voice, key, salt = '', renderForCaching = false}) {
|
function makeFilePath({key, salt = '', extension}) {
|
||||||
const extension = getFileExtension({vendor, renderForCaching, voice});
|
|
||||||
return `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt}`)}.${extension}`;
|
return `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt}`)}.${extension}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileExtension({vendor, voice, renderForCaching = false}) {
|
|
||||||
const mp3Extension = 'mp3';
|
|
||||||
const r8Extension = 'r8';
|
|
||||||
const wavExtension = 'wav';
|
|
||||||
|
|
||||||
switch (vendor) {
|
|
||||||
case 'azure':
|
|
||||||
case 'microsoft':
|
|
||||||
if (!renderForCaching && !JAMBONES_DISABLE_TTS_STREAMING || JAMBONES_TTS_TRIM_SILENCE) {
|
|
||||||
return r8Extension;
|
|
||||||
} else {
|
|
||||||
return mp3Extension;
|
|
||||||
}
|
|
||||||
case 'deepgram':
|
|
||||||
case 'elevenlabs':
|
|
||||||
case 'rimelabs':
|
|
||||||
case 'playht':
|
|
||||||
if (renderForCaching || JAMBONES_DISABLE_TTS_STREAMING) {
|
|
||||||
return mp3Extension;
|
|
||||||
} else {
|
|
||||||
return r8Extension;
|
|
||||||
}
|
|
||||||
case 'nuance':
|
|
||||||
case 'nvidia':
|
|
||||||
case 'verbio':
|
|
||||||
return r8Extension;
|
|
||||||
case 'google':
|
|
||||||
// google voice cloning just support wav.
|
|
||||||
if (typeof voice === 'object' && voice.voice_cloning_key) {
|
|
||||||
return wavExtension;
|
|
||||||
} else {
|
|
||||||
return mp3Extension;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// If vendor is custom
|
|
||||||
if (vendor.startsWith('custom')) {
|
|
||||||
if (renderForCaching || JAMBONES_DISABLE_TTS_STREAMING) {
|
|
||||||
return mp3Extension;
|
|
||||||
} else {
|
|
||||||
return r8Extension;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mp3Extension;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const noopLogger = {
|
const noopLogger = {
|
||||||
info: () => {},
|
info: () => {},
|
||||||
|
|||||||
Generated
+3168
-2726
File diff suppressed because it is too large
Load Diff
+8
-6
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@jambonz/speech-utils",
|
"name": "@jambonz/speech-utils",
|
||||||
"version": "0.1.22",
|
"version": "0.2.30",
|
||||||
"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",
|
||||||
@@ -26,22 +26,24 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/jambonz/speech-utils#readme",
|
"homepage": "https://github.com/jambonz/speech-utils#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"23": "^0.0.0",
|
||||||
"@aws-sdk/client-polly": "^3.496.0",
|
"@aws-sdk/client-polly": "^3.496.0",
|
||||||
"@aws-sdk/client-sts": "^3.496.0",
|
"@aws-sdk/client-sts": "^3.496.0",
|
||||||
"@google-cloud/text-to-speech": "^5.5.0",
|
"@cartesia/cartesia-js": "^2.2.7",
|
||||||
|
"@google-cloud/text-to-speech": "^6.4.0",
|
||||||
"@grpc/grpc-js": "^1.9.14",
|
"@grpc/grpc-js": "^1.9.14",
|
||||||
"@jambonz/realtimedb-helpers": "^0.8.7",
|
"@jambonz/realtimedb-helpers": "^0.8.7",
|
||||||
"bent": "^7.3.12",
|
"bent": "^7.3.12",
|
||||||
"debug": "^4.3.4",
|
"debug": "^4.3.4",
|
||||||
"form-urlencoded": "^6.1.4",
|
"form-urlencoded": "^6.1.4",
|
||||||
"google-protobuf": "^3.21.2",
|
"google-protobuf": "^3.21.2",
|
||||||
"ibm-watson": "^8.0.0",
|
"ibm-watson": "^11.0.0",
|
||||||
"microsoft-cognitiveservices-speech-sdk": "1.38.0",
|
"microsoft-cognitiveservices-speech-sdk": "1.38.0",
|
||||||
"openai": "^4.25.0",
|
"openai": "^4.98.0",
|
||||||
"undici": "^6.4.0"
|
"undici": "^7.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"config": "^3.3.11",
|
"config": "^4.2.0",
|
||||||
"eslint": "^9.3.0",
|
"eslint": "^9.3.0",
|
||||||
"eslint-plugin-promise": "^6.2.0",
|
"eslint-plugin-promise": "^6.2.0",
|
||||||
"husky": "^9.0.11",
|
"husky": "^9.0.11",
|
||||||
|
|||||||
+520
-23
@@ -5,7 +5,7 @@ const fs = require('fs');
|
|||||||
const {makeSynthKey} = require('../lib/utils');
|
const {makeSynthKey} = require('../lib/utils');
|
||||||
const logger = require('pino')();
|
const logger = require('pino')();
|
||||||
const bent = require('bent');
|
const bent = require('bent');
|
||||||
const getJSON = bent('json')
|
const getJSON = bent('json');
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason, p) => {
|
process.on('unhandledRejection', (reason, p) => {
|
||||||
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
||||||
@@ -41,6 +41,7 @@ test('Google speech synth tests', async(t) => {
|
|||||||
gender: 'FEMALE',
|
gender: 'FEMALE',
|
||||||
text: 'This is a test. This is only a test',
|
text: 'This is a test. This is only a test',
|
||||||
salt: 'foo.bar',
|
salt: 'foo.bar',
|
||||||
|
renderForCaching: true,
|
||||||
});
|
});
|
||||||
t.ok(!opts.servedFromCache, `successfully synthesized google audio to ${opts.filePath}`);
|
t.ok(!opts.servedFromCache, `successfully synthesized google audio to ${opts.filePath}`);
|
||||||
|
|
||||||
@@ -55,6 +56,7 @@ test('Google speech synth tests', async(t) => {
|
|||||||
language: 'en-GB',
|
language: 'en-GB',
|
||||||
gender: 'FEMALE',
|
gender: 'FEMALE',
|
||||||
text: 'This is a test. This is only a test',
|
text: 'This is a test. This is only a test',
|
||||||
|
renderForCaching: true,
|
||||||
});
|
});
|
||||||
t.ok(opts.servedFromCache, `successfully retrieved cached google audio from ${opts.filePath}`);
|
t.ok(opts.servedFromCache, `successfully retrieved cached google audio from ${opts.filePath}`);
|
||||||
|
|
||||||
@@ -78,6 +80,7 @@ test('Google speech synth tests', async(t) => {
|
|||||||
language: 'en-GB',
|
language: 'en-GB',
|
||||||
gender: 'FEMALE',
|
gender: 'FEMALE',
|
||||||
text: 'This is a test. This is only a test',
|
text: 'This is a test. This is only a test',
|
||||||
|
renderForCaching: true,
|
||||||
});
|
});
|
||||||
t.ok(!opts.servedFromCache, `successfully synthesized google audio regardless of current cache to ${opts.filePath}`);
|
t.ok(!opts.servedFromCache, `successfully synthesized google audio regardless of current cache to ${opts.filePath}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -114,7 +117,8 @@ GCP_CUSTOM_VOICE_FILE nor GCP_CUSTOM_VOICE_JSON_KEY provided, GCP_CUSTOM_VOICE_M
|
|||||||
voice: {
|
voice: {
|
||||||
reportedUsage: 'REALTIME',
|
reportedUsage: 'REALTIME',
|
||||||
model: process.env.GCP_CUSTOM_VOICE_MODEL
|
model: process.env.GCP_CUSTOM_VOICE_MODEL
|
||||||
}
|
},
|
||||||
|
renderForCaching: true,
|
||||||
});
|
});
|
||||||
t.ok(!opts.servedFromCache, `successfully synthesized google custom voice audio to ${opts.filePath}`);
|
t.ok(!opts.servedFromCache, `successfully synthesized google custom voice audio to ${opts.filePath}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -156,7 +160,8 @@ GCP_VOICE_CLONING_FILE nor GCP_VOICE_CLONING_JSON_KEY is not provided`);
|
|||||||
text: 'This is a test. This is only a test. This is a test. This is only a test. This is a test. This is only a test',
|
text: 'This is a test. This is only a test. This is a test. This is only a test. This is a test. This is only a test',
|
||||||
voice: {
|
voice: {
|
||||||
voice_cloning_key
|
voice_cloning_key
|
||||||
}
|
},
|
||||||
|
renderForCaching: true,
|
||||||
});
|
});
|
||||||
t.ok(!opts.servedFromCache, `successfully synthesized google voice cloning audio to ${opts.filePath}`);
|
t.ok(!opts.servedFromCache, `successfully synthesized google voice cloning audio to ${opts.filePath}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -166,6 +171,374 @@ GCP_VOICE_CLONING_FILE nor GCP_VOICE_CLONING_JSON_KEY is not provided`);
|
|||||||
client.quit();
|
client.quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Google Gemini TTS synth tests', async(t) => {
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.GCP_FILE && !process.env.GCP_JSON_KEY) {
|
||||||
|
t.pass('skipping Google Gemini TTS synth tests since neither GCP_FILE nor GCP_JSON_KEY provided');
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const str = process.env.GCP_JSON_KEY || fs.readFileSync(process.env.GCP_FILE);
|
||||||
|
const creds = JSON.parse(str);
|
||||||
|
const geminiModel = process.env.GCP_GEMINI_TTS_MODEL || 'gemini-2.5-flash-tts';
|
||||||
|
|
||||||
|
// Test Gemini TTS with model and instructions (both required for Gemini)
|
||||||
|
let result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Kore',
|
||||||
|
model: geminiModel,
|
||||||
|
text: 'Hello, this is a test of Google Gemini text to speech.',
|
||||||
|
instructions: 'Speak clearly and naturally.',
|
||||||
|
renderForCaching: true,
|
||||||
|
});
|
||||||
|
t.ok(!result.servedFromCache, `successfully synthesized Google Gemini TTS audio to ${result.filePath}`);
|
||||||
|
t.ok(result.filePath.endsWith('.mp3'), 'Gemini TTS audio file has correct extension');
|
||||||
|
|
||||||
|
// Test Gemini TTS with different voice and instructions
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Charon',
|
||||||
|
model: geminiModel,
|
||||||
|
text: 'Welcome to our service. How can I help you today?',
|
||||||
|
instructions: 'Speak in a warm, friendly and professional tone.',
|
||||||
|
renderForCaching: true,
|
||||||
|
});
|
||||||
|
t.ok(!result.servedFromCache, `successfully synthesized Gemini TTS with instructions to ${result.filePath}`);
|
||||||
|
|
||||||
|
// Test cache retrieval
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Kore',
|
||||||
|
model: geminiModel,
|
||||||
|
text: 'Hello, this is a test of Google Gemini text to speech.',
|
||||||
|
instructions: 'Speak clearly and naturally.',
|
||||||
|
renderForCaching: true,
|
||||||
|
});
|
||||||
|
t.ok(result.servedFromCache, `successfully retrieved Gemini TTS audio from cache ${result.filePath}`);
|
||||||
|
|
||||||
|
// Test SSML stripping (Gemini doesn't support SSML)
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Leda',
|
||||||
|
model: geminiModel,
|
||||||
|
text: '<speak>This SSML should be stripped for Gemini TTS.</speak>',
|
||||||
|
instructions: 'Speak naturally.',
|
||||||
|
disableTtsCache: true,
|
||||||
|
renderForCaching: true,
|
||||||
|
});
|
||||||
|
t.ok(!result.servedFromCache, `successfully synthesized Gemini TTS with SSML stripped to ${result.filePath}`);
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
t.end(err);
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Google TTS streaming tests (!JAMBONES_DISABLE_TTS_STREAMING)', async(t) => {
|
||||||
|
// Ensure streaming is enabled (default behavior)
|
||||||
|
delete process.env.JAMBONES_DISABLE_TTS_STREAMING;
|
||||||
|
|
||||||
|
// Clear require cache to reload config with new env var
|
||||||
|
delete require.cache[require.resolve('../lib/config')];
|
||||||
|
delete require.cache[require.resolve('../lib/synth-audio')];
|
||||||
|
delete require.cache[require.resolve('..')];
|
||||||
|
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.GCP_FILE && !process.env.GCP_JSON_KEY) {
|
||||||
|
t.pass('skipping Google TTS streaming tests since neither GCP_FILE nor GCP_JSON_KEY provided');
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const str = process.env.GCP_JSON_KEY || fs.readFileSync(process.env.GCP_FILE);
|
||||||
|
const creds = JSON.parse(str);
|
||||||
|
const geminiModel = process.env.GCP_GEMINI_TTS_MODEL || 'gemini-2.5-flash-tts';
|
||||||
|
|
||||||
|
// Test 1: Standard voice streaming (use_live_api=0)
|
||||||
|
let result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'en-US-Wavenet-D',
|
||||||
|
gender: 'MALE',
|
||||||
|
text: 'This is a test of standard voice streaming.',
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.startsWith('say:'), 'Standard voice returns streaming say: path');
|
||||||
|
t.ok(result.filePath.includes('vendor=google'), 'Standard voice streaming path contains vendor=google');
|
||||||
|
t.ok(result.filePath.includes('api_mode=tts'), 'Standard voice uses api_mode=tts');
|
||||||
|
t.ok(result.filePath.includes('voice=en-US-Wavenet-D'), 'Standard voice streaming path contains voice');
|
||||||
|
// Verify credentials are base64 encoded (no raw JSON braces that would break FreeSWitch parsing)
|
||||||
|
t.ok(result.filePath.includes('credentials='), 'Standard voice streaming path contains credentials');
|
||||||
|
t.ok(!result.filePath.includes('credentials={'), 'Credentials are not raw JSON (base64 encoded)');
|
||||||
|
|
||||||
|
// Test 2: HD voice streaming (api_mode=live)
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'en-US-Chirp3-HD-Charon',
|
||||||
|
text: 'This is a test of HD voice streaming.',
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.startsWith('say:'), 'HD voice returns streaming say: path');
|
||||||
|
t.ok(result.filePath.includes('vendor=google'), 'HD voice streaming path contains vendor=google');
|
||||||
|
t.ok(result.filePath.includes('api_mode=live'), 'HD voice uses api_mode=live');
|
||||||
|
t.ok(result.filePath.includes('voice=en-US-Chirp3-HD-Charon'), 'HD voice streaming path contains voice');
|
||||||
|
|
||||||
|
// Test 3: Gemini TTS streaming (api_mode=gemini)
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Kore',
|
||||||
|
model: geminiModel,
|
||||||
|
text: 'This is a test of Gemini TTS streaming.',
|
||||||
|
instructions: 'Speak naturally.',
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.startsWith('say:'), 'Gemini TTS returns streaming say: path');
|
||||||
|
t.ok(result.filePath.includes('vendor=google'), 'Gemini TTS streaming path contains vendor=google');
|
||||||
|
t.ok(result.filePath.includes('api_mode=gemini'), 'Gemini TTS uses api_mode=gemini');
|
||||||
|
t.ok(result.filePath.includes(`model_name=${geminiModel}`), 'Gemini TTS streaming path contains model_name');
|
||||||
|
t.ok(result.filePath.includes('prompt=Speak naturally.'), 'Gemini TTS streaming path contains prompt');
|
||||||
|
|
||||||
|
// Test 4: Gemini TTS with SSML stripping in streaming mode
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Leda',
|
||||||
|
model: geminiModel,
|
||||||
|
text: '<speak>This SSML should be stripped.</speak>',
|
||||||
|
instructions: 'Speak naturally.',
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.startsWith('say:'), 'Gemini TTS with SSML returns streaming say: path');
|
||||||
|
t.ok(!result.filePath.includes('<speak>'), 'SSML tags are stripped from streaming path');
|
||||||
|
t.ok(result.filePath.includes('This SSML should be stripped.'), 'Text content is preserved after SSML stripping');
|
||||||
|
|
||||||
|
// Test 5: Gemini TTS with prompt containing special characters
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Kore',
|
||||||
|
model: geminiModel,
|
||||||
|
text: 'Testing special characters in prompt.',
|
||||||
|
options: { prompt: 'Speak in a warm, friendly tone' },
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.startsWith('say:'), 'Gemini TTS with special chars returns streaming say: path');
|
||||||
|
// Commas in prompt should be replaced with semicolons
|
||||||
|
t.ok(result.filePath.includes('prompt=Speak in a warm; friendly tone'), 'Commas in prompt are escaped to semicolons');
|
||||||
|
|
||||||
|
// Test 6: options.apiMode override (force live on standard voice)
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'en-US-Wavenet-D',
|
||||||
|
text: 'Testing apiMode option override to live.',
|
||||||
|
options: { apiMode: 'live' },
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.includes('api_mode=live'), 'options.apiMode=live overrides default for standard voice');
|
||||||
|
|
||||||
|
// Test 7: options.apiMode override (force gemini without model)
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Kore',
|
||||||
|
text: 'Testing apiMode option override to gemini.',
|
||||||
|
options: { apiMode: 'gemini' },
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.includes('api_mode=gemini'), 'options.apiMode=gemini overrides default');
|
||||||
|
|
||||||
|
// Test 8: options.apiMode override (force tts on HD voice)
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'en-US-Chirp3-HD-Charon',
|
||||||
|
text: 'Testing apiMode option override to tts on HD voice.',
|
||||||
|
options: { apiMode: 'tts' },
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(result.filePath.includes('api_mode=tts'), 'options.apiMode=tts overrides HD voice default');
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
t.end(err);
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Google TTS non-streaming tests (JAMBONES_DISABLE_TTS_STREAMING=true)', async(t) => {
|
||||||
|
// Enable streaming disable flag
|
||||||
|
process.env.JAMBONES_DISABLE_TTS_STREAMING = 'true';
|
||||||
|
|
||||||
|
// Clear require cache to reload config with new env var
|
||||||
|
delete require.cache[require.resolve('../lib/config')];
|
||||||
|
delete require.cache[require.resolve('../lib/synth-audio')];
|
||||||
|
delete require.cache[require.resolve('..')];
|
||||||
|
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.GCP_FILE && !process.env.GCP_JSON_KEY) {
|
||||||
|
t.pass('skipping Google TTS non-streaming tests since neither GCP_FILE nor GCP_JSON_KEY provided');
|
||||||
|
delete process.env.JAMBONES_DISABLE_TTS_STREAMING;
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const str = process.env.GCP_JSON_KEY || fs.readFileSync(process.env.GCP_FILE);
|
||||||
|
const creds = JSON.parse(str);
|
||||||
|
const geminiModel = process.env.GCP_GEMINI_TTS_MODEL || 'gemini-2.5-flash-tts';
|
||||||
|
|
||||||
|
// Test 1: Standard voice falls back to non-streaming API
|
||||||
|
let result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'en-US-Wavenet-D',
|
||||||
|
gender: 'MALE',
|
||||||
|
text: 'This is a test with streaming disabled.',
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(!result.filePath.startsWith('say:'), 'Standard voice does NOT return streaming say: path when disabled');
|
||||||
|
t.ok(result.filePath.endsWith('.mp3'), 'Standard voice returns mp3 file path');
|
||||||
|
|
||||||
|
// Test 2: HD voice falls back to non-streaming API
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'en-US-Chirp3-HD-Charon',
|
||||||
|
text: 'This is a test of HD voice with streaming disabled.',
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(!result.filePath.startsWith('say:'), 'HD voice does NOT return streaming say: path when disabled');
|
||||||
|
t.ok(result.filePath.endsWith('.mp3'), 'HD voice returns mp3 file path');
|
||||||
|
|
||||||
|
// Test 3: Gemini TTS falls back to non-streaming API
|
||||||
|
result = await synthAudio(stats, {
|
||||||
|
vendor: 'google',
|
||||||
|
credentials: {
|
||||||
|
credentials: {
|
||||||
|
client_email: creds.client_email,
|
||||||
|
private_key: creds.private_key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Kore',
|
||||||
|
model: geminiModel,
|
||||||
|
text: 'This is a test of Gemini TTS with streaming disabled.',
|
||||||
|
instructions: 'Speak naturally.',
|
||||||
|
disableTtsCache: true
|
||||||
|
});
|
||||||
|
t.ok(!result.filePath.startsWith('say:'), 'Gemini TTS does NOT return streaming say: path when disabled');
|
||||||
|
t.ok(result.filePath.endsWith('.mp3'), 'Gemini TTS returns mp3 file path');
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
t.end(err);
|
||||||
|
} finally {
|
||||||
|
// Clean up: restore default behavior
|
||||||
|
delete process.env.JAMBONES_DISABLE_TTS_STREAMING;
|
||||||
|
delete require.cache[require.resolve('../lib/config')];
|
||||||
|
delete require.cache[require.resolve('../lib/synth-audio')];
|
||||||
|
delete require.cache[require.resolve('..')];
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
test('AWS speech synth tests', async(t) => {
|
test('AWS speech synth tests', async(t) => {
|
||||||
const fn = require('..');
|
const fn = require('..');
|
||||||
const {synthAudio, client} = fn(opts, logger);
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
@@ -185,6 +558,7 @@ test('AWS speech synth tests', async(t) => {
|
|||||||
language: 'en-US',
|
language: 'en-US',
|
||||||
voice: 'Joey',
|
voice: 'Joey',
|
||||||
text: 'This is a test. This is only a test',
|
text: 'This is a test. This is only a test',
|
||||||
|
renderForCaching: true,
|
||||||
});
|
});
|
||||||
t.ok(!opts.servedFromCache, `successfully synthesized aws audio to ${opts.filePath}`);
|
t.ok(!opts.servedFromCache, `successfully synthesized aws audio to ${opts.filePath}`);
|
||||||
|
|
||||||
@@ -198,6 +572,7 @@ test('AWS speech synth tests', async(t) => {
|
|||||||
language: 'en-US',
|
language: 'en-US',
|
||||||
voice: 'Joey',
|
voice: 'Joey',
|
||||||
text: 'This is a test. This is only a test',
|
text: 'This is a test. This is only a test',
|
||||||
|
renderForCaching: true,
|
||||||
});
|
});
|
||||||
t.ok(opts.servedFromCache, `successfully retrieved aws audio from cache ${opts.filePath}`);
|
t.ok(opts.servedFromCache, `successfully retrieved aws audio from cache ${opts.filePath}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -552,6 +927,7 @@ test('Custom Vendor speech synth tests', async(t) => {
|
|||||||
language: 'en-US',
|
language: 'en-US',
|
||||||
voice: 'English-US.Female-1',
|
voice: 'English-US.Female-1',
|
||||||
text: 'This is a test. This is only a test',
|
text: 'This is a test. This is only a test',
|
||||||
|
renderForCaching: true
|
||||||
});
|
});
|
||||||
t.ok(!opts.servedFromCache, `successfully synthesized custom vendor audio to ${opts.filePath}`);
|
t.ok(!opts.servedFromCache, `successfully synthesized custom vendor audio to ${opts.filePath}`);
|
||||||
t.ok(opts.filePath.endsWith('wav'), 'audio is cached as wav file');
|
t.ok(opts.filePath.endsWith('wav'), 'audio is cached as wav file');
|
||||||
@@ -573,6 +949,7 @@ test('Custom Vendor speech synth tests', async(t) => {
|
|||||||
language: 'en-US',
|
language: 'en-US',
|
||||||
voice: 'English-US.Female-1',
|
voice: 'English-US.Female-1',
|
||||||
text: 'This is a test. This is only a test',
|
text: 'This is a test. This is only a test',
|
||||||
|
renderForCaching: true
|
||||||
});
|
});
|
||||||
t.ok(opts.servedFromCache, `successfully get custom vendor cached audio to ${opts.filePath}`);
|
t.ok(opts.servedFromCache, `successfully get custom vendor cached audio to ${opts.filePath}`);
|
||||||
t.ok(opts.filePath.endsWith('wav'), 'audio is cached as wav file');
|
t.ok(opts.filePath.endsWith('wav'), 'audio is cached as wav file');
|
||||||
@@ -587,6 +964,7 @@ test('Custom Vendor speech synth tests', async(t) => {
|
|||||||
language: 'en-US',
|
language: 'en-US',
|
||||||
voice: 'English-US.Female-1',
|
voice: 'English-US.Female-1',
|
||||||
text: '<speak>This is a test. This is only a test</speak>',
|
text: '<speak>This is a test. This is only a test</speak>',
|
||||||
|
renderForCaching: true
|
||||||
});
|
});
|
||||||
t.ok(!opts.servedFromCache, `successfully synthesized Custom Vendor audio to ${opts.filePath}`);
|
t.ok(!opts.servedFromCache, `successfully synthesized Custom Vendor audio to ${opts.filePath}`);
|
||||||
obj = await getJSON(`http://127.0.0.1:3100/lastRequest/somethingnew2`);
|
obj = await getJSON(`http://127.0.0.1:3100/lastRequest/somethingnew2`);
|
||||||
@@ -686,7 +1064,100 @@ test('PlayHT3.0 speech synth tests', async(t) => {
|
|||||||
await testPlayHT(t, 'Play3.0');
|
await testPlayHT(t, 'Play3.0');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('rimelabs speech synth tests', async(t) => {
|
test('Cartesia speech synth tests', async(t) => {
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.CARTESIA_API_KEY) {
|
||||||
|
t.pass('skipping Cartesia speech synth tests since CARTESIA_API_KEY is not provided');
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
const text = 'Hi there and welcome to jambones! ' + Date.now();
|
||||||
|
try {
|
||||||
|
const opts = await synthAudio(stats, {
|
||||||
|
vendor: 'cartesia',
|
||||||
|
credentials: {
|
||||||
|
api_key: process.env.CARTESIA_API_KEY,
|
||||||
|
model_id: 'sonic-english',
|
||||||
|
options: JSON.stringify({
|
||||||
|
speed: 1,
|
||||||
|
emotion: 'female_happy',
|
||||||
|
})
|
||||||
|
},
|
||||||
|
language: 'en',
|
||||||
|
voice: '694f9389-aac1-45b6-b726-9d9369183238',
|
||||||
|
text,
|
||||||
|
renderForCaching: true
|
||||||
|
});
|
||||||
|
t.ok(!opts.servedFromCache, `successfully cartesia eleven audio to ${opts.filePath}`);
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(JSON.stringify(err));
|
||||||
|
t.end(err);
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('inworld speech synth', async(t) => {
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.INWORLD_API_KEY) {
|
||||||
|
t.pass('skipping inworld speech synth tests since INWORLD_API_KEY is not provided');
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
const text = 'Hi there and welcome to jambones!';
|
||||||
|
try {
|
||||||
|
const opts = await synthAudio(stats, {
|
||||||
|
vendor: 'inworld',
|
||||||
|
credentials: {
|
||||||
|
api_key: process.env.INWORLD_API_KEY,
|
||||||
|
model_id: 'inworld-tts-1'
|
||||||
|
},
|
||||||
|
language: 'en',
|
||||||
|
voice: 'Ashley',
|
||||||
|
text,
|
||||||
|
renderForCaching: true
|
||||||
|
});
|
||||||
|
t.ok(!opts.servedFromCache, `successfully synthesized inworld audio to ${opts.filePath}`);
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(JSON.stringify(err));
|
||||||
|
t.end(err);
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resemble speech synth', async(t) => {
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.RESEMBLE_API_KEY) {
|
||||||
|
t.pass('skipping resemble speech synth tests since RESEMBLE_API_KEY is not provided');
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
const text = '<speak prompt="Speak in an excited, upbeat tone">Hello from Resemble!</speak>';
|
||||||
|
try {
|
||||||
|
const opts = await synthAudio(stats, {
|
||||||
|
vendor: 'resemble',
|
||||||
|
credentials: {
|
||||||
|
api_key: process.env.RESEMBLE_API_KEY,
|
||||||
|
},
|
||||||
|
language: 'en',
|
||||||
|
voice: '3f5fb9f1',
|
||||||
|
text,
|
||||||
|
renderForCaching: true
|
||||||
|
});
|
||||||
|
t.ok(!opts.servedFromCache, `successfully synthesized resemble audio to ${opts.filePath}`);
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(JSON.stringify(err));
|
||||||
|
t.end(err);
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('rimelabs speech synth tests mist', async(t) => {
|
||||||
const fn = require('..');
|
const fn = require('..');
|
||||||
const {synthAudio, client} = fn(opts, logger);
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
@@ -696,7 +1167,7 @@ test('rimelabs speech synth tests', async(t) => {
|
|||||||
}
|
}
|
||||||
const text = 'Hi there and welcome to jambones!';
|
const text = 'Hi there and welcome to jambones!';
|
||||||
try {
|
try {
|
||||||
let opts = await synthAudio(stats, {
|
const opts = await synthAudio(stats, {
|
||||||
vendor: 'rimelabs',
|
vendor: 'rimelabs',
|
||||||
credentials: {
|
credentials: {
|
||||||
api_key: process.env.RIMELABS_API_KEY,
|
api_key: process.env.RIMELABS_API_KEY,
|
||||||
@@ -706,7 +1177,7 @@ test('rimelabs speech synth tests', async(t) => {
|
|||||||
reduceLatency: false
|
reduceLatency: false
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
language: 'en-US',
|
language: 'eng',
|
||||||
voice: 'amber',
|
voice: 'amber',
|
||||||
text,
|
text,
|
||||||
renderForCaching: true
|
renderForCaching: true
|
||||||
@@ -720,6 +1191,40 @@ test('rimelabs speech synth tests', async(t) => {
|
|||||||
client.quit();
|
client.quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('rimelabs speech synth tests mistv2', async(t) => {
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.RIMELABS_API_KEY) {
|
||||||
|
t.pass('skipping rimelabs speech synth tests since RIMELABS_API_KEY is not provided');
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
const text = 'Hi there and welcome to jambones!';
|
||||||
|
try {
|
||||||
|
const opts = await synthAudio(stats, {
|
||||||
|
vendor: 'rimelabs',
|
||||||
|
credentials: {
|
||||||
|
api_key: process.env.RIMELABS_API_KEY,
|
||||||
|
model_id: 'mistv2',
|
||||||
|
options: JSON.stringify({
|
||||||
|
speedAlpha: 1.0,
|
||||||
|
reduceLatency: false
|
||||||
|
})
|
||||||
|
},
|
||||||
|
language: 'spa',
|
||||||
|
voice: 'pablo',
|
||||||
|
text,
|
||||||
|
renderForCaching: true
|
||||||
|
});
|
||||||
|
t.ok(!opts.servedFromCache, `successfully synthesized rimelabs mistv2 audio to ${opts.filePath}`);
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(JSON.stringify(err));
|
||||||
|
t.end(err);
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
test('whisper speech synth tests', async(t) => {
|
test('whisper speech synth tests', async(t) => {
|
||||||
const fn = require('..');
|
const fn = require('..');
|
||||||
const {synthAudio, client} = fn(opts, logger);
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
@@ -819,7 +1324,8 @@ test('TTS Cache tests', async(t) => {
|
|||||||
// save some random tts keys to cache
|
// save some random tts keys to cache
|
||||||
const minRecords = 8;
|
const minRecords = 8;
|
||||||
for (const i in Array(minRecords).fill(0)) {
|
for (const i in Array(minRecords).fill(0)) {
|
||||||
await client.set(makeSynthKey({vendor: i, language: i, voice: i, engine: i, text: i}), i);
|
await client.set(makeSynthKey({vendor: i, language: i, voice: i, engine: i, model: i, text: i,
|
||||||
|
instructions: i}), i);
|
||||||
}
|
}
|
||||||
const count = await getTtsSize();
|
const count = await getTtsSize();
|
||||||
t.ok(count >= minRecords, 'getTtsSize worked.');
|
t.ok(count >= minRecords, 'getTtsSize worked.');
|
||||||
@@ -838,7 +1344,7 @@ test('TTS Cache tests', async(t) => {
|
|||||||
try {
|
try {
|
||||||
// save some random tts keys to cache
|
// save some random tts keys to cache
|
||||||
for (const i in Array(10).fill(0)) {
|
for (const i in Array(10).fill(0)) {
|
||||||
await client.set(makeSynthKey({vendor: i, language: i, voice: i, engine: i, text: i}), i);
|
await client.set(makeSynthKey({vendor: i, language: i, voice: i, engine: i, text: i, instructions: i}), i);
|
||||||
}
|
}
|
||||||
// save a specific key to tts cache
|
// save a specific key to tts cache
|
||||||
const opts = {vendor: 'aws', language: 'en-US', voice: 'MALE', engine: 'Engine', text: 'Hello World!'};
|
const opts = {vendor: 'aws', language: 'en-US', voice: 'MALE', engine: 'Engine', text: 'Hello World!'};
|
||||||
@@ -854,24 +1360,15 @@ test('TTS Cache tests', async(t) => {
|
|||||||
language: 'non-existing',
|
language: 'non-existing',
|
||||||
voice: 'non-existing',
|
voice: 'non-existing',
|
||||||
});
|
});
|
||||||
t.ok(purgedCountWhenErrored === 0, `purged no records when specified key was not found`);
|
t.ok(purgedCountWhenErrored === 0, 'purged no records when specified key was not found');
|
||||||
t.ok(error, `error returned when specified key was not found`);
|
t.ok(error, 'error returned when specified key was not found');
|
||||||
|
|
||||||
// make sure other tts keys are still there
|
// make sure other tts keys are still there
|
||||||
const cached = await client.keys('tts:*')
|
const cached = await client.keys('tts:*');
|
||||||
t.ok(cached.length >= 1, `successfully kept all non-specified tts records in cache`);
|
t.ok(cached.length >= 1, 'successfully kept all non-specified tts records in cache');
|
||||||
|
|
||||||
// retrieve keys from cache and check the key contains the file extension
|
|
||||||
let key = cached[0];
|
|
||||||
t.ok(key.includes('mp3'), `tts cache extension shoult be part of the key and equal mp3`);
|
|
||||||
|
|
||||||
process.env.VG_TRIM_TTS_SILENCE = 'true';
|
process.env.VG_TRIM_TTS_SILENCE = 'true';
|
||||||
await client.set(makeSynthKey({ vendor: 'azure' }), 'value');
|
await client.set(makeSynthKey({ vendor: 'azure' }), 'value');
|
||||||
|
|
||||||
const r8Keys = await client.keys('tts:r8*');
|
|
||||||
key = r8Keys[0];
|
|
||||||
t.ok(key.includes('r8'), `tts cache extension shoult be part of the key and equal r8`);
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(JSON.stringify(err));
|
console.error(JSON.stringify(err));
|
||||||
t.end(err);
|
t.end(err);
|
||||||
@@ -885,10 +1382,10 @@ test('TTS Cache tests', async(t) => {
|
|||||||
const account_sid = "12412512_cabc_5aff"
|
const account_sid = "12412512_cabc_5aff"
|
||||||
const account_sid2 = "22412512_cabc_5aff"
|
const account_sid2 = "22412512_cabc_5aff"
|
||||||
for (const i in Array(minRecords).fill(0)) {
|
for (const i in Array(minRecords).fill(0)) {
|
||||||
await client.set(makeSynthKey({account_sid, vendor: i, language: i, voice: i, engine: i, text: i}), i);
|
await client.set(makeSynthKey({account_sid, vendor: i, language: i, voice: i, engine: i, text: i, instructions: i}), i);
|
||||||
}
|
}
|
||||||
for (const i in Array(minRecords).fill(0)) {
|
for (const i in Array(minRecords).fill(0)) {
|
||||||
await client.set(makeSynthKey({account_sid: account_sid2, vendor: i, language: i, voice: i, engine: i, text: i}), i);
|
await client.set(makeSynthKey({account_sid: account_sid2, vendor: i, language: i, voice: i, engine: i, text: i, instructions: i}), i);
|
||||||
}
|
}
|
||||||
const {purgedCount} = await purgeTtsCache({account_sid});
|
const {purgedCount} = await purgeTtsCache({account_sid});
|
||||||
t.equal(purgedCount, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid}`);
|
t.equal(purgedCount, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user