mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-10-03 23:33:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d914b26cac | ||
|
|
7c7be6bbb1 | ||
|
|
46127bb763 | ||
|
|
17305325ff | ||
|
|
0e883cf82a | ||
|
|
2f3a766713 | ||
|
|
b429645ec2 | ||
|
|
0becf33bab | ||
|
|
3d3875741c |
+37
-10
@@ -32,7 +32,7 @@ const {
|
|||||||
const {SynthesizeSpeechRequest} = require('../stubs/riva/proto/riva_tts_pb');
|
const {SynthesizeSpeechRequest} = require('../stubs/riva/proto/riva_tts_pb');
|
||||||
const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
|
const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
|
||||||
const debug = require('debug')('jambonz:realtimedb-helpers');
|
const debug = require('debug')('jambonz:realtimedb-helpers');
|
||||||
const EXPIRES = process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 3600 * 24; // cache tts for 24 hours
|
const EXPIRES = (process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 24 * 60) * 60; // cache tts for 24 hours
|
||||||
const TMP_FOLDER = '/tmp';
|
const TMP_FOLDER = '/tmp';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,7 +155,8 @@ async function synthAudio(client, logger, stats, { account_sid,
|
|||||||
audioBuffer = await synthWellSaid(logger, {credentials, stats, language, voice, text, filePath});
|
audioBuffer = await synthWellSaid(logger, {credentials, stats, language, voice, text, filePath});
|
||||||
break;
|
break;
|
||||||
case vendor.startsWith('custom') ? vendor : 'cant_match_value':
|
case vendor.startsWith('custom') ? vendor : 'cant_match_value':
|
||||||
audioBuffer = await synthCustomVendor(logger, {credentials, stats, language, voice, text});
|
({ audioBuffer, filePath } = await synthCustomVendor(logger,
|
||||||
|
{credentials, stats, language, voice, text, filePath}));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(`synthAudio: unsupported speech vendor ${vendor}`);
|
assert(`synthAudio: unsupported speech vendor ${vendor}`);
|
||||||
@@ -183,7 +184,7 @@ async function synthAudio(client, logger, stats, { account_sid,
|
|||||||
|
|
||||||
const synthPolly = async(logger, {credentials, stats, language, voice, engine, text}) => {
|
const synthPolly = async(logger, {credentials, stats, language, voice, engine, text}) => {
|
||||||
try {
|
try {
|
||||||
const polly = new PollyClient(credentials);
|
const polly = new PollyClient({...credentials});
|
||||||
const opts = {
|
const opts = {
|
||||||
Engine: engine,
|
Engine: engine,
|
||||||
OutputFormat: 'mp3',
|
OutputFormat: 'mp3',
|
||||||
@@ -440,30 +441,56 @@ const synthNvidia = async(client, logger, {credentials, stats, language, voice,
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// CustomVendor accept only mp3
|
const synthCustomVendor = async(logger, {credentials, stats, language, voice, text, filePath}) => {
|
||||||
const synthCustomVendor = async(logger, {credentials, stats, language, voice, text}) => {
|
|
||||||
const {vendor, auth_token, custom_tts_url} = credentials;
|
const {vendor, auth_token, custom_tts_url} = credentials;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const post = bent('POST', 'buffer', {
|
const post = bent('POST', {
|
||||||
'Authorization': `Bearer ${auth_token}`,
|
'Authorization': `Bearer ${auth_token}`,
|
||||||
'Accept': 'audio/mpeg',
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
});
|
});
|
||||||
|
|
||||||
const mp3 = await post(custom_tts_url, {
|
const response = await post(custom_tts_url, {
|
||||||
language,
|
language,
|
||||||
format: 'audio/mpeg',
|
|
||||||
voice,
|
voice,
|
||||||
type: text.startsWith('<speak>') ? 'ssml' : 'text',
|
type: text.startsWith('<speak>') ? 'ssml' : 'text',
|
||||||
text
|
text
|
||||||
});
|
});
|
||||||
|
|
||||||
return mp3;
|
const regex = /\.[^\.]*$/g;
|
||||||
|
const mime = response.headers['content-type'];
|
||||||
|
const buffer = await response.arrayBuffer();
|
||||||
|
return {
|
||||||
|
audioBuffer: buffer,
|
||||||
|
filePath: filePath.replace(regex, getFileExtFromMime(mime))
|
||||||
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.info({err}, `Vendor ${vendor} returned error`);
|
logger.info({err}, `Vendor ${vendor} returned error`);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getFileExtFromMime = (mime) => {
|
||||||
|
switch (mime) {
|
||||||
|
case 'audio/wav':
|
||||||
|
case 'audio/x-wav':
|
||||||
|
return '.wav';
|
||||||
|
case /audio\/l16.*rate=8000/.test(mime) ? mime : 'cant match value':
|
||||||
|
return '.r8';
|
||||||
|
case /audio\/l16.*rate=16000/.test(mime) ? mime : 'cant match value':
|
||||||
|
return '.r16';
|
||||||
|
case /audio\/l16.*rate=24000/.test(mime) ? mime : 'cant match value':
|
||||||
|
return '.r24';
|
||||||
|
case /audio\/l16.*rate=32000/.test(mime) ? mime : 'cant match value':
|
||||||
|
return '.r32';
|
||||||
|
case /audio\/l16.*rate=48000/.test(mime) ? mime : 'cant match value':
|
||||||
|
return '.r48';
|
||||||
|
case 'audio/mpeg':
|
||||||
|
case 'audio/mp3':
|
||||||
|
return '.mp3';
|
||||||
|
default:
|
||||||
|
return '.wav';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = synthAudio;
|
module.exports = synthAudio;
|
||||||
|
|||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@jambonz/speech-utils",
|
"name": "@jambonz/speech-utils",
|
||||||
"version": "0.0.2",
|
"version": "0.0.5",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@jambonz/speech-utils",
|
"name": "@jambonz/speech-utils",
|
||||||
"version": "0.0.2",
|
"version": "0.0.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@aws-sdk/client-polly": "^3.276.0",
|
"@aws-sdk/client-polly": "^3.276.0",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@jambonz/speech-utils",
|
"name": "@jambonz/speech-utils",
|
||||||
"version": "0.0.2",
|
"version": "0.0.5",
|
||||||
"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",
|
||||||
|
|||||||
@@ -348,7 +348,6 @@ test('Custom Vendor speech synth tests', async(t) => {
|
|||||||
let obj = await getJSON(`http://127.0.0.1:3100/lastRequest/somethingnew`);
|
let obj = await getJSON(`http://127.0.0.1:3100/lastRequest/somethingnew`);
|
||||||
t.ok(obj.headers.Authorization == 'Bearer some_jwt_token', 'Custom Vendor Authentication Header is correct');
|
t.ok(obj.headers.Authorization == 'Bearer some_jwt_token', 'Custom Vendor Authentication Header is correct');
|
||||||
t.ok(obj.body.language == 'en-US', 'Custom Vendor Language is correct');
|
t.ok(obj.body.language == 'en-US', 'Custom Vendor Language is correct');
|
||||||
t.ok(obj.body.format == 'audio/mpeg', 'Custom Vendor format is correct');
|
|
||||||
t.ok(obj.body.voice == 'English-US.Female-1', 'Custom Vendor voice is correct');
|
t.ok(obj.body.voice == 'English-US.Female-1', 'Custom Vendor voice is correct');
|
||||||
t.ok(obj.body.type == 'text', 'Custom Vendor type is correct');
|
t.ok(obj.body.type == 'text', 'Custom Vendor type is correct');
|
||||||
t.ok(obj.body.text == 'This is a test. This is only a test', 'Custom Vendor text is correct');
|
t.ok(obj.body.text == 'This is a test. This is only a test', 'Custom Vendor text is correct');
|
||||||
|
|||||||
Reference in New Issue
Block a user