2323
2424'use strict' ;
2525
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' ) ;
2927
3028// [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
3435 encoding : 'LINEAR16' ,
3536 sampleRate : 16000
36- } , ( err , results ) => {
37- if ( err ) {
38- callback ( err ) ;
39- return ;
40- }
37+ } ;
4138
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+ } ) ;
4546}
4647// [END speech_sync_recognize]
4748
4849// [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
5256 encoding : 'LINEAR16' ,
5357 sampleRate : 16000
54- } , ( err , operation ) => {
55- if ( err ) {
56- callback ( err ) ;
57- return ;
58- }
58+ } ;
5959
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+ } ) ;
6773}
6874// [END speech_async_recognize]
6975
7076// [START speech_streaming_recognize]
77+ const fs = require ( 'fs' ) ;
78+
7179function streamingRecognize ( filename , callback ) {
80+ // Instantiates a client
81+ const speech = Speech ( ) ;
82+
7283 const options = {
7384 config : {
85+ // Configure these settings based on the audio you're transcribing
7486 encoding : 'LINEAR16' ,
7587 sampleRate : 16000
7688 }
@@ -90,9 +102,15 @@ function streamingRecognize (filename, callback) {
90102// [END speech_streaming_recognize]
91103
92104// [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+
94111 const options = {
95112 config : {
113+ // Configure these settings based on the audio you're transcribing
96114 encoding : 'LINEAR16' ,
97115 sampleRate : 16000
98116 }
@@ -110,43 +128,39 @@ function streamingMicRecognize (filename) {
110128}
111129// [END speech_streaming_mic_recognize]
112130
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` )
129132 . 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` )
146161 . wrap ( 120 )
147162 . 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