-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
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.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove filepath? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?)There was a problem hiding this comment.
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 theconst
keyword. I also added anengines
field topackage.json
.