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 ) ;
0 commit comments