Google Cloud Speech To Text
Since Camel 4.19
Only producer is supported
The Google Cloud Speech To Text component provides access to Google Cloud Speech-to-Text API via the Google Cloud Speech Client for Java.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-google-speech-to-text</artifactId>
<!-- use the same version as your Camel core version -->
<version>x.x.x</version>
</dependency> Authentication Configuration
Google Cloud Speech To Text component authentication is targeted for use with the GCP Service Accounts. For more information, please refer to Google Cloud Authentication.
When you have the service account key, you can provide authentication credentials to your application code. Google security credentials can be set through the component endpoint:
String endpoint = "google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json"; Or by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS :
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
URI Format
google-speech-to-text://operation[?options]
You can append query options to the URI in the following format, ?options=value&option2=value&…
For example, in order to perform speech recognition on audio data, use the following snippet:
-
Java
-
XML
-
YAML
from("direct:start")
.to("google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json"); <route>
<from uri="direct:start"/>
<to uri="google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: google-speech-to-text://recognize
parameters:
serviceAccountKey: /home/user/Downloads/my-key.json Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Google Cloud Speech To Text component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
Endpoint Options
The Google Cloud Speech To Text endpoint is configured using URI syntax:
google-speech-to-text:operation
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Service account key to authenticate an application as a service account. | String | ||
The encoding of the audio data. Supported values: LINEAR16, FLAC, MULAW, AMR, AMR_WB, OGG_OPUS, SPEEX_WITH_HEADER_BYTE, WEBM_OPUS, MP3. | LINEAR16 | String | |
The language of the audio data as a BCP-47 language tag (e.g., en-US, fr-FR). | en-US | String | |
Specifies if the request is a pojo request. | false | boolean | |
The sample rate in Hertz of the audio data. Valid values range from 8000 to 48000. | Integer | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Autowired The client to use during service invocation. | SpeechClient |
Message Headers
The Google Cloud Speech To Text component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelGoogleCloudSpeechToTextOperation (producer) Constant: | The operation to perform. Enum values:
| GoogleCloudSpeechToTextOperations | |
CamelGoogleCloudSpeechToTextResponseObject (producer) Constant: | The response object resulting from the Google Cloud Speech-to-Text API invocation. | RecognizeResponse |
Message Body
The message body at each side of the Google Cloud Speech To Text component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The audio to transcribe, sent inline in the encoding and sample rate of the encoding and sampleRateHertz options (LINEAR16 by default). A stream or a file is read into bytes and a String is sent as its bytes, a Map, a POJO or no body fails. With pojoRequest=true a RecognizeRequest of the Google Cloud SDK: another body fails.
|
Producer output |
| The transcript: the best alternative of each result, one line per result, or an empty String when nothing is recognized, also with pojoRequest=true. The RecognizeResponse is in the CamelGoogleCloudSpeechToTextResponseObject header. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers are described in the Camel Catalog.
Usage
Message body
The message body should contain the audio data as a byte[].
When pojoRequest=true, the body should be a com.google.cloud.speech.v1.RecognizeRequest instance instead.
Google Cloud Speech To Text Producer operations
Google Cloud Speech To Text component provides the following operation on the producer side:
-
recognize
The operation is specified as part of the endpoint URI (e.g., google-speech-to-text://recognize). You can override the operation at runtime by setting the GoogleCloudSpeechToTextOperation message header.
Advanced component configuration
If you need to have more control over the SpeechClient instance configuration, you can create your own instance and refer to it in your Camel google-speech-to-text component configuration:
-
Java
-
XML
-
YAML
from("direct:start")
.to("google-speech-to-text://recognize?client=#mySpeechClient"); <route>
<from uri="direct:start"/>
<to uri="google-speech-to-text://recognize?client=#mySpeechClient"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: google-speech-to-text://recognize
parameters:
client: "#mySpeechClient" Google Cloud Speech To Text Producer Operation examples
-
recognize: this operation transcribes audio data to text. The message body should contain the audio data as abyte[].
-
Java
-
XML
-
YAML
from("direct:start")
.to("google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json&encoding=LINEAR16&sampleRateHertz=16000&languageCode=en-US")
.log("body:${body}")
.to("mock:result"); <route>
<from uri="direct:start"/>
<to uri="google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json&encoding=LINEAR16&sampleRateHertz=16000&languageCode=en-US"/>
<log message="body:${body}"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: google-speech-to-text://recognize
parameters:
serviceAccountKey: /home/user/Downloads/my-key.json
encoding: LINEAR16
sampleRateHertz: 16000
languageCode: en-US
- log:
message: "body:${body}"
- to:
uri: mock:result This operation will return the transcribed text as a String. The full RecognizeResponse is available via the GoogleCloudSpeechToTextResponseObject message header.
-
Using different audio encodings:
-
Java
-
XML
-
YAML
from("direct:start")
.to("google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json&encoding=FLAC&languageCode=fr-FR")
.log("body:${body}")
.to("mock:result"); <route>
<from uri="direct:start"/>
<to uri="google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json&encoding=FLAC&languageCode=fr-FR"/>
<log message="body:${body}"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: google-speech-to-text://recognize
parameters:
serviceAccountKey: /home/user/Downloads/my-key.json
encoding: FLAC
languageCode: fr-FR
- log:
message: "body:${body}"
- to:
uri: mock:result -
Using POJO request for full control:
from("direct:start")
.process(exchange -> {
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setSampleRateHertz(16000)
.setLanguageCode("en-US")
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setContent(ByteString.copyFrom(audioData))
.build();
RecognizeRequest request = RecognizeRequest.newBuilder()
.setConfig(config)
.setAudio(audio)
.build();
exchange.getIn().setBody(request);
})
.to("google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json&pojoRequest=true")
.log("body:${body}")
.to("mock:result"); When using pojoRequest=true, the body should be a RecognizeRequest and the transcribed text is returned as the body.
-
Overriding the operation at runtime via header:
-
Java
-
XML
-
YAML
from("direct:start")
.setHeader("CamelGoogleCloudSpeechToTextOperation", constant("recognize"))
.to("google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json")
.log("body:${body}")
.to("mock:result"); <route>
<from uri="direct:start"/>
<setHeader name="CamelGoogleCloudSpeechToTextOperation">
<constant>recognize</constant>
</setHeader>
<to uri="google-speech-to-text://recognize?serviceAccountKey=/home/user/Downloads/my-key.json"/>
<log message="body:${body}"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- setHeader:
name: CamelGoogleCloudSpeechToTextOperation
constant: recognize
- to:
uri: google-speech-to-text://recognize
parameters:
serviceAccountKey: /home/user/Downloads/my-key.json
- log:
message: "body:${body}"
- to:
uri: mock:result