-
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
Add Cloud Speech sample. #110
Merged
Changes from all commits
Commits
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Cloud Speech API samples | ||
|
||
These samples require two environment variables to be set: | ||
|
||
- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can | ||
download one from your Google project's "credentials" page. | ||
- `GCLOUD_PROJECT` - ID of your Google project. | ||
|
||
See [gcloud-node authentication][auth] for more details. | ||
|
||
[auth]: https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication | ||
|
||
## Run a sample | ||
|
||
Install dependencies first: | ||
|
||
npm install | ||
|
||
### Recognition sample | ||
|
||
Execute the sample: | ||
|
||
node recognize "/path/to/audio.file" |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "nodejs-docs-samples-speech", | ||
"description": "Node.js samples for Google Cloud Speech API.", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"author": "Google Inc.", | ||
"engines": { | ||
"node": ">=0.10.x" | ||
}, | ||
"scripts": { | ||
"recognize": "node recognize" | ||
}, | ||
"dependencies": { | ||
"googleapis": "^6.1.0" | ||
}, | ||
"devDependencies": { | ||
"async": "^1.5.0" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// 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'; | ||
|
||
// [START app] | ||
// [START import_libraries] | ||
var google = require('googleapis'); | ||
var async = require('async'); | ||
var fs = require('fs'); | ||
// [END import_libraries] | ||
|
||
// Url to discovery doc file | ||
// [START discovery_doc] | ||
var url = 'https://speech.googleapis.com/$discovery/rest'; | ||
// [END discovery_doc] | ||
|
||
// [START authenticating] | ||
function getSpeechService(callback) { | ||
// Acquire credentials | ||
google.auth.getApplicationDefault(function (err, authClient) { | ||
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' | ||
]); | ||
} | ||
|
||
// Load the speach service using acquired credentials | ||
console.log('Loading speech service...'); | ||
google.discoverAPI({ | ||
url: url, | ||
version: 'v1', | ||
auth: authClient | ||
}, function (err, speechService) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, speechService, authClient); | ||
}); | ||
}); | ||
} | ||
// [END authenticating] | ||
|
||
// [START construct_request] | ||
function prepareRequest(inputFile, callback) { | ||
fs.readFile(inputFile, function (err, audioFile) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
console.log('Got audio file!'); | ||
var encoded = new Buffer(audioFile).toString('base64'); | ||
var payload = { | ||
initialRequest: { | ||
encoding: 'LINEAR16', | ||
sampleRate: 16000 | ||
}, | ||
audioRequest: { | ||
content: encoded | ||
} | ||
}; | ||
return callback(null, payload); | ||
}); | ||
} | ||
// [END construct_request] | ||
|
||
function main(inputFile, callback) { | ||
var requestPayload; | ||
|
||
async.waterfall([ | ||
function (cb) { | ||
prepareRequest(inputFile, cb); | ||
}, | ||
function (payload, cb) { | ||
requestPayload = payload; | ||
getSpeechService(cb); | ||
}, | ||
// [START send_request] | ||
function sendRequest(speechService, authClient, cb) { | ||
console.log('Analyzing speech...'); | ||
speechService.speech.recognize({ | ||
auth: authClient, | ||
resource: requestPayload | ||
}, function (err, result) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
console.log('result:', JSON.stringify(result, null, 2)); | ||
cb(null, result); | ||
}); | ||
} | ||
// [END send_request] | ||
], callback); | ||
} | ||
|
||
// [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); | ||
} | ||
// [END run_application] | ||
// [END app] | ||
|
||
exports.main = main; |
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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 test = require('ava'); | ||
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. 'ava'? Looks like 'java' without the 'j'. I'm assuming this works? :) 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. Yes, this is correct. |
||
var path = require('path'); | ||
var recognizeExample = require('../../speech/recognize'); | ||
|
||
test.cb('should list entries', function (t) { | ||
recognizeExample.main( | ||
path.resolve('../../speech/resources/audio.raw'), | ||
function (err, result) { | ||
t.ifError(err); | ||
t.ok(result); | ||
t.ok(result.responses); | ||
t.is(result.responses.length, 1); | ||
t.ok(result.responses[0].results); | ||
t.end(); | ||
} | ||
); | ||
}); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
out.* | ||
out.png | ||
out.* |
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.
I believe speech requires a service account for the time being.
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.
I got it to work both with an API key and letting it pick credentials.