@jambonz/speech-utils 0.0.36 → 0.0.38
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/index.js +1 -0
- package/lib/add-file-to-cache.js +30 -0
- package/lib/synth-audio.js +4 -0
- package/package.json +1 -1
- package/test/synth.js +9 -1
package/index.js
CHANGED
|
@@ -12,6 +12,7 @@ module.exports = (opts, logger) => {
|
|
|
12
12
|
client,
|
|
13
13
|
getTtsSize: require('./lib/get-tts-size').bind(null, client, logger),
|
|
14
14
|
purgeTtsCache: require('./lib/purge-tts-cache').bind(null, client, logger),
|
|
15
|
+
addFileToCache: require('./lib/add-file-to-cache').bind(null, client, logger),
|
|
15
16
|
synthAudio: require('./lib/synth-audio').bind(null, client, logger),
|
|
16
17
|
getNuanceAccessToken: require('./lib/get-nuance-access-token').bind(null, client, logger),
|
|
17
18
|
getIbmAccessToken: require('./lib/get-ibm-access-token').bind(null, client, logger),
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const fs = require('fs/promises');
|
|
2
|
+
const {noopLogger, makeSynthKey} = require('./utils');
|
|
3
|
+
const EXPIRES = (process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 4 * 60) * 60; // cache tts for 4 hours
|
|
4
|
+
|
|
5
|
+
async function addFileToCache(client, logger, path,
|
|
6
|
+
{account_sid, vendor, language, voice, deploymentId, engine, text}) {
|
|
7
|
+
let key;
|
|
8
|
+
logger = logger || noopLogger;
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
key = makeSynthKey({
|
|
12
|
+
account_sid,
|
|
13
|
+
vendor,
|
|
14
|
+
language: language || '',
|
|
15
|
+
voice: voice || deploymentId,
|
|
16
|
+
engine,
|
|
17
|
+
text,
|
|
18
|
+
});
|
|
19
|
+
const audioBuffer = await fs.readFile(path);
|
|
20
|
+
await client.setex(key, EXPIRES, audioBuffer.toString('base64'));
|
|
21
|
+
} catch (err) {
|
|
22
|
+
logger.error(err, 'addFileToCache: Error');
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
logger.debug(`addFileToCache: added ${path} to cache with key ${key}`);
|
|
27
|
+
return key;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = addFileToCache;
|
package/lib/synth-audio.js
CHANGED
|
@@ -143,6 +143,10 @@ async function synthAudio(client, logger, stats, { account_sid,
|
|
|
143
143
|
(
|
|
144
144
|
process.env.JAMBONES_TTS_TRIM_SILENCE &&
|
|
145
145
|
['microsoft', 'azure'].includes(vendor)
|
|
146
|
+
) ||
|
|
147
|
+
(
|
|
148
|
+
process.env.JAMBONES_ELEVENLABS_STREAMING &&
|
|
149
|
+
vendor === 'elevenlabs'
|
|
146
150
|
)
|
|
147
151
|
) {
|
|
148
152
|
filePath = `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt || ''}`)}.r8`;
|
package/package.json
CHANGED
package/test/synth.js
CHANGED
|
@@ -20,7 +20,7 @@ const stats = {
|
|
|
20
20
|
|
|
21
21
|
test('Google speech synth tests', async(t) => {
|
|
22
22
|
const fn = require('..');
|
|
23
|
-
const {synthAudio, client} = fn(opts, logger);
|
|
23
|
+
const {synthAudio, addFileToCache, client} = fn(opts, logger);
|
|
24
24
|
|
|
25
25
|
if (!process.env.GCP_FILE && !process.env.GCP_JSON_KEY) {
|
|
26
26
|
t.pass('skipping google speech synth tests since neither GCP_FILE nor GCP_JSON_KEY provided');
|
|
@@ -58,6 +58,14 @@ test('Google speech synth tests', async(t) => {
|
|
|
58
58
|
});
|
|
59
59
|
t.ok(opts.servedFromCache, `successfully retrieved cached google audio from ${opts.filePath}`);
|
|
60
60
|
|
|
61
|
+
const success = await addFileToCache(opts.filePath, {
|
|
62
|
+
vendor: 'google',
|
|
63
|
+
language: 'en-GB',
|
|
64
|
+
gender: 'FEMALE',
|
|
65
|
+
text: 'This is a test. This is only a test'
|
|
66
|
+
});
|
|
67
|
+
t.ok(success, `successfully added ${opts.filePath} to cache`);
|
|
68
|
+
|
|
61
69
|
opts = await synthAudio(stats, {
|
|
62
70
|
vendor: 'google',
|
|
63
71
|
credentials: {
|