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

Upgrade Speech samples. #217

Merged
merged 2 commits into from
Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 23 additions & 23 deletions speech/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ recognition technologies into developer applications.

* [Setup](#setup)
* [Samples](#samples)
* [Recognition](#recognition)
* [Recognize](#recognize)

## Setup

Expand All @@ -28,32 +28,32 @@ recognition technologies into developer applications.

## Samples

### Recognition
### Recognize

View the [documentation][recognition_docs] or the [source code][recognition_code].
View the [documentation][recognize_docs] or the [source code][recognize_code].

__Run the sample:__
__Usage:__ `node recognize --help`

Usage: `node recognize <path-to-audio-file>`
```
Commands:
sync <filename> Detects speech in an audio file.
async <filename> Creates a job to detect speech in an audio file, and waits for the job to complete.
stream <filename> Detects speech in an audio file by streaming it to the Speech API.
listen Detects speech in a microphone input stream.

Example:
Options:
--help Show help [boolean]

node recognize resources/audio.raw
Examples:
node recognize sync ./resources/audio.raw Detects speech in "./resources/audio.raw".
node recognize async ./resources/audio.raw Creates a job to detect speech in "./resources/audio.raw", and waits for
the job to complete.
node recognize stream ./resources/audio.raw Detects speech in "./resources/audio.raw" by streaming it to the Speech
API.
node recognize listen ./resources/audio.raw Detects speech in a microphone input stream.

[recognition_docs]: https://cloud.google.com/speech/
[recognition_code]: recognize.js
For more information, see https://cloud.google.com/speech/docs
```

### 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 resources/audio.raw

[recognition_streaming_docs]: https://cloud.google.com/speech/
[recognition_streaming_code]: recognize_streaming.js
[recognize_docs]: https://cloud.google.com/speech/docs
[recognize_code]: recognize.js
10 changes: 4 additions & 6 deletions speech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
},
"dependencies": {
"async": "^1.5.2",
"google-auto-auth": "^0.2.4",
"google-proto-files": "^0.3.0",
"googleapis": "^12.0.0",
"grpc": "^0.15.0"
"@google-cloud/speech": "^0.1.1",
"node-record-lpcm16": "^0.1.4",
"yargs": "^5.0.0"
},
"devDependencies": {
"mocha": "^2.5.3"
"mocha": "^3.0.2"
}
}
204 changes: 119 additions & 85 deletions speech/recognize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,106 +11,140 @@
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';
/**
* This application demonstrates how to perform basic recognize operations with
* with the Google Cloud Speech API.
*
* For more information, see the README.md under /speech and the documentation
* at https://cloud.google.com/speech/docs.
*/

// [START app]
// [START import_libraries]
var google = require('googleapis');
var async = require('async');
var fs = require('fs');
'use strict';

// Get a reference to the speech service
var speech = google.speech('v1beta1').speech;
// [END import_libraries]
const fs = require('fs');
const record = require('node-record-lpcm16');
const speech = require('@google-cloud/speech')();

// [START authenticating]
function getAuthClient (callback) {
// Acquire credentials
google.auth.getApplicationDefault(function (err, authClient) {
// [START speech_sync_recognize]
function syncRecognize (filename, callback) {
// Detect speech in the audio file, e.g. "./resources/audio.raw"
speech.recognize(filename, {
encoding: 'LINEAR16',
sampleRate: 16000
}, (err, results) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason we're using this callback pattern instead of the standard function (a,b) { ... } one? (Brevity?)

Copy link
Member Author

@jmdobry jmdobry Sep 27, 2016

Choose a reason for hiding this comment

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

With this PR, I decided to start using the ES2015 features of JavaScript that are supported in the latest 4.x LTS release of Node.js. See https://github.com/nodejs/LTS and http://node.green/

With ES2015, one would use arrow functions ((a, b) => { ...) as much as possible. You'll notice in this PR I also started using the const keyword. I also added an engines field to package.json.

if (err) {
return callback(err);
}

// The createScopedRequired method returns true when running on GAE or a
// local developer machine. In that case, the desired scopes must be passed
// in manually. When the code is running in GCE or a Managed VM, the scopes
// are pulled from the GCE metadata server.
// See https://cloud.google.com/compute/docs/authentication for more
// information.
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// Scopes can be specified either as an array or as a single,
// space-delimited string.
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/cloud-platform'
]);
callback(err);
return;
}

return callback(null, authClient);
console.log('Results:', results);
callback();
});
}
// [END authenticating]
// [END speech_sync_recognize]

// [START construct_request]
function prepareRequest (inputFile, callback) {
fs.readFile(inputFile, function (err, audioFile) {
// [START speech_async_recognize]
function asyncRecognize (filename, callback) {
// Detect speech in the audio file, e.g. "./resources/audio.raw"
speech.startRecognition(filename, {
encoding: 'LINEAR16',
sampleRate: 16000
}, (err, operation) => {
if (err) {
return callback(err);
callback(err);
return;
}
console.log('Got audio file!');
var encoded = new Buffer(audioFile).toString('base64');
var payload = {
config: {
encoding: 'LINEAR16',
sampleRate: 16000
},
audio: {
content: encoded
}
};
return callback(null, payload);

operation
.on('error', callback)
.on('complete', (results) => {
console.log('Results:', results);
callback();
});
});
}
// [END construct_request]

function main (inputFile, callback) {
var requestPayload;

async.waterfall([
function (cb) {
prepareRequest(inputFile, cb);
},
function (payload, cb) {
requestPayload = payload;
getAuthClient(cb);
},
// [START send_request]
function sendRequest (authClient, cb) {
console.log('Analyzing speech...');
speech.syncrecognize({
auth: authClient,
resource: requestPayload
}, function (err, result) {
if (err) {
return cb(err);
}
console.log('result:', JSON.stringify(result, null, 2));
cb(null, result);
});
// [END speech_async_recognize]

// [START speech_streaming_recognize]
function streamingRecognize (filename, callback) {
const options = {
config: {
encoding: 'LINEAR16',
sampleRate: 16000
}
// [END send_request]
], callback);
};

// Create a recognize stream
const recognizeStream = speech.createRecognizeStream(options)
.on('error', callback)
.on('data', (data) => {
console.log('Data received: %j', data);
callback();
});

// Stream an audio file from disk to the Speech API, e.g. "./resources/audio.raw"
fs.createReadStream(filename).pipe(recognizeStream);
}
// [END speech_streaming_recognize]

// [START run_application]
if (module === require.main) {
if (process.argv.length < 3) {
console.log('Usage: node recognize <inputFile>');
process.exit();
}
var inputFile = process.argv[2];
main(inputFile, console.log);
// [START speech_streaming_mic_recognize]
function streamingMicRecognize (filename) {
const options = {
config: {
encoding: 'LINEAR16',
sampleRate: 16000
}
};

// Create a recognize stream
const recognizeStream = speech.createRecognizeStream(options)
.on('error', console.error)
.on('data', (data) => process.stdout.write(data.results));

// Start recording and send the microphone input to the Speech API
record.start({ sampleRate: 16000 }).pipe(recognizeStream);

console.log('Listening, press Ctrl+C to stop.');
}
// [END run_application]
// [END app]
// [END speech_streaming_mic_recognize]

// The command-line program
var cli = require('yargs');
var utils = require('../utils');

exports.main = main;
var program = module.exports = {
syncRecognize: syncRecognize,
asyncRecognize: asyncRecognize,
streamingRecognize: streamingRecognize,
streamingMicRecognize: streamingMicRecognize,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};

cli
.demand(1)
.command('sync <filename>', 'Detects speech in an audio file.', {}, function (options) {
program.syncRecognize(options.filename, utils.makeHandler(false));
})
.command('async <filename>', 'Creates a job to detect speech in an audio file, and waits for the job to complete.', {}, function (options) {
program.asyncRecognize(options.filename, utils.makeHandler(false));
})
.command('stream <filename>', 'Detects speech in an audio file by streaming it to the Speech API.', {}, function (options) {
program.streamingRecognize(options.filename, utils.makeHandler(false));
})
.command('listen', 'Detects speech in a microphone input stream.', {}, function () {
program.streamingMicRecognize();
})
.example('node $0 sync ./resources/audio.raw', 'Detects speech in "./resources/audio.raw".')
.example('node $0 async ./resources/audio.raw', 'Creates a job to detect speech in "./resources/audio.raw", and waits for the job to complete.')
.example('node $0 stream ./resources/audio.raw', 'Detects speech in "./resources/audio.raw" by streaming it to the Speech API.')
.example('node $0 listen ./resources/audio.raw', 'Detects speech in a microphone input stream.')
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove filepath?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/speech/docs');

if (module === require.main) {
program.main(process.argv.slice(2));
}
Loading