Skip to content

Commit 01108b0

Browse files
authored
Switch Speech sample to v1beta1 (#146)
* Switch Speech samples to v1beta1 * Now consuming Speech v1beta1 proto file from NPM package. * Make lint happy. * Fix relative path.
1 parent da6a401 commit 01108b0

File tree

3 files changed

+52
-60
lines changed

3 files changed

+52
-60
lines changed

speech/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"async": "^1.5.2",
1313
"google-auto-auth": "^0.2.4",
14-
"google-proto-files": "^0.2.4",
14+
"google-proto-files": "^0.3.0",
1515
"googleapis": "^7.1.0",
1616
"grpc": "^0.15.0"
1717
}

speech/recognize.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@ var async = require('async');
2020
var fs = require('fs');
2121
// [END import_libraries]
2222

23-
// Url to discovery doc file
24-
// [START discovery_doc]
25-
var url = 'https://speech.googleapis.com/$discovery/rest';
26-
// [END discovery_doc]
27-
2823
// [START authenticating]
29-
function getSpeechService (callback) {
24+
function getSpeechService (host, callback) {
3025
// Acquire credentials
3126
google.auth.getApplicationDefault(function (err, authClient) {
3227
if (err) {
@@ -49,9 +44,16 @@ function getSpeechService (callback) {
4944

5045
// Load the speach service using acquired credentials
5146
console.log('Loading speech service...');
47+
48+
// Url to discovery doc file
49+
// [START discovery_doc]
50+
host = host || 'speech.googleapis.com';
51+
var url = 'https://' + host + '/$discovery/rest';
52+
// [END discovery_doc]
53+
5254
google.discoverAPI({
5355
url: url,
54-
version: 'v1',
56+
version: 'v1beta1',
5557
auth: authClient
5658
}, function (err, speechService) {
5759
if (err) {
@@ -72,11 +74,11 @@ function prepareRequest (inputFile, callback) {
7274
console.log('Got audio file!');
7375
var encoded = new Buffer(audioFile).toString('base64');
7476
var payload = {
75-
initialRequest: {
77+
config: {
7678
encoding: 'LINEAR16',
7779
sampleRate: 16000
7880
},
79-
audioRequest: {
81+
audio: {
8082
content: encoded
8183
}
8284
};
@@ -85,7 +87,7 @@ function prepareRequest (inputFile, callback) {
8587
}
8688
// [END construct_request]
8789

88-
function main (inputFile, callback) {
90+
function main (inputFile, host, callback) {
8991
var requestPayload;
9092

9193
async.waterfall([
@@ -94,12 +96,12 @@ function main (inputFile, callback) {
9496
},
9597
function (payload, cb) {
9698
requestPayload = payload;
97-
getSpeechService(cb);
99+
getSpeechService(host, cb);
98100
},
99101
// [START send_request]
100102
function sendRequest (speechService, authClient, cb) {
101103
console.log('Analyzing speech...');
102-
speechService.speech.recognize({
104+
speechService.speech.syncrecognize({
103105
auth: authClient,
104106
resource: requestPayload
105107
}, function (err, result) {
@@ -117,11 +119,12 @@ function main (inputFile, callback) {
117119
// [START run_application]
118120
if (module === require.main) {
119121
if (process.argv.length < 3) {
120-
console.log('Usage: node recognize <inputFile>');
122+
console.log('Usage: node recognize <inputFile> [speech_api_host]');
121123
process.exit();
122124
}
123125
var inputFile = process.argv[2];
124-
main(inputFile, console.log);
126+
var host = process.argv[3];
127+
main(inputFile, host || 'speech.googleapis.com', console.log);
125128
}
126129
// [END run_application]
127130
// [END app]

speech/recognize_streaming.js

+34-45
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ var path = require('path');
1919
var grpc = require('grpc');
2020
var googleProtoFiles = require('google-proto-files');
2121
var googleAuth = require('google-auto-auth');
22+
var Transform = require('stream').Transform;
2223

2324
// [START proto]
2425
var PROTO_ROOT_DIR = googleProtoFiles('..');
25-
var PROTO_FILE_PATH = googleProtoFiles('cloud', 'speech', 'v1', 'cloud_speech.proto');
2626

2727
var protoDescriptor = grpc.load({
2828
root: PROTO_ROOT_DIR,
29-
file: path.relative(PROTO_ROOT_DIR, PROTO_FILE_PATH)
29+
file: path.relative(PROTO_ROOT_DIR, googleProtoFiles.speech.v1beta1)
3030
}, 'proto', {
3131
binaryAsBase64: true,
3232
convertFieldsToCamelCase: true
3333
});
34-
var speechProto = protoDescriptor.google.cloud.speech.v1;
34+
var speechProto = protoDescriptor.google.cloud.speech.v1beta1;
3535
// [END proto]
3636

3737
// [START authenticating]
38-
function getSpeechService (callback) {
38+
function getSpeechService (host, callback) {
3939
var googleAuthClient = googleAuth({
4040
scopes: [
4141
'https://www.googleapis.com/auth/cloud-platform'
@@ -53,72 +53,60 @@ function getSpeechService (callback) {
5353
);
5454

5555
console.log('Loading speech service...');
56-
var stub = new speechProto.Speech('speech.googleapis.com', credentials);
56+
var stub = new speechProto.Speech(host, credentials);
5757
return callback(null, stub);
5858
});
5959
}
6060
// [END authenticating]
6161

62-
// [START construct_request]
63-
function getAudioFile (inputFile, callback) {
64-
fs.readFile(inputFile, function (err, audioFile) {
65-
if (err) {
66-
return callback(err);
67-
}
68-
console.log('Got audio file!');
69-
return callback(null, audioFile);
70-
});
71-
}
72-
// [END construct_request]
73-
74-
function main (inputFile, callback) {
75-
var audioFile;
76-
62+
function main (inputFile, host, callback) {
7763
async.waterfall([
7864
function (cb) {
79-
getAudioFile(inputFile, cb);
80-
},
81-
function (_audioFile, cb) {
82-
audioFile = _audioFile;
83-
getSpeechService(cb);
65+
getSpeechService(host, cb);
8466
},
8567
// [START send_request]
8668
function sendRequest (speechService, cb) {
8769
console.log('Analyzing speech...');
8870
var responses = [];
89-
var call = speechService.recognize();
71+
var call = speechService.streamingRecognize();
9072

9173
// Listen for various responses
9274
call.on('error', cb);
9375
call.on('data', function (recognizeResponse) {
9476
if (recognizeResponse) {
9577
responses.push(recognizeResponse);
78+
if (recognizeResponse.results && recognizeResponse.results.length) {
79+
console.log(JSON.stringify(recognizeResponse.results, null, 2));
80+
}
9681
}
9782
});
9883
call.on('end', function () {
9984
cb(null, responses);
10085
});
10186

10287
// Write the initial recognize reqeust
103-
call.write(new speechProto.RecognizeRequest({
104-
initialRequest: new speechProto.InitialRecognizeRequest({
105-
encoding: 'LINEAR16',
106-
sampleRate: 16000,
88+
call.write({
89+
streamingConfig: {
90+
config: {
91+
encoding: 'LINEAR16',
92+
sampleRate: 16000
93+
},
10794
interimResults: false,
108-
continuous: false,
109-
enableEndpointerEvents: false
110-
})
111-
}));
112-
113-
// Write an audio request
114-
call.write(new speechProto.RecognizeRequest({
115-
audioRequest: new speechProto.AudioRequest({
116-
content: audioFile
117-
})
118-
}));
95+
singleUtterance: false
96+
}
97+
});
11998

120-
// Signal that we're done writing
121-
call.end();
99+
var toRecognizeRequest = new Transform({ objectMode: true });
100+
toRecognizeRequest._transform = function (chunk, encoding, done) {
101+
done(null, {
102+
audioContent: chunk
103+
});
104+
};
105+
106+
// Stream the audio to the Speech API
107+
fs.createReadStream(inputFile)
108+
.pipe(toRecognizeRequest)
109+
.pipe(call);
122110
}
123111
// [END send_request]
124112
], callback);
@@ -127,11 +115,12 @@ function main (inputFile, callback) {
127115
// [START run_application]
128116
if (module === require.main) {
129117
if (process.argv.length < 3) {
130-
console.log('Usage: node recognize_streaming <inputFile>');
118+
console.log('Usage: node recognize_streaming <inputFile> [speech_api_host]');
131119
process.exit();
132120
}
133121
var inputFile = process.argv[2];
134-
main(inputFile, console.log);
122+
var host = process.argv[3];
123+
main(inputFile, host || 'speech.googleapis.com', console.log);
135124
}
136125
// [END run_application]
137126

0 commit comments

Comments
 (0)