@jambonz/speech-utils 0.0.35 → 0.0.37

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 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,29 @@
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
+ logger = logger || noopLogger;
8
+
9
+ try {
10
+ const key = makeSynthKey({
11
+ account_sid,
12
+ vendor,
13
+ language: language || '',
14
+ voice: voice || deploymentId,
15
+ engine,
16
+ text,
17
+ });
18
+ const audioBuffer = fs.readFile(path);
19
+ await client.setex(key, EXPIRES, audioBuffer.toString('base64'));
20
+ } catch (err) {
21
+ logger.error(err, 'addFileToCache: Error');
22
+ return false;
23
+ }
24
+
25
+ logger.debug(`addFileToCache: added ${path} to cache`);
26
+ return true;
27
+ }
28
+
29
+ module.exports = addFileToCache;
@@ -613,6 +613,7 @@ const synthElevenlabs = async(logger, {credentials, options, stats, language, vo
613
613
  params += `{api_key=${api_key}`;
614
614
  params += `,model_id=${model_id}`;
615
615
  params += `,optimize_streaming_latency=${opts.optimize_streaming_latency || 2}`;
616
+ params += ',write_cache_file=1';
616
617
  if (opts.voice_settings?.similarity_boost) params += `,similarity_boost=${opts.voice_settings.similarity_boost}`;
617
618
  if (opts.voice_settings?.stability) params += `,stability=${opts.voice_settings.stability}`;
618
619
  if (opts.voice_settings?.style) params += `,style=${opts.voice_settings.style}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jambonz/speech-utils",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "description": "TTS-related speech utilities for jambonz",
5
5
  "main": "index.js",
6
6
  "author": "Dave Horton",
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: {