Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Samples: extract speech synthesis samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Taylor committed Mar 23, 2018
1 parent fbe2481 commit 562a440
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 56 deletions.
58 changes: 58 additions & 0 deletions samples/listVoices.js
@@ -0,0 +1,58 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

function listVoices() {
// [START tts_list_voices]
const textToSpeech = require('@google-cloud/text-to-speech');

var client = new textToSpeech.TextToSpeechClient();

client.listVoices({})
.then(results => {
const voices = results[0].voices;

console.log('Voices:');
voices.forEach((voice) => {
console.log(`Name: ${voice.name}`);
console.log(` SSML Gender: ${voice.ssmlGender}`);
console.log(` Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`)
console.log(` Supported languages:`)
voice.languageCodes.forEach((languageCode) => {
console.log(` ${languageCode}`);
});
})
})
.catch(err => {
console.error('ERROR:', err);
});
// [END tts_list_voices]
}

require(`yargs`) // eslint-disable-line
.demand(1)
.command(
`list-voices`,
`List supported voices.`,
{},
opts => listVoices()
)
.example(`node $0 list-voices`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/text-to-speech/docs`)
.help()
.strict().argv;
50 changes: 8 additions & 42 deletions samples/textToSpeech.js → samples/synthesize.js
Expand Up @@ -15,33 +15,6 @@

'use strict';

function listVoices() {
// [START tts_list_voices]
const textToSpeech = require('@google-cloud/text-to-speech');

var client = new textToSpeech.TextToSpeechClient();

client.listVoices({})
.then(results => {
const voices = results[0].voices;

console.log('Voices:');
voices.forEach((voice) => {
console.log(`Name: ${voice.name}`);
console.log(` SSML Gender: ${voice.ssmlGender}`);
console.log(` Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`)
console.log(` Supported languages:`)
voice.languageCodes.forEach((languageCode) => {
console.log(` ${languageCode}`);
});
})
})
.catch(err => {
console.error('ERROR:', err);
});
// [END tts_list_voices]
}

function synthesizeText(text, outputFile) {
// [START tts_synthesize_text]
const textToSpeech = require('@google-cloud/text-to-speech');
Expand Down Expand Up @@ -173,31 +146,25 @@ function synthesizeSsmlFile(ssmlFile, outputFile) {
require(`yargs`) // eslint-disable-line
.demand(1)
.command(
`list-voices`,
`List supported languages.`,
{},
opts => listVoices()
)
.command(
`synthesize-text <text>`,
`text <text>`,
`Synthesizes audio file from text`,
{},
opts => synthesizeText(opts.text, opts.outputFile)
)
.command(
`synthesize-ssml <ssml>`,
`ssml <ssml>`,
`Synthesizes audio file from SSML`,
{},
opts => synthesizeSsml(opts.ssml, opts.outputFile)
)
.command(
`synthesize-text-file <textFile>`,
`text-file <textFile>`,
`Synthesizes audio file from text in a file`,
{},
opts => synthesizeTextFile(opts.textFile, opts.outputFile)
)
.command(
`synthesize-ssml-file <ssmlFile>`,
`ssml-file <ssmlFile>`,
`Synthesizes audio file from SSML in a file`,
{},
opts => synthesizeSsmlFile(opts.ssmlFile, opts.outputFile)
Expand All @@ -211,11 +178,10 @@ require(`yargs`) // eslint-disable-line
type: 'string'
}
})
.example(`node $0 list-voices`)
.example(`node $0 synthesize-text "hello" -o hello.mp3`)
.example(`node $0 synthesize-ssml "<?xml..." -o hello.mp3`)
.example(`node $0 synthesize-text-file filename.txt -o output.mp3`)
.example(`node $0 synthesize-ssml-file filename.ssml -o output.mp3`)
.example(`node $0 text "hello" -o hello.mp3`)
.example(`node $0 ssml "<?xml..." -o hello.mp3`)
.example(`node $0 text-file filename.txt -o output.mp3`)
.example(`node $0 ssml-file filename.ssml -o output.mp3`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/text-to-speech/docs`)
Expand Down
34 changes: 34 additions & 0 deletions samples/system-test/listVoices.test.js
@@ -0,0 +1,34 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const cmd = `node listVoices.js`;
const cwd = path.join(__dirname, `..`);

test.before(tools.checkCredentials);

test(`should list voices`, async t => {
const output = await tools.runAsync(
`${cmd} list-voices`,
cwd
);
t.true(output.includes(`SSML Gender: FEMALE`));
t.true(output.includes(`Natural Sample Rate Hertz: 24000`));
});
Expand Up @@ -20,7 +20,7 @@ const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const cmd = `node textToSpeech.js`;
const cmd = `node synthesize.js`;
const cwd = path.join(__dirname, `..`);
const text = `Hello there.`;
const ssml = `<?xml version="1.0"?>
Expand All @@ -47,19 +47,10 @@ test.after.always(async () => {
await fs.unlink(outputFile);
});

test(`should list voice`, async t => {
const output = await tools.runAsync(
`${cmd} list-voices`,
cwd
);
t.true(output.includes(`SSML Gender: FEMALE`));
t.true(output.includes(`Natural Sample Rate Hertz: 24000`));
});

test(`should synthesize audio from text`, async t => {
t.false(fs.existsSync(outputFile));
const output = await tools.runAsync(
`${cmd} synthesize-text '${text}' --outputFile '${outputFile}'`,
`${cmd} text '${text}' --outputFile '${outputFile}'`,
cwd
);
t.true(output.includes(`Saved synthesized text to local audio file ${outputFile}`));
Expand All @@ -69,7 +60,7 @@ test(`should synthesize audio from text`, async t => {
test(`should synthesize audio from ssml`, async t => {
t.false(fs.existsSync(outputFile));
const output = await tools.runAsync(
`${cmd} synthesize-ssml '${ssml}' --outputFile '${outputFile}'`,
`${cmd} ssml '${ssml}' --outputFile '${outputFile}'`,
cwd
);
t.true(output.includes(`Saved synthesized text to local audio file ${outputFile}`));
Expand All @@ -79,7 +70,7 @@ test(`should synthesize audio from ssml`, async t => {
test(`should synthesize audio from text file`, async t => {
t.false(fs.existsSync(outputFile));
const output = await tools.runAsync(
`${cmd} synthesize-text-file '${files[0].localPath}' --outputFile '${outputFile}'`,
`${cmd} text-file '${files[0].localPath}' --outputFile '${outputFile}'`,
cwd
);
t.true(output.includes(`Saved synthesized text to local audio file ${outputFile}`));
Expand All @@ -89,7 +80,7 @@ test(`should synthesize audio from text file`, async t => {
test(`should synthesize audio from ssml file`, async t => {
t.false(fs.existsSync(outputFile));
const output = await tools.runAsync(
`${cmd} synthesize-ssml-file '${files[1].localPath}' --outputFile '${outputFile}'`,
`${cmd} ssml-file '${files[1].localPath}' --outputFile '${outputFile}'`,
cwd
);
t.true(output.includes(`Saved synthesized text to local audio file ${outputFile}`));
Expand Down

0 comments on commit 562a440

Please sign in to comment.