mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-10-03 23:33:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c18fbacd1b | ||
|
|
b0fee6bbf1 | ||
|
|
f6cead6e92 | ||
|
|
05fc96edc0 | ||
|
|
6794a0b3be | ||
|
|
1a04fd736c | ||
|
|
1846203807 | ||
|
|
75be8658c1 | ||
|
|
91a5eebbaf | ||
|
|
c96f1e86ee | ||
|
|
8016c0886a | ||
|
|
8f216e64d8 | ||
|
|
e1f4486e01 | ||
|
|
b0d6272974 | ||
|
|
ef23b0807a | ||
|
|
ab7e25243d | ||
|
|
bf0ea14423 | ||
|
|
305dabd84b | ||
|
|
b6a3fa5081 | ||
|
|
73feadc4c4 | ||
|
|
aad0f4d62c | ||
|
|
602b0cc60e | ||
|
|
8511e762c8 | ||
|
|
b75d3068c9 | ||
|
|
461178e726 | ||
|
|
e0e4d47340 | ||
|
|
a595faa378 | ||
|
|
7fd1e1a3c3 | ||
|
|
7f6a3d349c | ||
|
|
50429ff535 | ||
|
|
3bf0ef8ea3 | ||
|
|
e9a5e83e36 | ||
|
|
86a64ac091 | ||
|
|
97e06b3ab3 | ||
|
|
09e833d910 | ||
|
|
8c4e12e54f | ||
|
|
2642bd71a4 | ||
|
|
c4feac916f | ||
|
|
2ec56f564e | ||
|
|
9399ddcb58 | ||
|
|
aebf4eda30 | ||
|
|
6b0bdfdf2f |
@@ -13,6 +13,11 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: lts/*
|
node-version: lts/*
|
||||||
- run: npm install
|
- run: npm install
|
||||||
|
- name: Install Docker Compose
|
||||||
|
run: |
|
||||||
|
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
||||||
|
sudo chmod +x /usr/local/bin/docker-compose
|
||||||
|
docker-compose --version
|
||||||
- run: npm run jslint
|
- run: npm run jslint
|
||||||
- run: sudo apt update && sudo apt install -y squid
|
- run: sudo apt update && sudo apt install -y squid
|
||||||
- run: sudo cp test/squid.conf /etc/squid/squid.conf
|
- run: sudo cp test/squid.conf /etc/squid/squid.conf
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
npm audit
|
#npm audit
|
||||||
npm run jslint:fix || true
|
npm run jslint:fix || true
|
||||||
npm test
|
npm test
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
const JAMBONES_TTS_TRIM_SILENCE = process.env.JAMBONES_TTS_TRIM_SILENCE;
|
const JAMBONES_TTS_TRIM_SILENCE = process.env.JAMBONES_TTS_TRIM_SILENCE;
|
||||||
const JAMBONES_DISABLE_TTS_STREAMING = process.env.JAMBONES_DISABLE_TTS_STREAMING;
|
const JAMBONES_DISABLE_TTS_STREAMING = process.env.JAMBONES_DISABLE_TTS_STREAMING;
|
||||||
|
const JAMBONES_DISABLE_AZURE_TTS_STREAMING = process.env.JAMBONES_DISABLE_AZURE_TTS_STREAMING;
|
||||||
|
const JAMBONES_EAGERLY_PRE_CACHE_AUDIO = process.env.JAMBONES_EAGERLY_PRE_CACHE_AUDIO;
|
||||||
|
|
||||||
const JAMBONES_HTTP_PROXY_IP = process.env.JAMBONES_HTTP_PROXY_IP;
|
const JAMBONES_HTTP_PROXY_IP = process.env.JAMBONES_HTTP_PROXY_IP;
|
||||||
const JAMBONES_HTTP_PROXY_PORT = process.env.JAMBONES_HTTP_PROXY_PORT;
|
const JAMBONES_HTTP_PROXY_PORT = process.env.JAMBONES_HTTP_PROXY_PORT;
|
||||||
@@ -13,9 +15,11 @@ const HTTP_TIMEOUT = 5000;
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
JAMBONES_TTS_TRIM_SILENCE,
|
JAMBONES_TTS_TRIM_SILENCE,
|
||||||
JAMBONES_DISABLE_TTS_STREAMING,
|
JAMBONES_DISABLE_TTS_STREAMING,
|
||||||
|
JAMBONES_DISABLE_AZURE_TTS_STREAMING,
|
||||||
JAMBONES_HTTP_PROXY_IP,
|
JAMBONES_HTTP_PROXY_IP,
|
||||||
JAMBONES_HTTP_PROXY_PORT,
|
JAMBONES_HTTP_PROXY_PORT,
|
||||||
JAMBONES_TTS_CACHE_DURATION_MINS,
|
JAMBONES_TTS_CACHE_DURATION_MINS,
|
||||||
|
JAMBONES_EAGERLY_PRE_CACHE_AUDIO,
|
||||||
TMP_FOLDER,
|
TMP_FOLDER,
|
||||||
HTTP_TIMEOUT
|
HTTP_TIMEOUT
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,20 +1,22 @@
|
|||||||
const { STSClient, GetSessionTokenCommand, AssumeRoleCommand } = require('@aws-sdk/client-sts');
|
const { STSClient, GetSessionTokenCommand, AssumeRoleCommand } = require('@aws-sdk/client-sts');
|
||||||
const {makeAwsKey, noopLogger} = require('./utils');
|
const {makeAwsKey, noopLogger} = require('./utils');
|
||||||
const debug = require('debug')('jambonz:speech-utils');
|
const debug = require('debug')('jambonz:speech-utils');
|
||||||
const EXPIRY = 3600;
|
const EXPIRY = process.env.AWS_STS_SESSION_DURATION || 3600;
|
||||||
|
// by default reset aws session before expiry time 10 mins
|
||||||
|
const CACHE_EXPIRY = process.env.AWS_STS_SESSION_RESET_EXPIRY || (EXPIRY - 600);
|
||||||
|
|
||||||
async function getAwsAuthToken(
|
async function getAwsAuthToken(
|
||||||
logger, createHash, retrieveHash,
|
logger, createHash, retrieveHash,
|
||||||
awsAccessKeyId, awsSecretAccessKey, awsRegion, roleArn = null) {
|
{accessKeyId, secretAccessKey, region, roleArn}) {
|
||||||
logger = logger || noopLogger;
|
logger = logger || noopLogger;
|
||||||
try {
|
try {
|
||||||
const key = makeAwsKey(roleArn || awsAccessKeyId);
|
const key = makeAwsKey(roleArn || accessKeyId);
|
||||||
const obj = await retrieveHash(key);
|
const obj = await retrieveHash(key);
|
||||||
if (obj) return {...obj, servedFromCache: true};
|
if (obj) return {...obj, servedFromCache: true};
|
||||||
|
|
||||||
let data;
|
let data;
|
||||||
if (roleArn) {
|
if (roleArn) {
|
||||||
const stsClient = new STSClient({ region: awsRegion});
|
const stsClient = new STSClient({ region });
|
||||||
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
||||||
const command = new AssumeRoleCommand(roleToAssume);
|
const command = new AssumeRoleCommand(roleToAssume);
|
||||||
|
|
||||||
@@ -22,10 +24,10 @@ async function getAwsAuthToken(
|
|||||||
} else {
|
} else {
|
||||||
/* access token not found in cache, so generate it using STS */
|
/* access token not found in cache, so generate it using STS */
|
||||||
const stsClient = new STSClient({
|
const stsClient = new STSClient({
|
||||||
region: awsRegion,
|
region,
|
||||||
credentials: {
|
credentials: {
|
||||||
accessKeyId: awsAccessKeyId,
|
accessKeyId,
|
||||||
secretAccessKey: awsSecretAccessKey,
|
secretAccessKey,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
||||||
@@ -39,8 +41,7 @@ async function getAwsAuthToken(
|
|||||||
securityToken: data.Credentials.SessionToken
|
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, CACHE_EXPIRY)
|
||||||
createHash(key, credentials, EXPIRY - 600)
|
|
||||||
.catch((err) => logger.error(err, `Error saving hash for key ${key}`));
|
.catch((err) => logger.error(err, `Error saving hash for key ${key}`));
|
||||||
|
|
||||||
return {...credentials, servedFromCache: false};
|
return {...credentials, servedFromCache: false};
|
||||||
|
|||||||
@@ -107,7 +107,12 @@ const getAwsVoices = async(_client, createHash, retrieveHash, logger, credential
|
|||||||
} else if (roleArn) {
|
} else if (roleArn) {
|
||||||
client = new PollyClient({
|
client = new PollyClient({
|
||||||
region,
|
region,
|
||||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
credentials: await getAwsAuthToken(
|
||||||
|
logger, createHash, retrieveHash,
|
||||||
|
{
|
||||||
|
region,
|
||||||
|
roleArn
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
client = new PollyClient({region});
|
client = new PollyClient({region});
|
||||||
|
|||||||
+86
-16
@@ -20,7 +20,8 @@ const {
|
|||||||
createKryptonClient,
|
createKryptonClient,
|
||||||
createRivaClient,
|
createRivaClient,
|
||||||
noopLogger,
|
noopLogger,
|
||||||
makeFilePath
|
makeFilePath,
|
||||||
|
makePlayhtKey
|
||||||
} = require('./utils');
|
} = require('./utils');
|
||||||
const getNuanceAccessToken = require('./get-nuance-access-token');
|
const getNuanceAccessToken = require('./get-nuance-access-token');
|
||||||
const getVerbioAccessToken = require('./get-verbio-token');
|
const getVerbioAccessToken = require('./get-verbio-token');
|
||||||
@@ -40,9 +41,11 @@ const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
|
|||||||
const debug = require('debug')('jambonz:realtimedb-helpers');
|
const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||||
const {
|
const {
|
||||||
JAMBONES_DISABLE_TTS_STREAMING,
|
JAMBONES_DISABLE_TTS_STREAMING,
|
||||||
|
JAMBONES_DISABLE_AZURE_TTS_STREAMING,
|
||||||
JAMBONES_HTTP_PROXY_IP,
|
JAMBONES_HTTP_PROXY_IP,
|
||||||
JAMBONES_HTTP_PROXY_PORT,
|
JAMBONES_HTTP_PROXY_PORT,
|
||||||
JAMBONES_TTS_CACHE_DURATION_MINS,
|
JAMBONES_TTS_CACHE_DURATION_MINS,
|
||||||
|
JAMBONES_EAGERLY_PRE_CACHE_AUDIO,
|
||||||
} = require('./config');
|
} = require('./config');
|
||||||
const EXPIRES = JAMBONES_TTS_CACHE_DURATION_MINS;
|
const EXPIRES = JAMBONES_TTS_CACHE_DURATION_MINS;
|
||||||
const OpenAI = require('openai');
|
const OpenAI = require('openai');
|
||||||
@@ -85,7 +88,7 @@ const trimTrailingSilence = (buffer) => {
|
|||||||
*/
|
*/
|
||||||
async function synthAudio(client, createHash, retrieveHash, logger, stats, { account_sid,
|
async function synthAudio(client, createHash, retrieveHash, logger, stats, { account_sid,
|
||||||
vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId,
|
vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId,
|
||||||
disableTtsCache, renderForCaching, disableTtsStreaming, options
|
disableTtsCache, renderForCaching = false, disableTtsStreaming, options
|
||||||
}) {
|
}) {
|
||||||
let audioBuffer;
|
let audioBuffer;
|
||||||
let servedFromCache = false;
|
let servedFromCache = false;
|
||||||
@@ -150,21 +153,48 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
|
|||||||
assert.ok(voice, 'synthAudio requires voice when verbio is used');
|
assert.ok(voice, 'synthAudio requires voice when verbio is used');
|
||||||
assert.ok(credentials.client_id, 'synthAudio requires client_id when verbio is used');
|
assert.ok(credentials.client_id, 'synthAudio requires client_id when verbio is used');
|
||||||
assert.ok(credentials.client_secret, 'synthAudio requires client_secret when verbio is used');
|
assert.ok(credentials.client_secret, 'synthAudio requires client_secret when verbio is used');
|
||||||
|
} else if ('deepgram' === vendor) {
|
||||||
|
if (!credentials.deepgram_tts_uri) {
|
||||||
|
assert.ok(credentials.api_key, 'synthAudio requires api_key when deepgram is used');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = makeSynthKey({
|
const key = makeSynthKey({
|
||||||
account_sid,
|
account_sid,
|
||||||
vendor,
|
vendor,
|
||||||
language: language || '',
|
language: language || '',
|
||||||
voice: voice || deploymentId,
|
voice: voice || deploymentId,
|
||||||
engine,
|
engine,
|
||||||
text
|
text,
|
||||||
|
renderForCaching
|
||||||
});
|
});
|
||||||
let filePath;
|
let filePath;
|
||||||
filePath = makeFilePath(vendor, key, salt);
|
filePath = makeFilePath({vendor, key, salt, renderForCaching});
|
||||||
debug(`synth key is ${key}`);
|
debug(`synth key is ${key}`);
|
||||||
let cached;
|
let cached;
|
||||||
if (!disableTtsCache) {
|
if (!disableTtsCache) {
|
||||||
cached = await client.get(key);
|
cached = await client.get(key);
|
||||||
|
/**
|
||||||
|
* If we are using tts streaming and also precaching audio, audio could have been cached by streaming (r8)
|
||||||
|
* or here in speech-utils due to precaching (mp3), so we need to check for both keys.
|
||||||
|
*/
|
||||||
|
if (!cached && JAMBONES_EAGERLY_PRE_CACHE_AUDIO) {
|
||||||
|
const preCachekey = makeSynthKey({
|
||||||
|
account_sid,
|
||||||
|
vendor,
|
||||||
|
language: language || '',
|
||||||
|
voice: voice || deploymentId,
|
||||||
|
engine,
|
||||||
|
text,
|
||||||
|
renderForCaching: true
|
||||||
|
});
|
||||||
|
cached = await client.get(preCachekey);
|
||||||
|
if (cached) {
|
||||||
|
// Precache audio is available update filpath with precache file extension.
|
||||||
|
filePath = makeFilePath({vendor, key, salt, renderForCaching: true});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (cached) {
|
if (cached) {
|
||||||
// found in cache - extend the expiry and use it
|
// found in cache - extend the expiry and use it
|
||||||
@@ -215,7 +245,7 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case 'playht':
|
case 'playht':
|
||||||
audioBuffer = await synthPlayHT(logger, {
|
audioBuffer = await synthPlayHT(client, logger, {
|
||||||
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming, filePath
|
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming, filePath
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@@ -280,7 +310,12 @@ const synthPolly = async(createHash, retrieveHash, logger,
|
|||||||
} else if (roleArn) {
|
} else if (roleArn) {
|
||||||
polly = new PollyClient({
|
polly = new PollyClient({
|
||||||
region,
|
region,
|
||||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
credentials: await getAwsAuthToken(
|
||||||
|
logger, createHash, retrieveHash,
|
||||||
|
{
|
||||||
|
region,
|
||||||
|
roleArn
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// AWS RoleArn assigned to Instance profile
|
// AWS RoleArn assigned to Instance profile
|
||||||
@@ -443,7 +478,8 @@ const synthMicrosoft = async(logger, {
|
|||||||
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>`;
|
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');
|
logger.info({content}, 'synthMicrosoft');
|
||||||
}
|
}
|
||||||
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
|
if (!JAMBONES_DISABLE_TTS_STREAMING && !JAMBONES_DISABLE_AZURE_TTS_STREAMING &&
|
||||||
|
!renderForCaching && !disableTtsStreaming) {
|
||||||
let params = '';
|
let params = '';
|
||||||
params += `{api_key=${apiKey}`;
|
params += `{api_key=${apiKey}`;
|
||||||
params += `,language=${language}`;
|
params += `,language=${language}`;
|
||||||
@@ -720,12 +756,37 @@ const synthElevenlabs = async(logger, {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const synthPlayHT = async(logger, {
|
const synthPlayHT = async(client, logger, {
|
||||||
credentials, options, stats, voice, text, renderForCaching, disableTtsStreaming
|
credentials, options, stats, voice, language, text, renderForCaching, disableTtsStreaming
|
||||||
}) => {
|
}) => {
|
||||||
const {api_key, user_id, voice_engine, options: credOpts} = credentials;
|
const {api_key, user_id, voice_engine, options: credOpts} = credentials;
|
||||||
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
|
const opts = !!options && Object.keys(options).length !== 0 ? options : JSON.parse(credOpts || '{}');
|
||||||
|
|
||||||
|
let synthesizeUrl = 'https://api.play.ht/api/v2/tts/stream';
|
||||||
|
|
||||||
|
// If model is play3.0, the synthesizeUrl is got from authentication endpoint
|
||||||
|
if (voice_engine === 'Play3.0') {
|
||||||
|
try {
|
||||||
|
const post = bent('https://api.play.ht', 'POST', 'json', 201, {
|
||||||
|
'AUTHORIZATION': api_key,
|
||||||
|
'X-USER-ID': user_id,
|
||||||
|
'Accept': 'application/json'
|
||||||
|
});
|
||||||
|
const key = makePlayhtKey(api_key);
|
||||||
|
const url = await client.get(key);
|
||||||
|
if (!url) {
|
||||||
|
const {inference_address, expires_at_ms} = await post('/api/v3/auth');
|
||||||
|
synthesizeUrl = inference_address;
|
||||||
|
const expiry = Math.floor((expires_at_ms - Date.now()) / 1000 - 30);
|
||||||
|
await client.set(key, inference_address, 'EX', expiry);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.info({err}, 'synth PlayHT returned error for authentication version 3.0');
|
||||||
|
stats.increment('tts.count', ['vendor:playht', 'accepted:no']);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* default to using the streaming interface, unless disabled by env var OR we want just a cache file */
|
/* default to using the streaming interface, unless disabled by env var OR we want just a cache file */
|
||||||
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
|
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
|
||||||
let params = '';
|
let params = '';
|
||||||
@@ -734,6 +795,7 @@ const synthPlayHT = async(logger, {
|
|||||||
params += ',vendor=playht';
|
params += ',vendor=playht';
|
||||||
params += `,voice=${voice}`;
|
params += `,voice=${voice}`;
|
||||||
params += `,voice_engine=${voice_engine}`;
|
params += `,voice_engine=${voice_engine}`;
|
||||||
|
params += `,synthesize_url=${synthesizeUrl}`;
|
||||||
params += ',write_cache_file=1';
|
params += ',write_cache_file=1';
|
||||||
if (opts.quality) params += `,quality=${opts.quality}`;
|
if (opts.quality) params += `,quality=${opts.quality}`;
|
||||||
if (opts.speed) params += `,speed=${opts.speed}`;
|
if (opts.speed) params += `,speed=${opts.speed}`;
|
||||||
@@ -743,6 +805,8 @@ const synthPlayHT = async(logger, {
|
|||||||
if (opts.voice_guidance) params += `,voice_guidance=${opts.voice_guidance}`;
|
if (opts.voice_guidance) params += `,voice_guidance=${opts.voice_guidance}`;
|
||||||
if (opts.style_guidance) params += `,style_guidance=${opts.style_guidance}`;
|
if (opts.style_guidance) params += `,style_guidance=${opts.style_guidance}`;
|
||||||
if (opts.text_guidance) params += `,text_guidance=${opts.text_guidance}`;
|
if (opts.text_guidance) params += `,text_guidance=${opts.text_guidance}`;
|
||||||
|
if (opts.top_p) params += `,top_p=${opts.top_p}`;
|
||||||
|
if (opts.repetition_penalty) params += `,repetition_penalty=${opts.repetition_penalty}`;
|
||||||
params += '}';
|
params += '}';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -753,14 +817,18 @@ const synthPlayHT = async(logger, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const post = bent('https://api.play.ht', 'POST', 'buffer', {
|
const post = bent('POST', 'buffer', {
|
||||||
'AUTHORIZATION': api_key,
|
...(voice_engine !== 'Play3.0' && {
|
||||||
'X-USER-ID': user_id,
|
'AUTHORIZATION': api_key,
|
||||||
|
'X-USER-ID': user_id,
|
||||||
|
}),
|
||||||
'Accept': 'audio/mpeg',
|
'Accept': 'audio/mpeg',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
});
|
});
|
||||||
const mp3 = await post('/api/v2/tts/stream', {
|
|
||||||
|
const mp3 = await post(synthesizeUrl, {
|
||||||
text,
|
text,
|
||||||
|
...(voice_engine === 'Play3.0' && { language }),
|
||||||
voice,
|
voice,
|
||||||
voice_engine,
|
voice_engine,
|
||||||
output_format: 'mp3',
|
output_format: 'mp3',
|
||||||
@@ -902,13 +970,14 @@ const synthWhisper = async(logger, {credentials, stats, voice, text, renderForCa
|
|||||||
};
|
};
|
||||||
|
|
||||||
const synthDeepgram = async(logger, {credentials, stats, model, text, renderForCaching, disableTtsStreaming}) => {
|
const synthDeepgram = async(logger, {credentials, stats, model, text, renderForCaching, disableTtsStreaming}) => {
|
||||||
const {api_key} = credentials;
|
const {api_key, deepgram_tts_uri} = credentials;
|
||||||
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
|
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
|
||||||
let params = '';
|
let params = '';
|
||||||
params += `{api_key=${api_key}`;
|
params += `{api_key=${api_key}`;
|
||||||
params += ',vendor=deepgram';
|
params += ',vendor=deepgram';
|
||||||
params += `,voice=${model}`;
|
params += `,voice=${model}`;
|
||||||
params += ',write_cache_file=1';
|
params += ',write_cache_file=1';
|
||||||
|
if (deepgram_tts_uri) params += `,endpoint=${deepgram_tts_uri}`;
|
||||||
params += '}';
|
params += '}';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -918,8 +987,9 @@ const synthDeepgram = async(logger, {credentials, stats, model, text, renderForC
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const post = bent('https://api.deepgram.com', 'POST', 'buffer', {
|
const post = bent(deepgram_tts_uri || 'https://api.deepgram.com', 'POST', 'buffer', {
|
||||||
'Authorization': `Token ${api_key}`,
|
// on-premise deepgram does not require to have api_key
|
||||||
|
...(api_key && {'Authorization': `Token ${api_key}`}),
|
||||||
'Accept': 'audio/mpeg',
|
'Accept': 'audio/mpeg',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
});
|
});
|
||||||
|
|||||||
+26
-9
@@ -16,29 +16,31 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
|
|||||||
*/
|
*/
|
||||||
//const nuanceClientMap = new Map();
|
//const nuanceClientMap = new Map();
|
||||||
|
|
||||||
function makeSynthKey({account_sid = '', vendor, language, voice, engine = '', text}) {
|
function makeSynthKey({
|
||||||
|
account_sid = '', vendor, language, voice, engine = '', text,
|
||||||
|
renderForCaching = false}) {
|
||||||
const hash = crypto.createHash('sha1');
|
const hash = crypto.createHash('sha1');
|
||||||
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
|
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
|
||||||
const hexHashKey = hash.digest('hex');
|
const hexHashKey = hash.digest('hex');
|
||||||
const accountKey = account_sid ? `:${account_sid}` : '';
|
const accountKey = account_sid ? `:${account_sid}` : '';
|
||||||
const namespace = vendor.startsWith('custom') ? vendor : getFileExtension(vendor);
|
const namespace = vendor.startsWith('custom') ? vendor : getFileExtension({vendor, renderForCaching});
|
||||||
const key = `tts${accountKey}:${namespace}:${hexHashKey}`;
|
const key = `tts${accountKey}:${namespace}:${hexHashKey}`;
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeFilePath(vendor, key, salt = '') {
|
function makeFilePath({vendor, key, salt = '', renderForCaching = false}) {
|
||||||
const extension = getFileExtension(vendor);
|
const extension = getFileExtension({vendor, renderForCaching});
|
||||||
return `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt}`)}.${extension}`;
|
return `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt}`)}.${extension}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileExtension(vendor) {
|
function getFileExtension({vendor, renderForCaching = false}) {
|
||||||
const mp3Extension = 'mp3';
|
const mp3Extension = 'mp3';
|
||||||
const r8Extension = 'r8';
|
const r8Extension = 'r8';
|
||||||
|
|
||||||
switch (vendor) {
|
switch (vendor) {
|
||||||
case 'azure':
|
case 'azure':
|
||||||
case 'microsoft':
|
case 'microsoft':
|
||||||
if (!JAMBONES_DISABLE_TTS_STREAMING || JAMBONES_TTS_TRIM_SILENCE) {
|
if (!renderForCaching && !JAMBONES_DISABLE_TTS_STREAMING || JAMBONES_TTS_TRIM_SILENCE) {
|
||||||
return r8Extension;
|
return r8Extension;
|
||||||
} else {
|
} else {
|
||||||
return mp3Extension;
|
return mp3Extension;
|
||||||
@@ -46,16 +48,25 @@ function getFileExtension(vendor) {
|
|||||||
case 'deepgram':
|
case 'deepgram':
|
||||||
case 'elevenlabs':
|
case 'elevenlabs':
|
||||||
case 'rimlabs':
|
case 'rimlabs':
|
||||||
if (!JAMBONES_DISABLE_TTS_STREAMING) {
|
case 'playht':
|
||||||
return r8Extension;
|
if (renderForCaching || JAMBONES_DISABLE_TTS_STREAMING) {
|
||||||
} else {
|
|
||||||
return mp3Extension;
|
return mp3Extension;
|
||||||
|
} else {
|
||||||
|
return r8Extension;
|
||||||
}
|
}
|
||||||
case 'nuance':
|
case 'nuance':
|
||||||
case 'nvidia':
|
case 'nvidia':
|
||||||
case 'verbio':
|
case 'verbio':
|
||||||
return r8Extension;
|
return r8Extension;
|
||||||
default:
|
default:
|
||||||
|
// If vendor is custom
|
||||||
|
if (vendor.startsWith('custom')) {
|
||||||
|
if (renderForCaching || JAMBONES_DISABLE_TTS_STREAMING) {
|
||||||
|
return mp3Extension;
|
||||||
|
} else {
|
||||||
|
return r8Extension;
|
||||||
|
}
|
||||||
|
}
|
||||||
return mp3Extension;
|
return mp3Extension;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,6 +98,11 @@ function makeAwsKey(awsAccessKeyId) {
|
|||||||
return `aws:${hash.digest('hex')}`;
|
return `aws:${hash.digest('hex')}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function makePlayhtKey(apiKey) {
|
||||||
|
const hash = crypto.createHash('sha1');
|
||||||
|
hash.update(apiKey);
|
||||||
|
return `playht:${hash.digest('hex')}`;
|
||||||
|
}
|
||||||
function makeVerbioKey(client_id) {
|
function makeVerbioKey(client_id) {
|
||||||
const hash = crypto.createHash('sha1');
|
const hash = crypto.createHash('sha1');
|
||||||
hash.update(client_id);
|
hash.update(client_id);
|
||||||
@@ -160,6 +176,7 @@ module.exports = {
|
|||||||
makeSynthKey,
|
makeSynthKey,
|
||||||
makeNuanceKey,
|
makeNuanceKey,
|
||||||
makeIbmKey,
|
makeIbmKey,
|
||||||
|
makePlayhtKey,
|
||||||
makeAwsKey,
|
makeAwsKey,
|
||||||
makeVerbioKey,
|
makeVerbioKey,
|
||||||
getNuanceAccessToken,
|
getNuanceAccessToken,
|
||||||
|
|||||||
Generated
+1565
-1391
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@jambonz/speech-utils",
|
"name": "@jambonz/speech-utils",
|
||||||
"version": "0.1.8",
|
"version": "0.1.17",
|
||||||
"description": "TTS-related speech utilities for jambonz",
|
"description": "TTS-related speech utilities for jambonz",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "Dave Horton",
|
"author": "Dave Horton",
|
||||||
@@ -36,7 +36,7 @@
|
|||||||
"form-urlencoded": "^6.1.4",
|
"form-urlencoded": "^6.1.4",
|
||||||
"google-protobuf": "^3.21.2",
|
"google-protobuf": "^3.21.2",
|
||||||
"ibm-watson": "^8.0.0",
|
"ibm-watson": "^8.0.0",
|
||||||
"microsoft-cognitiveservices-speech-sdk": "1.36.0",
|
"microsoft-cognitiveservices-speech-sdk": "1.38.0",
|
||||||
"openai": "^4.25.0",
|
"openai": "^4.25.0",
|
||||||
"undici": "^6.4.0"
|
"undici": "^6.4.0"
|
||||||
},
|
},
|
||||||
|
|||||||
+10
-2
@@ -19,12 +19,20 @@ test('AWS - create and cache auth token', async(t) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
let obj = await getAwsAuthToken(process.env.AWS_ACCESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY, process.env.AWS_REGION);
|
let obj = await getAwsAuthToken({
|
||||||
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
||||||
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
||||||
|
region: process.env.AWS_REGION
|
||||||
|
});
|
||||||
//console.log({obj}, 'received auth token from AWS');
|
//console.log({obj}, 'received auth token from AWS');
|
||||||
t.ok(obj.securityToken && !obj.servedFromCache, 'successfullY generated auth token from AWS');
|
t.ok(obj.securityToken && !obj.servedFromCache, 'successfullY generated auth token from AWS');
|
||||||
|
|
||||||
await sleep(250);
|
await sleep(250);
|
||||||
obj = await getAwsAuthToken(process.env.AWS_ACCESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY, process.env.AWS_REGION);
|
obj = await getAwsAuthToken({
|
||||||
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
||||||
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
||||||
|
region: process.env.AWS_REGION
|
||||||
|
});
|
||||||
//console.log({obj}, 'received auth token from AWS - second request');
|
//console.log({obj}, 'received auth token from AWS - second request');
|
||||||
t.ok(obj.securityToken && obj.servedFromCache, 'successfully received access token from cache');
|
t.ok(obj.securityToken && obj.servedFromCache, 'successfully received access token from cache');
|
||||||
|
|
||||||
|
|||||||
+16
-8
@@ -574,9 +574,9 @@ test('Elevenlabs speech synth tests', async(t) => {
|
|||||||
t.end(err);
|
t.end(err);
|
||||||
}
|
}
|
||||||
client.quit();
|
client.quit();
|
||||||
})
|
});
|
||||||
|
|
||||||
test('PlayHT speech synth tests', async(t) => {
|
const testPlayHT = async(t, voice_engine) => {
|
||||||
const fn = require('..');
|
const fn = require('..');
|
||||||
const {synthAudio, client} = fn(opts, logger);
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
@@ -584,26 +584,26 @@ test('PlayHT speech synth tests', async(t) => {
|
|||||||
t.pass('skipping PlayHT speech synth tests since PLAYHT_API_KEY or PLAYHT_USER_ID is/are not provided');
|
t.pass('skipping PlayHT speech synth tests since PLAYHT_API_KEY or PLAYHT_USER_ID is/are not provided');
|
||||||
return t.end();
|
return t.end();
|
||||||
}
|
}
|
||||||
const text = 'Hi there and welcome to jambones!';
|
const text = 'Hi there and welcome to jambones! ' + Date.now();
|
||||||
try {
|
try {
|
||||||
let opts = await synthAudio(stats, {
|
const opts = await synthAudio(stats, {
|
||||||
vendor: 'playht',
|
vendor: 'playht',
|
||||||
credentials: {
|
credentials: {
|
||||||
api_key: process.env.PLAYHT_API_KEY,
|
api_key: process.env.PLAYHT_API_KEY,
|
||||||
user_id: process.env.PLAYHT_USER_ID,
|
user_id: process.env.PLAYHT_USER_ID,
|
||||||
voice_engine: 'PlayHT2.0-turbo',
|
voice_engine,
|
||||||
options: JSON.stringify({
|
options: JSON.stringify({
|
||||||
quality: "medium",
|
quality: 'medium',
|
||||||
speed: 1,
|
speed: 1,
|
||||||
seed: 1,
|
seed: 1,
|
||||||
temperature: 1,
|
temperature: 1,
|
||||||
emotion: "female_happy",
|
emotion: 'female_happy',
|
||||||
voice_guidance: 3,
|
voice_guidance: 3,
|
||||||
style_guidance: 20,
|
style_guidance: 20,
|
||||||
text_guidance: 1,
|
text_guidance: 1,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
language: 'en-US',
|
language: 'english',
|
||||||
voice: 's3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json',
|
voice: 's3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json',
|
||||||
text,
|
text,
|
||||||
renderForCaching: true
|
renderForCaching: true
|
||||||
@@ -615,6 +615,14 @@ test('PlayHT speech synth tests', async(t) => {
|
|||||||
t.end(err);
|
t.end(err);
|
||||||
}
|
}
|
||||||
client.quit();
|
client.quit();
|
||||||
|
};
|
||||||
|
|
||||||
|
test('PlayHT speech synth tests', async(t) => {
|
||||||
|
await testPlayHT(t, 'PlayHT2.0-turbo');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PlayHT3.0 speech synth tests', async(t) => {
|
||||||
|
await testPlayHT(t, 'Play3.0');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('rimelabs speech synth tests', async(t) => {
|
test('rimelabs speech synth tests', async(t) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user