2626function syncRecognize ( filename , encoding , sampleRateHertz , languageCode ) {
2727 // [START speech_sync_recognize]
2828 // Imports the Google Cloud client library
29+ const fs = require ( 'fs' ) ;
2930 const Speech = require ( '@google-cloud/speech' ) ;
3031
3132 // Instantiates a client
@@ -43,18 +44,25 @@ function syncRecognize (filename, encoding, sampleRateHertz, languageCode) {
4344 // The BCP-47 language code to use, e.g. 'en-US'
4445 // const languageCode = 'en-US';
4546
46- const request = {
47+ const config = {
4748 encoding : encoding ,
4849 sampleRateHertz : sampleRateHertz ,
4950 languageCode : languageCode
5051 } ;
52+ const audio = {
53+ content : fs . readFileSync ( filename ) . toString ( 'base64' )
54+ } ;
55+
56+ const request = {
57+ config : config ,
58+ audio : audio
59+ } ;
5160
5261 // Detects speech in the audio file
53- speech . recognize ( filename , request )
62+ speech . recognize ( request )
5463 . then ( ( results ) => {
55- const transcription = results [ 0 ] ;
56-
57- console . log ( `Transcription: ${ transcription } ` ) ;
64+ const transcription = results [ 0 ] . results [ 0 ] . alternatives [ 0 ] . transcript ;
65+ console . log ( `Transcription: ` , transcription ) ;
5866 } )
5967 . catch ( ( err ) => {
6068 console . error ( 'ERROR:' , err ) ;
@@ -82,18 +90,25 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
8290 // The BCP-47 language code to use, e.g. 'en-US'
8391 // const languageCode = 'en-US';
8492
85- const request = {
93+ const config = {
8694 encoding : encoding ,
8795 sampleRateHertz : sampleRateHertz ,
8896 languageCode : languageCode
8997 } ;
98+ const audio = {
99+ uri : gcsUri
100+ } ;
101+
102+ const request = {
103+ config : config ,
104+ audio : audio
105+ } ;
90106
91107 // Detects speech in the audio file
92- speech . recognize ( gcsUri , request )
108+ speech . recognize ( request )
93109 . then ( ( results ) => {
94- const transcription = results [ 0 ] ;
95-
96- console . log ( `Transcription: ${ transcription } ` ) ;
110+ const transcription = results [ 0 ] . results [ 0 ] . alternatives [ 0 ] . transcript ;
111+ console . log ( `Transcription: ` , transcription ) ;
97112 } )
98113 . catch ( ( err ) => {
99114 console . error ( 'ERROR:' , err ) ;
@@ -105,6 +120,7 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
105120 // [START speech_async_recognize]
106121 // Imports the Google Cloud client library
107122 const Speech = require ( '@google-cloud/speech' ) ;
123+ const fs = require ( 'fs' ) ;
108124
109125 // Instantiates a client
110126 const speech = Speech ( ) ;
@@ -121,22 +137,30 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
121137 // The BCP-47 language code to use, e.g. 'en-US'
122138 // const languageCode = 'en-US';
123139
124- const request = {
140+ const config = {
125141 encoding : encoding ,
126142 sampleRateHertz : sampleRateHertz ,
127143 languageCode : languageCode
128144 } ;
145+ const audio = {
146+ content : fs . readFileSync ( filename ) . toString ( 'base64' )
147+ } ;
148+
149+ const request = {
150+ config : config ,
151+ audio : audio
152+ } ;
129153
130154 // Detects speech in the audio file. This creates a recognition job that you
131155 // can wait for now, or get its result later.
132- speech . startRecognition ( filename , request )
156+ speech . longRunningRecognize ( request )
133157 . then ( ( results ) => {
134158 const operation = results [ 0 ] ;
135159 // Get a Promise representation of the final result of the job
136160 return operation . promise ( ) ;
137161 } )
138162 . then ( ( results ) => {
139- const transcription = results [ 0 ] ;
163+ const transcription = results [ 0 ] . results [ 0 ] . alternatives [ 0 ] . transcript ;
140164 console . log ( `Transcription: ${ transcription } ` ) ;
141165 } )
142166 . catch ( ( err ) => {
@@ -165,22 +189,31 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
165189 // The BCP-47 language code to use, e.g. 'en-US'
166190 // const languageCode = 'en-US';
167191
168- const request = {
192+ const config = {
169193 encoding : encoding ,
170194 sampleRateHertz : sampleRateHertz ,
171195 languageCode : languageCode
172196 } ;
173197
198+ const audio = {
199+ uri : gcsUri
200+ } ;
201+
202+ const request = {
203+ config : config ,
204+ audio : audio
205+ } ;
206+
174207 // Detects speech in the audio file. This creates a recognition job that you
175208 // can wait for now, or get its result later.
176- speech . startRecognition ( gcsUri , request )
209+ speech . longRunningRecognize ( request )
177210 . then ( ( results ) => {
178211 const operation = results [ 0 ] ;
179212 // Get a Promise representation of the final result of the job
180213 return operation . promise ( ) ;
181214 } )
182215 . then ( ( results ) => {
183- const transcription = results [ 0 ] ;
216+ const transcription = results [ 0 ] . results [ 0 ] . alternatives [ 0 ] . transcript ;
184217 console . log ( `Transcription: ${ transcription } ` ) ;
185218 } )
186219 . catch ( ( err ) => {
@@ -221,10 +254,11 @@ function streamingRecognize (filename, encoding, sampleRateHertz, languageCode)
221254 } ;
222255
223256 // Stream the audio to the Google Cloud Speech API
224- const recognizeStream = speech . createRecognizeStream ( request )
257+ const recognizeStream = speech . streamingRecognize ( request )
225258 . on ( 'error' , console . error )
226259 . on ( 'data' , ( data ) => {
227- console . log ( `Transcription: ${ data . results } ` ) ;
260+ console . log (
261+ `Transcription: ${ data . results [ 0 ] . alternatives [ 0 ] . transcript } ` ) ;
228262 } ) ;
229263
230264 // Stream an audio file from disk to the Speech API, e.g. "./resources/audio.raw"
@@ -261,9 +295,13 @@ function streamingMicRecognize (encoding, sampleRateHertz, languageCode) {
261295 } ;
262296
263297 // Create a recognize stream
264- const recognizeStream = speech . createRecognizeStream ( request )
298+ const recognizeStream = speech . streamingRecognize ( request )
265299 . on ( 'error' , console . error )
266- . on ( 'data' , ( data ) => process . stdout . write ( data . results ) ) ;
300+ . on ( 'data' , ( data ) =>
301+ process . stdout . write (
302+ ( data . results [ 0 ] && data . results [ 0 ] . alternatives [ 0 ] )
303+ ? `Transcription: ${ data . results [ 0 ] . alternatives [ 0 ] . transcript } \n`
304+ : `\n\nReached transcription time limit, press Ctrl+C\n` ) ) ;
267305
268306 // Start recording and send the microphone input to the Speech API
269307 record
0 commit comments