mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-10-03 23:33:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b5430d61d | ||
|
|
9fc8fe8341 | ||
|
|
b31e40b8a5 | ||
|
|
62e1c69f69 | ||
|
|
bc68b672ac | ||
|
|
da1e279128 | ||
|
|
60bcfe07d7 | ||
|
|
d343c81088 | ||
|
|
706f6d5808 | ||
|
|
1b1a0f19d0 | ||
|
|
119ac50f7f | ||
|
|
cb479f04d5 | ||
|
|
7e21e0b666 | ||
|
|
dabdb5b584 | ||
|
|
f36ba027d0 | ||
|
|
08aae32975 | ||
|
|
4cfc730d92 | ||
|
|
4ef8538bcd | ||
|
|
16a746398f | ||
|
|
4a75f353ee | ||
|
|
bbff7963fd | ||
|
|
7b1d226403 | ||
|
|
95c29ce105 | ||
|
|
a46ae01d9c | ||
|
|
562dd0ac79 | ||
|
|
a0612bd1f9 | ||
|
|
2dcbf2b0f1 | ||
|
|
048b7e871e | ||
|
|
4cae96eacb | ||
|
|
1e57d00b55 | ||
|
|
4ead5ee417 | ||
|
|
a517f37473 | ||
|
|
3ac5a98e3a | ||
|
|
689fab6857 | ||
|
|
be3c484527 | ||
|
|
9827f7405d | ||
|
|
906a6b52b8 | ||
|
|
1f0b9ff539 | ||
|
|
b6f8368357 | ||
|
|
6bc487b3d1 | ||
|
|
9699abe0a3 | ||
|
|
5e1e7b17c6 | ||
|
|
1f52cd4f08 | ||
|
|
7df4e2f4c7 | ||
|
|
e99f7c5087 | ||
|
|
c7d981c23a | ||
|
|
95c54b9b12 | ||
|
|
48192aeba1 | ||
|
|
8f931cd8a5 | ||
|
|
144baafe94 | ||
|
|
ed3e513419 | ||
|
|
8d93fdc42a | ||
|
|
ea523a7a1d | ||
|
|
750ed97312 | ||
|
|
73baa81177 | ||
|
|
f86234a769 | ||
|
|
d564f24e6c | ||
|
|
464d8462d9 | ||
|
|
6853f0e342 | ||
|
|
507045dcba | ||
|
|
08758bbbff | ||
|
|
a3aa1169b8 | ||
|
|
7cae19a4e5 | ||
|
|
8c4d5a7cee | ||
|
|
eb2c39072b | ||
|
|
e5932ffc18 | ||
|
|
0a98c6a376 | ||
|
|
ea153e9833 | ||
|
|
b5daeff047 | ||
|
|
da02926c9a | ||
|
|
da3cdbb7aa | ||
|
|
625f147137 | ||
|
|
2e5687978e | ||
|
|
897481d34c | ||
|
|
bd5282e681 | ||
|
|
95a1384f02 | ||
|
|
35deeecf70 | ||
|
|
9d2ac3273f | ||
|
|
b1049aad7f | ||
|
|
40f51e7509 | ||
|
|
a0e2fe167c | ||
|
|
1fa853faa3 | ||
|
|
95e8d942b8 | ||
|
|
7a91876cd7 | ||
|
|
d07344ba3b | ||
|
|
44d8af2a96 | ||
|
|
b530db9a62 | ||
|
|
4c166c8eb4 | ||
|
|
8246dbea21 | ||
|
|
0084f6a468 |
@@ -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
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
# speech-utils
|
||||
TTS-related speech utilities for jambonz
|
||||
# speech-utils 
|
||||
TTS-related speech utilities for jambonz.
|
||||
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
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}) {
|
||||
logger = logger || noopLogger;
|
||||
|
||||
try {
|
||||
const key = makeSynthKey({
|
||||
account_sid,
|
||||
vendor,
|
||||
language: language || '',
|
||||
voice: voice || deploymentId,
|
||||
engine,
|
||||
text,
|
||||
});
|
||||
const audioBuffer = fs.readFile(path);
|
||||
await client.setex(key, EXPIRES, audioBuffer.toString('base64'));
|
||||
} catch (err) {
|
||||
logger.error(err, 'addFileToCache: Error');
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.debug(`addFileToCache: added ${path} to cache`);
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = addFileToCache;
|
||||
@@ -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;
|
||||
@@ -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) {
|
||||
|
||||
+192
-7
@@ -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`);
|
||||
}
|
||||
@@ -177,6 +194,23 @@ 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});
|
||||
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}));
|
||||
@@ -247,7 +281,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'
|
||||
},
|
||||
@@ -297,6 +332,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,
|
||||
@@ -306,7 +383,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);
|
||||
@@ -314,16 +401,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>')) {
|
||||
@@ -509,6 +603,97 @@ 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 || '{}');
|
||||
|
||||
/* if the env is set to stream then bag out, unless we are specifically rendering to generate a cache file */
|
||||
if (process.env.JAMBONES_ELEVENLABS_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, ' ')}`,
|
||||
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}) => {
|
||||
const {api_key, model_id, baseURL, timeout} = credentials;
|
||||
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
@@ -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,
|
||||
|
||||
Generated
+4207
-4052
File diff suppressed because it is too large
Load Diff
+14
-12
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@jambonz/speech-utils",
|
||||
"version": "0.0.16",
|
||||
"version": "0.0.37",
|
||||
"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.347.1",
|
||||
"@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
@@ -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();
|
||||
});
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
require('./docker_start');
|
||||
require('./synth');
|
||||
require('./list-voices');
|
||||
require('./aws');
|
||||
require('./ibm');
|
||||
require('./nuance');
|
||||
require('./docker_stop');
|
||||
|
||||
@@ -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
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user