Skip to main content

Overview

Create a custom voice clone from a reference audio file and use it to generate speech. The cloned voice captures the unique characteristics of the original speaker.

Requirements

  • Reference audio file (10-30 seconds of clear speech)
  • Supported formats: WAV, MP3, FLAC, OGG
  • Clean audio with minimal background noise works best

Python

import os
from camb.client import CambAI, save_stream_to_file

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

def clone_voice():
    # Create custom voice from reference audio
    print("Creating custom voice from reference audio...")
    custom_voice = client.voice_cloning.create_custom_voice(
        file=open("reference.wav", "rb"),
        voice_name="my-cloned-voice",
        gender=1,  # 1 = male, 2 = female
        description="Custom cloned voice",
        language=1,  # 1 = English
        enhance_audio=True
    )

    print(f"Voice created! ID: {custom_voice.voice_id}")

    # Generate speech with the cloned voice
    print("Generating speech with cloned voice...")
    response = client.text_to_speech.tts(
        text="Hello! This is my cloned voice speaking.",
        voice_id=custom_voice.voice_id,
        language="en-us",
        speech_model="mars-flash"
    )

    save_stream_to_file(response, "cloned_output.wav")
    print("Audio saved to cloned_output.wav")

clone_voice()

TypeScript

import 'dotenv/config';
import { CambClient, saveStreamToFile } from '@camb-ai/sdk';
import * as fs from 'fs';

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

async function cloneVoice() {
    const referenceAudioPath = 'reference.wav';

    if (!fs.existsSync(referenceAudioPath)) {
        console.error('Reference audio file not found');
        console.log('Requirements:');
        console.log('  - Audio: 10-30 seconds of clear speech');
        console.log('  - Formats: WAV, MP3, FLAC, OGG');
        return;
    }

    // Create custom voice from reference audio
    console.log('Creating custom voice from reference audio...');
    const customVoice = await client.voiceCloning.createCustomVoice({
        file: fs.createReadStream(referenceAudioPath),
        voice_name: `cloned-voice-${Date.now()}`,
        gender: 1, // 1 = male, 2 = female
        description: 'Custom cloned voice',
        language: 1, // 1 = English
        enhance_audio: true
    });

    console.log(`Voice created! ID: ${customVoice.voice_id}`);

    // Generate speech with the cloned voice
    console.log('Generating speech with cloned voice...');
    const response = await client.textToSpeech.tts({
        text: 'Hello! This is my cloned voice speaking.',
        voice_id: customVoice.voice_id,
        language: 'en-us',
        speech_model: 'mars-flash',
        output_configuration: { format: 'wav' }
    });

    await saveStreamToFile(response, 'cloned_output.wav');
    console.log('Audio saved to cloned_output.wav');
}

cloneVoice();

Parameters

ParameterDescriptionValues
genderVoice gender1 = male, 2 = female
languageVoice language1 = English
enhance_audioImprove audio qualitytrue / false

Tips

  • Use high-quality reference audio for best results
  • 15-20 seconds of speech is ideal
  • Avoid background music or noise in reference audio
  • The cloned voice is saved to your account for future use