v1. SpeechClient
Service that implements Google Cloud Speech API.
Constructor
SpeechClient
new SpeechClient(options)
Construct an instance of SpeechClient.
Parameter |
|||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Optional object The configuration object. See the subsequent parameters for more details. Values in
|
Properties
port
The port for this API service.
scopes
The scopes needed to make gRPC calls for every method defined in this service.
servicePath
The DNS address for this API service.
Methods
getProjectId
getProjectId(callback)
Return the project ID used by this class.
Parameter |
|
---|---|
callback |
function(Error, string) the callback to be called with the current project Id. |
longRunningRecognize
longRunningRecognize(request, options, callback) returns Promise
Performs asynchronous speech recognition: receive results via the google.longrunning.Operations interface. Returns either an
Operation.error
or an Operation.response
which contains a LongRunningRecognizeResponse
message.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is a gax.Operation object. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is a gax.Operation object. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const speech = require('@google-cloud/speech');
var client = new speech.v1.SpeechClient({
// optional auth parameters.
});
var encoding = 'FLAC';
var sampleRateHertz = 44100;
var languageCode = 'en-US';
var config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
};
var uri = 'gs://bucket_name/file_name.flac';
var audio = {
uri: uri,
};
var request = {
config: config,
audio: audio,
};
// Handle the operation using the promise pattern.
client.longRunningRecognize(request)
.then(responses => {
var operation = responses[0];
var initialApiResponse = responses[1];
// Operation#promise starts polling for the completion of the LRO.
return operation.promise();
})
.then(responses => {
// The final result of the operation.
var result = responses[0];
// The metadata value of the completed operation.
var metadata = responses[1];
// The response of the api call returning the complete operation.
var finalApiResponse = responses[2];
})
.catch(err => {
console.error(err);
});
var encoding = 'FLAC';
var sampleRateHertz = 44100;
var languageCode = 'en-US';
var config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
};
var uri = 'gs://bucket_name/file_name.flac';
var audio = {
uri: uri,
};
var request = {
config: config,
audio: audio,
};
// Handle the operation using the event emitter pattern.
client.longRunningRecognize(request)
.then(responses => {
var operation = responses[0];
var initialApiResponse = responses[1];
// Adding a listener for the "complete" event starts polling for the
// completion of the operation.
operation.on('complete', (result, metadata, finalApiResponse) => {
// doSomethingWith(result);
});
// Adding a listener for the "progress" event causes the callback to be
// called on any change in metadata when the operation is polled.
operation.on('progress', (metadata, apiResponse) => {
// doSomethingWith(metadata)
});
// Adding a listener for the "error" event handles any errors found during polling.
operation.on('error', err => {
// throw(err);
});
})
.catch(err => {
console.error(err);
});
recognize
recognize(request, options, callback) returns Promise
Performs synchronous speech recognition: receive results after all audio has been sent and processed.
Parameter |
|||||||
---|---|---|---|---|---|---|---|
request |
Object The request object that will be sent. Values in
|
||||||
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
||||||
callback |
Optional function(nullable Error, nullable Object) The function which will be called with the result of the API call. The second parameter to the callback is an object representing RecognizeResponse. |
- Returns
-
Promise
- The promise which resolves to an array. The first element of the array is an object representing RecognizeResponse. The promise has a method named "cancel" which cancels the ongoing API call.
Example
const speech = require('@google-cloud/speech');
var client = new speech.v1.SpeechClient({
// optional auth parameters.
});
var encoding = 'FLAC';
var sampleRateHertz = 44100;
var languageCode = 'en-US';
var config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
};
var uri = 'gs://bucket_name/file_name.flac';
var audio = {
uri: uri,
};
var request = {
config: config,
audio: audio,
};
client.recognize(request)
.then(responses => {
var response = responses[0];
// doThingsWith(response)
})
.catch(err => {
console.error(err);
});
streamingRecognize
streamingRecognize(options) returns Stream
Performs bidirectional streaming speech recognition: receive results while sending audio. This method is only available via the gRPC API (not REST).
Parameter |
|
---|---|
options |
Optional Object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
- Returns
-
Stream
An object stream which is both readable and writable. It accepts objects representing StreamingRecognizeRequest for write() method, and will emit objects representing StreamingRecognizeResponse on 'data' event asynchronously.
Example
const speech = require('@google-cloud/speech');
var client = new speech.v1.SpeechClient({
// optional auth parameters.
});
var stream = client.streamingRecognize().on('data', response => {
// doThingsWith(response)
});
var request = {};
// Write request objects.
stream.write(request);
streamingRecognize
streamingRecognize(config, options) returns stream
Performs bidirectional streaming speech recognition: receive results while sending audio. This method is only available via the gRPC API (not REST).
Parameter |
|
---|---|
config |
object The configuration for the stream. This is appropriately wrapped and sent as the first argument. It should be an object conforming to the StreamingRecognitionConfig structure. |
options |
Optional object Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details. |
- Returns
-
stream
An object stream which is both readable and writable. It accepts raw audio for the
write()
method, and will emit objects representing StreamingRecognizeResponse on the 'data' event asynchronously.
Example
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();
const stream = client.streamingRecognize({
config: {
encoding: 'LINEAR16',
languageCode: 'en-us',
sampleRateHertz: 44100,
},
}).on('data', function(response) {
// doThingsWith(response);
});
const request = {};
// Write request objects.
stream.write(request);