Compare commits

..
49 Commits
Author SHA1 Message Date
Dave Horton 8246dbea21 0.0.17 2023-07-19 10:10:39 -04:00
Dave Horton 0084f6a468 update deps 2023-07-19 10:09:31 -04:00
Dave Horton 98f679f43a 0.0.16 2023-07-19 10:04:17 -04:00
Dave Horton 7e7841b5ff Merge pull request #23 from jambonz/feature/trim-silence
trim trailing silence from azure tts when JAMBONES_TTS_TRIM_SILENCE i…
2023-07-19 10:03:42 -04:00
Dave Horton 75ce537db1 linting 2023-07-19 10:02:10 -04:00
Dave Horton 830be783b8 trim trailing silence from azure tts when JAMBONES_TTS_TRIM_SILENCE is set 2023-07-19 10:00:31 -04:00
Dave Horton 38c3219425 Merge pull request #21 from jambonz/snyk-fix-255777535fcb48b70c964213f7dd9fe8
[Snyk] Security upgrade @aws-sdk/client-polly from 3.303.0 to 3.347.1
2023-06-07 13:09:27 -04:00
snyk-bot e37b96a9c2 fix: package.json & package-lock.json to reduce vulnerabilities
The following vulnerabilities are fixed with an upgrade:
- https://snyk.io/vuln/SNYK-JS-FASTXMLPARSER-5668858
2023-06-07 15:35:04 +00:00
Dave Horton c184fbae26 0.0.15 2023-06-03 09:15:48 -04:00
Dave Horton 66d33ebd60 change default tts cache duration to 4 hours 2023-06-03 09:15:45 -04:00
Dave Horton 98e1bf62f9 0.0.14 2023-05-31 11:15:38 -04:00
Dave Horton 63edbc2883 Merge pull request #20 from jambonz/feat/clear_tts
feat: ioredis and getsize of tts cache
2023-05-31 11:13:48 -04:00
Quan HL fb75de6af5 feat: ioredis and getsize of tts cache 2023-05-31 21:56:50 +07:00
Quan HL 617f7af4af feat: ioredis and getsize of tts cache 2023-05-31 21:53:18 +07:00
Dave Horton 6c5c8e734f 0.0.13 2023-05-10 07:39:21 -04:00
Dave Horton 4349cd7e40 Merge pull request #19 from jambonz/fix/nvidia
fixes for riva tts
2023-05-10 07:38:58 -04:00
Dave Horton b6058ca242 fix nvidia test 2023-05-10 07:36:25 -04:00
Dave Horton 68cbd63bbd minor logging 2023-05-09 13:59:38 -04:00
Dave Horton 521560e276 fixes for riva tts 2023-05-09 13:57:41 -04:00
Dave Horton d606141f57 fix issue in prev commit for microsoft 2023-04-01 13:19:27 -04:00
Dave Horton 0d58954537 bump version 2023-04-01 10:48:05 -04:00
Dave Horton 7c0eafded3 Merge pull request #18 from jambonz/fix/microsoft-buffer
fix: microsft retrun arrayBuffer not buffer, convert it now to buffer
2023-04-01 10:46:40 -04:00
Quan HL ab7d145288 fix: microsft retrun arrayBuffer not buffer, convert it now to buffer 2023-04-01 13:39:33 +07:00
Dave Horton 11746d3f22 bump version and minor changes 2023-03-31 20:05:07 -04:00
Dave Horton 3fcfbd10a1 Merge pull request #17 from jambonz/fix/imterim_audio_cut
fix: use synthesized audio data directly from microsoft sdk
2023-03-31 20:03:38 -04:00
Quan HL df8acbed0e fix: audioData is getter 2023-04-01 06:58:55 +07:00
Quan HL 4296ed7256 fix: user synthesized audio data directly from microsoft sdk 2023-04-01 06:37:33 +07:00
Quan HL f4b271c7b3 fix: user synthesized audio data directly from microsoft sdk 2023-04-01 06:36:15 +07:00
Dave Horton d5c71de27d update to latest speech packages 2023-03-31 15:56:18 -04:00
Dave Horton 1ab7cb20e6 bump version 2023-03-24 14:47:35 -04:00
Dave Horton ba1052e629 Merge pull request #15 from jambonz/feature/get-tts-voices
add functions to retrieve voices for google and tts
2023-03-24 14:46:05 -04:00
Dave Horton 58270ad87f add functions to retrieve voices for google and tts 2023-03-24 14:43:51 -04:00
Dave Horton b172caee49 add support for nuance tts on-prem 2023-03-24 14:09:58 -04:00
Dave Horton 58374ca0fa Merge pull request #14 from jambonz/feature/nuance-onprem
add support for Nuance TTS on-prem (vs hosted)
2023-03-24 14:07:19 -04:00
Dave Horton 5a7e0d37f4 add support for Nuance TTS on-prem (vs hosted) 2023-03-24 14:05:18 -04:00
Dave Horton 6fa68bc712 more changes trying to get AWS V3 sdk properly configured 2023-03-21 08:20:29 -04:00
Dave Horton d914b26cac change to PollyClient 2023-03-21 07:52:06 -04:00
Dave Horton 7c7be6bbb1 bugfix: AWS polly 2023-03-20 15:33:00 -04:00
Dave Horton 46127bb763 bump version 2023-03-15 08:52:57 -04:00
Dave Horton 17305325ff Merge pull request #13 from jambonz/feat/aws-v3
fix: custom tts support multiple audio types
2023-03-15 08:51:25 -04:00
Quan HL 0e883cf82a fix: custom tts support multiple audio types 2023-03-15 13:43:35 +07:00
Dave Horton 2f3a766713 Merge pull request #12 from jambonz/#10
fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit
2023-03-09 19:45:28 -05:00
Quan HL b429645ec2 fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit 2023-03-10 06:41:22 +07:00
Quan HL 0becf33bab fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit 2023-03-10 06:41:07 +07:00
Quan HL 3d3875741c fix: JAMBONES_TTS_CACHE_DURATION_MINS in minute unit 2023-03-10 06:31:17 +07:00
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
16 changed files with 2297 additions and 2013 deletions
+3 -1
View File
@@ -8,6 +8,8 @@
},
"redis-auth": {
"host": "127.0.0.1",
"port": 3380
"port": 3380,
"username": "daveh",
"password": "foobarbazzle"
}
}
+16 -7
View File
@@ -1,15 +1,23 @@
const {noopLogger} = require('./lib/utils');
const promisify = require('@jambonz/promisify-redis');
const redis = promisify(require('redis'));
const Redis = require('ioredis');
module.exports = (opts, logger) => {
const {host = '127.0.0.1', port = 6379, tls = false} = opts;
logger = logger || noopLogger;
const connectionOpts = {...opts};
// Support legacy app
if (process.env.JAMBONES_REDIS_USERNAME && process.env.JAMBONES_REDIS_PASSWORD) {
if (Array.isArray(connectionOpts)) {
for (const o of opts) {
o.username = process.env.JAMBONES_REDIS_USERNAME;
o.password = process.env.JAMBONES_REDIS_PASSWORD;
}
} else {
connectionOpts.username = process.env.JAMBONES_REDIS_USERNAME;
connectionOpts.password = process.env.JAMBONES_REDIS_PASSWORD;
}
}
const url = process.env.JAMBONES_REDIS_USERNAME && process.env.JAMBONES_REDIS_PASSWORD ?
`${process.env.JAMBONES_REDIS_USERNAME}:${process.env.JAMBONES_REDIS_PASSWORD}@${host}:${port}` :
`${host}:${port}`;
const client = redis.createClient(tls ? `rediss://${url}` : `redis://${url}`);
const client = new Redis(connectionOpts);
['ready', 'connect', 'reconnecting', 'error', 'end', 'warning']
.forEach((event) => {
client.on(event, (...args) => {
@@ -23,6 +31,7 @@ module.exports = (opts, logger) => {
return {
client,
getTtsSize: require('./lib/get-tts-size').bind(null, client, logger),
purgeTtsCache: require('./lib/purge-tts-cache').bind(null, client, logger),
synthAudio: require('./lib/synth-audio').bind(null, client, logger),
getNuanceAccessToken: require('./lib/get-nuance-access-token').bind(null, client, logger),
+1 -1
View File
@@ -9,7 +9,7 @@ async function getIbmAccessToken(client, logger, apiKey) {
logger = logger || noopLogger;
try {
const key = makeIbmKey(apiKey);
const access_token = await client.getAsync(key);
const access_token = await client.get(key);
if (access_token) return {access_token, servedFromCache: true};
/* access token not found in cache, so fetch it from Ibm */
+1 -1
View File
@@ -9,7 +9,7 @@ async function getNuanceAccessToken(client, logger, clientId, secret, scope) {
logger = logger || noopLogger;
try {
const key = makeNuanceKey(clientId, secret, scope);
const access_token = await client.getAsync(key);
const access_token = await client.get(key);
if (access_token) return {access_token, servedFromCache: true};
/* access token not found in cache, so fetch it from Nuance */
+11
View File
@@ -0,0 +1,11 @@
async function getTtsSize(client, logger, pattern = null) {
let keys;
if (pattern) {
keys = await client.keys(pattern);
} else {
keys = await client.keys('tts:*');
}
return keys.length;
}
module.exports = getTtsSize;
+42 -6
View File
@@ -1,9 +1,11 @@
const assert = require('assert');
const {noopLogger, createNuanceClient} = require('./utils');
const {noopLogger, createNuanceClient, createKryptonClient} = require('./utils');
const getNuanceAccessToken = require('./get-nuance-access-token');
const {GetVoicesRequest, Voice} = require('../stubs/nuance/synthesizer_pb');
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const ttsGoogle = require('@google-cloud/text-to-speech');
const { PollyClient, DescribeVoicesCommand } = require('@aws-sdk/client-polly');
const getIbmVoices = async(client, logger, credentials) => {
const {tts_region, tts_api_key} = credentials;
@@ -21,15 +23,20 @@ const getIbmVoices = async(client, logger, credentials) => {
};
const getNuanceVoices = async(client, logger, credentials) => {
const {client_id: clientId, secret: secret} = credentials;
const {client_id: clientId, secret: secret, nuance_tts_uri} = credentials;
return new Promise(async(resolve, reject) => {
/* get a nuance access token */
let token, nuanceClient;
try {
const access_token = await getNuanceAccessToken(client, logger, clientId, secret, 'tts');
token = access_token.access_token;
nuanceClient = await createNuanceClient(token);
if (nuance_tts_uri) {
nuanceClient = await createKryptonClient(nuance_tts_uri);
}
else {
const access_token = await getNuanceAccessToken(client, logger, clientId, secret, 'tts');
token = access_token.access_token;
nuanceClient = await createNuanceClient(token);
}
} catch (err) {
logger.error({err}, 'getTtsVoices: error retrieving access token');
return reject(err);
@@ -75,6 +82,30 @@ const getNuanceVoices = async(client, logger, credentials) => {
});
};
const getGoogleVoices = async(_client, logger, credentials) => {
const client = new ttsGoogle.TextToSpeechClient({credentials});
return await client.listVoices();
};
const getAwsVoices = async(_client, logger, credentials) => {
try {
const {region, accessKeyId, secretAccessKey} = credentials;
const client = new PollyClient({
region,
credentials: {
accessKeyId,
secretAccessKey
}
});
const command = new DescribeVoicesCommand({LanguageCode: 'en-US'});
const response = await client.send(command);
return response;
} catch (err) {
logger.info({err}, 'testMicrosoftTts - failed to list voices for region ${region}');
throw err;
}
};
/**
* Synthesize speech to an mp3 file, and also cache the generated speech
* in redis (base64 format) for 24 hours so as to avoid unnecessarily paying
@@ -94,7 +125,7 @@ const getNuanceVoices = async(client, logger, credentials) => {
async function getTtsVoices(client, logger, {vendor, credentials}) {
logger = logger || noopLogger;
assert.ok(['nuance', 'ibm'].includes(vendor),
assert.ok(['nuance', 'ibm', 'google', 'aws', 'polly'].includes(vendor),
`getTtsVoices not supported for vendor ${vendor}`);
switch (vendor) {
@@ -102,6 +133,11 @@ async function getTtsVoices(client, logger, {vendor, credentials}) {
return getNuanceVoices(client, logger, credentials);
case 'ibm':
return getIbmVoices(client, logger, credentials);
case 'google':
return getGoogleVoices(client, logger, credentials);
case 'aws':
case 'polly':
return getAwsVoices(client, logger, credentials);
default:
break;
}
+11 -5
View File
@@ -11,25 +11,31 @@ 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;
try {
if (all) {
const keys = await client.keysAsync('tts:*');
purgedCount = await client.delAsync(keys);
const keys = await client.keys('tts:*');
purgedCount = await client.del(keys);
} else {
} else if (account_sid && !vendor && !language && !voice && !engine && !text) {
const keys = await client.keys(`tts:${account_sid}:*`);
purgedCount = await client.del(keys);
}
else {
const key = makeSynthKey({
account_sid,
vendor,
language: language || '',
voice: voice || deploymentId,
engine,
text,
});
purgedCount = await client.delAsync(key);
purgedCount = await client.del(key);
if (purgedCount === 0) error = 'Specified item not found';
}
+113 -47
View File
@@ -2,21 +2,19 @@ const assert = require('assert');
const fs = require('fs');
const bent = require('bent');
const ttsGoogle = require('@google-cloud/text-to-speech');
//const Polly = require('aws-sdk/clients/polly');
const { PollyClient, SynthesizeSpeechCommand } = require('@aws-sdk/client-polly');
const sdk = require('microsoft-cognitiveservices-speech-sdk');
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const {
AudioConfig,
ResultReason,
SpeechConfig,
SpeechSynthesizer,
CancellationDetails,
SpeechSynthesisOutputFormat
} = sdk;
const {makeSynthKey, createNuanceClient, noopLogger, createRivaClient} = require('./utils');
const {makeSynthKey, createNuanceClient, createKryptonClient, createRivaClient, noopLogger} = require('./utils');
const getNuanceAccessToken = require('./get-nuance-access-token');
const {
SynthesisRequest,
@@ -32,9 +30,27 @@ 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 || 4 * 60) * 60; // cache tts for 4 hours
const TMP_FOLDER = '/tmp';
const trimTrailingSilence = (buffer) => {
assert.ok(buffer instanceof Buffer, 'trimTrailingSilence - argument is not a Buffer');
let offset = buffer.length;
while (offset > 0) {
// Get 16-bit value from the buffer (read in reverse)
const value = buffer.readUInt16BE(offset - 2);
if (value !== 0) {
break;
}
offset -= 2;
}
// Trim the silence from the end
return offset === buffer.length ? buffer : buffer.subarray(0, offset);
};
/**
* Synthesize speech to an mp3 file, and also cache the generated speech
* in redis (base64 format) for 24 hours so as to avoid unnecessarily paying
@@ -52,7 +68,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;
@@ -75,13 +91,15 @@ async function synthAudio(client, logger, stats, {
}
else if ('nuance' === vendor) {
assert.ok(voice, 'synthAudio requires voice when nuance is used');
assert.ok(credentials.client_id, 'synthAudio requires client_id in credentials when nuance is used');
assert.ok(credentials.secret, 'synthAudio requires client_id in credentials when nuance is used');
if (!credentials.nuance_tts_uri) {
assert.ok(credentials.client_id, 'synthAudio requires client_id in credentials when nuance is used');
assert.ok(credentials.secret, 'synthAudio requires client_id in credentials when nuance is used');
}
}
else if ('nvidia' === vendor) {
assert.ok(voice, 'synthAudio requires voice when nvidia is used');
assert.ok(language, 'synthAudio requires language when nvidia is used');
assert.ok(credentials.riva_uri, 'synthAudio requires riva_uri in credentials when nuance is used');
assert.ok(credentials.riva_server_uri, 'synthAudio requires riva_server_uri in credentials when nvidia is used');
}
else if ('ibm' === vendor) {
assert.ok(voice, 'synthAudio requires voice when ibm is used');
@@ -95,8 +113,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,
@@ -104,14 +122,19 @@ async function synthAudio(client, logger, stats, {
text
});
let filePath;
if (['nuance', 'nvidia'].includes(vendor)) {
if (['nuance', 'nvidia'].includes(vendor) ||
(
process.env.JAMBONES_TTS_TRIM_SILENCE &&
['microsoft', 'azure'].includes(vendor)
)
) {
filePath = `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt || ''}`)}.r8`;
}
else filePath = `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt || ''}`)}.mp3`;
debug(`synth key is ${key}`);
let cached;
if (!disableTtsCache) {
cached = await client.getAsync(key);
cached = await client.get(key);
}
if (cached) {
// found in cache - extend the expiry and use it
@@ -119,7 +142,7 @@ async function synthAudio(client, logger, stats, {
servedFromCache = true;
stats.increment('tts.cache.requests', ['found:yes']);
audioBuffer = Buffer.from(cached, 'base64');
client.expireAsync(key, EXPIRES).catch((err) => logger.info(err, 'Error setting expires'));
client.expire(key, EXPIRES).catch((err) => logger.info(err, 'Error setting expires'));
}
if (!cached) {
// not found in cache - go get it from speech vendor and add to cache
@@ -155,7 +178,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}`);
@@ -167,10 +191,8 @@ async function synthAudio(client, logger, stats, {
debug(`tts rtt time for ${text.length} chars on ${vendorLabel}: ${rtt}`);
logger.info(`tts rtt time for ${text.length} chars on ${vendorLabel}: ${rtt}`);
client.setexAsync(key, EXPIRES, audioBuffer.toString('base64'))
client.setex(key, EXPIRES, audioBuffer.toString('base64'))
.catch((err) => logger.error(err, `error calling setex on key ${key}`));
if (['microsoft'].includes(vendor)) return {filePath, servedFromCache, rtt};
}
return new Promise((resolve, reject) => {
@@ -183,7 +205,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',
@@ -278,6 +307,7 @@ const synthMicrosoft = async(logger, {
}) => {
try {
const {api_key: apiKey, region, use_custom_tts, custom_tts_endpoint} = credentials;
const trimSilence = filePath.endsWith('.r8');
let content = text;
const speechConfig = SpeechConfig.fromSubscription(apiKey, region);
speechConfig.speechSynthesisLanguage = language;
@@ -291,9 +321,10 @@ const synthMicrosoft = async(logger, {
*/
if (!content.startsWith('<speak')) content = `<speak>${text}</speak>`;
}
speechConfig.speechSynthesisOutputFormat = SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3;
const config = AudioConfig.fromAudioFileOutput(filePath);
const synthesizer = new SpeechSynthesizer(speechConfig, config);
speechConfig.speechSynthesisOutputFormat = trimSilence ?
SpeechSynthesisOutputFormat.Raw8Khz16BitMonoPcm :
SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3;
const synthesizer = new SpeechSynthesizer(speechConfig);
if (content.startsWith('<speak>')) {
/* microsoft enforces some properties and uses voice xml element so if the user did not supply do it for them */
@@ -318,12 +349,11 @@ const synthMicrosoft = async(logger, {
reject(cancellation.errorDetails);
break;
case ResultReason.SynthesizingAudioCompleted:
stats.increment('tts.count', ['vendor:microsoft', 'accepted:yes']);
let buffer = Buffer.from(result.audioData);
if (trimSilence) buffer = trimTrailingSilence(buffer);
resolve(buffer);
synthesizer.close();
fs.readFile(filePath, (err, data) => {
if (err) return reject(err);
resolve(data);
});
stats.increment('tts.count', ['vendor:microsoft', 'accepted:yes']);
break;
default:
logger.info({result}, 'synthAudio: (Microsoft) unexpected result');
@@ -363,10 +393,16 @@ const synthWellSaid = async(logger, {credentials, stats, language, voice, gender
};
const synthNuance = async(client, logger, {credentials, stats, voice, model, text}) => {
/* get a nuance access token */
const {client_id, secret} = credentials;
const {access_token} = await getNuanceAccessToken(client, logger, client_id, secret, 'tts');
const nuanceClient = await createNuanceClient(access_token);
let nuanceClient;
const {client_id, secret, nuance_tts_uri} = credentials;
if (nuance_tts_uri) {
nuanceClient = await createKryptonClient(nuance_tts_uri);
}
else {
/* get a nuance access token */
const {access_token} = await getNuanceAccessToken(client, logger, client_id, secret, 'tts');
nuanceClient = await createNuanceClient(access_token);
}
const v = new Voice();
const p = new AudioParameters();
@@ -417,21 +453,25 @@ const synthNuance = async(client, logger, {credentials, stats, voice, model, tex
};
const synthNvidia = async(client, logger, {credentials, stats, language, voice, model, text}) => {
const {riva_uri} = credentials;
const rivaClient = await createRivaClient(riva_uri);
const request = new SynthesizeSpeechRequest();
request.setVoiceName(voice);
request.setLanguageCode(language);
request.setSampleRateHz(8000);
request.setEncoding(AudioEncoding.LINEAR_PCM);
request.setText(text);
const {riva_server_uri} = credentials;
let rivaClient, request;
try {
rivaClient = await createRivaClient(riva_server_uri);
request = new SynthesizeSpeechRequest();
request.setVoiceName(voice);
request.setLanguageCode(language);
request.setSampleRateHz(8000);
request.setEncoding(AudioEncoding.LINEAR_PCM);
request.setText(text);
} catch (err) {
logger.info({err}, 'error creating riva client');
return Promise.reject(err);
}
return new Promise((resolve, reject) => {
console.log(`language ${language} voice ${voice} model ${model} text ${text}`);
rivaClient.synthesize(request, (err, response) => {
if (err) {
console.error(err);
logger.info({err, voice, language}, 'error synthesizing speech using Nvidia');
return reject(err);
}
resolve(Buffer.from(response.getAudio()));
@@ -440,30 +480,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;
+8 -2
View File
@@ -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 = {
@@ -77,6 +77,11 @@ const getNuanceAccessToken = async(clientId, secret, scope = 'asr tts') => {
return json.access_token;
};
const createKryptonClient = async(uri) => {
const client = new SynthesizerClient(uri, grpc.credentials.createInsecure());
return client;
};
const createNuanceClient = async(access_token) => {
//if (nuanceClientMap.has(access_token)) return nuanceClientMap.get(access_token);
@@ -108,6 +113,7 @@ module.exports = {
makeIbmKey,
getNuanceAccessToken,
createNuanceClient,
createKryptonClient,
createRivaClient,
makeBasicAuthHeader,
NUANCE_AUTH_ENDPOINT,
+1758 -1913
View File
File diff suppressed because it is too large Load Diff
+10 -11
View File
@@ -1,6 +1,6 @@
{
"name": "@jambonz/speech-utils",
"version": "0.0.1",
"version": "0.0.17",
"description": "TTS-related speech utilities for jambonz",
"main": "index.js",
"author": "Dave Horton",
@@ -9,7 +9,7 @@
"test": "test"
},
"scripts": {
"test": "NODE_ENV=test JAMBONES_REDIS_USERNAME=daveh JAMBONES_REDIS_PASSWORD=foobarbazzle node test/ ",
"test": "NODE_ENV=test node test/ ",
"coverage": "nyc --reporter html --report-dir ./coverage npm run test",
"jslint": "eslint index.js lib",
"build": "./build_stubs.sh"
@@ -24,18 +24,17 @@
},
"homepage": "https://github.com/jambonz/speech-utils#readme",
"dependencies": {
"@aws-sdk/client-polly": "^3.276.0",
"@google-cloud/text-to-speech": "^4.2.0",
"@grpc/grpc-js": "^1.8.7",
"@jambonz/realtimedb-helpers": "^0.6.3",
"aws-sdk": "^2.1310.0",
"@aws-sdk/client-polly": "^3.359.0",
"@google-cloud/text-to-speech": "^4.2.1",
"@grpc/grpc-js": "^1.8.13",
"bent": "^7.3.12",
"debug": "^4.3.4",
"google-protobuf": "^3.21.2",
"ibm-watson": "^7.1.2",
"form-urlencoded": "^6.1.0",
"microsoft-cognitiveservices-speech-sdk": "^1.25.0",
"undici": "^5.19.1"
"google-protobuf": "^3.21.2",
"ibm-watson": "^8.0.0",
"microsoft-cognitiveservices-speech-sdk": "^1.26.0",
"ioredis": "^5.3.2",
"undici": "^5.21.0"
},
"devDependencies": {
"config": "^3.3.9",
+2 -2
View File
@@ -31,7 +31,7 @@ test('IBM - create access key', async(t) => {
//console.log({obj}, 'received access token from IBM - second request');
t.ok(obj.access_token && obj.servedFromCache, 'successfully received access token from cache');
await client.flushallAsync();
await client.flushall();
t.end();
}
catch (err) {
@@ -65,7 +65,7 @@ test('IBM - retrieve tts voices test', async(t) => {
t.ok(voices.length > 0 && voices[0].language,
`GetVoices: successfully retrieved ${voices.length} voices from IBM`);
await client.flushallAsync();
await client.flushall();
t.end();
+1 -2
View File
@@ -1,5 +1,4 @@
require('./docker_start');
require('./synth');
require('./nuance');
require('./ibm');
require('./list-voices');
require('./docker_stop');
+205
View File
@@ -0,0 +1,205 @@
const test = require('tape').test ;
const config = require('config');
const opts = config.get('redis');
const fs = require('fs');
const logger = require('pino')({level: 'error'});
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
const stats = {
increment: () => {},
histogram: () => {}
};
test('IBM - create access key', async(t) => {
const fn = require('..');
const {client, getIbmAccessToken} = fn(opts, logger);
if (!process.env.IBM_API_KEY ) {
t.pass('skipping IBM test since no IBM api_key provided');
t.end();
client.quit();
return;
}
try {
let obj = await getIbmAccessToken(process.env.IBM_API_KEY);
//console.log({obj}, 'received access token from IBM');
t.ok(obj.access_token && !obj.servedFromCache, 'successfull received access token from IBM');
obj = await getIbmAccessToken(process.env.IBM_API_KEY);
//console.log({obj}, 'received access token from IBM - second request');
t.ok(obj.access_token && obj.servedFromCache, 'successfully received access token from cache');
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('IBM - retrieve tts voices test', async(t) => {
const fn = require('..');
const {client, getTtsVoices} = fn(opts, logger);
if (!process.env.IBM_TTS_API_KEY || !process.env.IBM_TTS_REGION) {
t.pass('skipping IBM test since no IBM api_key and/or region provided');
t.end();
client.quit();
return;
}
try {
const opts = {
vendor: 'ibm',
credentials: {
tts_api_key: process.env.IBM_TTS_API_KEY,
tts_region: process.env.IBM_TTS_REGION
}
};
const obj = await getTtsVoices(opts);
const {voices} = obj.result;
//console.log(JSON.stringify(voices));
t.ok(voices.length > 0 && voices[0].language,
`GetVoices: successfully retrieved ${voices.length} voices from IBM`);
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('Nuance hosted tests', async(t) => {
const fn = require('..');
const {client, getTtsVoices} = fn(opts, logger);
if (!process.env.NUANCE_CLIENT_ID || !process.env.NUANCE_SECRET ) {
t.pass('skipping Nuance hosted test since no Nuance client_id and secret provided');
t.end();
client.quit();
return;
}
try {
const opts = {
vendor: 'nuance',
credentials: {
client_id: process.env.NUANCE_CLIENT_ID,
secret: process.env.NUANCE_SECRET
}
};
let voices = await getTtsVoices(opts);
t.ok(voices.length > 0 && voices[0].language,
`GetVoices: successfully retrieved ${voices.length} voices from Nuance`);
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('Nuance on-prem tests', async(t) => {
const fn = require('..');
const {client, getTtsVoices} = fn(opts, logger);
if (!process.env.NUANCE_TTS_URI ) {
t.pass('skipping Nuance on-prem test since no Nuance uri provided');
t.end();
client.quit();
return;
}
try {
const opts = {
vendor: 'nuance',
credentials: {
nuance_tts_uri: process.env.NUANCE_TTS_URI
}
};
let voices = await getTtsVoices(opts);
t.ok(voices.length > 0 && voices[0].language,
`GetVoices: successfully retrieved ${voices.length} voices from Nuance`);
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('Google tests', async(t) => {
const fn = require('..');
const {client, getTtsVoices} = fn(opts, logger);
if (!process.env.GCP_FILE && !process.env.GCP_JSON_KEY) {
t.pass('skipping google speech 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 credentials = JSON.parse(str);
const opts = {
vendor: 'google',
credentials
};
let result = await getTtsVoices(opts);
t.ok(result[0].voices.length > 0, `GetVoices: successfully retrieved ${result[0].voices.length} voices from Google`);
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('AWS tests', async(t) => {
const fn = require('..');
const {client, getTtsVoices} = fn(opts, logger);
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY || !process.env.AWS_REGION) {
t.pass('skipping AWS speech synth tests since AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, or AWS_REGION not provided');
return t.end();
}
try {
const opts = {
vendor: 'aws',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
}
};
let result = await getTtsVoices(opts);
t.ok(result?.Voices?.length > 0, `GetVoices: successfully retrieved ${result.Voices.length} voices from AWS`);
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
+36 -5
View File
@@ -12,12 +12,12 @@ const stats = {
histogram: () => {}
};
test('Nuance tests', async(t) => {
test('Nuance hosted tests', async(t) => {
const fn = require('..');
const {client, getTtsVoices} = fn(opts, logger);
if (!process.env.NUANCE_CLIENT_ID || !process.env.NUANCE_SECRET ) {
t.pass('skipping Nuance test since no Nuance client_id and secret provided');
t.pass('skipping Nuance hosted test since no Nuance client_id and secret provided');
t.end();
client.quit();
return;
@@ -31,12 +31,43 @@ test('Nuance tests', async(t) => {
}
};
let voices = await getTtsVoices(opts);
//console.log(`received ${voices.length} voices from Nuance`);
//console.log(JSON.stringify(voices));
t.ok(voices.length > 0 && voices[0].language,
`GetVoices: successfully retrieved ${voices.length} voices from Nuance`);
await client.flushallAsync();
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('Nuance on-prem tests', async(t) => {
const fn = require('..');
const {client, getTtsVoices} = fn(opts, logger);
if (!process.env.NUANCE_TTS_URI ) {
t.pass('skipping Nuance on-prem test since no Nuance uri provided');
t.end();
client.quit();
return;
}
try {
const opts = {
vendor: 'nuance',
credentials: {
nuance_tts_uri: process.env.NUANCE_TTS_URI
}
};
let voices = await getTtsVoices(opts);
t.ok(voices.length > 0 && voices[0].language,
`GetVoices: successfully retrieved ${voices.length} voices from Nuance`);
await client.flushall();
t.end();
+79 -10
View File
@@ -212,7 +212,7 @@ test('Azure custom voice speech synth tests', async(t) => {
client.quit();
});
test('Nuance speech synth tests', async(t) => {
test('Nuance hosted speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
@@ -251,6 +251,43 @@ test('Nuance speech synth tests', async(t) => {
client.quit();
});
test('Nuance on-prem speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
if (!process.env.NUANCE_TTS_URI) {
t.pass('skipping Nuance on prem speech synth tests since NUANCE_TTS_URI not provided');
return t.end();
}
try {
let opts = await synthAudio(stats, {
vendor: 'nuance',
credentials: {
nuance_tts_uri: process.env.NUANCE_TTS_URI
},
language: 'en-US',
voice: 'Evan',
text: 'This is a test of on-prem. This is only a test',
});
t.ok(!opts.servedFromCache, `successfully synthesized nuance audio to ${opts.filePath}`);
opts = await synthAudio(stats, {
vendor: 'nuance',
credentials: {
nuance_tts_uri: process.env.NUANCE_TTS_URI
},
language: 'en-US',
voice: 'Evan',
text: 'This is a test of on-prem. This is only a test',
});
t.ok(opts.servedFromCache, `successfully retrieved nuance audio from cache ${opts.filePath}`);
} catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('Nvidia speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
@@ -263,7 +300,7 @@ test('Nvidia speech synth tests', async(t) => {
let opts = await synthAudio(stats, {
vendor: 'nvidia',
credentials: {
riva_uri: process.env.RIVA_URI,
riva_server_uri: process.env.RIVA_URI,
},
language: 'en-US',
voice: 'English-US.Female-1',
@@ -274,7 +311,7 @@ test('Nvidia speech synth tests', async(t) => {
opts = await synthAudio(stats, {
vendor: 'nvidia',
credentials: {
riva_uri: process.env.RIVA_URI,
riva_server_uri: process.env.RIVA_URI,
},
language: 'en-US',
voice: 'English-US.Female-1',
@@ -348,7 +385,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');
@@ -377,18 +413,21 @@ test('Custom Vendor speech synth tests', async(t) => {
test('TTS Cache tests', async(t) => {
const fn = require('..');
const {purgeTtsCache, client} = fn(opts, logger);
const {purgeTtsCache, getTtsSize, client} = fn(opts, logger);
try {
// save some random tts keys to cache
const minRecords = 8;
for (const i in Array(minRecords).fill(0)) {
await client.setAsync(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}), i);
}
const count = await getTtsSize();
t.ok(count >= minRecords, 'getTtsSize worked.');
const {purgedCount} = await purgeTtsCache();
t.ok(purgedCount >= minRecords, `successfully purged at least ${minRecords} tts records from cache`);
const cached = (await client.keysAsync('tts:*')).length;
const cached = (await client.keys('tts:*')).length;
t.equal(cached, 0, `successfully purged all tts records from cache`);
} catch (err) {
@@ -399,11 +438,11 @@ test('TTS Cache tests', async(t) => {
try {
// save some random tts keys to cache
for (const i in Array(10).fill(0)) {
await client.setAsync(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}), i);
}
// save a specific key to tts cache
const opts = {vendor: 'aws', language: 'en-US', voice: 'MALE', engine: 'Engine', text: 'Hello World!'};
await client.setAsync(makeSynthKey(opts), opts.text);
await client.set(makeSynthKey(opts), opts.text);
const {purgedCount} = await purgeTtsCache({all: false, ...opts});
t.ok(purgedCount === 1, `successfully purged one specific tts record from cache`);
@@ -419,7 +458,7 @@ test('TTS Cache tests', async(t) => {
t.ok(error, `error returned when specified key was not found`);
// make sure other tts keys are still there
const cached = (await client.keysAsync('tts:*')).length;
const cached = (await client.keys('tts:*')).length;
t.ok(cached >= 1, `successfully kept all non-specified tts records in cache`);
} catch (err) {
@@ -427,5 +466,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.set(makeSynthKey({account_sid, vendor: i, language: i, voice: i, engine: i, text: i}), i);
}
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);
}
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.keys('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.keys('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();
});