Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Speech API streaming sample. #144

Merged
merged 5 commits into from
Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions speech/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ Example:

[recognition_docs]: https://cloud.google.com/speech/
[recognition_code]: recognize.js

### Recognition (Streaming)

View the [documentation][recognition_streaming_docs] or the [source code][recognition_streaming_code].

__Run the sample:__

Usage: `node recognize_streaming <path-to-audio-file>`

Example:

node recognize_streaming "/path/to/audio.file"

[recognition_streaming_docs]: https://cloud.google.com/speech/
[recognition_streaming_code]: recognize_streaming.js
9 changes: 5 additions & 4 deletions speech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"node": ">=0.10.x"
},
"dependencies": {
"googleapis": "^7.1.0"
},
"devDependencies": {
"async": "^1.5.2"
"async": "^1.5.2",
"google-auto-auth": "^0.2.4",
"google-proto-files": "^0.2.4",
"googleapis": "^7.1.0",
"grpc": "^0.15.0"
}
}
138 changes: 138 additions & 0 deletions speech/recognize_streaming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// 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');

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

var protoDescriptor = grpc.load({
root: PROTO_ROOT_DIR,
file: path.relative(PROTO_ROOT_DIR, PROTO_FILE_PATH)
}, 'proto', {
binaryAsBase64: true,
convertFieldsToCamelCase: true
});
var speechProto = protoDescriptor.google.cloud.speech.v1;
// [END proto]

// [START authenticating]
function getSpeechService (callback) {
var authClient = googleAuth({
scopes: [
'https://www.googleapis.com/auth/cloud-platform'
]
});

authClient.getAuthClient(function (err, authClient) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't authClient alias a variable in the outer scope? Should we give it a different name?

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('speech.googleapis.com', credentials);
return callback(null, stub);
});
}
// [END authenticating]

// [START construct_request]
function getAudioFile (inputFile, callback) {
fs.readFile(inputFile, function (err, audioFile) {
if (err) {
return callback(err);
}
console.log('Got audio file!');
return callback(null, audioFile);
});
}
// [END construct_request]

function main (inputFile, callback) {
var audioFile;

async.waterfall([
function (cb) {
getAudioFile(inputFile, cb);
},
function (_audioFile, cb) {
audioFile = _audioFile;
getSpeechService(cb);
},
// [START send_request]
function sendRequest (speechService, cb) {
console.log('Analyzing speech...');
var responses = [];
var call = speechService.recognize();

// Listen for various responses
call.on('error', cb);
call.on('data', function (recognizeResponse) {
if (recognizeResponse) {
responses.push(recognizeResponse);
}
});
call.on('end', function () {
cb(null, responses);
});

// Write the initial recognize reqeust
call.write(new speechProto.RecognizeRequest({
initialRequest: new speechProto.InitialRecognizeRequest({
encoding: 'LINEAR16',
sampleRate: 16000,
interimResults: false,
continuous: false,
enableEndpointerEvents: false
})
}));

// Write an audio request
call.write(new speechProto.RecognizeRequest({
audioRequest: new speechProto.AudioRequest({
content: audioFile
})
}));

// Signal that we're done writing
call.end();
}
// [END send_request]
], callback);
}

// [START run_application]
if (module === require.main) {
if (process.argv.length < 3) {
console.log('Usage: node recognize_streaming <inputFile>');
process.exit();
}
var inputFile = process.argv[2];
main(inputFile, console.log);
}
// [END run_application]

exports.main = main;
6 changes: 4 additions & 2 deletions test/functions/sendgrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function getMockContext () {
};
}

describe('functions:pubsub', function () {
describe('functions:sendgrid', function () {
it('Send fails if not a POST request', function () {
var expectedMsg = 'Only POST requests are accepted';
var mocks = getMocks();
Expand Down Expand Up @@ -566,7 +566,9 @@ describe('functions:pubsub', function () {
bucket: 'event-bucket',
name: name
});

sendgridSample.mocks.job.on('error', function (err) {
assert.equal(err, error);
});
setTimeout(function () {
sendgridSample.mocks.job.emit('error', error);
}, 10);
Expand Down
35 changes: 35 additions & 0 deletions test/speech/recognize_streaming.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 path = require('path');
var recognizeExample = require('../../speech/recognize_streaming');

describe('speech:recognize_streaming', function () {
it('should list entries', function (done) {
recognizeExample.main(
path.join(__dirname, '../../speech/resources/audio.raw'),
function (err, results) {
assert(!err);
assert(results);
assert(results.length === 1);
assert(results[0].results);
assert(console.log.calledWith('Got audio file!'));
assert(console.log.calledWith('Loading speech service...'));
assert(console.log.calledWith('Analyzing speech...'));
done();
}
);
});
});