Compare commits

..
99 Commits
Author SHA1 Message Date
Dave Horton ced1a0ef0d 0.0.42 2024-02-20 20:34:51 -05:00
Dave Horton 1609d0b205 Merge pull request #57 from jambonz/feat/whisper_tts_stream
support whisper streaming
2024-02-20 20:33:21 -05:00
Quan HL ef8ada2793 wip 2024-02-20 20:52:14 +07:00
Quan HL 444ad2522f rebase 2024-02-19 15:32:50 +07:00
Dave Horton 1caea60803 0.0.41 2024-02-12 21:06:48 -05:00
Dave Horton 97c3588cfd bug: JAMBONES_DISABLE_TTS_STREAMING is now the env 2024-02-12 21:06:38 -05:00
Dave Horton da3aa5aadb 0.0.40 2024-02-12 12:43:11 -05:00
Dave Horton 2fe89f132c change elevenlabs default to streaming, can be disabled by env 2024-02-12 12:41:21 -05:00
Dave Horton 4bca840ba2 0.0.39 2024-02-08 14:55:50 -05:00
Dave Horton f858ccb781 for tts streaming we need to replace CR and LF with spaces, as we can not send text with those characters to freeswitch currently 2024-02-08 14:55:39 -05:00
Quan HL 3cf9894b44 support whisper streaming 2024-02-05 11:49:38 +07:00
Dave Horton 436b15d648 0.0.38 2024-01-26 11:09:24 -05:00
Dave Horton c3b7ea4cd1 fix prev commit 2024-01-26 11:08:06 -05:00
Dave Horton 4b5430d61d 0.0.37 2024-01-26 09:54:51 -05:00
Dave Horton 9fc8fe8341 Merge pull request #56 from jambonz/feat/cache-streaming
add function to add an audio file generated externally to cache
2024-01-26 09:54:24 -05:00
Dave Horton b31e40b8a5 add function to add an audio file generated externally to cache 2024-01-26 09:52:32 -05:00
Dave Horton 62e1c69f69 0.0.36 2024-01-25 13:14:31 -05:00
Dave Horton bc68b672ac Merge pull request #55 from jambonz/tts-streaming-cache-attribute
add tts param to indicate caching
2024-01-25 13:14:00 -05:00
Dave Horton da1e279128 add tts param to indicate caching 2024-01-25 13:07:49 -05:00
Dave Horton 60bcfe07d7 0.0.35 2024-01-25 09:40:14 -05:00
Dave Horton d343c81088 Merge pull request #54 from jambonz/feat/fix_getVoice_aws
fix get aws voice should not be limit in en-US
2024-01-25 09:39:36 -05:00
Quan HL 706f6d5808 fix get aws voice should not be limit in en-US 2024-01-25 21:37:38 +07:00
Dave Horton 1b1a0f19d0 0.0.34 2024-01-22 15:18:21 -05:00
Dave Horton 119ac50f7f Merge pull request #53 from jambonz/elevenlabs-streaming
Elevenlabs streaming
2024-01-22 15:17:33 -05:00
Dave Horton cb479f04d5 add param to synthAuydio to indicate whether audio is being generated specifically for caching purposes 2024-01-22 08:10:00 -05:00
Dave Horton 7e21e0b666 changes to support elevenlabs tts streaming 2024-01-21 21:36:53 -05:00
Dave Horton dabdb5b584 if streaming env is set prepare to use streaming tts 2024-01-20 14:15:13 -05:00
Dave Horton f36ba027d0 0.0.33 2023-12-25 22:13:29 -05:00
Dave Horton 08aae32975 Merge pull request #51 from jambonz/feat/deepgram
support deepgram
2023-12-25 22:10:50 -05:00
Quan HL 4cfc730d92 support deepgram 2023-12-26 09:26:31 +07:00
Dave Horton 4ef8538bcd 0.0.32 2023-12-18 10:21:12 -05:00
Dave Horton 16a746398f add CI badge to README 2023-12-06 10:06:43 -05:00
Dave Horton 4a75f353ee Merge pull request #35 from jambonz/feature/azure-proxy-support
add support for SetProxy when using azure tts
2023-12-06 09:57:11 -05:00
Dave Horton bbff7963fd make test explicit 2023-12-06 09:54:19 -05:00
Dave Horton 7b1d226403 test proxy using azure 2023-12-06 09:54:16 -05:00
Dave Horton 95c29ce105 squid config file so we can test azure proxy setting 2023-12-06 09:53:35 -05:00
Dave Horton a46ae01d9c add support for JAMBONES_HTTP_PROXY_IP and JAMBONES_HTTP_PROXY_PORT for azure tts 2023-12-06 09:53:35 -05:00
Dave Horton 562dd0ac79 0.0.31 2023-12-05 20:36:43 -05:00
Dave Horton a0612bd1f9 Merge pull request #50 from jambonz/fix/reuse_redis
cannot assign redis-client as createHash and retrieveHash
2023-12-03 19:58:58 -05:00
Quan HL 2dcbf2b0f1 cannot assign redis-client as createHash and retrieveHash 2023-12-04 06:17:02 +07:00
Dave Horton 048b7e871e 0.0.30 2023-11-30 16:11:34 -05:00
Dave Horton 4cae96eacb rename aws token from sessionToken to securityToken for consistency with AWS docs 2023-11-30 16:11:23 -05:00
Dave Horton 1e57d00b55 0.0.29 2023-11-30 08:56:56 -05:00
Dave Horton 4ead5ee417 Merge pull request #47 from jambonz/feat/update_elevenlabs
support elevenlabs options
2023-11-30 08:53:19 -05:00
Quan HL a517f37473 wip 2023-11-30 16:37:44 +07:00
Quan HL 3ac5a98e3a get elevenlabs options from syntheizier.options 2023-11-30 15:56:22 +07:00
Quan HL 689fab6857 get elevenlabs options from syntheizier.options 2023-11-30 15:48:26 +07:00
Quan HL be3c484527 support elevenlabs options 2023-11-30 13:29:51 +07:00
Dave Horton 9827f7405d 0.0.28 2023-11-29 14:17:58 -05:00
Dave Horton 906a6b52b8 fix issue with conflicting EC2 permissions calling AWS STS 2023-11-29 14:17:46 -05:00
Dave Horton 1f0b9ff539 0.0.27 2023-11-27 09:45:39 -05:00
Dave Horton b6f8368357 Merge pull request #46 from jambonz/feat/aws-sts-auth-token
Feat/aws sts auth token
2023-11-27 09:44:38 -05:00
Dave Horton 6bc487b3d1 logging 2023-11-27 09:37:49 -05:00
Dave Horton 9699abe0a3 dowgrade azure to 1.32.0 per: https://github.com/microsoft/cognitive-services-speech-sdk-js/issues/752 2023-11-27 09:32:03 -05:00
Dave Horton 5e1e7b17c6 remove console logging in tests 2023-11-27 09:27:18 -05:00
Dave Horton 1f52cd4f08 added function to get an AWS security token using STS 2023-11-27 09:26:41 -05:00
Dave Horton 7df4e2f4c7 0.0.26 2023-11-14 08:48:14 -05:00
Dave Horton e99f7c5087 Merge pull request #43 from jambonz/feat/realtimedb
use realtimedb-helper for initiate redis connection
2023-11-10 07:49:28 -05:00
Quan HL c7d981c23a wip 2023-11-10 10:37:22 +07:00
Quan HL 95c54b9b12 use realtimedb-helper for initiate redis connection 2023-11-10 10:34:50 +07:00
Dave Horton 48192aeba1 Merge pull request #42 from jambonz/gh-actions
update gihub actions to test openai and elevenlabs
2023-11-09 08:44:14 -05:00
Dave Horton 8f931cd8a5 update gihub actions to test openai and elevenlabs 2023-11-09 08:42:32 -05:00
Dave Horton 144baafe94 0.0.25 2023-11-09 08:32:07 -05:00
Dave Horton ed3e513419 fix google speech test 2023-11-09 08:31:59 -05:00
Dave Horton 8d93fdc42a Merge pull request #41 from jambonz/feat/openai
support whisper tts
2023-11-09 08:30:24 -05:00
Quan HL ea523a7a1d wip 2023-11-09 12:59:20 +07:00
Quan HL 750ed97312 update review 2023-11-09 09:19:47 +07:00
Quan HL 73baa81177 update review 2023-11-09 09:15:54 +07:00
Quan HL f86234a769 support openai 2023-11-09 07:10:07 +07:00
Dave Horton d564f24e6c 0.0.24 2023-10-30 19:54:56 -04:00
Dave Horton 464d8462d9 Merge pull request #39 from jambonz/feat/google_custom_voice_01
fix google custom voice
2023-10-30 19:54:44 -04:00
Quan HL 6853f0e342 fix google custom voice 2023-10-31 06:24:52 +07:00
Dave Horton 507045dcba 0.0.23 2023-10-29 22:07:02 -04:00
Dave Horton 08758bbbff Merge pull request #38 from jambonz/feat/google_custom_voice
feat support google custom voice
2023-10-29 22:06:41 -04:00
Hoan Luu Huu a3aa1169b8 feat support google custom voice 2023-10-30 01:55:50 +00:00
Hoan Luu Huu 7cae19a4e5 feat support google custom voice 2023-10-30 01:54:02 +00:00
Hoan Luu Huu 8c4d5a7cee feat support google custom voice 2023-10-30 01:52:48 +00:00
Dave Horton eb2c39072b 0.0.22 2023-10-14 13:18:08 +02:00
Dave Horton e5932ffc18 Merge pull request #34 from jambonz/feat/elevenlabs
add elevenlabs
2023-10-14 07:17:14 -04:00
Quan HL 0a98c6a376 fix review comment 2023-10-14 18:13:04 +07:00
Quan HL ea153e9833 add elevenlabs 2023-10-12 14:22:15 +07:00
Dave Horton b5daeff047 0.0.21 2023-09-08 07:58:11 -04:00
Dave Horton da02926c9a Merge pull request #31 from jambonz/fix/onprem-azure
fix raw audio downloaded from onprem azure
2023-09-08 07:57:36 -04:00
Quan HL da3cdbb7aa fix raw audio downloaded from onprem azure 2023-09-08 15:42:57 +07:00
Dave Horton 625f147137 0.0.20 2023-08-30 21:08:52 -04:00
Dave Horton 2e5687978e 0.0.19 2023-08-30 21:08:41 -04:00
Dave Horton 897481d34c Merge pull request #29 from jambonz/feat/azure_fromhost
support self hosted microsoft
2023-08-30 21:07:44 -04:00
Quan HL bd5282e681 fix 2023-08-28 20:05:55 +07:00
Quan HL 95a1384f02 wip 2023-08-25 15:57:43 +07:00
Quan HL 35deeecf70 wip 2023-08-25 15:57:23 +07:00
Quan HL 9d2ac3273f wip 2023-08-25 13:53:09 +07:00
Quan HL b1049aad7f wip 2023-08-25 13:49:04 +07:00
Quan HL 40f51e7509 wip 2023-08-11 18:25:57 +07:00
Quan HL a0e2fe167c wip 2023-08-11 16:04:11 +07:00
Quan HL 1fa853faa3 wip 2023-08-11 13:30:11 +07:00
Quan HL 95e8d942b8 fix jslint 2023-08-09 18:00:17 +07:00
Quan HL 7a91876cd7 support self hosted microsoft 2023-08-09 17:56:38 +07:00
Dave Horton d07344ba3b Merge pull request #28 from jambonz/revert/ssml-silence-trim
revert change to _not_ trim silence when azure ssml is used
2023-07-25 12:34:22 -04:00
Dave Horton 44d8af2a96 revert change to _not_ trim silence when azure ssml is used 2023-07-25 11:08:06 -04:00
14 changed files with 4367 additions and 3657 deletions
+10 -1
View File
@@ -14,6 +14,9 @@ jobs:
node-version: lts/*
- run: npm install
- run: npm run jslint
- run: sudo apt update && sudo apt install -y squid
- run: sudo cp test/squid.conf /etc/squid/squid.conf
- run: sudo systemctl start squid
- run: npm test
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
@@ -24,4 +27,10 @@ jobs:
IBM_TTS_API_KEY: ${{ secrets.IBM_TTS_API_KEY }}
IBM_TTS_REGION: ${{ secrets.IBM_TTS_REGION }}
MICROSOFT_API_KEY: ${{ secrets.MICROSOFT_API_KEY }}
MICROSOFT_REGION: ${{ secrets.MICROSOFT_REGION }}
MICROSOFT_REGION: ${{ secrets.MICROSOFT_REGION }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ELEVENLABS_API_KEY: ${{ secrets.ELEVENLABS_API_KEY }}
ELEVENLABS_VOICE_ID: ${{ secrets.ELEVENLABS_VOICE_ID }}
ELEVENLABS_MODEL_ID: ${{ secrets.ELEVENLABS_MODEL_ID }}
JAMBONES_HTTP_PROXY_IP: 127.0.0.1
JAMBONES_HTTP_PROXY_PORT: 3128
+3 -2
View File
@@ -1,2 +1,3 @@
# speech-utils
TTS-related speech utilities for jambonz
# speech-utils ![CI](https://github.com/jambonz/speech-utils/workflows/CI/badge.svg)
TTS-related speech utilities for jambonz.
+7 -26
View File
@@ -1,41 +1,22 @@
const {noopLogger} = require('./lib/utils');
const Redis = require('ioredis');
module.exports = (opts, logger) => {
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 client = new Redis(connectionOpts);
['ready', 'connect', 'reconnecting', 'error', 'end', 'warning']
.forEach((event) => {
client.on(event, (...args) => {
if ('error' === event) {
if (process.env.NODE_ENV === 'test' && args[0]?.code === 'ECONNREFUSED') return;
logger.error({...args}, '@jambonz/realtimedb-helpers - redis error');
}
else logger.debug({args}, `redis event ${event}`);
});
});
const {
client,
createHash,
retrieveHash
} = require('@jambonz/realtimedb-helpers')(opts, logger);
return {
client,
getTtsSize: require('./lib/get-tts-size').bind(null, client, logger),
purgeTtsCache: require('./lib/purge-tts-cache').bind(null, client, logger),
addFileToCache: require('./lib/add-file-to-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),
getIbmAccessToken: require('./lib/get-ibm-access-token').bind(null, client, logger),
getAwsAuthToken: require('./lib/get-aws-sts-token').bind(null, logger, createHash, retrieveHash),
getTtsVoices: require('./lib/get-tts-voices').bind(null, client, logger),
};
};
+30
View File
@@ -0,0 +1,30 @@
const fs = require('fs/promises');
const {noopLogger, makeSynthKey} = require('./utils');
const EXPIRES = (process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 4 * 60) * 60; // cache tts for 4 hours
async function addFileToCache(client, logger, path,
{account_sid, vendor, language, voice, deploymentId, engine, text}) {
let key;
logger = logger || noopLogger;
try {
key = makeSynthKey({
account_sid,
vendor,
language: language || '',
voice: voice || deploymentId,
engine,
text,
});
const audioBuffer = await fs.readFile(path);
await client.setex(key, EXPIRES, audioBuffer.toString('base64'));
} catch (err) {
logger.error(err, 'addFileToCache: Error');
return;
}
logger.debug(`addFileToCache: added ${path} to cache with key ${key}`);
return key;
}
module.exports = addFileToCache;
+45
View File
@@ -0,0 +1,45 @@
const { STSClient, GetSessionTokenCommand } = require('@aws-sdk/client-sts');
const {makeAwsKey, noopLogger} = require('./utils');
const debug = require('debug')('jambonz:speech-utils');
const EXPIRY = 3600;
async function getAwsAuthToken(
logger,
createHash, retrieveHash,
awsAccessKeyId, awsSecretAccessKey, awsRegion) {
logger = logger || noopLogger;
try {
const key = makeAwsKey(awsAccessKeyId);
const obj = await retrieveHash(key);
if (obj) return {...obj, servedFromCache: true};
/* access token not found in cache, so generate it using STS */
const stsClient = new STSClient({
region: awsRegion,
credentials: {
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
}
});
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
const data = await stsClient.send(command);
const credentials = {
accessKeyId: data.Credentials.AccessKeyId,
secretAccessKey: data.Credentials.SecretAccessKey,
securityToken: data.Credentials.SessionToken
};
/* expire 10 minutes before the hour, so we don't lose the use of it during a call */
createHash(key, credentials, EXPIRY - 600)
.catch((err) => logger.error(err, `Error saving hash for key ${key}`));
return {...credentials, servedFromCache: false};
} catch (err) {
debug(err, 'getAwsAuthToken: Error retrieving AWS auth token');
logger.error(err, 'getAwsAuthToken: Error retrieving AWS auth token');
throw err;
}
}
module.exports = getAwsAuthToken;
+1 -1
View File
@@ -97,7 +97,7 @@ const getAwsVoices = async(_client, logger, credentials) => {
secretAccessKey
}
});
const command = new DescribeVoicesCommand({LanguageCode: 'en-US'});
const command = new DescribeVoicesCommand({});
const response = await client.send(command);
return response;
} catch (err) {
+217 -9
View File
@@ -14,7 +14,13 @@ const {
CancellationDetails,
SpeechSynthesisOutputFormat
} = sdk;
const {makeSynthKey, createNuanceClient, createKryptonClient, createRivaClient, noopLogger} = require('./utils');
const {
makeSynthKey,
createNuanceClient,
createKryptonClient,
createRivaClient,
noopLogger
} = require('./utils');
const getNuanceAccessToken = require('./get-nuance-access-token');
const {
SynthesisRequest,
@@ -32,6 +38,7 @@ const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
const debug = require('debug')('jambonz:realtimedb-helpers');
const EXPIRES = (process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 4 * 60) * 60; // cache tts for 4 hours
const TMP_FOLDER = '/tmp';
const OpenAI = require('openai');
const trimTrailingSilence = (buffer) => {
@@ -69,14 +76,16 @@ const trimTrailingSilence = (buffer) => {
* the synthesized audio, and a variable indicating whether it was served from cache
*/
async function synthAudio(client, logger, stats, { account_sid,
vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId, disableTtsCache
vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId,
disableTtsCache, renderForCaching, options
}) {
let audioBuffer;
let servedFromCache = false;
let rtt;
logger = logger || noopLogger;
assert.ok(['google', 'aws', 'polly', 'microsoft', 'wellsaid', 'nuance', 'nvidia', 'ibm'].includes(vendor) ||
assert.ok(['google', 'aws', 'polly', 'microsoft',
'wellsaid', 'nuance', 'nvidia', 'ibm', 'elevenlabs', 'whisper', 'deepgram'].includes(vendor) ||
vendor.startsWith('custom'),
`synthAudio supported vendors are google, aws, microsoft, nuance, nvidia and wellsaid, not ${vendor}`);
if ('google' === vendor) {
@@ -110,6 +119,14 @@ async function synthAudio(client, logger, stats, { account_sid,
language = 'en-US'; // WellSaid only supports English atm
assert.ok(voice, 'synthAudio requires voice when wellsaid is used');
assert.ok(!text.startsWith('<speak'), 'wellsaid does not support SSML tags');
} else if ('elevenlabs' === vendor) {
assert.ok(voice, 'synthAudio requires voice when elevenlabs is used');
assert.ok(credentials.api_key, 'synthAudio requires api_key when elevenlabs is used');
assert.ok(credentials.model_id, 'synthAudio requires model_id when elevenlabs is used');
} else if ('whisper' === vendor) {
assert.ok(voice, 'synthAudio requires voice when whisper is used');
assert.ok(credentials.model_id, 'synthAudio requires model when whisper is used');
assert.ok(credentials.api_key, 'synthAudio requires api_key when whisper is used');
} else if (vendor.startsWith('custom')) {
assert.ok(credentials.custom_tts_url, `synthAudio requires custom_tts_url in credentials when ${vendor} is used`);
}
@@ -125,8 +142,11 @@ async function synthAudio(client, logger, stats, { account_sid,
if (['nuance', 'nvidia'].includes(vendor) ||
(
process.env.JAMBONES_TTS_TRIM_SILENCE &&
['microsoft', 'azure'].includes(vendor) &&
!text.startsWith('<speak')
['microsoft', 'azure'].includes(vendor)
) ||
(
!process.env.JAMBONES_DISABLE_TTS_STREAMING &&
vendor === 'elevenlabs'
)
) {
filePath = `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt || ''}`)}.r8`;
@@ -178,6 +198,27 @@ async function synthAudio(client, logger, stats, { account_sid,
case 'wellsaid':
audioBuffer = await synthWellSaid(logger, {credentials, stats, language, voice, text, filePath});
break;
case 'elevenlabs':
audioBuffer = await synthElevenlabs(logger, {
credentials, options, stats, language, voice, text, renderForCaching, filePath
});
if (typeof audioBuffer === 'object' && audioBuffer.filePath) {
return audioBuffer;
}
else {
audioBuffer = await synthElevenlabs(logger, {credentials, options, stats, language, voice, text, filePath});
}
break;
case 'whisper':
audioBuffer = await synthWhisper(logger, {credentials, stats, voice, text, renderForCaching});
if (typeof audioBuffer === 'object' && audioBuffer.filePath) {
return audioBuffer;
}
audioBuffer = await synthWhisper(logger, {credentials, stats, voice, text});
break;
case 'deepgram':
audioBuffer = await synthDeepgram(logger, {credentials, stats, model, text});
break;
case vendor.startsWith('custom') ? vendor : 'cant_match_value':
({ audioBuffer, filePath } = await synthCustomVendor(logger,
{credentials, stats, language, voice, text, filePath}));
@@ -248,7 +289,8 @@ const synthGoogle = async(logger, {credentials, stats, language, voice, gender,
const client = new ttsGoogle.TextToSpeechClient(credentials);
const opts = {
voice: {
name: voice,
...(typeof voice === 'string' && {name: voice}),
...(typeof voice === 'object' && {customVoice: voice}),
languageCode: language,
ssmlGender: gender || 'SSML_VOICE_GENDER_UNSPECIFIED'
},
@@ -298,6 +340,48 @@ const synthIbm = async(logger, {credentials, stats, voice, text}) => {
}
};
async function _synthOnPremMicrosoft(logger, {
credentials,
stats,
language,
voice,
text,
filePath
}) {
const {use_custom_tts, custom_tts_endpoint_url} = credentials;
let content = text;
if (use_custom_tts && !content.startsWith('<speak')) {
/**
* Note: it seems that to use custom voice ssml is required with the voice attribute
* Otherwise sending plain text we get "Voice does not match"
*/
content = `<speak>${text}</speak>`;
}
if (content.startsWith('<speak>')) {
/* microsoft enforces some properties and uses voice xml element so if the user did not supply do it for them */
const words = content.slice(7, -8).trim().replace(/(\r\n|\n|\r)/gm, ' ');
// eslint-disable-next-line max-len
content = `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="${language}"><voice name="${voice}">${words}</voice></speak>`;
logger.info({content}, 'synthMicrosoft');
}
try {
const trimSilence = filePath.endsWith('.r8');
const post = bent('POST', 'buffer', {
'X-Microsoft-OutputFormat': trimSilence ? 'raw-8khz-16bit-mono-pcm' : 'audio-16khz-32kbitrate-mono-mp3',
'Content-Type': 'application/ssml+xml',
'User-Agent': 'Jambonz'
});
const mp3 = await post(custom_tts_endpoint_url, content);
return mp3;
} catch (err) {
logger.info({err}, '_synthMicrosoftByHttp returned error');
throw err;
}
}
const synthMicrosoft = async(logger, {
credentials,
stats,
@@ -307,7 +391,17 @@ const synthMicrosoft = async(logger, {
filePath
}) => {
try {
const {api_key: apiKey, region, use_custom_tts, custom_tts_endpoint} = credentials;
const {api_key: apiKey, region, use_custom_tts, custom_tts_endpoint, custom_tts_endpoint_url} = credentials;
if (use_custom_tts && custom_tts_endpoint_url) {
return await _synthOnPremMicrosoft(logger, {
credentials,
stats,
language,
voice,
text,
filePath
});
}
const trimSilence = filePath.endsWith('.r8');
let content = text;
const speechConfig = SpeechConfig.fromSubscription(apiKey, region);
@@ -315,16 +409,23 @@ const synthMicrosoft = async(logger, {
speechConfig.speechSynthesisVoiceName = voice;
if (use_custom_tts && custom_tts_endpoint) {
speechConfig.endpointId = custom_tts_endpoint;
}
if (use_custom_tts && !content.startsWith('<speak')) {
/**
* Note: it seems that to use custom voice ssml is required with the voice attribute
* Otherwise sending plain text we get "Voice does not match"
*/
if (!content.startsWith('<speak')) content = `<speak>${text}</speak>`;
content = `<speak>${text}</speak>`;
}
speechConfig.speechSynthesisOutputFormat = trimSilence ?
SpeechSynthesisOutputFormat.Raw8Khz16BitMonoPcm :
SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3;
if (process.env.JAMBONES_HTTP_PROXY_IP && process.env.JAMBONES_HTTP_PROXY_PORT) {
logger.debug(
`synthMicrosoft: using proxy ${process.env.JAMBONES_HTTP_PROXY_IP}:${process.env.JAMBONES_HTTP_PROXY_PORT}`);
speechConfig.setProxy(process.env.JAMBONES_HTTP_PROXY_IP, process.env.JAMBONES_HTTP_PROXY_PORT);
}
const synthesizer = new SpeechSynthesizer(speechConfig);
if (content.startsWith('<speak>')) {
@@ -510,6 +611,113 @@ const synthCustomVendor = async(logger, {credentials, stats, language, voice, te
}
};
const synthElevenlabs = async(logger, {credentials, options, stats, language, voice, text, renderForCaching}) => {
const {api_key, model_id, options: credOpts} = credentials;
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
/* default to using the streaming interface, unless disabled by env var OR we want just a cache file */
if (!process.env.JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching) {
let params = '';
params += `{api_key=${api_key}`;
params += `,model_id=${model_id}`;
params += `,optimize_streaming_latency=${opts.optimize_streaming_latency || 2}`;
params += ',write_cache_file=1';
if (opts.voice_settings?.similarity_boost) params += `,similarity_boost=${opts.voice_settings.similarity_boost}`;
if (opts.voice_settings?.stability) params += `,stability=${opts.voice_settings.stability}`;
if (opts.voice_settings?.style) params += `,style=${opts.voice_settings.style}`;
if (opts.voice_settings?.use_speaker_boost === false) params += ',use_speaker_boost=false';
params += '}';
return {
filePath: `say:${params}${text.replace(/\n/g, ' ').replace(/\r/g, ' ')}`,
servedFromCache: false,
rtt: 0
};
}
const optimize_streaming_latency = opts.optimize_streaming_latency ?
`?optimize_streaming_latency=${opts.optimize_streaming_latency}` : '';
try {
const post = bent('https://api.elevenlabs.io', 'POST', 'buffer', {
'xi-api-key': api_key,
'Accept': 'audio/mpeg',
'Content-Type': 'application/json'
});
const mp3 = await post(`/v1/text-to-speech/${voice}${optimize_streaming_latency}`, {
text,
model_id,
voice_settings: {
stability: 0.5,
similarity_boost: 0.5
},
...opts
});
return mp3;
} catch (err) {
logger.info({err}, 'synth Elevenlabs returned error');
stats.increment('tts.count', ['vendor:elevenlabs', 'accepted:no']);
throw err;
}
};
const synthWhisper = async(logger, {credentials, stats, voice, text, renderForCaching}) => {
const {api_key, model_id, baseURL, timeout, speed} = credentials;
/* if the env is set to stream then bag out, unless we are specifically rendering to generate a cache file */
if (!process.env.JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching) {
let params = '';
params += `{api_key=${api_key}`;
params += `,model_id=${model_id}`;
params += `,voice=${voice}`;
params += ',write_cache_file=1';
if (speed) params += `,speed=${speed}`;
params += '}';
return {
filePath: `say:${params}${text.replace(/\n/g, ' ')}`,
servedFromCache: false,
rtt: 0
};
}
try {
const openai = new OpenAI.OpenAI({
apiKey: api_key,
timeout: timeout || 5000,
...(baseURL && {baseURL})
});
const mp3 = await openai.audio.speech.create({
model: model_id,
voice,
input: text,
response_format: 'mp3'
});
return Buffer.from(await mp3.arrayBuffer());
} catch (err) {
logger.info({err}, 'synth whisper returned error');
stats.increment('tts.count', ['vendor:openai', 'accepted:no']);
throw err;
}
};
const synthDeepgram = async(logger, {credentials, stats, model, text}) => {
const {api_key} = credentials;
try {
const post = bent('https://api.beta.deepgram.com', 'POST', 'buffer', {
'Authorization': `Token ${api_key}`,
'Accept': 'audio/mpeg',
'Content-Type': 'application/json'
});
const mp3 = await post(`/v1/speak?model=${model}`, {
text
});
return mp3;
} catch (err) {
logger.info({err}, 'synth Deepgram returned error');
stats.increment('tts.count', ['vendor:deepgram', 'accepted:no']);
throw err;
}
};
const getFileExtFromMime = (mime) => {
switch (mime) {
case 'audio/wav':
+7 -1
View File
@@ -43,6 +43,12 @@ function makeIbmKey(apiKey) {
return `ibm:${hash.digest('hex')}`;
}
function makeAwsKey(awsAccessKeyId) {
const hash = crypto.createHash('sha1');
hash.update(awsAccessKeyId);
return `aws:${hash.digest('hex')}`;
}
function makeNuanceKey(clientId, secret, scope) {
const hash = crypto.createHash('sha1');
hash.update(`${clientId}:${secret}:${scope}`);
@@ -106,11 +112,11 @@ const createRivaClient = async(rivaUri) => {
return client;
};
module.exports = {
makeSynthKey,
makeNuanceKey,
makeIbmKey,
makeAwsKey,
getNuanceAccessToken,
createNuanceClient,
createKryptonClient,
+3762 -3600
View File
File diff suppressed because it is too large Load Diff
+14 -12
View File
@@ -1,6 +1,6 @@
{
"name": "@jambonz/speech-utils",
"version": "0.0.18",
"version": "0.0.42",
"description": "TTS-related speech utilities for jambonz",
"main": "index.js",
"author": "Dave Horton",
@@ -24,24 +24,26 @@
},
"homepage": "https://github.com/jambonz/speech-utils#readme",
"dependencies": {
"@aws-sdk/client-polly": "^3.359.0",
"@google-cloud/text-to-speech": "^4.2.1",
"@grpc/grpc-js": "^1.8.13",
"@aws-sdk/client-polly": "^3.496.0",
"@aws-sdk/client-sts": "^3.496.0",
"@google-cloud/text-to-speech": "^5.0.2",
"@grpc/grpc-js": "^1.9.14",
"@jambonz/realtimedb-helpers": "^0.8.7",
"bent": "^7.3.12",
"debug": "^4.3.4",
"form-urlencoded": "^6.1.0",
"form-urlencoded": "^6.1.4",
"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"
"microsoft-cognitiveservices-speech-sdk": "1.34.0",
"openai": "^4.25.0",
"undici": "^6.4.0"
},
"devDependencies": {
"config": "^3.3.9",
"eslint": "^8.33.0",
"config": "^3.3.10",
"eslint": "^8.56.0",
"eslint-plugin-promise": "^6.1.1",
"nyc": "^15.1.0",
"pino": "^7.2.0",
"tape": "^5.1.1"
"pino": "^8.17.0",
"tape": "^5.7.3"
}
}
+40
View File
@@ -0,0 +1,40 @@
const test = require('tape').test ;
const config = require('config');
const opts = config.get('redis');
const logger = require('pino')({level: 'error'});
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
test('AWS - create and cache auth token', async(t) => {
const fn = require('..');
const {client, getAwsAuthToken} = fn(opts, logger);
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY || !process.env.AWS_REGION) {
t.pass('skipping AWS auth token tests since no AWS credentials provided');
t.end();
client.quit();
return;
}
try {
let obj = await getAwsAuthToken(process.env.AWS_ACCESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY, process.env.AWS_REGION);
//console.log({obj}, 'received auth token from AWS');
t.ok(obj.securityToken && !obj.servedFromCache, 'successfullY generated auth token from AWS');
await sleep(250);
obj = await getAwsAuthToken(process.env.AWS_ACCESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY, process.env.AWS_REGION);
//console.log({obj}, 'received auth token from AWS - second request');
t.ok(obj.securityToken && obj.servedFromCache, 'successfully received access token from cache');
await client.flushall();
t.end();
}
catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
+3
View File
@@ -1,4 +1,7 @@
require('./docker_start');
require('./synth');
require('./list-voices');
require('./aws');
require('./ibm');
require('./nuance');
require('./docker_stop');
+85
View File
@@ -0,0 +1,85 @@
#
# Recommended minimum configuration:
#
# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 0.0.0.1-0.255.255.255 # RFC 1122 "this" network (LAN)
acl localnet src 10.0.0.0/8 # RFC 1918 local private network (LAN)
acl localnet src 100.64.0.0/10 # RFC 6598 shared address space (CGN)
acl localnet src 169.254.0.0/16 # RFC 3927 link-local (directly plugged) machines
acl localnet src 172.16.0.0/12 # RFC 1918 local private network (LAN)
acl localnet src 192.168.0.0/16 # RFC 1918 local private network (LAN)
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports
# Deny CONNECT to other than secure SSL ports
http_access allow CONNECT !SSL_ports
# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager
# This default configuration only allows localhost requests because a more
# permissive Squid installation could introduce new attack vectors into the
# network by proxying external TCP connections to unprotected services.
http_access allow localhost
# The two deny rules below are unnecessary in this default configuration
# because they are followed by a "deny all" rule. However, they may become
# critically important when you start allowing external requests below them.
# Protect web applications running on the same server as Squid. They often
# assume that only local users can access them at "localhost" ports.
http_access deny to_localhost
# Protect cloud servers that provide local users with sensitive info about
# their server via certain well-known link-local (a.k.a. APIPA) addresses.
http_access deny to_linklocal
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
# For example, to allow access from your local networks, you may uncomment the
# following rule (and/or add rules that match your definition of "local"):
# http_access allow localnet
# And finally deny all other access to this proxy
http_access deny all
# Squid normally listens to port 3128
http_port 3128
# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /usr/local/var/cache/squid 100 16 256
# Leave coredumps in the first cache dir
coredump_dir /usr/local/var/cache/squid
#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320
+143 -5
View File
@@ -20,7 +20,7 @@ const stats = {
test('Google speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
const {synthAudio, addFileToCache, client} = 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');
@@ -38,7 +38,7 @@ test('Google speech synth tests', async(t) => {
},
},
language: 'en-GB',
gender: 'MALE',
gender: 'FEMALE',
text: 'This is a test. This is only a test',
salt: 'foo.bar',
});
@@ -53,11 +53,19 @@ test('Google speech synth tests', async(t) => {
},
},
language: 'en-GB',
gender: 'MALE',
gender: 'FEMALE',
text: 'This is a test. This is only a test',
});
t.ok(opts.servedFromCache, `successfully retrieved cached google audio from ${opts.filePath}`);
const success = await addFileToCache(opts.filePath, {
vendor: 'google',
language: 'en-GB',
gender: 'FEMALE',
text: 'This is a test. This is only a test'
});
t.ok(success, `successfully added ${opts.filePath} to cache`);
opts = await synthAudio(stats, {
vendor: 'google',
credentials: {
@@ -68,7 +76,7 @@ test('Google speech synth tests', async(t) => {
},
disableTtsCache: true,
language: 'en-GB',
gender: 'MALE',
gender: 'FEMALE',
text: 'This is a test. This is only a test',
});
t.ok(!opts.servedFromCache, `successfully synthesized google audio regardless of current cache to ${opts.filePath}`);
@@ -79,6 +87,40 @@ test('Google speech synth tests', async(t) => {
client.quit();
});
test('Google speech Custom voice synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
if (!process.env.GCP_CUSTOM_VOICE_FILE && !process.env.GCP_CUSTOM_VOICE_JSON_KEY || !process.env.GCP_CUSTOM_VOICE_MODEL) {
t.pass('skipping google speech synth tests since neither GCP_CUSTOM_VOICE_FILE nor GCP_CUSTOM_VOICE_JSON_KEY provided, GCP_CUSTOM_VOICE_MODEL is not provided');
return t.end();
}
try {
const str = process.env.GCP_CUSTOM_VOICE_JSON_KEY || fs.readFileSync(process.env.GCP_CUSTOM_VOICE_FILE);
const creds = JSON.parse(str);
let opts = await synthAudio(stats, {
vendor: 'google',
credentials: {
credentials: {
client_email: creds.client_email,
private_key: creds.private_key,
},
},
language: 'en-AU',
text: 'This is a test. This is only a test',
voice: {
reportedUsage:"REALTIME",
model: process.env.GCP_CUSTOM_VOICE_MODEL
}
});
t.ok(!opts.servedFromCache, `successfully synthesized google custom voice audio to ${opts.filePath}`);
} catch (err) {
console.error(err);
t.end(err);
}
client.quit();
});
test('AWS speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
@@ -148,7 +190,9 @@ test('Azure speech synth tests', async(t) => {
text: longText,
});
t.ok(!opts.servedFromCache, `successfully synthesized microsoft audio to ${opts.filePath}`);
if (process.env.JAMBONES_HTTP_PROXY_IP && process.env.JAMBONES_HTTP_PROXY_PORT) {
t.pass('successfully used proxy to reach microsoft tts service');
}
opts = await synthAudio(stats, {
vendor: 'microsoft',
@@ -411,6 +455,100 @@ test('Custom Vendor speech synth tests', async(t) => {
client.quit();
});
test('Elevenlabs speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
if (!process.env.ELEVENLABS_API_KEY || !process.env.ELEVENLABS_VOICE_ID || !process.env.ELEVENLABS_MODEL_ID) {
t.pass('skipping ElevenLabs speech synth tests since ELEVENLABS_API_KEY or ELEVENLABS_VOICE_ID or ELEVENLABS_MODEL_ID not provided');
return t.end();
}
const text = 'Hi there and welcome to jambones!';
try {
let opts = await synthAudio(stats, {
vendor: 'elevenlabs',
credentials: {
api_key: process.env.ELEVENLABS_API_KEY,
model_id: process.env.ELEVENLABS_MODEL_ID,
options: JSON.stringify({
optimize_streaming_latency: 1,
voice_settings: {
similarity_boost: 1,
stability: 0.8,
style: 1,
use_speaker_boost: true
}
})
},
language: 'en-US',
voice: process.env.ELEVENLABS_VOICE_ID,
text,
});
t.ok(!opts.servedFromCache, `successfully synthesized eleven audio to ${opts.filePath}`);
} catch (err) {
console.error(JSON.stringify(err));
t.end(err);
}
client.quit();
})
test('whisper speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
if (!process.env.OPENAI_API_KEY) {
t.pass('skipping OPENAI speech synth tests since OPENAI_API_KEY not provided');
return t.end();
}
const text = 'Hi there and welcome to jambones!';
try {
let opts = await synthAudio(stats, {
vendor: 'whisper',
credentials: {
api_key: process.env.OPENAI_API_KEY,
model_id: 'tts-1'
},
language: 'en-US',
voice: 'alloy',
text,
});
t.ok(!opts.servedFromCache, `successfully synthesized whisper audio to ${opts.filePath}`);
} catch (err) {
console.error(JSON.stringify(err));
t.end(err);
}
client.quit();
})
test('Deepgram speech synth tests', async(t) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);
if (!process.env.DEEPGRAM_API_KEY) {
t.pass('skipping Deepgram speech synth tests since DEEPGRAM_API_KEY');
return t.end();
}
const text = 'Hi there and welcome to jambones!';
try {
let opts = await synthAudio(stats, {
vendor: 'deepgram',
credentials: {
api_key: process.env.DEEPGRAM_API_KEY
},
model: 'alpha-asteria-en-v2',
text,
});
t.ok(!opts.servedFromCache, `successfully synthesized deepgram audio to ${opts.filePath}`);
} catch (err) {
console.error(JSON.stringify(err));
t.end(err);
}
client.quit();
})
test('TTS Cache tests', async(t) => {
const fn = require('..');
const {purgeTtsCache, getTtsSize, client} = fn(opts, logger);