From 8c4d5a7cee704c3b1a3e94d5f3fc007e62192a20 Mon Sep 17 00:00:00 2001 From: Hoan Luu Huu <110280845+xquanluu@users.noreply.github.com> Date: Mon, 30 Oct 2023 01:52:48 +0000 Subject: [PATCH] feat support google custom voice --- lib/synth-audio.js | 4 ++-- test/synth.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/synth-audio.js b/lib/synth-audio.js index e88a93c..a8c35b2 100644 --- a/lib/synth-audio.js +++ b/lib/synth-audio.js @@ -257,9 +257,9 @@ const synthGoogle = async(logger, {credentials, stats, language, voice, gender, const client = new ttsGoogle.TextToSpeechClient(credentials); const opts = { voice: { - name: voice, + ...( typeof voice === 'string' && {name: voice, ssmlGender: gender || 'SSML_VOICE_GENDER_UNSPECIFIED'}), + ...( typeof voice === 'object' && {custom_voice: voice}), languageCode: language, - ssmlGender: gender || 'SSML_VOICE_GENDER_UNSPECIFIED' }, audioConfig: {audioEncoding: 'MP3'} }; diff --git a/test/synth.js b/test/synth.js index 6ef5b20..d6920a0 100644 --- a/test/synth.js +++ b/test/synth.js @@ -79,6 +79,40 @@ test('Google speech synth tests', async(t) => { client.quit(); }); +test('Google speech Custom voice synth tests', async(t) => { + const fn = require('..'); + const {synthAudio, client} = fn(opts, logger); + + if (!process.env.GCP_CUSTOM_VOICE_FILE && !process.env.GCP_CUSTOM_VOICE_JSON_KEY || !process.env.GCP_CUSTOM_VOICE_MODEL) { + 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'); + return t.end(); + } + try { + const str = process.env.GCP_CUSTOM_VOICE_JSON_KEY || fs.readFileSync(process.env.GCP_CUSTOM_VOICE_FILE); + const creds = JSON.parse(str); + let opts = await synthAudio(stats, { + vendor: 'google', + credentials: { + credentials: { + client_email: creds.client_email, + private_key: creds.private_key, + }, + }, + language: 'en-AU', + text: 'This is a test. This is only a test', + voice: { + reportedUsage:"REALTIME", + model: process.env.GCP_CUSTOM_VOICE_MODEL + } + }); + t.ok(!opts.servedFromCache, `successfully synthesized google custom voice audio to ${opts.filePath}`); + } catch (err) { + console.error(err); + t.end(err); + } + client.quit(); +}); + test('AWS speech synth tests', async(t) => { const fn = require('..'); const {synthAudio, client} = fn(opts, logger);