IBM Watson Text to Speech
Since Camel 4.17
Only producer is supported
The IBM Watson Text to Speech component allows you to convert written text into natural-sounding speech using IBM Watson Text to Speech service.
Prerequisites
You must have a valid IBM Cloud account and an instance of the Watson Text to Speech service. More information is available at IBM Watson Text to Speech.
URI Format
ibm-watson-text-to-speech:label[?options]
You can append query options to the URI in the following format:
?option=value&option2=value&…
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 IBM Watson Text to Speech component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The URL of the Text to Speech service instance, for example https://api.eu-de.text-to-speech.watson.cloud.ibm.com. If not set, the IBM Watson SDK uses https://api.us-south.text-to-speech.watson.cloud.ibm.com, unless its external configuration, such as the TEXT_TO_SPEECH_URL environment variable, gives a URL. | String | ||
The audio format (MIME type) that synthesize returns, for example audio/wav, audio/mp3, audio/flac, audio/ogg;codecs=opus or audio/webm. audio/l16, audio/alaw and audio/mulaw also need the sampling rate, as in audio/l16;rate=22050. The CamelIBMWatsonTTSAccept header replaces this option. | audio/wav | String | |
The shared configuration of the endpoints. Each endpoint starts from a copy of it, and its endpoint options override the values. | WatsonTextToSpeechConfiguration | ||
The customization ID (a GUID) of the custom model that synthesize uses, which defines custom pronunciations of words. The language of the custom model must match the language of the voice, and the custom model must belong to the service instance of the apiKey. If not set, the voice is used with no customization. The CamelIBMWatsonTTSCustomizationId header replaces this option. | String | ||
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 | |
The operation that the producer performs: synthesize converts text to audio, listVoices and getVoice return the voices, listCustomModels and getCustomModel the custom models, and getPronunciation the pronunciation of a word. The CamelIBMWatsonTTSOperation header replaces this option, and the producer fails if neither is set. Enum values:
| WatsonTextToSpeechOperations | ||
The voice that synthesize speaks with, for example en-US_AllisonV3Voice, en-GB_KateV3Voice or es-ES_EnriqueV3Voice. Use a voice of the language of the text; listVoices returns the available voices. getPronunciation returns the pronunciation in the language of this voice. The CamelIBMWatsonTTSVoice header replaces this option. | en-US_MichaelV3Voice | String | |
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 | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean | |
Required The IBM Cloud API key of the Text to Speech service instance. The client gets IAM access tokens with it. The endpoint fails to start without it. | String |
Endpoint Options
The IBM Watson Text to Speech endpoint is configured using URI syntax:
ibm-watson-text-to-speech:label
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The URL of the Text to Speech service instance, for example https://api.eu-de.text-to-speech.watson.cloud.ibm.com. If not set, the IBM Watson SDK uses https://api.us-south.text-to-speech.watson.cloud.ibm.com, unless its external configuration, such as the TEXT_TO_SPEECH_URL environment variable, gives a URL. | String | ||
The audio format (MIME type) that synthesize returns, for example audio/wav, audio/mp3, audio/flac, audio/ogg;codecs=opus or audio/webm. audio/l16, audio/alaw and audio/mulaw also need the sampling rate, as in audio/l16;rate=22050. The CamelIBMWatsonTTSAccept header replaces this option. | audio/wav | String | |
The customization ID (a GUID) of the custom model that synthesize uses, which defines custom pronunciations of words. The language of the custom model must match the language of the voice, and the custom model must belong to the service instance of the apiKey. If not set, the voice is used with no customization. The CamelIBMWatsonTTSCustomizationId header replaces this option. | String | ||
The operation that the producer performs: synthesize converts text to audio, listVoices and getVoice return the voices, listCustomModels and getCustomModel the custom models, and getPronunciation the pronunciation of a word. The CamelIBMWatsonTTSOperation header replaces this option, and the producer fails if neither is set. Enum values:
| WatsonTextToSpeechOperations | ||
The voice that synthesize speaks with, for example en-US_AllisonV3Voice, en-GB_KateV3Voice or es-ES_EnriqueV3Voice. Use a voice of the language of the text; listVoices returns the available voices. getPronunciation returns the pronunciation in the language of this voice. The CamelIBMWatsonTTSVoice header replaces this option. | en-US_MichaelV3Voice | String | |
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 | |
Required The IBM Cloud API key of the Text to Speech service instance. The client gets IAM access tokens with it. The endpoint fails to start without it. | String |
Message Headers
The IBM Watson Text to Speech component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelIBMWatsonTTSOperation (producer) Constant: | The operation that the producer performs. It replaces the operation option. | String | |
CamelIBMWatsonTTSText (producer) Constant: | The text that synthesize converts to speech, as plain text or SSML, at most 5 KB including the SSML tags. If not set, the producer reads it from the message body. | String | |
CamelIBMWatsonTTSVoice (producer) Constant: | The voice for synthesize and getPronunciation. It replaces the voice option. synthesize sets it on the result to the voice it used. | String | |
CamelIBMWatsonTTSAccept (producer) Constant: | The audio format (MIME type) that synthesize returns. It replaces the accept option. synthesize sets it on the result to the format it requested. | String | |
CamelIBMWatsonTTSCustomizationId (producer) Constant: | The customization ID (a GUID) of the custom model that synthesize uses. It replaces the customizationId option. | String | |
CamelIBMWatsonTTSWord (producer) Constant: | The word whose pronunciation getPronunciation returns. If not set, the producer reads it from the message body. | String | |
CamelIBMWatsonTTSFormat (producer) Constant: | The phoneme format of the pronunciation that getPronunciation returns: ipa (International Phonetic Alphabet) or ibm (IBM Symbolic Phonetic Representation). If not set, the service uses its default format. | String | |
CamelIBMWatsonTTSLanguage (producer) Constant: | The language for listCustomModels, for example en-US, to list only the custom models of that language. If not set, all custom models of the service instance are listed. | String | |
CamelIBMWatsonTTSModelId (producer) Constant: | The customization ID (a GUID) of the custom model that getCustomModel returns. If not set, the producer reads it from the message body. | String | |
CamelIBMWatsonTTSVoiceName (producer) Constant: | The name of the voice that getVoice returns, for example en-US_AllisonV3Voice. If not set, the producer reads it from the message body. | String |
Message Body
The message body at each side of the IBM Watson Text to Speech 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 text to synthesize, or the voice name, custom model ID or word for getVoice, getCustomModel and getPronunciation, read as text unless the CamelIBMWatsonTTSText, CamelIBMWatsonTTSVoiceName, CamelIBMWatsonTTSModelId or CamelIBMWatsonTTSWord header gives it. No body fails, and a Map or a POJO is sent as its toString(). listVoices and listCustomModels do not read the body. |
Producer output |
| synthesize gives the audio in the format of accept (audio/wav by default) as the InputStream of the HTTP response, which can be read once: write it to a file or convert it to bytes. listVoices gives a List of Voice and listCustomModels a List of CustomModel of the IBM Watson SDK.
|
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, the sides and the tools that use them are described in Message Body Types.
Required Watson Text to Speech component options
You must provide the apiKey to access IBM Watson Text to Speech. Optionally, you can specify a custom serviceUrl if you’re using a dedicated or private instance.
Usage
Watson Text to Speech Producer operations
The IBM Watson Text to Speech component provides the following operations:
-
synthesize - Convert text to speech audio
-
listVoices - Get available voices for synthesis
-
getVoice - Get information about a specific voice
-
listCustomModels - List custom voice models
-
getCustomModel - Get information about a custom voice model
-
getPronunciation - Get pronunciation for a specific word
If you don’t specify an operation explicitly, you must set it via the operation parameter.
Examples
Synthesize Text to Speech
Convert text to speech using the default voice:
-
Java
-
XML
-
YAML
from("direct:start")
.setHeader("CamelIBMWatsonTTSText", constant("Hello, welcome to IBM Watson Text to Speech!"))
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize")
.to("file:/var/audio?fileName=output.wav"); <route>
<from uri="direct:start"/>
<setHeader name="CamelIBMWatsonTTSText">
<constant>Hello, welcome to IBM Watson Text to Speech!</constant>
</setHeader>
<to uri="ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize"/>
<to uri="file:/var/audio?fileName=output.wav"/>
</route> - route:
from:
uri: direct:start
steps:
- setHeader:
name: CamelIBMWatsonTTSText
constant: "Hello, welcome to IBM Watson Text to Speech!"
- to:
uri: ibm-watson-text-to-speech:myTTS
parameters:
apiKey: "RAW(yourApiKey)"
operation: synthesize
- to:
uri: file:/var/audio
parameters:
fileName: output.wav This will synthesize the text and produce an audio WAV file.
Synthesize with Custom Voice
Convert text to speech using a specific voice:
-
Java
-
XML
-
YAML
from("direct:start")
.setBody(constant("Bonjour, bienvenue sur IBM Watson!"))
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize&voice=fr-FR_NicolasV3Voice&accept=audio/mp3")
.to("file:/var/audio?fileName=output.mp3"); <route>
<from uri="direct:start"/>
<setBody>
<constant>Bonjour, bienvenue sur IBM Watson!</constant>
</setBody>
<to uri="ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize&voice=fr-FR_NicolasV3Voice&accept=audio/mp3"/>
<to uri="file:/var/audio?fileName=output.mp3"/>
</route> - route:
from:
uri: direct:start
steps:
- setBody:
constant: "Bonjour, bienvenue sur IBM Watson!"
- to:
uri: ibm-watson-text-to-speech:myTTS
parameters:
apiKey: "RAW(yourApiKey)"
operation: synthesize
voice: fr-FR_NicolasV3Voice
accept: audio/mp3
- to:
uri: file:/var/audio
parameters:
fileName: output.mp3 This will synthesize French text using the Nicolas voice and produce an MP3 file.
Available Voices
Some commonly used voices include:
-
English (US): en-US_AllisonV3Voice, en-US_MichaelV3Voice, en-US_LisaV3Voice
-
English (UK): en-GB_KateV3Voice, en-GB_CharlotteV3Voice
-
Spanish (ES): es-ES_EnriqueV3Voice, es-ES_LauraV3Voice
-
French (FR): fr-FR_NicolasV3Voice, fr-FR_ReneeV3Voice
-
German (DE): de-DE_BirgitV3Voice, de-DE_DieterV3Voice
-
Italian (IT): it-IT_FrancescaV3Voice
-
Japanese (JP): ja-JP_EmiV3Voice
-
Portuguese (BR): pt-BR_IsabelaV3Voice
List Available Voices
Get a list of all available voices:
from("direct:listVoices")
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=listVoices")
.process(exchange -> {
List<Voice> voices = exchange.getMessage().getBody(List.class);
voices.forEach(voice -> {
System.out.println("Voice: " + voice.getName() +
" - Language: " + voice.getLanguage() +
" - Description: " + voice.getDescription());
});
}); Get Voice Information
Get detailed information about a specific voice:
from("direct:getVoice")
.setHeader("CamelIBMWatsonTTSVoiceName", constant("en-US_AllisonV3Voice"))
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=getVoice")
.process(exchange -> {
Voice voice = exchange.getMessage().getBody(Voice.class);
System.out.println("Voice details: " + voice);
}); Audio Format Options
The component supports various audio formats via the accept parameter:
-
audio/wav (default) - WAV format, uncompressed
-
audio/mp3 - MP3 format, compressed
-
audio/ogg - Ogg Vorbis format
-
audio/flac - FLAC format, lossless compression
-
audio/webm - WebM format
Example with MP3 output:
-
Java
-
XML
-
YAML
from("direct:mp3")
.setBody(constant("This will be an MP3 file"))
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize&accept=audio/mp3")
.to("file:/var/audio?fileName=speech.mp3"); <route>
<from uri="direct:mp3"/>
<setBody>
<constant>This will be an MP3 file</constant>
</setBody>
<to uri="ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize&accept=audio/mp3"/>
<to uri="file:/var/audio?fileName=speech.mp3"/>
</route> - route:
from:
uri: direct:mp3
steps:
- setBody:
constant: This will be an MP3 file
- to:
uri: ibm-watson-text-to-speech:myTTS
parameters:
apiKey: "RAW(yourApiKey)"
operation: synthesize
accept: audio/mp3
- to:
uri: file:/var/audio
parameters:
fileName: speech.mp3 Using Custom Voice Models
If you have created a custom voice model, you can use it for synthesis:
-
Java
-
XML
-
YAML
from("direct:customVoice")
.setBody(constant("Text to synthesize with custom voice"))
.setHeader("CamelIBMWatsonTTSCustomizationId", constant("your-customization-guid"))
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize")
.to("file:/var/audio"); <route>
<from uri="direct:customVoice"/>
<setBody>
<constant>Text to synthesize with custom voice</constant>
</setBody>
<setHeader name="CamelIBMWatsonTTSCustomizationId">
<constant>your-customization-guid</constant>
</setHeader>
<to uri="ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=synthesize"/>
<to uri="file:/var/audio"/>
</route> - route:
from:
uri: direct:customVoice
steps:
- setBody:
constant: Text to synthesize with custom voice
- setHeader:
name: CamelIBMWatsonTTSCustomizationId
constant: your-customization-guid
- to:
uri: ibm-watson-text-to-speech:myTTS
parameters:
apiKey: "RAW(yourApiKey)"
operation: synthesize
- to:
uri: file:/var/audio List Custom Models
List all your custom voice models:
from("direct:listCustomModels")
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=listCustomModels")
.process(exchange -> {
List<CustomModel> models = exchange.getMessage().getBody(List.class);
models.forEach(model -> {
System.out.println("Model: " + model.getCustomizationId() +
" - Name: " + model.getName() +
" - Language: " + model.getLanguage());
});
}); Get Pronunciation
Get the pronunciation for a specific word:
from("direct:pronunciation")
.setHeader("CamelIBMWatsonTTSWord", constant("synthesize"))
.setHeader("CamelIBMWatsonTTSFormat", constant("ipa"))
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&operation=getPronunciation")
.process(exchange -> {
Pronunciation pronunciation = exchange.getMessage().getBody(Pronunciation.class);
System.out.println("IPA Pronunciation: " + pronunciation.getPronunciation());
}); Watson Text to Speech Authentication
IBM Watson Text to Speech uses IBM Cloud IAM (Identity and Access Management) for authentication. You need to provide your IBM Cloud API key.
You can create API keys in the IBM Cloud console: 1. Go to https://cloud.ibm.com/iam/apikeys 2. Click "Create an IBM Cloud API key" 3. Copy the API key and use it in your Camel routes
For more information about authentication, see the IBM Watson TTS documentation.
Watson Text to Speech Endpoints
If you have a dedicated or regional instance, you can specify a custom service URL:
-
Java
-
XML
-
YAML
from("direct:start")
.setBody(constant("Hello World"))
.to("ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&serviceUrl=https://api.eu-gb.text-to-speech.watson.cloud.ibm.com&operation=synthesize")
.to("file:/var/audio"); <route>
<from uri="direct:start"/>
<setBody>
<constant>Hello World</constant>
</setBody>
<to uri="ibm-watson-text-to-speech:myTTS?apiKey=RAW(yourApiKey)&serviceUrl=https://api.eu-gb.text-to-speech.watson.cloud.ibm.com&operation=synthesize"/>
<to uri="file:/var/audio"/>
</route> - route:
from:
uri: direct:start
steps:
- setBody:
constant: Hello World
- to:
uri: ibm-watson-text-to-speech:myTTS
parameters:
apiKey: "RAW(yourApiKey)"
serviceUrl: "https://api.eu-gb.text-to-speech.watson.cloud.ibm.com"
operation: synthesize
- to:
uri: file:/var/audio Common regional endpoints: - Dallas: https://api.us-south.text-to-speech.watson.cloud.ibm.com - Washington DC: https://api.us-east.text-to-speech.watson.cloud.ibm.com - Frankfurt: https://api.eu-de.text-to-speech.watson.cloud.ibm.com - London: https://api.eu-gb.text-to-speech.watson.cloud.ibm.com - Tokyo: https://api.jp-tok.text-to-speech.watson.cloud.ibm.com - Sydney: https://api.au-syd.text-to-speech.watson.cloud.ibm.com
Integration Tests
This component includes comprehensive integration tests that validate the functionality against the actual IBM Watson Text to Speech service. These tests are disabled by default to prevent accidental API calls during regular builds.
Prerequisites for Running Integration Tests
-
IBM Cloud Account: You need a valid IBM Cloud account
-
Watson Text to Speech Service: Create a Watson Text to Speech service instance in IBM Cloud
-
API Credentials: Obtain your API key and service URL from the IBM Cloud console
To get your credentials:
-
Log in to IBM Cloud Console
-
Navigate to your Text to Speech service instance
-
Go to "Manage" → "Credentials"
-
Copy your API Key and Service URL
Running Integration Tests
Integration tests are executed with the verify goal and require system properties:
mvn verify \
-Dcamel.ibm.watson.tts.apiKey=YOUR_API_KEY \
-Dcamel.ibm.watson.tts.serviceUrl=YOUR_SERVICE_URL Alternatively, using environment variables:
export CAMEL_IBM_WATSON_TTS_API_KEY=YOUR_API_KEY
export CAMEL_IBM_WATSON_TTS_SERVICE_URL=YOUR_SERVICE_URL
mvn verify \
-Dcamel.ibm.watson.tts.apiKey=${CAMEL_IBM_WATSON_TTS_API_KEY} \
-Dcamel.ibm.watson.tts.serviceUrl=${CAMEL_IBM_WATSON_TTS_SERVICE_URL} Integration Test Coverage
The integration tests cover all major operations:
Synthesis Operations:
-
Basic text-to-speech with default voice
-
Text-to-speech with custom voices (Allison, Michael, Kate)
-
Multiple audio formats (WAV, MP3)
-
Multiple languages (English, Spanish, French, German)
-
Longer text passages
Voice Operations:
-
Listing all available voices
-
Getting detailed information about specific voices
Pronunciation Operations:
-
Getting IPA pronunciation for words
File Output Operations:
-
Saving synthesized speech to MP3 files
-
Saving synthesized speech to WAV files
-
Creating audio files with different voices
-
Creating multilingual audio files
Generated Audio Files
When integration tests run successfully, audio files are created in target/audio-output/:
-
test-output.mp3- Sample MP3 file -
test-output.wav- Sample WAV file -
michael.mp3,allison.mp3,kate.mp3- Different voice samples -
english.mp3,spanish.mp3,french.mp3,german.mp3- Multilingual samples
These files can be played with any media player to verify audio quality and compare different voices and languages.
Important Notes
-
Integration tests make real API calls to IBM Watson and may incur charges
-
Tests are automatically skipped during regular
mvn testexecution -
Audio files in
target/are cleaned withmvn clean -
File format validation checks MP3 ID3 tags and WAV RIFF headers
-
All tests include proper resource cleanup
Example Output
[INFO] Running org.apache.camel.component.ibm.watson.tts.integration.WatsonTextToSpeechIT
Created output directory: target/audio-output
Successfully synthesized text with default voice. Bytes read: 44032
Found 28 voices
Voice: en-US_MichaelV3Voice - Language: en-US - Gender: male
Voice: en-US_AllisonV3Voice - Language: en-US - Gender: female
Successfully saved MP3 file: target/audio-output/test-output.mp3 (size: 51234 bytes)
Successfully created audio files in 4 different languages
[INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0 Dependencies
Maven users will need to add the following dependency to their pom.xml.
pom.xml
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ibm-watson-text-to-speech</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> where x.x.x is the version number of Camel.