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

Speech partial veneer #2459

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"**/test/*/**/*",
"**/node_modules/",
"**/.coverage/**/*.js",
"**/smoke-test/**/*.js",
]
}
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
**/node_modules/
**/coverage
**/gapic*.js
**/smoke-test/**/*
144 changes: 49 additions & 95 deletions packages/speech/README.md
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
33 changes: 11 additions & 22 deletions packages/speech/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"repository": "GoogleCloudPlatform/google-cloud-node",
"name": "@google-cloud/speech",
"version": "0.9.4",
"author": "Google Inc.",
Expand Down Expand Up @@ -37,10 +38,8 @@
"files": [
"src",
"AUTHORS",
"CONTRIBUTORS",
"COPYING"
],
"repository": "googlecloudplatform/google-cloud-node",
"keywords": [
"google apis client",
"google api client",
Expand All @@ -51,36 +50,26 @@
"google cloud",
"cloud",
"google speech",
"speech"
"speech",
"Google Cloud Speech API"
],
"dependencies": {
"@google-cloud/common": "^0.13.0",
"@google-cloud/common-grpc": "^0.3.0",
"events-intercept": "^2.0.0",
"extend": "^3.0.0",
"google-gax": "^0.13.0",
"google-proto-files": "^0.12.0",
"is": "^3.1.0",
"propprop": "^0.3.1",
"google-gax": "^0.13.2",
"extend": "^3.0.0",
"pumpify": "^1.3.5",
"request": "^2.74.0",
"stream-events": "^1.0.1",
"string-format-obj": "^1.1.0",
"through2": "^2.0.1"
"through2": "^2.0.3"
},
"devDependencies": {
"@google-cloud/storage": "*",
"async": "^2.0.1",
"methmeth": "^1.1.0",
"mocha": "^3.0.2",
"proxyquire": "^1.7.10",
"tmp": "^0.0.31",
"uuid": "^3.0.1"
"mocha": "^3.2.0",
"power-assert": "^1.4.2",
"sinon": "^2.2.0"
},
"scripts": {
"publish-module": "node ../../scripts/publish.js speech",
"test": "mocha test/*.js",
"system-test": "mocha system-test/*.js --no-timeouts --bail"
"smoke-test": "mocha smoke-test/*.js --timeout 5000",
"test": "mocha test/*.js"
},
"license": "Apache-2.0",
"engines": {
Expand Down
50 changes: 50 additions & 0 deletions packages/speech/smoke-test/speech_smoke_test.js
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);
});
});
113 changes: 113 additions & 0 deletions packages/speech/src/helpers.js
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 = {};

This comment was marked as spam.


/**
* 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;
};
Loading