mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-10-04 07:43:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f183852961 | ||
|
|
34c3e01729 | ||
|
|
9b2b16199e | ||
|
|
9112c5f0ea | ||
|
|
50783dfd0a | ||
|
|
ca0ef76fe1 | ||
|
|
7c91c537e4 | ||
|
|
9747526664 | ||
|
|
31a0c7b02c | ||
|
|
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 | ||
|
|
d214c3184f | ||
|
|
c335a508a3 | ||
|
|
d7797d5691 | ||
|
|
b81f3313cb | ||
|
|
49fe64744c | ||
|
|
106238cca5 | ||
|
|
01735ccf2f | ||
|
|
ce81cb8d24 | ||
|
|
c9d7a9046f | ||
|
|
a9ba383667 | ||
|
|
06b97bcb0f | ||
|
|
90152b36ef | ||
|
|
92484b7441 | ||
|
|
398904785b |
@@ -1 +0,0 @@
|
||||
test/*
|
||||
-126
@@ -1,126 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"node": true,
|
||||
"es6": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaFeatures": {
|
||||
"jsx": false,
|
||||
"modules": false
|
||||
},
|
||||
"ecmaVersion": 2020
|
||||
},
|
||||
"plugins": ["promise"],
|
||||
"rules": {
|
||||
"promise/always-return": "error",
|
||||
"promise/no-return-wrap": "error",
|
||||
"promise/param-names": "error",
|
||||
"promise/catch-or-return": "error",
|
||||
"promise/no-native": "off",
|
||||
"promise/no-nesting": "warn",
|
||||
"promise/no-promise-in-callback": "warn",
|
||||
"promise/no-callback-in-promise": "warn",
|
||||
"promise/no-return-in-finally": "warn",
|
||||
|
||||
// Possible Errors
|
||||
// http://eslint.org/docs/rules/#possible-errors
|
||||
"comma-dangle": [2, "only-multiline"],
|
||||
"no-control-regex": 2,
|
||||
"no-debugger": 2,
|
||||
"no-dupe-args": 2,
|
||||
"no-dupe-keys": 2,
|
||||
"no-duplicate-case": 2,
|
||||
"no-empty-character-class": 2,
|
||||
"no-ex-assign": 2,
|
||||
"no-extra-boolean-cast" : 2,
|
||||
"no-extra-parens": [2, "functions"],
|
||||
"no-extra-semi": 2,
|
||||
"no-func-assign": 2,
|
||||
"no-invalid-regexp": 2,
|
||||
"no-irregular-whitespace": 2,
|
||||
"no-negated-in-lhs": 2,
|
||||
"no-obj-calls": 2,
|
||||
"no-proto": 2,
|
||||
"no-unexpected-multiline": 2,
|
||||
"no-unreachable": 2,
|
||||
"use-isnan": 2,
|
||||
"valid-typeof": 2,
|
||||
|
||||
// Best Practices
|
||||
// http://eslint.org/docs/rules/#best-practices
|
||||
"no-fallthrough": 2,
|
||||
"no-octal": 2,
|
||||
"no-redeclare": 2,
|
||||
"no-self-assign": 2,
|
||||
"no-unused-labels": 2,
|
||||
|
||||
// Strict Mode
|
||||
// http://eslint.org/docs/rules/#strict-mode
|
||||
"strict": [2, "never"],
|
||||
|
||||
// Variables
|
||||
// http://eslint.org/docs/rules/#variables
|
||||
"no-delete-var": 2,
|
||||
"no-undef": 2,
|
||||
"no-unused-vars": [2, {"args": "none"}],
|
||||
|
||||
// Node.js and CommonJS
|
||||
// http://eslint.org/docs/rules/#nodejs-and-commonjs
|
||||
"no-mixed-requires": 2,
|
||||
"no-new-require": 2,
|
||||
"no-path-concat": 2,
|
||||
"no-restricted-modules": [2, "sys", "_linklist"],
|
||||
|
||||
// Stylistic Issues
|
||||
// http://eslint.org/docs/rules/#stylistic-issues
|
||||
"comma-spacing": 2,
|
||||
"eol-last": 2,
|
||||
"indent": [2, 2, {"SwitchCase": 1}],
|
||||
"keyword-spacing": 2,
|
||||
"max-len": [2, 120, 2],
|
||||
"new-parens": 2,
|
||||
"no-mixed-spaces-and-tabs": 2,
|
||||
"no-multiple-empty-lines": [2, {"max": 2}],
|
||||
"no-trailing-spaces": [2, {"skipBlankLines": false }],
|
||||
"quotes": [2, "single", "avoid-escape"],
|
||||
"semi": 2,
|
||||
"space-before-blocks": [2, "always"],
|
||||
"space-before-function-paren": [2, "never"],
|
||||
"space-in-parens": [2, "never"],
|
||||
"space-infix-ops": 2,
|
||||
"space-unary-ops": 2,
|
||||
|
||||
// ECMAScript 6
|
||||
// http://eslint.org/docs/rules/#ecmascript-6
|
||||
"arrow-parens": [2, "always"],
|
||||
"arrow-spacing": [2, {"before": true, "after": true}],
|
||||
"constructor-super": 2,
|
||||
"no-class-assign": 2,
|
||||
"no-confusing-arrow": 2,
|
||||
"no-const-assign": 2,
|
||||
"no-dupe-class-members": 2,
|
||||
"no-new-symbol": 2,
|
||||
"no-this-before-super": 2,
|
||||
"prefer-const": 2
|
||||
},
|
||||
"globals": {
|
||||
"DTRACE_HTTP_CLIENT_REQUEST" : false,
|
||||
"LTTNG_HTTP_CLIENT_REQUEST" : false,
|
||||
"COUNTER_HTTP_CLIENT_REQUEST" : false,
|
||||
"DTRACE_HTTP_CLIENT_RESPONSE" : false,
|
||||
"LTTNG_HTTP_CLIENT_RESPONSE" : false,
|
||||
"COUNTER_HTTP_CLIENT_RESPONSE" : false,
|
||||
"DTRACE_HTTP_SERVER_REQUEST" : false,
|
||||
"LTTNG_HTTP_SERVER_REQUEST" : false,
|
||||
"COUNTER_HTTP_SERVER_REQUEST" : false,
|
||||
"DTRACE_HTTP_SERVER_RESPONSE" : false,
|
||||
"LTTNG_HTTP_SERVER_RESPONSE" : false,
|
||||
"COUNTER_HTTP_SERVER_RESPONSE" : false,
|
||||
"DTRACE_NET_STREAM_END" : false,
|
||||
"LTTNG_NET_STREAM_END" : false,
|
||||
"COUNTER_NET_SERVER_CONNECTION_CLOSE" : false,
|
||||
"DTRACE_NET_SERVER_CONNECTION" : false,
|
||||
"LTTNG_NET_SERVER_CONNECTION" : false,
|
||||
"COUNTER_NET_SERVER_CONNECTION" : false
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,11 @@ jobs:
|
||||
with:
|
||||
node-version: lts/*
|
||||
- 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: sudo apt update && sudo apt install -y squid
|
||||
- run: sudo cp test/squid.conf /etc/squid/squid.conf
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
#npm audit
|
||||
npm run jslint:fix || true
|
||||
npm test
|
||||
@@ -0,0 +1,140 @@
|
||||
const promise = require('eslint-plugin-promise');
|
||||
const globals = require('globals');
|
||||
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
files: ['**/*.js', '*.js'],
|
||||
languageOptions: {
|
||||
|
||||
ecmaVersion: 2020,
|
||||
parserOptions: {
|
||||
'node': true,
|
||||
'es6': true,
|
||||
ecmaFeatures: {
|
||||
jsx: false,
|
||||
modules: false,
|
||||
}
|
||||
},
|
||||
sourceType: 'module',
|
||||
globals: {
|
||||
...globals.node,
|
||||
'DTRACE_HTTP_CLIENT_REQUEST': false,
|
||||
'LTTNG_HTTP_CLIENT_REQUEST': false,
|
||||
'COUNTER_HTTP_CLIENT_REQUEST': false,
|
||||
'DTRACE_HTTP_CLIENT_RESPONSE': false,
|
||||
'LTTNG_HTTP_CLIENT_RESPONSE': false,
|
||||
'COUNTER_HTTP_CLIENT_RESPONSE': false,
|
||||
'DTRACE_HTTP_SERVER_REQUEST': false,
|
||||
'LTTNG_HTTP_SERVER_REQUEST': false,
|
||||
'COUNTER_HTTP_SERVER_REQUEST': false,
|
||||
'DTRACE_HTTP_SERVER_RESPONSE': false,
|
||||
'LTTNG_HTTP_SERVER_RESPONSE': false,
|
||||
'COUNTER_HTTP_SERVER_RESPONSE': false,
|
||||
'DTRACE_NET_STREAM_END': false,
|
||||
'LTTNG_NET_STREAM_END': false,
|
||||
'COUNTER_NET_SERVER_CONNECTION_CLOSE': false,
|
||||
'DTRACE_NET_SERVER_CONNECTION': false,
|
||||
'LTTNG_NET_SERVER_CONNECTION': false,
|
||||
'COUNTER_NET_SERVER_CONNECTION': false
|
||||
},
|
||||
|
||||
},
|
||||
'plugins': {
|
||||
promise
|
||||
},
|
||||
'rules': {
|
||||
'promise/always-return': 'error',
|
||||
'promise/no-return-wrap': 'error',
|
||||
'promise/param-names': 'error',
|
||||
'promise/catch-or-return': 'error',
|
||||
'promise/no-native': 'off',
|
||||
'promise/no-nesting': 'warn',
|
||||
'promise/no-promise-in-callback': 'warn',
|
||||
'promise/no-callback-in-promise': 'warn',
|
||||
'promise/no-return-in-finally': 'warn',
|
||||
|
||||
// Possible Errors
|
||||
// http://eslint.org/docs/rules/#possible-errors
|
||||
'comma-dangle': [2, 'only-multiline'],
|
||||
'no-control-regex': 2,
|
||||
'no-debugger': 2,
|
||||
'no-dupe-args': 2,
|
||||
'no-dupe-keys': 2,
|
||||
'no-duplicate-case': 2,
|
||||
'no-empty-character-class': 2,
|
||||
'no-ex-assign': 2,
|
||||
'no-extra-boolean-cast': 2,
|
||||
'no-extra-parens': [2, 'functions'],
|
||||
'no-extra-semi': 2,
|
||||
'no-func-assign': 2,
|
||||
'no-invalid-regexp': 2,
|
||||
'no-irregular-whitespace': 2,
|
||||
'no-negated-in-lhs': 2,
|
||||
'no-obj-calls': 2,
|
||||
'no-proto': 2,
|
||||
'no-unexpected-multiline': 2,
|
||||
'no-unreachable': 2,
|
||||
'use-isnan': 2,
|
||||
'valid-typeof': 2,
|
||||
|
||||
// Best Practices
|
||||
// http://eslint.org/docs/rules/#best-practices
|
||||
'no-fallthrough': 2,
|
||||
'no-octal': 2,
|
||||
'no-redeclare': 2,
|
||||
'no-self-assign': 2,
|
||||
'no-unused-labels': 2,
|
||||
|
||||
// Strict Mode
|
||||
// http://eslint.org/docs/rules/#strict-mode
|
||||
'strict': [2, 'never'],
|
||||
|
||||
// Variables
|
||||
// http://eslint.org/docs/rules/#variables
|
||||
'no-delete-var': 2,
|
||||
'no-undef': 2,
|
||||
'no-unused-vars': [2, { 'args': 'none' }],
|
||||
|
||||
// Node.js and CommonJS
|
||||
// http://eslint.org/docs/rules/#nodejs-and-commonjs
|
||||
'no-mixed-requires': 2,
|
||||
'no-new-require': 2,
|
||||
'no-path-concat': 2,
|
||||
'no-restricted-modules': [2, 'sys', '_linklist'],
|
||||
|
||||
// Stylistic Issues
|
||||
// http://eslint.org/docs/rules/#stylistic-issues
|
||||
'comma-spacing': 2,
|
||||
'eol-last': 2,
|
||||
'indent': [2, 2, { 'SwitchCase': 1 }],
|
||||
'keyword-spacing': 2,
|
||||
'max-len': [2, 120, 2],
|
||||
'new-parens': 2,
|
||||
'no-mixed-spaces-and-tabs': 2,
|
||||
'no-multiple-empty-lines': [2, { 'max': 2 }],
|
||||
'no-trailing-spaces': [2, { 'skipBlankLines': false }],
|
||||
'quotes': [2, 'single', 'avoid-escape'],
|
||||
'semi': 2,
|
||||
'space-before-blocks': [2, 'always'],
|
||||
'space-before-function-paren': [2, 'never'],
|
||||
'space-in-parens': [2, 'never'],
|
||||
'space-infix-ops': 2,
|
||||
'space-unary-ops': 2,
|
||||
|
||||
// ECMAScript 6
|
||||
// http://eslint.org/docs/rules/#ecmascript-6
|
||||
'arrow-parens': [2, 'always'],
|
||||
'arrow-spacing': [2, { 'before': true, 'after': true }],
|
||||
'constructor-super': 2,
|
||||
'no-class-assign': 2,
|
||||
'no-confusing-arrow': 2,
|
||||
'no-const-assign': 2,
|
||||
'no-dupe-class-members': 2,
|
||||
'no-new-symbol': 2,
|
||||
'no-this-before-super': 2,
|
||||
'prefer-const': 2
|
||||
},
|
||||
'ignores': []
|
||||
}
|
||||
];
|
||||
+6
-1
@@ -1,9 +1,12 @@
|
||||
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_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_PORT = process.env.JAMBONES_HTTP_PROXY_PORT;
|
||||
const JAMBONES_TTS_CACHE_DURATION_MINS = (parseInt(process.env.JAMBONES_TTS_CACHE_DURATION_MINS) || 4 * 60) * 60; // cache tts for 4 hours
|
||||
const JAMBONES_TTS_CACHE_DURATION_MINS =
|
||||
(parseInt(process.env.JAMBONES_TTS_CACHE_DURATION_MINS) || 4 * 60) * 60; // cache tts for 4 hours
|
||||
|
||||
const TMP_FOLDER = '/tmp';
|
||||
|
||||
@@ -12,9 +15,11 @@ const HTTP_TIMEOUT = 5000;
|
||||
module.exports = {
|
||||
JAMBONES_TTS_TRIM_SILENCE,
|
||||
JAMBONES_DISABLE_TTS_STREAMING,
|
||||
JAMBONES_DISABLE_AZURE_TTS_STREAMING,
|
||||
JAMBONES_HTTP_PROXY_IP,
|
||||
JAMBONES_HTTP_PROXY_PORT,
|
||||
JAMBONES_TTS_CACHE_DURATION_MINS,
|
||||
JAMBONES_EAGERLY_PRE_CACHE_AUDIO,
|
||||
TMP_FOLDER,
|
||||
HTTP_TIMEOUT
|
||||
};
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
const { STSClient, GetSessionTokenCommand, AssumeRoleCommand } = require('@aws-sdk/client-sts');
|
||||
const {makeAwsKey, noopLogger} = require('./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(
|
||||
logger, createHash, retrieveHash,
|
||||
awsAccessKeyId, awsSecretAccessKey, awsRegion, roleArn = null) {
|
||||
{accessKeyId, secretAccessKey, region, roleArn}) {
|
||||
logger = logger || noopLogger;
|
||||
try {
|
||||
const key = makeAwsKey(roleArn || awsAccessKeyId);
|
||||
const key = makeAwsKey(roleArn || accessKeyId);
|
||||
const obj = await retrieveHash(key);
|
||||
if (obj) return {...obj, servedFromCache: true};
|
||||
|
||||
let data;
|
||||
if (roleArn) {
|
||||
const stsClient = new STSClient({ region: awsRegion});
|
||||
const stsClient = new STSClient({ region });
|
||||
const roleToAssume = { RoleArn: roleArn, RoleSessionName: 'Jambonz_Speech', DurationSeconds: EXPIRY};
|
||||
const command = new AssumeRoleCommand(roleToAssume);
|
||||
|
||||
@@ -22,10 +24,10 @@ async function getAwsAuthToken(
|
||||
} else {
|
||||
/* access token not found in cache, so generate it using STS */
|
||||
const stsClient = new STSClient({
|
||||
region: awsRegion,
|
||||
region,
|
||||
credentials: {
|
||||
accessKeyId: awsAccessKeyId,
|
||||
secretAccessKey: awsSecretAccessKey,
|
||||
accessKeyId,
|
||||
secretAccessKey,
|
||||
}
|
||||
});
|
||||
const command = new GetSessionTokenCommand({DurationSeconds: EXPIRY});
|
||||
@@ -39,8 +41,7 @@ async function getAwsAuthToken(
|
||||
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)
|
||||
createHash(key, credentials, CACHE_EXPIRY)
|
||||
.catch((err) => logger.error(err, `Error saving hash for key ${key}`));
|
||||
|
||||
return {...credentials, servedFromCache: false};
|
||||
|
||||
@@ -107,7 +107,12 @@ const getAwsVoices = async(_client, createHash, retrieveHash, logger, credential
|
||||
} else if (roleArn) {
|
||||
client = new PollyClient({
|
||||
region,
|
||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
||||
credentials: await getAwsAuthToken(
|
||||
logger, createHash, retrieveHash,
|
||||
{
|
||||
region,
|
||||
roleArn
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
client = new PollyClient({region});
|
||||
|
||||
+99
-23
@@ -20,7 +20,8 @@ const {
|
||||
createKryptonClient,
|
||||
createRivaClient,
|
||||
noopLogger,
|
||||
makeFilePath
|
||||
makeFilePath,
|
||||
makePlayhtKey
|
||||
} = require('./utils');
|
||||
const getNuanceAccessToken = require('./get-nuance-access-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 {
|
||||
JAMBONES_DISABLE_TTS_STREAMING,
|
||||
JAMBONES_DISABLE_AZURE_TTS_STREAMING,
|
||||
JAMBONES_HTTP_PROXY_IP,
|
||||
JAMBONES_HTTP_PROXY_PORT,
|
||||
JAMBONES_TTS_CACHE_DURATION_MINS,
|
||||
JAMBONES_EAGERLY_PRE_CACHE_AUDIO,
|
||||
} = require('./config');
|
||||
const EXPIRES = JAMBONES_TTS_CACHE_DURATION_MINS;
|
||||
const OpenAI = require('openai');
|
||||
@@ -85,7 +88,7 @@ const trimTrailingSilence = (buffer) => {
|
||||
*/
|
||||
async function synthAudio(client, createHash, retrieveHash, logger, stats, { account_sid,
|
||||
vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId,
|
||||
disableTtsCache, renderForCaching, disableTtsStreaming, options
|
||||
disableTtsCache, renderForCaching = false, disableTtsStreaming, options
|
||||
}) {
|
||||
let audioBuffer;
|
||||
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(credentials.client_id, 'synthAudio requires client_id 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({
|
||||
account_sid,
|
||||
vendor,
|
||||
language: language || '',
|
||||
voice: voice || deploymentId,
|
||||
engine,
|
||||
text
|
||||
text,
|
||||
renderForCaching
|
||||
});
|
||||
let filePath;
|
||||
filePath = makeFilePath(vendor, key, salt);
|
||||
filePath = makeFilePath({vendor, key, salt, renderForCaching});
|
||||
debug(`synth key is ${key}`);
|
||||
let cached;
|
||||
if (!disableTtsCache) {
|
||||
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) {
|
||||
// found in cache - extend the expiry and use it
|
||||
@@ -194,7 +224,7 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
|
||||
case 'microsoft':
|
||||
vendorLabel = 'microsoft';
|
||||
audioBuffer = await synthMicrosoft(logger, {credentials, stats, language, voice, text, deploymentId,
|
||||
filePath, renderForCaching, disableTtsStreaming});
|
||||
filePath, renderForCaching, disableTtsStreaming});
|
||||
break;
|
||||
case 'nuance':
|
||||
model = model || 'enhanced';
|
||||
@@ -212,21 +242,21 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
|
||||
case 'elevenlabs':
|
||||
audioBuffer = await synthElevenlabs(logger, {
|
||||
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming, filePath
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'playht':
|
||||
audioBuffer = await synthPlayHT(logger, {
|
||||
audioBuffer = await synthPlayHT(client, logger, {
|
||||
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming, filePath
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'rimelabs':
|
||||
audioBuffer = await synthRimelabs(logger, {
|
||||
credentials, options, stats, language, voice, text, renderForCaching, disableTtsStreaming, filePath
|
||||
});
|
||||
});
|
||||
break;
|
||||
case 'whisper':
|
||||
audioBuffer = await synthWhisper(logger, {
|
||||
credentials, stats, voice, text, renderForCaching, disableTtsStreaming});
|
||||
credentials, stats, voice, text, renderForCaching, disableTtsStreaming});
|
||||
break;
|
||||
case 'verbio':
|
||||
audioBuffer = await synthVerbio(client, logger, {
|
||||
@@ -235,7 +265,7 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
|
||||
break;
|
||||
case 'deepgram':
|
||||
audioBuffer = await synthDeepgram(logger, {credentials, stats, model, text,
|
||||
renderForCaching, disableTtsStreaming});
|
||||
renderForCaching, disableTtsStreaming});
|
||||
break;
|
||||
case vendor.startsWith('custom') ? vendor : 'cant_match_value':
|
||||
({ audioBuffer, filePath } = await synthCustomVendor(logger,
|
||||
@@ -244,7 +274,7 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
|
||||
default:
|
||||
assert(`synthAudio: unsupported speech vendor ${vendor}`);
|
||||
}
|
||||
if('filePath' in audioBuffer) return audioBuffer;
|
||||
if ('filePath' in audioBuffer) return audioBuffer;
|
||||
const diff = process.hrtime(startAt);
|
||||
const time = diff[0] * 1e3 + diff[1] * 1e-6;
|
||||
rtt = time.toFixed(0);
|
||||
@@ -280,7 +310,12 @@ const synthPolly = async(createHash, retrieveHash, logger,
|
||||
} else if (roleArn) {
|
||||
polly = new PollyClient({
|
||||
region,
|
||||
credentials: await getAwsAuthToken(logger, createHash, retrieveHash, null, null, region, roleArn),
|
||||
credentials: await getAwsAuthToken(
|
||||
logger, createHash, retrieveHash,
|
||||
{
|
||||
region,
|
||||
roleArn
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
// 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>`;
|
||||
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 = '';
|
||||
params += `{api_key=${apiKey}`;
|
||||
params += `,language=${language}`;
|
||||
@@ -684,6 +720,8 @@ const synthElevenlabs = async(logger, {
|
||||
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';
|
||||
if (opts.previous_text) params += `,previous_text=${opts.previous_text}`;
|
||||
if (opts.next_text) params += `,next_text=${opts.next_text}`;
|
||||
params += '}';
|
||||
|
||||
return {
|
||||
@@ -718,12 +756,40 @@ const synthElevenlabs = async(logger, {
|
||||
}
|
||||
};
|
||||
|
||||
const synthPlayHT = async(logger, {
|
||||
credentials, options, stats, voice, text, renderForCaching, disableTtsStreaming
|
||||
const synthPlayHT = async(client, logger, {
|
||||
credentials, options, stats, voice, language, text, renderForCaching, disableTtsStreaming
|
||||
}) => {
|
||||
const {api_key, user_id, voice_engine, options: credOpts} = credentials;
|
||||
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);
|
||||
} else {
|
||||
// Use cached URL
|
||||
synthesizeUrl = url;
|
||||
}
|
||||
} 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 */
|
||||
if (!JAMBONES_DISABLE_TTS_STREAMING && !renderForCaching && !disableTtsStreaming) {
|
||||
let params = '';
|
||||
@@ -732,7 +798,9 @@ const synthPlayHT = async(logger, {
|
||||
params += ',vendor=playht';
|
||||
params += `,voice=${voice}`;
|
||||
params += `,voice_engine=${voice_engine}`;
|
||||
params += `,synthesize_url=${synthesizeUrl}`;
|
||||
params += ',write_cache_file=1';
|
||||
params += `,language=${language}`;
|
||||
if (opts.quality) params += `,quality=${opts.quality}`;
|
||||
if (opts.speed) params += `,speed=${opts.speed}`;
|
||||
if (opts.seed) params += `,style=${opts.seed}`;
|
||||
@@ -741,6 +809,8 @@ const synthPlayHT = async(logger, {
|
||||
if (opts.voice_guidance) params += `,voice_guidance=${opts.voice_guidance}`;
|
||||
if (opts.style_guidance) params += `,style_guidance=${opts.style_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 += '}';
|
||||
|
||||
return {
|
||||
@@ -751,14 +821,18 @@ const synthPlayHT = async(logger, {
|
||||
}
|
||||
|
||||
try {
|
||||
const post = bent('https://api.play.ht', 'POST', 'buffer', {
|
||||
'AUTHORIZATION': api_key,
|
||||
'X-USER-ID': user_id,
|
||||
const post = bent('POST', 'buffer', {
|
||||
...(voice_engine !== 'Play3.0' && {
|
||||
'AUTHORIZATION': api_key,
|
||||
'X-USER-ID': user_id,
|
||||
}),
|
||||
'Accept': 'audio/mpeg',
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
const mp3 = await post('/api/v2/tts/stream', {
|
||||
|
||||
const mp3 = await post(synthesizeUrl, {
|
||||
text,
|
||||
...(voice_engine === 'Play3.0' && { language }),
|
||||
voice,
|
||||
voice_engine,
|
||||
output_format: 'mp3',
|
||||
@@ -900,13 +974,14 @@ const synthWhisper = async(logger, {credentials, stats, voice, text, renderForCa
|
||||
};
|
||||
|
||||
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) {
|
||||
let params = '';
|
||||
params += `{api_key=${api_key}`;
|
||||
params += ',vendor=deepgram';
|
||||
params += `,voice=${model}`;
|
||||
params += ',write_cache_file=1';
|
||||
if (deepgram_tts_uri) params += `,endpoint=${deepgram_tts_uri}`;
|
||||
params += '}';
|
||||
|
||||
return {
|
||||
@@ -916,8 +991,9 @@ const synthDeepgram = async(logger, {credentials, stats, model, text, renderForC
|
||||
};
|
||||
}
|
||||
try {
|
||||
const post = bent('https://api.deepgram.com', 'POST', 'buffer', {
|
||||
'Authorization': `Token ${api_key}`,
|
||||
const post = bent(deepgram_tts_uri || 'https://api.deepgram.com', 'POST', 'buffer', {
|
||||
// on-premise deepgram does not require to have api_key
|
||||
...(api_key && {'Authorization': `Token ${api_key}`}),
|
||||
'Accept': 'audio/mpeg',
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
+28
-10
@@ -16,45 +16,57 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||
*/
|
||||
//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');
|
||||
hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
|
||||
const hexHashKey = hash.digest('hex');
|
||||
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}`;
|
||||
return key;
|
||||
}
|
||||
|
||||
function makeFilePath(vendor, key, salt = '') {
|
||||
const extension = getFileExtension(vendor);
|
||||
function makeFilePath({vendor, key, salt = '', renderForCaching = false}) {
|
||||
const extension = getFileExtension({vendor, renderForCaching});
|
||||
return `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt}`)}.${extension}`;
|
||||
}
|
||||
|
||||
function getFileExtension(vendor) {
|
||||
function getFileExtension({vendor, renderForCaching = false}) {
|
||||
const mp3Extension = 'mp3';
|
||||
const r8Extension = 'r8';
|
||||
|
||||
switch (vendor) {
|
||||
case 'azure':
|
||||
case 'microsoft':
|
||||
if (!JAMBONES_DISABLE_TTS_STREAMING || JAMBONES_TTS_TRIM_SILENCE) {
|
||||
if (!renderForCaching && !JAMBONES_DISABLE_TTS_STREAMING || JAMBONES_TTS_TRIM_SILENCE) {
|
||||
return r8Extension;
|
||||
} else {
|
||||
return mp3Extension;
|
||||
}
|
||||
case 'deepgram':
|
||||
case 'elevenlabs':
|
||||
case 'rimlabs':
|
||||
if (!JAMBONES_DISABLE_TTS_STREAMING) {
|
||||
return r8Extension;
|
||||
} else {
|
||||
case 'rimelabs':
|
||||
case 'playht':
|
||||
if (renderForCaching || JAMBONES_DISABLE_TTS_STREAMING) {
|
||||
return mp3Extension;
|
||||
} else {
|
||||
return r8Extension;
|
||||
}
|
||||
case 'nuance':
|
||||
case 'nvidia':
|
||||
case 'verbio':
|
||||
return r8Extension;
|
||||
default:
|
||||
// If vendor is custom
|
||||
if (vendor.startsWith('custom')) {
|
||||
if (renderForCaching || JAMBONES_DISABLE_TTS_STREAMING) {
|
||||
return mp3Extension;
|
||||
} else {
|
||||
return r8Extension;
|
||||
}
|
||||
}
|
||||
return mp3Extension;
|
||||
}
|
||||
}
|
||||
@@ -86,6 +98,11 @@ function makeAwsKey(awsAccessKeyId) {
|
||||
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) {
|
||||
const hash = crypto.createHash('sha1');
|
||||
hash.update(client_id);
|
||||
@@ -159,6 +176,7 @@ module.exports = {
|
||||
makeSynthKey,
|
||||
makeNuanceKey,
|
||||
makeIbmKey,
|
||||
makePlayhtKey,
|
||||
makeAwsKey,
|
||||
makeVerbioKey,
|
||||
getNuanceAccessToken,
|
||||
|
||||
Generated
+1974
-1735
File diff suppressed because it is too large
Load Diff
+11
-9
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@jambonz/speech-utils",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.20",
|
||||
"description": "TTS-related speech utilities for jambonz",
|
||||
"main": "index.js",
|
||||
"author": "Dave Horton",
|
||||
@@ -12,8 +12,9 @@
|
||||
"test": "NODE_ENV=test node test/ ",
|
||||
"coverage": "nyc --reporter html --report-dir ./coverage npm run test",
|
||||
"jslint": "eslint index.js lib",
|
||||
"jslint:fix": "eslint --fix '**/*.js'",
|
||||
"build": "./build_stubs.sh"
|
||||
"jslint:fix": "npm run jslint --fix",
|
||||
"build": "./build_stubs.sh",
|
||||
"prepare": "husky"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -35,16 +36,17 @@
|
||||
"form-urlencoded": "^6.1.4",
|
||||
"google-protobuf": "^3.21.2",
|
||||
"ibm-watson": "^8.0.0",
|
||||
"microsoft-cognitiveservices-speech-sdk": "1.36.0",
|
||||
"microsoft-cognitiveservices-speech-sdk": "1.38.0",
|
||||
"openai": "^4.25.0",
|
||||
"undici": "^6.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"config": "^3.3.10",
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-plugin-promise": "^6.1.1",
|
||||
"config": "^3.3.11",
|
||||
"eslint": "^9.3.0",
|
||||
"eslint-plugin-promise": "^6.2.0",
|
||||
"husky": "^9.0.11",
|
||||
"nyc": "^15.1.0",
|
||||
"pino": "^8.17.0",
|
||||
"tape": "^5.7.3"
|
||||
"pino": "^9.1.0",
|
||||
"tape": "^5.7.5"
|
||||
}
|
||||
}
|
||||
|
||||
+10
-2
@@ -19,12 +19,20 @@ test('AWS - create and cache auth token', async(t) => {
|
||||
return;
|
||||
}
|
||||
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');
|
||||
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);
|
||||
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');
|
||||
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);
|
||||
}
|
||||
client.quit();
|
||||
})
|
||||
});
|
||||
|
||||
test('PlayHT speech synth tests', async(t) => {
|
||||
const testPlayHT = async(t, voice_engine) => {
|
||||
const fn = require('..');
|
||||
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');
|
||||
return t.end();
|
||||
}
|
||||
const text = 'Hi there and welcome to jambones!';
|
||||
const text = 'Hi there and welcome to jambones! ' + Date.now();
|
||||
try {
|
||||
let opts = await synthAudio(stats, {
|
||||
const opts = await synthAudio(stats, {
|
||||
vendor: 'playht',
|
||||
credentials: {
|
||||
api_key: process.env.PLAYHT_API_KEY,
|
||||
user_id: process.env.PLAYHT_USER_ID,
|
||||
voice_engine: 'PlayHT2.0-turbo',
|
||||
voice_engine,
|
||||
options: JSON.stringify({
|
||||
quality: "medium",
|
||||
quality: 'medium',
|
||||
speed: 1,
|
||||
seed: 1,
|
||||
temperature: 1,
|
||||
emotion: "female_happy",
|
||||
emotion: 'female_happy',
|
||||
voice_guidance: 3,
|
||||
style_guidance: 20,
|
||||
text_guidance: 1,
|
||||
})
|
||||
},
|
||||
language: 'en-US',
|
||||
language: 'english',
|
||||
voice: 's3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json',
|
||||
text,
|
||||
renderForCaching: true
|
||||
@@ -615,6 +615,14 @@ test('PlayHT speech synth tests', async(t) => {
|
||||
t.end(err);
|
||||
}
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user