23
23
24
24
'use strict' ;
25
25
26
- const fs = require ( 'fs' ) ;
27
- const record = require ( 'node-record-lpcm16' ) ;
28
- const speech = require ( '@google-cloud/speech' ) ( ) ;
26
+ const Speech = require ( '@google-cloud/speech' ) ;
29
27
30
28
// [START speech_sync_recognize]
31
- function syncRecognize ( filename , callback ) {
32
- // Detect speech in the audio file, e.g. "./resources/audio.raw"
33
- speech . recognize ( filename , {
29
+ function syncRecognize ( filename ) {
30
+ // Instantiates a client
31
+ const speech = Speech ( ) ;
32
+
33
+ const config = {
34
+ // Configure these settings based on the audio you're transcribing
34
35
encoding : 'LINEAR16' ,
35
36
sampleRate : 16000
36
- } , ( err , results ) => {
37
- if ( err ) {
38
- callback ( err ) ;
39
- return ;
40
- }
37
+ } ;
41
38
42
- console . log ( 'Results:' , results ) ;
43
- callback ( ) ;
44
- } ) ;
39
+ // Detects speech in the audio file, e.g. "./resources/audio.raw"
40
+ return speech . recognize ( filename , config )
41
+ . then ( ( results ) => {
42
+ const transcription = results [ 0 ] ;
43
+ console . log ( `Transcription: ${ transcription } ` ) ;
44
+ return transcription ;
45
+ } ) ;
45
46
}
46
47
// [END speech_sync_recognize]
47
48
48
49
// [START speech_async_recognize]
49
- function asyncRecognize ( filename , callback ) {
50
- // Detect speech in the audio file, e.g. "./resources/audio.raw"
51
- speech . startRecognition ( filename , {
50
+ function asyncRecognize ( filename ) {
51
+ // Instantiates a client
52
+ const speech = Speech ( ) ;
53
+
54
+ const config = {
55
+ // Configure these settings based on the audio you're transcribing
52
56
encoding : 'LINEAR16' ,
53
57
sampleRate : 16000
54
- } , ( err , operation ) => {
55
- if ( err ) {
56
- callback ( err ) ;
57
- return ;
58
- }
58
+ } ;
59
59
60
- operation
61
- . on ( 'error' , callback )
62
- . on ( 'complete' , ( results ) => {
63
- console . log ( 'Results:' , results ) ;
64
- callback ( ) ;
65
- } ) ;
66
- } ) ;
60
+ // Detects speech in the audio file, e.g. "./resources/audio.raw"
61
+ // This creates a recognition job that you can wait for now, or get its result
62
+ // later.
63
+ return speech . startRecognition ( filename , config )
64
+ . then ( ( results ) => {
65
+ const operation = results [ 0 ] ;
66
+ // Get a Promise represention the final result of the job
67
+ return operation . promise ( ) ;
68
+ } )
69
+ . then ( ( transcription ) => {
70
+ console . log ( `Transcription: ${ transcription } ` ) ;
71
+ return transcription ;
72
+ } ) ;
67
73
}
68
74
// [END speech_async_recognize]
69
75
70
76
// [START speech_streaming_recognize]
77
+ const fs = require ( 'fs' ) ;
78
+
71
79
function streamingRecognize ( filename , callback ) {
80
+ // Instantiates a client
81
+ const speech = Speech ( ) ;
82
+
72
83
const options = {
73
84
config : {
85
+ // Configure these settings based on the audio you're transcribing
74
86
encoding : 'LINEAR16' ,
75
87
sampleRate : 16000
76
88
}
@@ -90,9 +102,15 @@ function streamingRecognize (filename, callback) {
90
102
// [END speech_streaming_recognize]
91
103
92
104
// [START speech_streaming_mic_recognize]
93
- function streamingMicRecognize ( filename ) {
105
+ const record = require ( 'node-record-lpcm16' ) ;
106
+
107
+ function streamingMicRecognize ( ) {
108
+ // Instantiates a client
109
+ const speech = Speech ( ) ;
110
+
94
111
const options = {
95
112
config : {
113
+ // Configure these settings based on the audio you're transcribing
96
114
encoding : 'LINEAR16' ,
97
115
sampleRate : 16000
98
116
}
@@ -110,43 +128,39 @@ function streamingMicRecognize (filename) {
110
128
}
111
129
// [END speech_streaming_mic_recognize]
112
130
113
- // The command-line program
114
- var cli = require ( 'yargs' ) ;
115
- var utils = require ( '../utils' ) ;
116
-
117
- var program = module . exports = {
118
- syncRecognize : syncRecognize ,
119
- asyncRecognize : asyncRecognize ,
120
- streamingRecognize : streamingRecognize ,
121
- streamingMicRecognize : streamingMicRecognize ,
122
- main : function ( args ) {
123
- // Run the command-line program
124
- cli . help ( ) . strict ( ) . parse ( args ) . argv ;
125
- }
126
- } ;
127
-
128
- cli
131
+ require ( `yargs` )
129
132
. demand ( 1 )
130
- . command ( 'sync <filename>' , 'Detects speech in an audio file.' , { } , function ( options ) {
131
- program . syncRecognize ( options . filename , utils . makeHandler ( false ) ) ;
132
- } )
133
- . command ( 'async <filename>' , 'Creates a job to detect speech in an audio file, and waits for the job to complete.' , { } , function ( options ) {
134
- program . asyncRecognize ( options . filename , utils . makeHandler ( false ) ) ;
135
- } )
136
- . command ( 'stream <filename>' , 'Detects speech in an audio file by streaming it to the Speech API.' , { } , function ( options ) {
137
- program . streamingRecognize ( options . filename , utils . makeHandler ( false ) ) ;
138
- } )
139
- . command ( 'listen' , 'Detects speech in a microphone input stream.' , { } , function ( ) {
140
- program . streamingMicRecognize ( ) ;
141
- } )
142
- . example ( 'node $0 sync ./resources/audio.raw' , 'Detects speech in "./resources/audio.raw".' )
143
- . example ( 'node $0 async ./resources/audio.raw' , 'Creates a job to detect speech in "./resources/audio.raw", and waits for the job to complete.' )
144
- . example ( 'node $0 stream ./resources/audio.raw' , 'Detects speech in "./resources/audio.raw" by streaming it to the Speech API.' )
145
- . example ( 'node $0 listen' , 'Detects speech in a microphone input stream.' )
133
+ . command (
134
+ `sync <filename>` ,
135
+ `Detects speech in an audio file.` ,
136
+ { } ,
137
+ ( opts ) => syncRecognize ( opts . filename )
138
+ )
139
+ . command (
140
+ `async <filename>` ,
141
+ `Creates a job to detect speech in an audio file, and waits for the job to complete.` ,
142
+ { } ,
143
+ ( opts ) => asyncRecognize ( opts . filename )
144
+ )
145
+ . command (
146
+ `stream <filename>` ,
147
+ `Detects speech in an audio file by streaming it to the Speech API.` ,
148
+ { } ,
149
+ ( opts ) => streamingRecognize ( opts . filename , ( ) => { } )
150
+ )
151
+ . command (
152
+ `listen` ,
153
+ `Detects speech in a microphone input stream.` ,
154
+ { } ,
155
+ streamingMicRecognize
156
+ )
157
+ . example ( `node $0 sync ./resources/audio.raw` )
158
+ . example ( `node $0 async ./resources/audio.raw` )
159
+ . example ( `node $0 stream ./resources/audio.raw` )
160
+ . example ( `node $0 listen` )
146
161
. wrap ( 120 )
147
162
. recommendCommands ( )
148
- . epilogue ( 'For more information, see https://cloud.google.com/speech/docs' ) ;
149
-
150
- if ( module === require . main ) {
151
- program . main ( process . argv . slice ( 2 ) ) ;
152
- }
163
+ . epilogue ( `For more information, see https://cloud.google.com/speech/docs` )
164
+ . help ( )
165
+ . strict ( )
166
+ . argv ;
0 commit comments