-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
209 additions
and
1 deletion.
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,123 @@ | ||
// Copyright 2015-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 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 auth(callback) { | ||
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' | ||
]); | ||
} | ||
callback(null, authClient); | ||
}); | ||
} | ||
|
||
function getSpeechService(authClient, callback) { | ||
google.discoverAPI({ | ||
url: url, | ||
version: 'v1', | ||
auth: authClient | ||
}, callback); | ||
} | ||
// [END authenticating] | ||
|
||
// [START construct_request] | ||
function main(inputFile, callback) { | ||
var authClient, audioFile; | ||
|
||
async.waterfall([ | ||
function (cb) { | ||
auth(cb); | ||
}, | ||
function (_authClient, cb) { | ||
authClient = _authClient; | ||
readFile(inputFile, cb); | ||
fs.readFile(inputFile, function (err, audioFile) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
console.log('Got audio file!'); | ||
return cb(null, audioFile); | ||
}); | ||
}, | ||
function (_audioFile, cb) { | ||
audioFile = _audioFile; | ||
console.log('Loading speech service...'); | ||
getSpeechService(authClient, cb); | ||
}, | ||
function (speechService, cb) { | ||
|
||
var encoded = new Buffer(audioFile).toString('base64'); | ||
|
||
console.log('Analyzing speech...'); | ||
speechService.speech.recognize({ | ||
auth: authClient, | ||
resource: { | ||
initialRequest: { | ||
encoding: 'LINEAR16', | ||
sampleRate: 16000 | ||
}, | ||
audioRequest: { | ||
content: encoded | ||
} | ||
} | ||
}, cb); | ||
} | ||
], function (err, result) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
console.log('result:', JSON.stringify(result, null, 2)); | ||
callback(null, result); | ||
}); | ||
} | ||
// [END construct_request] | ||
|
||
exports.main = main; | ||
|
||
// [START run_application] | ||
if (module === require.main) { | ||
if (process.argv.length < 3) { | ||
console.log('Usage: node recognize <inputFile>'); | ||
process.exit(); | ||
} | ||
var inputFile = process.argv[3]; | ||
main(inputFile, console.log); | ||
} | ||
// [END run_application] |
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'); | ||
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(); | ||
} | ||
); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.