> This is a page from the ElevenLabs documentation. For a complete page index, fetch https://elevenlabs.io/docs/llms.txt. For the full documentation in a single file, fetch https://elevenlabs.io/docs/llms-full.txt.

# 声音设计快速入门

本指南将介绍如何使用声音设计 API 通过提示词设计声音。

## 使用声音设计 API

> **Note**
>
> 本指南假定你已[设置 API 密钥和 SDK](/docs/zh/eleven-api/quickstart)。如果尚未设置，请先完成
> 快速入门。要通过扬声器播放音频，可能还需要安装
> [MPV](https://mpv.io/) 和/或 [ffmpeg](https://ffmpeg.org/)。

#### 发起 API 请求

通过提示词设计声音分为两个步骤：

1. 根据提示词生成预览。
2. 选择最佳预览，并据此创建新声音。

先根据提示词生成预览。

根据所选语言，新建名为 `example.py` 或 `example.mts` 的文件，然后添加以下代码：

```python maxLines=0
# example.py
from dotenv import load_dotenv
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
import base64

load_dotenv()

elevenlabs = ElevenLabs(
  api_key=os.getenv("ELEVENLABS_API_KEY"),
)

voices = elevenlabs.text_to_voice.design(
    model_id="eleven_multilingual_ttv_v2",
    voice_description="A massive evil ogre speaking at a quick pace. He has a silly and resonant tone.",
    text="Your weapons are but toothpicks to me. Surrender now and I may grant you a swift end. I've toppled kingdoms and devoured armies. What hope do you have against me?",
)

for preview in voices.previews:
    # Convert base64 to audio buffer
    audio_buffer = base64.b64decode(preview.audio_base_64)

    print(f"Playing preview: {preview.generated_voice_id}")

    play(audio_buffer)
```

```typescript maxLines=0
// example.ts
import { ElevenLabsClient, play } from "@elevenlabs/elevenlabs-js";
import "dotenv/config";
import { Readable } from 'node:stream';
import { Buffer } from 'node:buffer';

const elevenlabs = new ElevenLabsClient();

const { previews } = await elevenlabs.textToVoice.design({
    modelId: "eleven_multilingual_ttv_v2",
    voiceDescription: "A massive evil ogre speaking at a quick pace. He has a silly and resonant tone.",
    text: "Your weapons are but toothpicks to me. Surrender now and I may grant you a swift end. I've toppled kingdoms and devoured armies. What hope do you have against me?",
});

for (const preview of previews) {
    // Convert base64 to buffer and create a Readable stream
    const audioStream = Readable.from(Buffer.from(preview.audioBase64, 'base64'));

    console.log(`Playing preview: ${preview.generatedVoiceId}`);

    // Play the audio using the stream
    await play(audioStream);
}
```

#### 执行代码

```python
python example.py
```

```typescript
npx tsx example.mts
```

应该会听到扬声器依次播放生成的声音预览。

#### 将生成的声音添加到声音库

生成预览并选出最喜欢的声音后，可通过生成的音色 ID 将其添加到声音库，以便与其他 API 配合使用。

```python
voice = elevenlabs.text_to_voice.create(
    voice_name="Jolly giant",
    voice_description="A huge giant, at least as tall as a building. A deep booming voice, loud and jolly.",
    # The generated voice ID of the preview you want to use,
    # using the first in the list for this example
    generated_voice_id=voices.previews[0].generated_voice_id
)

print(voice.voice_id)
```

```typescript
const voice = await elevenlabs.textToVoice.create({
    voiceName: "Jolly giant",
    voiceDescription: "A huge giant, at least as tall as a building. A deep booming voice, loud and jolly.",
    // The generated voice ID of the preview you want to use,
    // using the first in the list for this example
    generatedVoiceId: previews[0].generatedVoiceId
});

// The ID of the newly created voice, use this to reference the voice in other APIs
console.log(voice.voiceId);
```

## 后续步骤

#### [即时语音克隆](/docs/zh/eleven-api/guides/how-to/voices/instant-voice-cloning)

使用现有音频克隆声音，而非根据描述生成声音。

#### [API 参考文档](/docs/zh/api-reference/text-to-voice/design)

完整的声音设计 API 参考文档和参数。