forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize_streaming.js
127 lines (112 loc) · 3.6 KB
/
recognize_streaming.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
var async = require('async');
var fs = require('fs');
var path = require('path');
var grpc = require('grpc');
var googleProtoFiles = require('google-proto-files');
var googleAuth = require('google-auto-auth');
var Transform = require('stream').Transform;
// [START proto]
var PROTO_ROOT_DIR = googleProtoFiles('..');
var protoDescriptor = grpc.load({
root: PROTO_ROOT_DIR,
file: path.relative(PROTO_ROOT_DIR, googleProtoFiles.speech.v1beta1)
}, 'proto', {
binaryAsBase64: true,
convertFieldsToCamelCase: true
});
var speechProto = protoDescriptor.google.cloud.speech.v1beta1;
// [END proto]
// [START authenticating]
function getSpeechService (host, callback) {
var googleAuthClient = googleAuth({
scopes: [
'https://www.googleapis.com/auth/cloud-platform'
]
});
googleAuthClient.getAuthClient(function (err, authClient) {
if (err) {
return callback(err);
}
var credentials = grpc.credentials.combineChannelCredentials(
grpc.credentials.createSsl(),
grpc.credentials.createFromGoogleCredential(authClient)
);
console.log('Loading speech service...');
var stub = new speechProto.Speech(host, credentials);
return callback(null, stub);
});
}
// [END authenticating]
function main (inputFile, host, callback) {
async.waterfall([
function (cb) {
getSpeechService(host, cb);
},
// [START send_request]
function sendRequest (speechService, cb) {
console.log('Analyzing speech...');
var responses = [];
var call = speechService.streamingRecognize();
// Listen for various responses
call.on('error', cb);
call.on('data', function (recognizeResponse) {
if (recognizeResponse) {
responses.push(recognizeResponse);
if (recognizeResponse.results && recognizeResponse.results.length) {
console.log(JSON.stringify(recognizeResponse.results, null, 2));
}
}
});
call.on('end', function () {
cb(null, responses);
});
// Write the initial recognize reqeust
call.write({
streamingConfig: {
config: {
encoding: 'LINEAR16',
sampleRate: 16000
},
interimResults: false,
singleUtterance: false
}
});
var toRecognizeRequest = new Transform({ objectMode: true });
toRecognizeRequest._transform = function (chunk, encoding, done) {
done(null, {
audioContent: chunk
});
};
// Stream the audio to the Speech API
fs.createReadStream(inputFile)
.pipe(toRecognizeRequest)
.pipe(call);
}
// [END send_request]
], callback);
}
// [START run_application]
if (module === require.main) {
if (process.argv.length < 3) {
console.log('Usage: node recognize_streaming <inputFile> [speech_api_host]');
process.exit();
}
var inputFile = process.argv[2];
var host = process.argv[3];
main(inputFile, host || 'speech.googleapis.com', console.log);
}
// [END run_application]
exports.main = main;