@jambonz/speech-utils 0.0.1 → 0.0.2

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.
@@ -11,7 +11,8 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
11
11
  * @param {string} opts.text - text or ssml to synthesize
12
12
  * @returns {object} result - {error, purgedCount}
13
13
  */
14
- async function purgeTtsCache(client, logger, {all, vendor, language, voice, deploymentId, engine, text} = {all: true}) {
14
+ async function purgeTtsCache(client, logger, {all, account_sid, vendor,
15
+ language, voice, deploymentId, engine, text} = {all: true}) {
15
16
  logger = logger || noopLogger;
16
17
 
17
18
  let purgedCount = 0, error;
@@ -21,8 +22,13 @@ async function purgeTtsCache(client, logger, {all, vendor, language, voice, depl
21
22
  const keys = await client.keysAsync('tts:*');
22
23
  purgedCount = await client.delAsync(keys);
23
24
 
24
- } else {
25
+ } else if (account_sid && !vendor && !language && !voice && !engine && !text) {
26
+ const keys = await client.keysAsync(`tts:${account_sid}:*`);
27
+ purgedCount = await client.delAsync(keys);
28
+ }
29
+ else {
25
30
  const key = makeSynthKey({
31
+ account_sid,
26
32
  vendor,
27
33
  language: language || '',
28
34
  voice: voice || deploymentId,
@@ -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 = 3600 * 24; // cache tts for 24 hours
35
+ const EXPIRES = process.env.JAMBONES_TTS_CACHE_DURATION_MINS || 3600 * 24; // cache tts for 24 hours
36
36
  const TMP_FOLDER = '/tmp';
37
37
 
38
38
  /**
@@ -52,7 +52,7 @@ const TMP_FOLDER = '/tmp';
52
52
  * @returns object containing filepath to an mp3 file in the /tmp folder containing
53
53
  * the synthesized audio, and a variable indicating whether it was served from cache
54
54
  */
55
- async function synthAudio(client, logger, stats, {
55
+ async function synthAudio(client, logger, stats, { account_sid,
56
56
  vendor, language, voice, gender, text, engine, salt, model, credentials, deploymentId, disableTtsCache
57
57
  }) {
58
58
  let audioBuffer;
@@ -95,8 +95,8 @@ async function synthAudio(client, logger, stats, {
95
95
  } else if (vendor.startsWith('custom')) {
96
96
  assert.ok(credentials.custom_tts_url, `synthAudio requires custom_tts_url in credentials when ${vendor} is used`);
97
97
  }
98
-
99
98
  const key = makeSynthKey({
99
+ account_sid,
100
100
  vendor,
101
101
  language: language || '',
102
102
  voice: voice || deploymentId,
package/lib/utils.js CHANGED
@@ -16,10 +16,10 @@ const debug = require('debug')('jambonz:realtimedb-helpers');
16
16
  */
17
17
  //const nuanceClientMap = new Map();
18
18
 
19
- function makeSynthKey({vendor, language, voice, engine = '', text}) {
19
+ function makeSynthKey({account_sid = '', vendor, language, voice, engine = '', text}) {
20
20
  const hash = crypto.createHash('sha1');
21
21
  hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
22
- return `tts:${hash.digest('hex')}`;
22
+ return `tts${account_sid ? (':' + account_sid) : ''}:${hash.digest('hex')}`;
23
23
  }
24
24
 
25
25
  const noopLogger = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jambonz/speech-utils",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "TTS-related speech utilities for jambonz",
5
5
  "main": "index.js",
6
6
  "author": "Dave Horton",
package/test/synth.js CHANGED
@@ -427,5 +427,35 @@ test('TTS Cache tests', async(t) => {
427
427
  t.end(err);
428
428
  }
429
429
 
430
+ try {
431
+ // clear cache
432
+ await purgeTtsCache();
433
+ // save some random tts keys to cache
434
+ const minRecords = 8;
435
+ const account_sid = "12412512_cabc_5aff"
436
+ const account_sid2 = "22412512_cabc_5aff"
437
+ for (const i in Array(minRecords).fill(0)) {
438
+ await client.setAsync(makeSynthKey({account_sid, vendor: i, language: i, voice: i, engine: i, text: i}), i);
439
+ }
440
+ for (const i in Array(minRecords).fill(0)) {
441
+ await client.setAsync(makeSynthKey({account_sid: account_sid2, vendor: i, language: i, voice: i, engine: i, text: i}), i);
442
+ }
443
+ const {purgedCount} = await purgeTtsCache({account_sid});
444
+ t.equal(purgedCount, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid}`);
445
+
446
+ let cached = (await client.keysAsync('tts:*')).length;
447
+ t.equal(cached, minRecords, `successfully purged all tts records from cache for account_sid:${account_sid}`);
448
+
449
+ const {purgedCount: purgedCount2} = await purgeTtsCache({account_sid: account_sid2});
450
+ t.equal(purgedCount2, minRecords, `successfully purged at least ${minRecords} tts records from cache for account_sid:${account_sid2}`);
451
+
452
+ cached = (await client.keysAsync('tts:*')).length;
453
+ t.equal(cached, 0, `successfully purged all tts records from cache`);
454
+
455
+ } catch (err) {
456
+ console.error(JSON.stringify(err));
457
+ t.end(err);
458
+ }
459
+
430
460
  client.quit();
431
461
  });