File tree 2 files changed +21
-9
lines changed
packages/google-cloud-node/samples
2 files changed +21
-9
lines changed Original file line number Diff line number Diff line change 18
18
"test" : " npm run system-test"
19
19
},
20
20
"dependencies" : {
21
- "@google-cloud/speech" : " 0.9.3 " ,
22
- "@google-cloud/storage" : " 1.1.1 " ,
21
+ "@google-cloud/speech" : " ^0.10.1 " ,
22
+ "@google-cloud/storage" : " ^1.2.0 " ,
23
23
"node-record-lpcm16" : " 0.3.0" ,
24
- "yargs" : " 8.0.2"
24
+ "yargs" : " ^ 8.0.2"
25
25
},
26
26
"devDependencies" : {
27
- "@google-cloud/nodejs-repo-tools" : " 1.4.15" ,
28
- "ava" : " 0.19.1" ,
27
+ "@google-cloud/nodejs-repo-tools" : " ^ 1.4.15" ,
28
+ "ava" : " ^ 0.19.1" ,
29
29
"proxyquire" : " 1.8.0" ,
30
30
"sinon" : " 2.3.4"
31
31
},
Original file line number Diff line number Diff line change 18
18
// [START speech_quickstart]
19
19
// Imports the Google Cloud client library
20
20
const Speech = require ( '@google-cloud/speech' ) ;
21
+ const fs = require ( 'fs' ) ;
21
22
22
23
// Your Google Cloud Platform project ID
23
- const projectId = 'YOUR_PROJECT_ID ' ;
24
+ const projectId = 'your-project-id ' ;
24
25
25
26
// Instantiates a client
26
27
const speechClient = Speech ( {
@@ -30,17 +31,28 @@ const speechClient = Speech({
30
31
// The name of the audio file to transcribe
31
32
const fileName = './resources/audio.raw' ;
32
33
34
+ // Reads a local audio file and converts it to base64
35
+ const file = fs . readFileSync ( fileName ) ;
36
+ const audioBytes = file . toString ( 'base64' ) ;
37
+
33
38
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
34
- const options = {
39
+ const audio = {
40
+ content : audioBytes
41
+ } ;
42
+ const config = {
35
43
encoding : 'LINEAR16' ,
36
44
sampleRateHertz : 16000 ,
37
45
languageCode : 'en-US'
38
46
} ;
47
+ const request = {
48
+ audio : audio ,
49
+ config : config
50
+ } ;
39
51
40
52
// Detects speech in the audio file
41
- speechClient . recognize ( fileName , options )
53
+ speechClient . recognize ( request )
42
54
. then ( ( results ) => {
43
- const transcription = results [ 0 ] ;
55
+ const transcription = results [ 0 ] . results [ 0 ] . alternatives [ 0 ] . transcript ;
44
56
console . log ( `Transcription: ${ transcription } ` ) ;
45
57
} )
46
58
. catch ( ( err ) => {
You can’t perform that action at this time.
0 commit comments