Skip to main content
Get started with the Camb.ai Java SDK in minutes

Overview

The Camb.ai Java SDK provides a simple interface to integrate high-quality text-to-speech into your applications. This quickstart will have you generating speech in under 5 minutes.

Installation

Gradle

Add the dependency to your build.gradle:
dependencies {
    implementation 'ai.camb:cambai-java-sdk:0.0.1'
}

Maven

Add the dependency to your pom.xml:
<dependency>
    <groupId>ai.camb</groupId>
    <artifactId>cambai-java-sdk</artifactId>
    <version>0.0.1</version>
</dependency>

Authentication

Get your API key from CAMB.AI Studio and set it as an environment variable:
export CAMB_API_KEY=your_api_key_here

Quick Start

Streaming Text-to-Speech

Generate and stream speech in real-time. The SDK returns an InputStream for the audio data:
import CambApiClient;
import resources.texttospeech.requests.CreateStreamTtsRequestPayload;
import resources.texttospeech.types.CreateStreamTtsRequestPayloadLanguage;
import resources.texttospeech.types.CreateStreamTtsRequestPayloadSpeechModel;
import java.io.InputStream;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Main {
    public static void main(String[] args) throws Exception {
        // Initialize the client
        CambApiClient client = CambApiClient.builder()
            .apiKey(System.getenv("CAMB_API_KEY"))
            .build();

        // Stream TTS audio
        InputStream audioStream = client.textToSpeech().tts(CreateStreamTtsRequestPayload.builder()
            .text("Hello! Welcome to Camb.ai text-to-speech.")
            .voiceId(147320)
            .language(CreateStreamTtsRequestPayloadLanguage.EN_US)
            .speechModel(CreateStreamTtsRequestPayloadSpeechModel.MARS_FLASH)
            .build());

        // Save to file
        File outputFile = new File("output.wav");
        Files.copy(audioStream, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

        System.out.println("Success! Audio saved to output.wav");
    }
}

Using the Helper Function

You can easily wrap the stream saving into a helper method:
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public static void saveStreamToFile(InputStream stream, String filename) throws IOException {
    Files.copy(stream, Path.of(filename), StandardCopyOption.REPLACE_EXISTING);
}

// Usage:
InputStream stream = client.textToSpeech().tts(payload);
saveStreamToFile(stream, "output.wav");

Choosing a Model

Camb.ai offers three MARS models optimized for different use cases:

MARS Flash

.speechModel(CreateStreamTtsRequestPayloadSpeechModel.MARS_FLASH)
Best for: Real-time voice agents, low-latency applications
Sample rate: 22.05kHz

MARS Pro

.speechModel(CreateStreamTtsRequestPayloadSpeechModel.MARS_PRO)
Best for: Audio production, high-quality content
Sample rate: 48kHz

MARS Instruct

.speechModel(CreateStreamTtsRequestPayloadSpeechModel.MARS_INSTRUCT)
.userInstructions("Speak in a warm, friendly tone")
Best for: Fine-grained control over tone and style
Sample rate: 22.05kHz

Listing Available Voices

Discover available voices for your application:
var voices = client.voiceCloning().listVoices();

for (var voice : voices) {
    System.out.println("ID: " + voice.getId() + ", Name: " + voice.getVoiceName());
}

Language Support

Camb.ai supports 140+ languages. Specify the language using the CreateStreamTtsRequestPayloadLanguage enum: Languages supported by each model mentioned at MARS Models.
// English (US)
.language(CreateStreamTtsRequestPayloadLanguage.EN_US)

// Spanish
.language(CreateStreamTtsRequestPayloadLanguage.ES_ES)

// French
.language(CreateStreamTtsRequestPayloadLanguage.FR_FR)

Error Handling

Handle common errors gracefully:
try {
    InputStream stream = client.textToSpeech().tts(payload);
} catch (Exception e) {
    System.err.println("Error generating speech: " + e.getMessage());
}

Using Custom Provider

For more details check this guide Custom Cloud Providers

Baseten Deployment

Initialize the client with your custom provider implementation. Baseten Provider Example
import providers.BasetenProvider;

ITtsProvider ttsProvider = new BasetenProvider(
    "YOUR_BASETEN_API_KEY",
    "YOUR_BASETEN_URL"
);

Next Steps

Resources