> ## Documentation Index
> Fetch the complete documentation index at: https://docs.camb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Voice Cloning

> Clone a voice from reference audio and generate speech with it

## 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

### Prerequisites

<Steps>
  <Step title="Create an account">
    Sign up at [CAMB.AI Studio](https://studio.camb.ai) if you haven't already.
  </Step>

  <Step title="Get your API key">
    Go to **Settings → API Keys** in Studio and copy your key. See [Authentication](/getting-started/authentication) for details.
  </Step>

  <Step title="Install the SDK">
    <CodeGroup>
      ```bash Python theme={null}
      pip install camb-sdk
      ```

      ```bash TypeScript theme={null}
      npm install @camb-ai/sdk
      ```
    </CodeGroup>

    Skip this step if you're using the [direct API](/tutorials/direct-api).
  </Step>

  <Step title="Set your API key to use in your code">
    ```bash theme={null}
    export CAMB_API_KEY="your_api_key_here"
    ```
  </Step>
</Steps>

***

## Code

<CodeGroup>
  ```python Python theme={null}
  import os
  from camb.client import CambAI, save_stream_to_file
  from camb.types import StreamTtsOutputConfiguration
  from camb.types.language_enums import Languages

  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=Languages.EN_US
      )

      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-8.1-flash-beta",
          output_configuration=StreamTtsOutputConfiguration(format="wav")
      )

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

  clone_voice()
  ```

  ```javascript TypeScript theme={null}
  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
      });

      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-8.1-flash-beta',
          output_configuration: { format: 'wav' }
      });

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

  cloneVoice();
  ```
</CodeGroup>

***

## Parameters

| Parameter  | Description    | Values                                                                               |
| ---------- | -------------- | ------------------------------------------------------------------------------------ |
| `gender`   | Voice gender   | `1` = male, `2` = female                                                             |
| `language` | Voice language | Use `Languages` enum (e.g., `Languages.EN_US`, `Languages.ES_ES`, `Languages.FR_FR`) |

***

## 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

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Emotional Voice Control" icon="sparkles" href="/tutorials/emotional-voice-control">
    Add emotional expression to your cloned voices with mars-instruct.
  </Card>

  <Card title="Text to Speech" icon="audio-lines" href="/tutorials/tts-with-sdk">
    Generate speech with any voice using the SDK.
  </Card>

  <Card title="TTS with Accents" icon="globe" href="/tutorials/tts-with-accents">
    Speak in 140+ language accents with the same voice.
  </Card>

  <Card title="API Reference" icon="book" href="/api-reference/endpoint/create-custom-voice">
    Full voice cloning API specification.
  </Card>

  <Card title="Text to Sound Effects" icon="volume-2" href="/tutorials/text-to-sound">
    Generate sound effects and music from text.
  </Card>
</CardGroup>
