26
26
function syncRecognize ( filename , encoding , sampleRateHertz , languageCode ) {
27
27
// [START speech_sync_recognize]
28
28
// Imports the Google Cloud client library
29
+ const fs = require ( 'fs' ) ;
29
30
const Speech = require ( '@google-cloud/speech' ) ;
30
31
31
32
// Instantiates a client
@@ -43,18 +44,25 @@ function syncRecognize (filename, encoding, sampleRateHertz, languageCode) {
43
44
// The BCP-47 language code to use, e.g. 'en-US'
44
45
// const languageCode = 'en-US';
45
46
46
- const request = {
47
+ const config = {
47
48
encoding : encoding ,
48
49
sampleRateHertz : sampleRateHertz ,
49
50
languageCode : languageCode
50
51
} ;
52
+ const audio = {
53
+ content : fs . readFileSync ( filename ) . toString ( 'base64' )
54
+ } ;
55
+
56
+ const request = {
57
+ config : config ,
58
+ audio : audio
59
+ } ;
51
60
52
61
// Detects speech in the audio file
53
- speech . recognize ( filename , request )
62
+ speech . recognize ( request )
54
63
. 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 ) ;
58
66
} )
59
67
. catch ( ( err ) => {
60
68
console . error ( 'ERROR:' , err ) ;
@@ -82,18 +90,25 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
82
90
// The BCP-47 language code to use, e.g. 'en-US'
83
91
// const languageCode = 'en-US';
84
92
85
- const request = {
93
+ const config = {
86
94
encoding : encoding ,
87
95
sampleRateHertz : sampleRateHertz ,
88
96
languageCode : languageCode
89
97
} ;
98
+ const audio = {
99
+ uri : gcsUri
100
+ } ;
101
+
102
+ const request = {
103
+ config : config ,
104
+ audio : audio
105
+ } ;
90
106
91
107
// Detects speech in the audio file
92
- speech . recognize ( gcsUri , request )
108
+ speech . recognize ( request )
93
109
. 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 ) ;
97
112
} )
98
113
. catch ( ( err ) => {
99
114
console . error ( 'ERROR:' , err ) ;
@@ -105,6 +120,7 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
105
120
// [START speech_async_recognize]
106
121
// Imports the Google Cloud client library
107
122
const Speech = require ( '@google-cloud/speech' ) ;
123
+ const fs = require ( 'fs' ) ;
108
124
109
125
// Instantiates a client
110
126
const speech = Speech ( ) ;
@@ -121,22 +137,30 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
121
137
// The BCP-47 language code to use, e.g. 'en-US'
122
138
// const languageCode = 'en-US';
123
139
124
- const request = {
140
+ const config = {
125
141
encoding : encoding ,
126
142
sampleRateHertz : sampleRateHertz ,
127
143
languageCode : languageCode
128
144
} ;
145
+ const audio = {
146
+ content : fs . readFileSync ( filename ) . toString ( 'base64' )
147
+ } ;
148
+
149
+ const request = {
150
+ config : config ,
151
+ audio : audio
152
+ } ;
129
153
130
154
// Detects speech in the audio file. This creates a recognition job that you
131
155
// can wait for now, or get its result later.
132
- speech . startRecognition ( filename , request )
156
+ speech . longRunningRecognize ( request )
133
157
. then ( ( results ) => {
134
158
const operation = results [ 0 ] ;
135
159
// Get a Promise representation of the final result of the job
136
160
return operation . promise ( ) ;
137
161
} )
138
162
. then ( ( results ) => {
139
- const transcription = results [ 0 ] ;
163
+ const transcription = results [ 0 ] . results [ 0 ] . alternatives [ 0 ] . transcript ;
140
164
console . log ( `Transcription: ${ transcription } ` ) ;
141
165
} )
142
166
. catch ( ( err ) => {
@@ -165,22 +189,31 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
165
189
// The BCP-47 language code to use, e.g. 'en-US'
166
190
// const languageCode = 'en-US';
167
191
168
- const request = {
192
+ const config = {
169
193
encoding : encoding ,
170
194
sampleRateHertz : sampleRateHertz ,
171
195
languageCode : languageCode
172
196
} ;
173
197
198
+ const audio = {
199
+ uri : gcsUri
200
+ } ;
201
+
202
+ const request = {
203
+ config : config ,
204
+ audio : audio
205
+ } ;
206
+
174
207
// Detects speech in the audio file. This creates a recognition job that you
175
208
// can wait for now, or get its result later.
176
- speech . startRecognition ( gcsUri , request )
209
+ speech . longRunningRecognize ( request )
177
210
. then ( ( results ) => {
178
211
const operation = results [ 0 ] ;
179
212
// Get a Promise representation of the final result of the job
180
213
return operation . promise ( ) ;
181
214
} )
182
215
. then ( ( results ) => {
183
- const transcription = results [ 0 ] ;
216
+ const transcription = results [ 0 ] . results [ 0 ] . alternatives [ 0 ] . transcript ;
184
217
console . log ( `Transcription: ${ transcription } ` ) ;
185
218
} )
186
219
. catch ( ( err ) => {
@@ -221,10 +254,11 @@ function streamingRecognize (filename, encoding, sampleRateHertz, languageCode)
221
254
} ;
222
255
223
256
// Stream the audio to the Google Cloud Speech API
224
- const recognizeStream = speech . createRecognizeStream ( request )
257
+ const recognizeStream = speech . streamingRecognize ( request )
225
258
. on ( 'error' , console . error )
226
259
. on ( 'data' , ( data ) => {
227
- console . log ( `Transcription: ${ data . results } ` ) ;
260
+ console . log (
261
+ `Transcription: ${ data . results [ 0 ] . alternatives [ 0 ] . transcript } ` ) ;
228
262
} ) ;
229
263
230
264
// Stream an audio file from disk to the Speech API, e.g. "./resources/audio.raw"
@@ -261,9 +295,13 @@ function streamingMicRecognize (encoding, sampleRateHertz, languageCode) {
261
295
} ;
262
296
263
297
// Create a recognize stream
264
- const recognizeStream = speech . createRecognizeStream ( request )
298
+ const recognizeStream = speech . streamingRecognize ( request )
265
299
. 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` ) ) ;
267
305
268
306
// Start recording and send the microphone input to the Speech API
269
307
record
0 commit comments