Skip to content

Commit 5cb93dd

Browse files
praveenqlogicJustinBeckwith
authored andcommitted
docs(samples): updated samples code to use async await (#118)
1 parent 1f2552d commit 5cb93dd

File tree

4 files changed

+206
-255
lines changed

4 files changed

+206
-255
lines changed

texttospeech/audioProfile.js

Lines changed: 81 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
*/
1515

1616
'use strict';
17-
18-
function synthesizeText(
17+
async function synthesizeText(
1918
text,
2019
outputFile,
2120
effectsProfileId,
@@ -27,6 +26,7 @@ function synthesizeText(
2726
// Imports the Google Cloud client library
2827
const speech = require('@google-cloud/text-to-speech');
2928
const fs = require('fs');
29+
const util = require('util');
3030

3131
// Creates a client
3232
const client = new speech.TextToSpeechClient();
@@ -37,91 +37,85 @@ function synthesizeText(
3737
audioConfig: {audioEncoding: 'MP3', effectsProfileId: effectsProfileId},
3838
};
3939

40-
client.synthesizeSpeech(request, (err, response) => {
41-
if (err) {
42-
console.error(`ERROR:`, err);
43-
return;
44-
}
45-
46-
fs.writeFile(outputFile, response.audioContent, 'binary', err => {
47-
if (err) {
48-
console.error('ERROR:', err);
49-
return;
50-
}
51-
console.log(`Audio content written to file: ${outputFile}`);
52-
});
53-
});
40+
const [response] = await client.synthesizeSpeech(request);
41+
const writeFile = util.promisify(fs.writeFile);
42+
await writeFile(outputFile, response.audioContent, 'binary');
43+
console.log(`Audio content written to file: ${outputFile}`);
5444
// [END tts_synthesize_text_audio_profile_beta]
5545
}
5646

57-
require(`yargs`)
58-
.demand(1)
59-
.command(
60-
`synthesize <text>`,
61-
`Detects speech in a local audio file.`,
62-
{},
63-
opts =>
64-
synthesizeText(
65-
opts.text,
66-
opts.outputFile,
67-
opts.effectsProfileId,
68-
opts.languageCode,
69-
opts.ssmlGender
70-
)
71-
)
72-
.options({
73-
text: {
74-
alias: 't',
75-
default: 'Hey Everybody! This is a test!',
76-
global: true,
77-
requiresArg: true,
78-
type: 'string',
79-
},
80-
outputFile: {
81-
alias: 'f',
82-
default: './resources/test.mp3',
83-
global: true,
84-
requiresArg: false,
85-
type: 'string',
86-
},
87-
effectsProfileId: {
88-
alias: 'e',
89-
default: 'telephony-class-application',
90-
global: true,
91-
requiresArg: true,
92-
type: 'string',
93-
},
94-
languageCode: {
95-
alias: 'l',
96-
default: 'en-US',
97-
global: true,
98-
requiresArg: true,
99-
tnodeype: 'string',
100-
},
101-
ssmlGender: {
102-
alias: 'g',
103-
default: 'FEMALE',
104-
global: true,
105-
requiresArg: true,
106-
type: 'string',
107-
},
108-
})
109-
.array(`effectsProfileId`)
110-
.example(`node $0 synthesize "Enter Phrase to Test Here"`)
111-
.example(
112-
`node $0 synthesize "This is optimized for Phone" -f ./resources/phone.mp3 -e telephony-class-application -l en-US`
113-
)
114-
.example(
115-
`node $0 synthesize "This is optimized for a Wearable, like a watch" -f ./resources/watch.mp3 -e wearable-class-device -l en-US`
116-
)
117-
.example(
118-
`node $0 synthesize "This is optimized for Home Entertainment System" -f ./resources/homestereo.mp3 -e large-home-entertainment-class-device`
119-
)
120-
.example(
121-
`node $0 synthesize "This is optimized for the Car" -f ./resources/car.mp3 -e large-automotive-class-device`
122-
)
123-
.wrap(120)
124-
.recommendCommands()
125-
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
126-
.help()
127-
.strict().argv;
47+
async function main() {
48+
require(`yargs`)
49+
.demand(1)
50+
.command(
51+
`synthesize <text>`,
52+
`Detects speech in a local audio file.`,
53+
{},
54+
opts =>
55+
synthesizeText(
56+
opts.text,
57+
opts.outputFile,
58+
opts.effectsProfileId,
59+
opts.languageCode,
60+
opts.ssmlGender
61+
)
62+
)
63+
.options({
64+
text: {
65+
alias: 't',
66+
default: 'Hey Everybody! This is a test!',
67+
global: true,
68+
requiresArg: true,
69+
type: 'string',
70+
},
71+
outputFile: {
72+
alias: 'f',
73+
default: './resources/test.mp3',
74+
global: true,
75+
requiresArg: false,
76+
type: 'string',
77+
},
78+
effectsProfileId: {
79+
alias: 'e',
80+
default: 'telephony-class-application',
81+
global: true,
82+
requiresArg: true,
83+
type: 'string',
84+
},
85+
languageCode: {
86+
alias: 'l',
87+
default: 'en-US',
88+
global: true,
89+
requiresArg: true,
90+
tnodeype: 'string',
91+
},
92+
ssmlGender: {
93+
alias: 'g',
94+
default: 'FEMALE',
95+
global: true,
96+
requiresArg: true,
97+
type: 'string',
98+
},
99+
})
100+
.array(`effectsProfileId`)
101+
.example(`node $0 synthesize "Enter Phrase to Test Here"`)
102+
.example(
103+
`node $0 synthesize "This is optimized for Phone" -f ./resources/phone.mp3 -e telephony-class-application -l en-US`
104+
)
105+
.example(
106+
`node $0 synthesize "This is optimized for a Wearable, like a watch" -f ./resources/watch.mp3 -e wearable-class-device -l en-US`
107+
)
108+
.example(
109+
`node $0 synthesize "This is optimized for Home Entertainment System" -f ./resources/homestereo.mp3 -e large-home-entertainment-class-device`
110+
)
111+
.example(
112+
`node $0 synthesize "This is optimized for the Car" -f ./resources/car.mp3 -e large-automotive-class-device`
113+
)
114+
.wrap(120)
115+
.recommendCommands()
116+
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
117+
.help()
118+
.strict().argv;
119+
}
120+
121+
main().catch(console.error);

texttospeech/listVoices.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,40 @@
1515

1616
'use strict';
1717

18-
function listVoices() {
18+
async function listVoices() {
1919
// [START tts_list_voices]
2020
const textToSpeech = require('@google-cloud/text-to-speech');
2121

2222
const client = new textToSpeech.TextToSpeechClient();
2323

24-
client
25-
.listVoices({})
26-
.then(results => {
27-
const voices = results[0].voices;
24+
const [result] = await client.listVoices({});
25+
const voices = result.voices;
2826

29-
console.log('Voices:');
30-
voices.forEach(voice => {
31-
console.log(`Name: ${voice.name}`);
32-
console.log(` SSML Voice Gender: ${voice.ssmlGender}`);
33-
console.log(
34-
` Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`
35-
);
36-
console.log(` Supported languages:`);
37-
voice.languageCodes.forEach(languageCode => {
38-
console.log(` ${languageCode}`);
39-
});
40-
});
41-
})
42-
.catch(err => {
43-
console.error('ERROR:', err);
27+
console.log('Voices:');
28+
voices.forEach(voice => {
29+
console.log(`Name: ${voice.name}`);
30+
console.log(` SSML Voice Gender: ${voice.ssmlGender}`);
31+
console.log(` Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`);
32+
console.log(` Supported languages:`);
33+
voice.languageCodes.forEach(languageCode => {
34+
console.log(` ${languageCode}`);
4435
});
36+
});
4537
// [END tts_list_voices]
4638
}
4739

40+
async function main() {
4841
require(`yargs`) // eslint-disable-line
49-
.demand(1)
50-
.command(`list-voices`, `List supported voices.`, {}, () => listVoices())
51-
.example(`node $0 list-voices`)
52-
.wrap(120)
53-
.recommendCommands()
54-
.epilogue(
55-
`For more information, see https://cloud.google.com/text-to-speech/docs`
56-
)
57-
.help()
58-
.strict().argv;
42+
.demand(1)
43+
.command(`list-voices`, `List supported voices.`, {}, () => listVoices())
44+
.example(`node $0 list-voices`)
45+
.wrap(120)
46+
.recommendCommands()
47+
.epilogue(
48+
`For more information, see https://cloud.google.com/text-to-speech/docs`
49+
)
50+
.help()
51+
.strict().argv;
52+
}
53+
54+
main().catch(console.error);

texttospeech/quickstart.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,36 @@
1515

1616
'use strict';
1717

18-
// [START tts_quickstart]
19-
const fs = require('fs');
20-
21-
// Imports the Google Cloud client library
22-
const textToSpeech = require('@google-cloud/text-to-speech');
23-
24-
// Creates a client
25-
const client = new textToSpeech.TextToSpeechClient();
26-
27-
// The text to synthesize
28-
const text = 'Hello, world!';
29-
30-
// Construct the request
31-
const request = {
32-
input: {text: text},
33-
// Select the language and SSML Voice Gender (optional)
34-
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
35-
// Select the type of audio encoding
36-
audioConfig: {audioEncoding: 'MP3'},
37-
};
38-
39-
// Performs the Text-to-Speech request
40-
client.synthesizeSpeech(request, (err, response) => {
41-
if (err) {
42-
console.error('ERROR:', err);
43-
return;
44-
}
45-
18+
async function main() {
19+
// [START tts_quickstart]
20+
const fs = require('fs');
21+
const util = require('util');
22+
23+
// Imports the Google Cloud client library
24+
const textToSpeech = require('@google-cloud/text-to-speech');
25+
26+
// Creates a client
27+
const client = new textToSpeech.TextToSpeechClient();
28+
29+
// The text to synthesize
30+
const text = 'Hello, world!';
31+
32+
// Construct the request
33+
const request = {
34+
input: {text: text},
35+
// Select the language and SSML Voice Gender (optional)
36+
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
37+
// Select the type of audio encoding
38+
audioConfig: {audioEncoding: 'MP3'},
39+
};
40+
41+
// Performs the Text-to-Speech request
42+
const [response] = await client.synthesizeSpeech(request);
4643
// Write the binary audio content to a local file
47-
fs.writeFile('output.mp3', response.audioContent, 'binary', err => {
48-
if (err) {
49-
console.error('ERROR:', err);
50-
return;
51-
}
52-
console.log('Audio content written to file: output.mp3');
53-
});
54-
});
55-
// [END tts_quickstart]
44+
const writeFile = util.promisify(fs.writeFile);
45+
await writeFile('output.mp3', response.audioContent, 'binary');
46+
console.log('Audio content written to file: output.mp3');
47+
// [END tts_quickstart]
48+
}
49+
50+
main().catch(console.error);

0 commit comments

Comments
 (0)