声音设计快速入门

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

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

使用声音设计 API

本指南假定你已设置 API 密钥和 SDK。如果尚未设置,请先完成 快速入门。要通过扬声器播放音频,可能还需要安装 MPV 和/或 ffmpeg。

1

发起 API 请求

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

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

先根据提示词生成预览。

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

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

执行代码

python example.py

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

3

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

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

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)

后续步骤