diff --git a/samples/audioProfile.js b/samples/audioProfile.js index aef6297a..e995493a 100644 --- a/samples/audioProfile.js +++ b/samples/audioProfile.js @@ -14,8 +14,7 @@ */ 'use strict'; - -function synthesizeText( +async function synthesizeText( text, outputFile, effectsProfileId, @@ -27,6 +26,7 @@ function synthesizeText( // Imports the Google Cloud client library const speech = require('@google-cloud/text-to-speech'); const fs = require('fs'); + const util = require('util'); // Creates a client const client = new speech.TextToSpeechClient(); @@ -37,91 +37,85 @@ function synthesizeText( audioConfig: {audioEncoding: 'MP3', effectsProfileId: effectsProfileId}, }; - client.synthesizeSpeech(request, (err, response) => { - if (err) { - console.error(`ERROR:`, err); - return; - } - - fs.writeFile(outputFile, response.audioContent, 'binary', err => { - if (err) { - console.error('ERROR:', err); - return; - } - console.log(`Audio content written to file: ${outputFile}`); - }); - }); + const [response] = await client.synthesizeSpeech(request); + const writeFile = util.promisify(fs.writeFile); + await writeFile(outputFile, response.audioContent, 'binary'); + console.log(`Audio content written to file: ${outputFile}`); // [END tts_synthesize_text_audio_profile_beta] } -require(`yargs`) - .demand(1) - .command( - `synthesize `, - `Detects speech in a local audio file.`, - {}, - opts => - synthesizeText( - opts.text, - opts.outputFile, - opts.effectsProfileId, - opts.languageCode, - opts.ssmlGender - ) - ) - .options({ - text: { - alias: 't', - default: 'Hey Everybody! This is a test!', - global: true, - requiresArg: true, - type: 'string', - }, - outputFile: { - alias: 'f', - default: './resources/test.mp3', - global: true, - requiresArg: false, - type: 'string', - }, - effectsProfileId: { - alias: 'e', - default: 'telephony-class-application', - global: true, - requiresArg: true, - type: 'string', - }, - languageCode: { - alias: 'l', - default: 'en-US', - global: true, - requiresArg: true, - tnodeype: 'string', - }, - ssmlGender: { - alias: 'g', - default: 'FEMALE', - global: true, - requiresArg: true, - type: 'string', - }, - }) - .array(`effectsProfileId`) - .example(`node $0 synthesize "Enter Phrase to Test Here"`) - .example( - `node $0 synthesize "This is optimized for Phone" -f ./resources/phone.mp3 -e telephony-class-application -l en-US` - ) - .example( - `node $0 synthesize "This is optimized for a Wearable, like a watch" -f ./resources/watch.mp3 -e wearable-class-device -l en-US` - ) - .example( - `node $0 synthesize "This is optimized for Home Entertainment System" -f ./resources/homestereo.mp3 -e large-home-entertainment-class-device` - ) - .example( - `node $0 synthesize "This is optimized for the Car" -f ./resources/car.mp3 -e large-automotive-class-device` - ) - .wrap(120) - .recommendCommands() - .epilogue(`For more information, see https://cloud.google.com/speech/docs`) - .help() - .strict().argv; +async function main() { + require(`yargs`) + .demand(1) + .command( + `synthesize `, + `Detects speech in a local audio file.`, + {}, + opts => + synthesizeText( + opts.text, + opts.outputFile, + opts.effectsProfileId, + opts.languageCode, + opts.ssmlGender + ) + ) + .options({ + text: { + alias: 't', + default: 'Hey Everybody! This is a test!', + global: true, + requiresArg: true, + type: 'string', + }, + outputFile: { + alias: 'f', + default: './resources/test.mp3', + global: true, + requiresArg: false, + type: 'string', + }, + effectsProfileId: { + alias: 'e', + default: 'telephony-class-application', + global: true, + requiresArg: true, + type: 'string', + }, + languageCode: { + alias: 'l', + default: 'en-US', + global: true, + requiresArg: true, + tnodeype: 'string', + }, + ssmlGender: { + alias: 'g', + default: 'FEMALE', + global: true, + requiresArg: true, + type: 'string', + }, + }) + .array(`effectsProfileId`) + .example(`node $0 synthesize "Enter Phrase to Test Here"`) + .example( + `node $0 synthesize "This is optimized for Phone" -f ./resources/phone.mp3 -e telephony-class-application -l en-US` + ) + .example( + `node $0 synthesize "This is optimized for a Wearable, like a watch" -f ./resources/watch.mp3 -e wearable-class-device -l en-US` + ) + .example( + `node $0 synthesize "This is optimized for Home Entertainment System" -f ./resources/homestereo.mp3 -e large-home-entertainment-class-device` + ) + .example( + `node $0 synthesize "This is optimized for the Car" -f ./resources/car.mp3 -e large-automotive-class-device` + ) + .wrap(120) + .recommendCommands() + .epilogue(`For more information, see https://cloud.google.com/speech/docs`) + .help() + .strict().argv; +} + +main().catch(console.error); diff --git a/samples/listVoices.js b/samples/listVoices.js index 28c31a0c..d6c98be3 100644 --- a/samples/listVoices.js +++ b/samples/listVoices.js @@ -15,44 +15,40 @@ 'use strict'; -function listVoices() { +async function listVoices() { // [START tts_list_voices] const textToSpeech = require('@google-cloud/text-to-speech'); const client = new textToSpeech.TextToSpeechClient(); - client - .listVoices({}) - .then(results => { - const voices = results[0].voices; + const [result] = await client.listVoices({}); + const voices = result.voices; - console.log('Voices:'); - voices.forEach(voice => { - console.log(`Name: ${voice.name}`); - console.log(` SSML Voice 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); + console.log('Voices:'); + voices.forEach(voice => { + console.log(`Name: ${voice.name}`); + console.log(` SSML Voice Gender: ${voice.ssmlGender}`); + console.log(` Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`); + console.log(` Supported languages:`); + voice.languageCodes.forEach(languageCode => { + console.log(` ${languageCode}`); }); + }); // [END tts_list_voices] } +async function main() { require(`yargs`) // eslint-disable-line - .demand(1) - .command(`list-voices`, `List supported voices.`, {}, () => 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; + .demand(1) + .command(`list-voices`, `List supported voices.`, {}, () => 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; +} + +main().catch(console.error); diff --git a/samples/quickstart.js b/samples/quickstart.js index 56d82e24..5ea3373c 100644 --- a/samples/quickstart.js +++ b/samples/quickstart.js @@ -15,41 +15,36 @@ 'use strict'; -// [START tts_quickstart] -const fs = require('fs'); - -// Imports the Google Cloud client library -const textToSpeech = require('@google-cloud/text-to-speech'); - -// Creates a client -const client = new textToSpeech.TextToSpeechClient(); - -// The text to synthesize -const text = 'Hello, world!'; - -// Construct the request -const request = { - input: {text: text}, - // Select the language and SSML Voice Gender (optional) - voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'}, - // Select the type of audio encoding - audioConfig: {audioEncoding: 'MP3'}, -}; - -// Performs the Text-to-Speech request -client.synthesizeSpeech(request, (err, response) => { - if (err) { - console.error('ERROR:', err); - return; - } - +async function main() { + // [START tts_quickstart] + const fs = require('fs'); + const util = require('util'); + + // Imports the Google Cloud client library + const textToSpeech = require('@google-cloud/text-to-speech'); + + // Creates a client + const client = new textToSpeech.TextToSpeechClient(); + + // The text to synthesize + const text = 'Hello, world!'; + + // Construct the request + const request = { + input: {text: text}, + // Select the language and SSML Voice Gender (optional) + voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'}, + // Select the type of audio encoding + audioConfig: {audioEncoding: 'MP3'}, + }; + + // Performs the Text-to-Speech request + const [response] = await client.synthesizeSpeech(request); // Write the binary audio content to a local file - fs.writeFile('output.mp3', response.audioContent, 'binary', err => { - if (err) { - console.error('ERROR:', err); - return; - } - console.log('Audio content written to file: output.mp3'); - }); -}); -// [END tts_quickstart] + const writeFile = util.promisify(fs.writeFile); + await writeFile('output.mp3', response.audioContent, 'binary'); + console.log('Audio content written to file: output.mp3'); + // [END tts_quickstart] +} + +main().catch(console.error); diff --git a/samples/synthesize.js b/samples/synthesize.js index 556e1305..39057d0c 100644 --- a/samples/synthesize.js +++ b/samples/synthesize.js @@ -14,11 +14,11 @@ */ 'use strict'; - -function synthesizeText(text, outputFile) { +async function synthesizeText(text, outputFile) { // [START tts_synthesize_text] const textToSpeech = require('@google-cloud/text-to-speech'); const fs = require('fs'); + const util = require('util'); const client = new textToSpeech.TextToSpeechClient(); @@ -33,28 +33,18 @@ function synthesizeText(text, outputFile) { voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'}, audioConfig: {audioEncoding: 'MP3'}, }; - - client.synthesizeSpeech(request, (err, response) => { - if (err) { - console.error('ERROR:', err); - return; - } - - fs.writeFile(outputFile, response.audioContent, 'binary', err => { - if (err) { - console.error('ERROR:', err); - return; - } - console.log(`Audio content written to file: ${outputFile}`); - }); - }); + const [response] = await client.synthesizeSpeech(request); + const writeFile = util.promisify(fs.writeFile); + await writeFile(outputFile, response.audioContent, 'binary'); + console.log(`Audio content written to file: ${outputFile}`); // [END tts_synthesize_text] } -function synthesizeSsml(ssml, outputFile) { +async function synthesizeSsml(ssml, outputFile) { // [START tts_synthesize_ssml] const textToSpeech = require('@google-cloud/text-to-speech'); const fs = require('fs'); + const util = require('util'); const client = new textToSpeech.TextToSpeechClient(); @@ -70,27 +60,18 @@ function synthesizeSsml(ssml, outputFile) { audioConfig: {audioEncoding: 'MP3'}, }; - client.synthesizeSpeech(request, (err, response) => { - if (err) { - console.error('ERROR:', err); - return; - } - - fs.writeFile(outputFile, response.audioContent, 'binary', err => { - if (err) { - console.error('ERROR:', err); - return; - } - console.log(`Audio content written to file: ${outputFile}`); - }); - }); + const [response] = await client.synthesizeSpeech(request); + const writeFile = util.promisify(fs.writeFile); + await writeFile(outputFile, response.audioContent, 'binary'); + console.log(`Audio content written to file: ${outputFile}`); // [END tts_synthesize_ssml] } -function synthesizeTextFile(textFile, outputFile) { +async function synthesizeTextFile(textFile, outputFile) { // [START tts_synthesize_text_file] const textToSpeech = require('@google-cloud/text-to-speech'); const fs = require('fs'); + const util = require('util'); const client = new textToSpeech.TextToSpeechClient(); @@ -106,27 +87,18 @@ function synthesizeTextFile(textFile, outputFile) { audioConfig: {audioEncoding: 'MP3'}, }; - client.synthesizeSpeech(request, (err, response) => { - if (err) { - console.error('ERROR:', err); - return; - } - - fs.writeFile(outputFile, response.audioContent, 'binary', err => { - if (err) { - console.error('ERROR:', err); - return; - } - console.log(`Audio content written to file: ${outputFile}`); - }); - }); + const [response] = await client.synthesizeSpeech(request); + const writeFile = util.promisify(fs.writeFile); + await writeFile(outputFile, response.audioContent, 'binary'); + console.log(`Audio content written to file: ${outputFile}`); // [END tts_synthesize_text_file] } -function synthesizeSsmlFile(ssmlFile, outputFile) { +async function synthesizeSsmlFile(ssmlFile, outputFile) { // [START tts_synthesize_ssml_file] const textToSpeech = require('@google-cloud/text-to-speech'); const fs = require('fs'); + const util = require('util'); const client = new textToSpeech.TextToSpeechClient(); @@ -142,60 +114,54 @@ function synthesizeSsmlFile(ssmlFile, outputFile) { audioConfig: {audioEncoding: 'MP3'}, }; - client.synthesizeSpeech(request, (err, response) => { - if (err) { - console.error('ERROR:', err); - return; - } - - fs.writeFile(outputFile, response.audioContent, 'binary', err => { - if (err) { - console.error('ERROR:', err); - return; - } - console.log(`Audio content written to file: ${outputFile}`); - }); - }); + const [response] = await client.synthesizeSpeech(request); + const writeFile = util.promisify(fs.writeFile); + await writeFile(outputFile, response.audioContent, 'binary'); + console.log(`Audio content written to file: ${outputFile}`); // [END tts_synthesize_ssml_file] } +async function main() { require(`yargs`) // eslint-disable-line - .demand(1) - .command(`text `, `Synthesizes audio file from text`, {}, opts => - synthesizeText(opts.text, opts.outputFile) - ) - .command(`ssml `, `Synthesizes audio file from SSML`, {}, opts => - synthesizeSsml(opts.ssml, opts.outputFile) - ) - .command( - `text-file `, - `Synthesizes audio file from text in a file`, - {}, - opts => synthesizeTextFile(opts.textFile, opts.outputFile) - ) - .command( - `ssml-file `, - `Synthesizes audio file from SSML in a file`, - {}, - opts => synthesizeSsmlFile(opts.ssmlFile, opts.outputFile) - ) - .options({ - outputFile: { - alias: 'o', - default: 'output.mp3', - global: true, - requiresArg: true, - type: 'string', - }, - }) - .example(`node $0 text "hello" -o hello.mp3`) - .example(`node $0 ssml "Hello there." -o hello.mp3`) - .example(`node $0 text-file resources/hello.txt -o output.mp3`) - .example(`node $0 ssml-file resources/hello.ssml -o output.mp3`) - .wrap(120) - .recommendCommands() - .epilogue( - `For more information, see https://cloud.google.com/text-to-speech/docs` - ) - .help() - .strict().argv; + .demand(1) + .command(`text `, `Synthesizes audio file from text`, {}, opts => + synthesizeText(opts.text, opts.outputFile) + ) + .command(`ssml `, `Synthesizes audio file from SSML`, {}, opts => + synthesizeSsml(opts.ssml, opts.outputFile) + ) + .command( + `text-file `, + `Synthesizes audio file from text in a file`, + {}, + opts => synthesizeTextFile(opts.textFile, opts.outputFile) + ) + .command( + `ssml-file `, + `Synthesizes audio file from SSML in a file`, + {}, + opts => synthesizeSsmlFile(opts.ssmlFile, opts.outputFile) + ) + .options({ + outputFile: { + alias: 'o', + default: 'output.mp3', + global: true, + requiresArg: true, + type: 'string', + }, + }) + .example(`node $0 text "hello" -o hello.mp3`) + .example(`node $0 ssml "Hello there." -o hello.mp3`) + .example(`node $0 text-file resources/hello.txt -o output.mp3`) + .example(`node $0 ssml-file resources/hello.ssml -o output.mp3`) + .wrap(120) + .recommendCommands() + .epilogue( + `For more information, see https://cloud.google.com/text-to-speech/docs` + ) + .help() + .strict().argv; +} + +main().catch(console.error);