@jambonz/speech-utils 0.1.19 → 0.1.21

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.
@@ -170,7 +170,7 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
170
170
  renderForCaching
171
171
  });
172
172
  let filePath;
173
- filePath = makeFilePath({vendor, key, salt, renderForCaching});
173
+ filePath = makeFilePath({vendor, voice, key, salt, renderForCaching});
174
174
  debug(`synth key is ${key}`);
175
175
  let cached;
176
176
  if (!disableTtsCache) {
@@ -192,7 +192,7 @@ async function synthAudio(client, createHash, retrieveHash, logger, stats, { acc
192
192
  cached = await client.get(preCachekey);
193
193
  if (cached) {
194
194
  // Precache audio is available update filpath with precache file extension.
195
- filePath = makeFilePath({vendor, key, salt, renderForCaching: true});
195
+ filePath = makeFilePath({vendor, voice, key, salt, renderForCaching: true});
196
196
  }
197
197
  }
198
198
  }
@@ -353,6 +353,44 @@ const synthPolly = async(createHash, retrieveHash, logger,
353
353
 
354
354
  const synthGoogle = async(logger, {credentials, stats, language, voice, gender, text}) => {
355
355
  const client = new ttsGoogle.TextToSpeechClient(credentials);
356
+ // If google custom voice cloning is used.
357
+ // At this time 31 Oct 2024, google node sdk has not support voice cloning yet.
358
+ if (typeof voice === 'object' && voice.voice_cloning_key) {
359
+ try {
360
+ const accessToken = await client.auth.getAccessToken();
361
+ const projectId = await client.getProjectId();
362
+
363
+ const post = bent('https://texttospeech.googleapis.com', 'POST', 'json', {
364
+ 'Authorization': `Bearer ${accessToken}`,
365
+ 'x-goog-user-project': projectId,
366
+ 'Content-Type': 'application/json; charset=utf-8'
367
+ });
368
+
369
+ const payload = {
370
+ input: {
371
+ text
372
+ },
373
+ voice: {
374
+ language_code: language,
375
+ voice_clone: {
376
+ voice_cloning_key: voice.voice_cloning_key
377
+ }
378
+ },
379
+ audioConfig: {
380
+ // Cloning voice at this time still in v1 beta version, and it support LINEAR16 in Wav format, 24.000Hz
381
+ audioEncoding: 'LINEAR16',
382
+ sample_rate_hertz: 24000
383
+ }
384
+ };
385
+
386
+ const wav = await post('/v1beta1/text:synthesize', payload);
387
+ return Buffer.from(wav.audioContent, 'base64');
388
+ } catch (err) {
389
+ logger.info({err: await err.text()}, 'synthGoogle returned error');
390
+ throw err;
391
+ }
392
+ }
393
+
356
394
  const opts = {
357
395
  voice: {
358
396
  ...(typeof voice === 'string' && {name: voice}),
package/lib/utils.js CHANGED
@@ -23,19 +23,20 @@ function makeSynthKey({
23
23
  hash.update(`${language}:${vendor}:${voice}:${engine}:${text}`);
24
24
  const hexHashKey = hash.digest('hex');
25
25
  const accountKey = account_sid ? `:${account_sid}` : '';
26
- const namespace = vendor.startsWith('custom') ? vendor : getFileExtension({vendor, renderForCaching});
26
+ const namespace = vendor.startsWith('custom') ? vendor : getFileExtension({vendor, voice, renderForCaching});
27
27
  const key = `tts${accountKey}:${namespace}:${hexHashKey}`;
28
28
  return key;
29
29
  }
30
30
 
31
- function makeFilePath({vendor, key, salt = '', renderForCaching = false}) {
32
- const extension = getFileExtension({vendor, renderForCaching});
31
+ function makeFilePath({vendor, voice, key, salt = '', renderForCaching = false}) {
32
+ const extension = getFileExtension({vendor, renderForCaching, voice});
33
33
  return `${TMP_FOLDER}/${key.replace('tts:', `tts-${salt}`)}.${extension}`;
34
34
  }
35
35
 
36
- function getFileExtension({vendor, renderForCaching = false}) {
36
+ function getFileExtension({vendor, voice, renderForCaching = false}) {
37
37
  const mp3Extension = 'mp3';
38
38
  const r8Extension = 'r8';
39
+ const wavExtension = 'wav';
39
40
 
40
41
  switch (vendor) {
41
42
  case 'azure':
@@ -47,7 +48,7 @@ function getFileExtension({vendor, renderForCaching = false}) {
47
48
  }
48
49
  case 'deepgram':
49
50
  case 'elevenlabs':
50
- case 'rimlabs':
51
+ case 'rimelabs':
51
52
  case 'playht':
52
53
  if (renderForCaching || JAMBONES_DISABLE_TTS_STREAMING) {
53
54
  return mp3Extension;
@@ -58,6 +59,13 @@ function getFileExtension({vendor, renderForCaching = false}) {
58
59
  case 'nvidia':
59
60
  case 'verbio':
60
61
  return r8Extension;
62
+ case 'google':
63
+ // google voice cloning just support wav.
64
+ if (typeof voice === 'object' && voice.voice_cloning_key) {
65
+ return wavExtension;
66
+ } else {
67
+ return mp3Extension;
68
+ }
61
69
  default:
62
70
  // If vendor is custom
63
71
  if (vendor.startsWith('custom')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jambonz/speech-utils",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "description": "TTS-related speech utilities for jambonz",
5
5
  "main": "index.js",
6
6
  "author": "Dave Horton",
@@ -28,7 +28,7 @@
28
28
  "dependencies": {
29
29
  "@aws-sdk/client-polly": "^3.496.0",
30
30
  "@aws-sdk/client-sts": "^3.496.0",
31
- "@google-cloud/text-to-speech": "^5.0.2",
31
+ "@google-cloud/text-to-speech": "^5.5.0",
32
32
  "@grpc/grpc-js": "^1.9.14",
33
33
  "@jambonz/realtimedb-helpers": "^0.8.7",
34
34
  "bent": "^7.3.12",
package/test/synth.js CHANGED
@@ -91,14 +91,17 @@ test('Google speech Custom voice synth tests', async(t) => {
91
91
  const fn = require('..');
92
92
  const {synthAudio, client} = fn(opts, logger);
93
93
 
94
- if (!process.env.GCP_CUSTOM_VOICE_FILE && !process.env.GCP_CUSTOM_VOICE_JSON_KEY || !process.env.GCP_CUSTOM_VOICE_MODEL) {
95
- t.pass('skipping google speech synth tests since neither GCP_CUSTOM_VOICE_FILE nor GCP_CUSTOM_VOICE_JSON_KEY provided, GCP_CUSTOM_VOICE_MODEL is not provided');
94
+ if (!process.env.GCP_CUSTOM_VOICE_FILE &&
95
+ !process.env.GCP_CUSTOM_VOICE_JSON_KEY ||
96
+ !process.env.GCP_CUSTOM_VOICE_MODEL) {
97
+ t.pass(`skipping google speech synth tests since neither
98
+ GCP_CUSTOM_VOICE_FILE nor GCP_CUSTOM_VOICE_JSON_KEY provided, GCP_CUSTOM_VOICE_MODEL is not provided`);
96
99
  return t.end();
97
100
  }
98
101
  try {
99
102
  const str = process.env.GCP_CUSTOM_VOICE_JSON_KEY || fs.readFileSync(process.env.GCP_CUSTOM_VOICE_FILE);
100
103
  const creds = JSON.parse(str);
101
- let opts = await synthAudio(stats, {
104
+ const opts = await synthAudio(stats, {
102
105
  vendor: 'google',
103
106
  credentials: {
104
107
  credentials: {
@@ -109,7 +112,7 @@ test('Google speech Custom voice synth tests', async(t) => {
109
112
  language: 'en-AU',
110
113
  text: 'This is a test. This is only a test',
111
114
  voice: {
112
- reportedUsage:"REALTIME",
115
+ reportedUsage: 'REALTIME',
113
116
  model: process.env.GCP_CUSTOM_VOICE_MODEL
114
117
  }
115
118
  });
@@ -121,6 +124,48 @@ test('Google speech Custom voice synth tests', async(t) => {
121
124
  client.quit();
122
125
  });
123
126
 
127
+ test('Google speech voice cloning synth tests', async(t) => {
128
+ const fn = require('..');
129
+ const {synthAudio, client} = fn(opts, logger);
130
+
131
+ if (!process.env.GCP_CUSTOM_VOICE_FILE &&
132
+ !process.env.GCP_CUSTOM_VOICE_JSON_KEY ||
133
+ !process.env.GCP_VOICE_CLONING_FILE &&
134
+ !process.env.GCP_VOICE_CLONING_JSON_KEY) {
135
+ t.pass(`skipping google speech synth tests since neither
136
+ GCP_CUSTOM_VOICE_FILE nor GCP_CUSTOM_VOICE_JSON_KEY provided,
137
+ GCP_VOICE_CLONING_FILE nor GCP_VOICE_CLONING_JSON_KEY is not provided`);
138
+ return t.end();
139
+ }
140
+ try {
141
+ const googleKey = process.env.GCP_CUSTOM_VOICE_JSON_KEY ||
142
+ fs.readFileSync(process.env.GCP_CUSTOM_VOICE_FILE);
143
+ const voice_cloning_key = process.env.GCP_VOICE_CLONING_JSON_KEY ||
144
+ fs.readFileSync(process.env.GCP_VOICE_CLONING_FILE).toString();
145
+ const creds = JSON.parse(googleKey);
146
+ const opts = await synthAudio(stats, {
147
+ vendor: 'google',
148
+ credentials: {
149
+ credentials: {
150
+ client_email: creds.client_email,
151
+ private_key: creds.private_key,
152
+ project_id: creds.project_id
153
+ },
154
+ },
155
+ language: 'en-US',
156
+ text: 'This is a test. This is only a test. This is a test. This is only a test. This is a test. This is only a test',
157
+ voice: {
158
+ voice_cloning_key
159
+ }
160
+ });
161
+ t.ok(!opts.servedFromCache, `successfully synthesized google voice cloning audio to ${opts.filePath}`);
162
+ } catch (err) {
163
+ console.error(err);
164
+ t.end(err);
165
+ }
166
+ client.quit();
167
+ });
168
+
124
169
  test('AWS speech synth tests', async(t) => {
125
170
  const fn = require('..');
126
171
  const {synthAudio, client} = fn(opts, logger);