@jambonz/speech-utils 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/synth-audio.js +36 -9
- package/package.json +1 -1
- package/test/synth.js +0 -1
package/lib/synth-audio.js
CHANGED
|
@@ -32,7 +32,7 @@ const {
|
|
|
32
32
|
const {SynthesizeSpeechRequest} = require('../stubs/riva/proto/riva_tts_pb');
|
|
33
33
|
const {AudioEncoding} = require('../stubs/riva/proto/riva_audio_pb');
|
|
34
34
|
const debug = require('debug')('jambonz:realtimedb-helpers');
|
|
35
|
-
const EXPIRES = process.env.JAMBONES_TTS_CACHE_DURATION_MINS ||
|
|
35
|
+
const EXPIRES = (process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 24 * 60) * 60; // cache tts for 24 hours
|
|
36
36
|
const TMP_FOLDER = '/tmp';
|
|
37
37
|
|
|
38
38
|
/**
|
|
@@ -155,7 +155,8 @@ async function synthAudio(client, logger, stats, { account_sid,
|
|
|
155
155
|
audioBuffer = await synthWellSaid(logger, {credentials, stats, language, voice, text, filePath});
|
|
156
156
|
break;
|
|
157
157
|
case vendor.startsWith('custom') ? vendor : 'cant_match_value':
|
|
158
|
-
audioBuffer = await synthCustomVendor(logger,
|
|
158
|
+
({ audioBuffer, filePath } = await synthCustomVendor(logger,
|
|
159
|
+
{credentials, stats, language, voice, text, filePath}));
|
|
159
160
|
break;
|
|
160
161
|
default:
|
|
161
162
|
assert(`synthAudio: unsupported speech vendor ${vendor}`);
|
|
@@ -440,30 +441,56 @@ const synthNvidia = async(client, logger, {credentials, stats, language, voice,
|
|
|
440
441
|
};
|
|
441
442
|
|
|
442
443
|
|
|
443
|
-
|
|
444
|
-
const synthCustomVendor = async(logger, {credentials, stats, language, voice, text}) => {
|
|
444
|
+
const synthCustomVendor = async(logger, {credentials, stats, language, voice, text, filePath}) => {
|
|
445
445
|
const {vendor, auth_token, custom_tts_url} = credentials;
|
|
446
446
|
|
|
447
447
|
try {
|
|
448
|
-
const post = bent('POST',
|
|
448
|
+
const post = bent('POST', {
|
|
449
449
|
'Authorization': `Bearer ${auth_token}`,
|
|
450
|
-
'Accept': 'audio/mpeg',
|
|
451
450
|
'Content-Type': 'application/json'
|
|
452
451
|
});
|
|
453
452
|
|
|
454
|
-
const
|
|
453
|
+
const response = await post(custom_tts_url, {
|
|
455
454
|
language,
|
|
456
|
-
format: 'audio/mpeg',
|
|
457
455
|
voice,
|
|
458
456
|
type: text.startsWith('<speak>') ? 'ssml' : 'text',
|
|
459
457
|
text
|
|
460
458
|
});
|
|
461
459
|
|
|
462
|
-
|
|
460
|
+
const regex = /\.[^\.]*$/g;
|
|
461
|
+
const mime = response.headers['content-type'];
|
|
462
|
+
const buffer = await response.arrayBuffer();
|
|
463
|
+
return {
|
|
464
|
+
audioBuffer: buffer,
|
|
465
|
+
filePath: filePath.replace(regex, getFileExtFromMime(mime))
|
|
466
|
+
};
|
|
463
467
|
} catch (err) {
|
|
464
468
|
logger.info({err}, `Vendor ${vendor} returned error`);
|
|
465
469
|
throw err;
|
|
466
470
|
}
|
|
467
471
|
};
|
|
468
472
|
|
|
473
|
+
const getFileExtFromMime = (mime) => {
|
|
474
|
+
switch (mime) {
|
|
475
|
+
case 'audio/wav':
|
|
476
|
+
case 'audio/x-wav':
|
|
477
|
+
return '.wav';
|
|
478
|
+
case /audio\/l16.*rate=8000/.test(mime) ? mime : 'cant match value':
|
|
479
|
+
return '.r8';
|
|
480
|
+
case /audio\/l16.*rate=16000/.test(mime) ? mime : 'cant match value':
|
|
481
|
+
return '.r16';
|
|
482
|
+
case /audio\/l16.*rate=24000/.test(mime) ? mime : 'cant match value':
|
|
483
|
+
return '.r24';
|
|
484
|
+
case /audio\/l16.*rate=32000/.test(mime) ? mime : 'cant match value':
|
|
485
|
+
return '.r32';
|
|
486
|
+
case /audio\/l16.*rate=48000/.test(mime) ? mime : 'cant match value':
|
|
487
|
+
return '.r48';
|
|
488
|
+
case 'audio/mpeg':
|
|
489
|
+
case 'audio/mp3':
|
|
490
|
+
return '.mp3';
|
|
491
|
+
default:
|
|
492
|
+
return '.wav';
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
|
|
469
496
|
module.exports = synthAudio;
|
package/package.json
CHANGED
package/test/synth.js
CHANGED
|
@@ -348,7 +348,6 @@ test('Custom Vendor speech synth tests', async(t) => {
|
|
|
348
348
|
let obj = await getJSON(`http://127.0.0.1:3100/lastRequest/somethingnew`);
|
|
349
349
|
t.ok(obj.headers.Authorization == 'Bearer some_jwt_token', 'Custom Vendor Authentication Header is correct');
|
|
350
350
|
t.ok(obj.body.language == 'en-US', 'Custom Vendor Language is correct');
|
|
351
|
-
t.ok(obj.body.format == 'audio/mpeg', 'Custom Vendor format is correct');
|
|
352
351
|
t.ok(obj.body.voice == 'English-US.Female-1', 'Custom Vendor voice is correct');
|
|
353
352
|
t.ok(obj.body.type == 'text', 'Custom Vendor type is correct');
|
|
354
353
|
t.ok(obj.body.text == 'This is a test. This is only a test', 'Custom Vendor text is correct');
|