-
Notifications
You must be signed in to change notification settings - Fork 591
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
Speech partial veneer #2459
Merged
stephenplusplus
merged 5 commits into
googleapis:master
from
lukesneeringer:speech-partial-veneer
Jul 15, 2017
Merged
Speech partial veneer #2459
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e322f73
This commit adds the Speech "partial Veneer", which shifts us to mostly
lukesneeringer 98e12a6
Do not lint auto-gen packages.
lukesneeringer 5557220
ignore helper files from docs
stephenplusplus 54d7772
ignore speech from system tests
stephenplusplus ac55bc1
address my feedback
stephenplusplus 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,6 @@ | |
"**/test/*/**/*", | ||
"**/node_modules/", | ||
"**/.coverage/**/*.js", | ||
"**/smoke-test/**/*.js", | ||
] | ||
} |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
**/node_modules/ | ||
**/coverage | ||
**/gapic*.js | ||
**/smoke-test/**/* |
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,105 +1,59 @@ | ||
# @google-cloud/speech ([Alpha][versioning]) | ||
> Cloud Speech Client Library for Node.js | ||
# Node.js Client for Google Cloud Speech API ([Beta](https://github.com/GoogleCloudPlatform/google-cloud-node#versioning)) | ||
|
||
*Looking for more Google APIs than just Speech? You might want to check out [`google-cloud`][google-cloud].* | ||
[Google Cloud Speech API][Product Documentation]: Google Cloud Speech API. | ||
- [Client Library Documentation][] | ||
- [Product Documentation][] | ||
|
||
- [API Documentation][gcloud-speech-docs] | ||
- [Official Documentation][cloud-speech-docs] | ||
## Quick Start | ||
In order to use this library, you first need to go through the following steps: | ||
|
||
1. [Select or create a Cloud Platform project.](https://console.cloud.google.com/project) | ||
2. [Enable the Google Cloud Speech API.](https://console.cloud.google.com/apis/api/speech) | ||
3. [Setup Authentication.](https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/master/guides/authentication) | ||
|
||
```sh | ||
$ npm install --save @google-cloud/speech | ||
### Installation | ||
``` | ||
```js | ||
var speech = require('@google-cloud/speech')({ | ||
projectId: 'grape-spaceship-123', | ||
keyFilename: '/path/to/keyfile.json' | ||
}); | ||
|
||
// Detect the speech in an audio file. | ||
speech.recognize('./audio.raw', { | ||
encoding: 'LINEAR16', | ||
sampleRateHertz: 16000 | ||
}, function(err, transcript) { | ||
// transcript = 'how old is the Brooklyn Bridge' | ||
}); | ||
|
||
// Detect the speech in an audio file stream. | ||
fs.createReadStream('./audio.raw') | ||
.on('error', console.error) | ||
.pipe(speech.createRecognizeStream({ | ||
config: { | ||
encoding: 'LINEAR16', | ||
sampleRateHertz: 16000 | ||
}, | ||
singleUtterance: false, | ||
interimResults: false | ||
})) | ||
.on('error', console.error) | ||
.on('data', function(data) { | ||
// data.results = "how old is the Brooklyn Bridge" | ||
}); | ||
|
||
// Promises are also supported by omitting callbacks. | ||
speech.recognize('./audio.raw', { | ||
encoding: 'LINEAR16', | ||
sampleRateHertz: 16000 | ||
}).then(function(data) { | ||
var transcript = data[0]; | ||
}); | ||
|
||
// It's also possible to integrate with third-party Promise libraries. | ||
var speech = require('@google-cloud/speech')({ | ||
promise: require('bluebird') | ||
}); | ||
``` | ||
|
||
|
||
## Authentication | ||
|
||
It's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Cloud services. | ||
|
||
### On Google Cloud Platform | ||
|
||
If you are running this client on Google Cloud Platform, we handle authentication for you with no configuration. You just need to make sure that when you [set up the GCE instance][gce-how-to], you add the correct scopes for the APIs you want to access. | ||
|
||
``` js | ||
var speech = require('@google-cloud/speech')(); | ||
// ...you're good to go! | ||
$ npm install --save @google-cloud/speech | ||
``` | ||
|
||
### Elsewhere | ||
|
||
If you are not running this client on Google Cloud Platform, you need a Google Developers service account. To create a service account: | ||
|
||
1. Visit the [Google Developers Console][dev-console]. | ||
2. Create a new project or click on an existing project. | ||
3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services): | ||
* Google Cloud Speech API | ||
4. Navigate to **APIs & auth** > **Credentials** and then: | ||
* If you want to use a new service account key, click on **Create credentials** and select **Service account key**. After the account key is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests. | ||
* If you want to generate a new service account key for an existing service account, click on **Generate new JSON key** and download the JSON key file. | ||
|
||
``` js | ||
var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' | ||
|
||
var speech = require('@google-cloud/speech')({ | ||
projectId: projectId, | ||
|
||
// The path to your key file: | ||
keyFilename: '/path/to/keyfile.json' | ||
|
||
// Or the contents of the key file: | ||
credentials: require('./path/to/keyfile.json') | ||
}); | ||
|
||
// ...you're good to go! | ||
### Preview | ||
#### SpeechClient | ||
```js | ||
var speech = require('@google-cloud/speech'); | ||
|
||
var client = speech({ | ||
// optional auth parameters. | ||
}); | ||
|
||
var languageCode = 'en-US'; | ||
var sampleRateHertz = 44100; | ||
var encoding = speech.v1.types.RecognitionConfig.AudioEncoding.FLAC; | ||
var config = { | ||
languageCode : languageCode, | ||
sampleRateHertz : sampleRateHertz, | ||
encoding : encoding | ||
}; | ||
var uri = 'gs://gapic-toolkit/hello.flac'; | ||
var audio = { | ||
uri : uri | ||
}; | ||
var request = { | ||
config: config, | ||
audio: audio | ||
}; | ||
client.recognize(request).then(function(responses) { | ||
var response = responses[0]; | ||
// doThingsWith(response) | ||
}) | ||
.catch(function(err) { | ||
console.error(err); | ||
}); | ||
``` | ||
|
||
### Next Steps | ||
- Read the [Client Library Documentation][] for Google Cloud Speech API to see other available methods on the client. | ||
- Read the [Google Cloud Speech API Product documentation][Product Documentation] to learn more about the product and see How-to Guides. | ||
- View this [repository's main README](https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/README.md) to see the full list of Cloud APIs that we cover. | ||
|
||
[versioning]: https://github.com/GoogleCloudPlatform/google-cloud-node#versioning | ||
[google-cloud]: https://github.com/GoogleCloudPlatform/google-cloud-node/ | ||
[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using | ||
[dev-console]: https://console.developers.google.com/project | ||
[gcloud-speech-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/speech | ||
[cloud-speech-docs]: https://cloud.google.com/speech | ||
[Client Library Documentation]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/speech | ||
[Product Documentation]: https://cloud.google.com/speech |
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,50 @@ | ||
/* | ||
* Copyright 2017, Google Inc. All rights reserved. | ||
* | ||
* 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'; | ||
|
||
describe('SpeechSmokeTest', function() { | ||
|
||
it('successfully makes a call to the service', function(done) { | ||
var speech = require('../src'); | ||
|
||
var client = speech.v1({ | ||
// optional auth parameters. | ||
}); | ||
|
||
var languageCode = 'en-US'; | ||
var sampleRateHertz = 44100; | ||
var encoding = speech.v1.types.RecognitionConfig.AudioEncoding.FLAC; | ||
var config = { | ||
languageCode : languageCode, | ||
sampleRateHertz : sampleRateHertz, | ||
encoding : encoding | ||
}; | ||
var uri = 'gs://gapic-toolkit/hello.flac'; | ||
var audio = { | ||
uri : uri | ||
}; | ||
var request = { | ||
config: config, | ||
audio: audio | ||
}; | ||
client.recognize(request).then(function(responses) { | ||
var response = responses[0]; | ||
console.log(response); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
}); |
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,113 @@ | ||
/*! | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* @module speech/helpers | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var pumpify = require('pumpify'); | ||
var streamEvents = require('stream-events'); | ||
var through = require('through2'); | ||
|
||
/*! | ||
* Return a dictionary-like object with helpers to augment the Speech | ||
* GAPIC. | ||
* | ||
* @return {Object} - An object with keys and functions which are placed | ||
* onto the pure GAPIC. | ||
*/ | ||
module.exports = () => { | ||
var methods = {}; | ||
|
||
/** | ||
* Performs bidirectional streaming speech recognition: receive results while | ||
* sending audio. This method is only available via the gRPC API (not REST). | ||
* | ||
* @param {Object} config | ||
* The configuration for the stream. This is appropriately wrapped and | ||
* sent as the first argument. It should be an object conforming to the | ||
* [StreamingRecognitionConfig]{@link StreamingRecognitionConfig} | ||
* structure. | ||
* @param {Object=} options | ||
* Optional parameters. You can override the default settings for this | ||
* call, e.g, timeout, retries, paginations, etc. See | ||
* [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} | ||
* for the details. | ||
* @returns {Stream} | ||
* An object stream which is both readable and writable. It accepts | ||
* [StreamingRecognizeRequest]{@link StreamingRecognizeRequest}-like | ||
* objects for the write() method, and will emit objects representing | ||
* [StreamingRecognizeResponse]{@link StreamingRecognizeResponse} on the | ||
* 'data' event asynchronously. | ||
* | ||
* @example | ||
* | ||
* var stream = speech.streamingRecognize({ | ||
* config: { | ||
* encoding: 'LINEAR16', | ||
* languageCode: 'en-us', | ||
* sampleRateHertz: 44100, | ||
* }, | ||
* }).on('data', function(response) { | ||
* // doThingsWith(response); | ||
* }); | ||
* var request = {}; | ||
* // Write request objects. | ||
* stream.write(request); | ||
*/ | ||
methods.streamingRecognize = function(config, options) { | ||
if (options === undefined) { | ||
options = {}; | ||
} | ||
|
||
var requestStream = this._streamingRecognize(options); | ||
|
||
// Format the audio content as input request for pipeline | ||
var recognizeStream = streamEvents(pumpify.obj()); | ||
|
||
recognizeStream.once('writing', function() { | ||
requestStream.on('error', function(err) { | ||
recognizeStream.destroy(err); | ||
}); | ||
|
||
requestStream.on('response', function(response) { | ||
recognizeStream.emit('response', response); | ||
}); | ||
|
||
// Write the initial configuration to the stream, | ||
requestStream.write({ | ||
streamingConfig: config | ||
}); | ||
|
||
this.setPipeline([ | ||
// Format the user's input. | ||
through.obj(function(obj, _, next) { | ||
next(null, { | ||
audioContent: obj | ||
}); | ||
}), | ||
|
||
requestStream | ||
]); | ||
}); | ||
|
||
return recognizeStream; | ||
}; | ||
|
||
return methods; | ||
}; |
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.
This comment was marked as spam.
Sorry, something went wrong.