Skip to main content

Overview

Translate text and generate speech in one API call. Input text in any language, get audio output in your target language.
This is different from the standard TTS API which only changes accents. Translated TTS actually translates your text before speaking.

Python

import os
import time
import requests
from camb.client import CambAI

client = CambAI(api_key=os.getenv("CAMB_API_KEY"))

def translate_and_speak():
    # Translate English to Spanish and generate speech
    # Language codes: 47 = English (US), 28 = Spanish (Spain)
    response = client.translated_tts.create_translated_tts(
        text="Hello, welcome to our service. We're glad to have you here.",
        source_language=47,  # English (US)
        target_language=28,  # Spanish (Spain)
        voice_id=144300      # Voice ID (required)
    )

    task_id = response.task_id
    print(f"Task created: {task_id}")

    # Poll for completion
    while True:
        status = client.translated_tts.get_translated_tts_task_status(task_id=task_id)
        print(f"Status: {status.status}")

        if status.status == "SUCCESS":
            # Get the audio URL and download
            result = client.text_to_speech.get_tts_run_info(
                run_id=status.run_id,
                output_type="file_url"
            )
            audio_response = requests.get(result.output_url)
            with open("translated_output.wav", "wb") as f:
                f.write(audio_response.content)
            print("Saved to translated_output.wav")
            break
        elif status.status == "FAILED":
            print("Translation failed!")
            break

        time.sleep(2)

translate_and_speak()

TypeScript

import { CambClient } from '@camb-ai/sdk';
import fs from 'fs';

const client = new CambClient({
    apiKey: process.env.CAMB_API_KEY
});

async function translateAndSpeak() {
    // Translate English to Spanish and generate speech
    // Language codes: 47 = English (US), 28 = Spanish (Spain)
    console.log('Translating and generating speech...');
    const response = await client.translatedTts.createTranslatedTts({
        text: "Hello, welcome to our service. We're glad to have you here.",
        source_language: 47,  // English (US)
        target_language: 28,  // Spanish (Spain)
        voice_id: 144300      // Voice ID (required)
    });

    const taskId = response.task_id;
    console.log(`Task created: ${taskId}`);

    // Poll for completion
    while (true) {
        const status = await client.translatedTts.getTranslatedTtsTaskStatus({
            task_id: taskId
        });

        console.log(`Status: ${status.status}`);

        if (status.status === 'SUCCESS') {
            // Small delay to ensure result is available in backend
            await new Promise(r => setTimeout(r, 1000));

            // Get the audio URL and download
            const result = await client.textToSpeech.getTtsRunInfo({
                run_id: status.run_id,
                output_type: 'file_url'
            });
            const audioResponse = await fetch(result.output_url);
            const buffer = Buffer.from(await audioResponse.arrayBuffer());
            fs.writeFileSync('translated_output.wav', buffer);
            console.log('Saved to translated_output.wav');
            break;
        } else if (status.status === 'FAILED') {
            console.log('Translation failed!');
            break;
        }

        await new Promise(r => setTimeout(r, 2000));
    }
}

translateAndSpeak();

Parameters

ParameterDescription
textInput text to translate
source_languageNumeric language ID of input (e.g., 47 for English)
target_languageNumeric language ID for output (e.g., 28 for Spanish)
voice_idVoice ID to use for speech generation (required)

Common Language IDs

LanguageID
English (US)47
Spanish (Spain)28
French (France)33
German22
Japanese45
Hindi (India)39
Portuguese (Brazil)73
Chinese (Mandarin)16

Use Cases

  • Global Announcements: Translate and speak messages in multiple languages
  • Content Localization: Convert podcasts/videos to other languages
  • Customer Support: Respond in customer’s native language
  • E-Learning: Translate course content automatically