此版本仍在开发中,尚不被认为是稳定的。对于最新的快照版本,请使用 Spring AI 1.0.1! |
ElevenLabs 文本转语音 (TTS)
介绍
ElevenLabs 使用深度学习提供听起来自然的语音合成软件。其 AI 音频模型可生成 32 种语言的逼真、多功能且上下文感知的语音、语音和音效。ElevenLabs 文本转语音 API 使用户能够通过超逼真的 AI 旁白将任何书籍、文章、PDF、时事通讯或文本变为现实。
前提条件
-
创建一个 ElevenLabs 帐户并获取 API 密钥。您可以在 ElevenLabs 注册页面注册。登录后,您的 API 密钥可以在您的个人资料页面上找到。
-
添加
spring-ai-elevenlabs
依赖于项目的构建文件。有关更多信息,请参阅依赖项管理部分。
自动配置
Spring AI 为 ElevenLabs 文本转语音客户端提供 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-elevenlabs</artifactId>
</dependency>
或 Gradlebuild.gradle
构建文件:
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-elevenlabs'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。 |
语音属性
连接属性
前缀spring.ai.elevenlabs
用作所有 ElevenLabs 相关配置(连接和 TTS 特定设置)的属性前缀。这是在ElevenLabsConnectionProperties
.
属性 |
描述 |
默认值 |
spring.ai.elevenlabs.base-url |
ElevenLabs API 的基本 URL。 |
|
spring.ai.elevenlabs.api-key |
您的 ElevenLabs API 密钥。 |
- |
配置属性
前缀spring.ai.elevenlabs.tts
用作配置 ElevenLabs 文本转语音客户端的属性前缀。这是在ElevenLabsSpeechProperties
.
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.elevenlabs.tts.options.model-id |
要使用的模型的 ID。 |
eleven_turbo_v2_5 |
spring.ai.elevenlabs.tts.options.voice-id |
要使用的语音的 ID。这是语音 ID,而不是语音名称。 |
9BWtsMINqrJLrRacOk9x |
spring.ai.elevenlabs.tts.options.output-format |
生成音频的输出格式。请参阅下面的输出格式。 |
mp3_22050_32 |
spring.ai.elevenlabs.tts.enabled |
启用或禁用 ElevenLabs 文本转语音客户端。 |
true |
基本 URL 和 API 密钥也可以专门为 TTS 配置,使用spring.ai.elevenlabs.tts.base-url 和spring.ai.elevenlabs.tts.api-key .但是,通常建议使用全局spring.ai.elevenlabs 为简单起见,除非您有特定原因为不同的 ElevenLabs 服务使用不同的凭据。越具体tts 属性将覆盖全局属性。 |
所有以spring.ai.elevenlabs.tts.options 可以在运行时覆盖。 |
枚举值 |
描述 |
MP3_22050_32 |
MP3,22.05 kHz,32 kbps |
MP3_44100_32 |
MP3,44.1 kHz,32 kbps |
MP3_44100_64 |
MP3,44.1 kHz,64 kbps |
MP3_44100_96 |
MP3,44.1 kHz,96 kbps |
MP3_44100_128 |
MP3,44.1 kHz,128 kbps |
MP3_44100_192 |
MP3,44.1 kHz,192 kbps |
PCM_8000 |
PCM,8 kHz |
PCM_16000 |
PCM,16 kHz |
PCM_22050 |
PCM,22.05 kHz |
PCM_24000 |
PCM,24 kHz |
PCM_44100 |
PCM,44.1 kHz |
PCM_48000 |
PCM,48 kHz |
ULAW_8000 |
μ律,8 kHz |
ALAW_8000 |
A 律,8 kHz |
OPUS_48000_32 |
Opus,48 kHz,32 kbps |
OPUS_48000_64 |
Opus,48 kHz,64 kbps |
OPUS_48000_96 |
Opus,48 kHz,96 kbps |
OPUS_48000_128 |
Opus,48 kHz,128 kbps |
OPUS_48000_192 |
Opus,48 kHz,192 kbps |
运行时选项
这ElevenLabsTextToSpeechOptions
class 提供了在发出文本转语音请求时使用的选项。启动时,由spring.ai.elevenlabs.tts
,但您可以在运行时覆盖这些。以下选项可用:
-
modelId
:要使用的模型的 ID。 -
voiceId
:要使用的语音的 ID。 -
outputFormat
:生成音频的输出格式。 -
voiceSettings
:包含语音设置的对象,例如stability
,similarityBoost
,style
,useSpeakerBoost
和speed
. -
enableLogging
:用于启用或禁用日志记录的布尔值。 -
languageCode
:输入文本的语言代码(例如,英语为“en”)。 -
pronunciationDictionaryLocators
:发音词典定位器的列表。 -
seed
:用于随机数生成的种子,以提高可重复性。 -
previousText
:在正文之前添加文本,用于多轮对话中的上下文。 -
nextText
:主文本后面的文本,用于多轮对话中的上下文。 -
previousRequestIds
:请求对话中前几个回合的 ID。 -
nextRequestIds
:请求对话中后续轮次的 ID。 -
applyTextNormalization
:应用文本规范化(“自动”、“开”或“关”)。 -
applyLanguageTextNormalization
:应用语言文本规范化。
例如:
ElevenLabsTextToSpeechOptions speechOptions = ElevenLabsTextToSpeechOptions.builder()
.model("eleven_multilingual_v2")
.voiceId("your_voice_id")
.outputFormat(ElevenLabsApi.OutputFormat.MP3_44100_128.getValue())
.build();
TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("Hello, this is a text-to-speech example.", speechOptions);
TextToSpeechResponse response = elevenLabsTextToSpeechModel.call(speechPrompt);
使用语音设置
您可以通过提供VoiceSettings
在选项中。这允许您控制稳定性和相似性等属性。
var voiceSettings = new ElevenLabsApi.SpeechRequest.VoiceSettings(0.75f, 0.75f, 0.0f, true);
ElevenLabsTextToSpeechOptions speechOptions = ElevenLabsTextToSpeechOptions.builder()
.model("eleven_multilingual_v2")
.voiceId("your_voice_id")
.voiceSettings(voiceSettings)
.build();
TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("This is a test with custom voice settings!", speechOptions);
TextToSpeechResponse response = elevenLabsTextToSpeechModel.call(speechPrompt);
手动配置
添加spring-ai-elevenlabs
对项目 Maven 的依赖pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-elevenlabs</artifactId>
</dependency>
或 Gradlebuild.gradle
构建文件:
dependencies {
implementation 'org.springframework.ai:spring-ai-elevenlabs'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。 |
接下来,创建一个ElevenLabsTextToSpeechModel
:
ElevenLabsApi elevenLabsApi = ElevenLabsApi.builder()
.apiKey(System.getenv("ELEVEN_LABS_API_KEY"))
.build();
ElevenLabsTextToSpeechModel elevenLabsTextToSpeechModel = ElevenLabsTextToSpeechModel.builder()
.elevenLabsApi(elevenLabsApi)
.defaultOptions(ElevenLabsTextToSpeechOptions.builder()
.model("eleven_turbo_v2_5")
.voiceId("your_voice_id") // e.g. "9BWtsMINqrJLrRacOk9x"
.outputFormat("mp3_44100_128")
.build())
.build();
// The call will use the default options configured above.
TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("Hello, this is a text-to-speech example.");
TextToSpeechResponse response = elevenLabsTextToSpeechModel.call(speechPrompt);
byte[] responseAsBytes = response.getResult().getOutput();
流式传输实时音频
ElevenLabs 语音 API 支持使用块传输编码进行实时音频流。这允许在生成整个音频文件之前开始音频播放。
ElevenLabsApi elevenLabsApi = ElevenLabsApi.builder()
.apiKey(System.getenv("ELEVEN_LABS_API_KEY"))
.build();
ElevenLabsTextToSpeechModel elevenLabsTextToSpeechModel = ElevenLabsTextToSpeechModel.builder()
.elevenLabsApi(elevenLabsApi)
.build();
ElevenLabsTextToSpeechOptions streamingOptions = ElevenLabsTextToSpeechOptions.builder()
.model("eleven_turbo_v2_5")
.voiceId("your_voice_id")
.outputFormat("mp3_44100_128")
.build();
TextToSpeechPrompt speechPrompt = new TextToSpeechPrompt("Today is a wonderful day to build something people love!", streamingOptions);
Flux<TextToSpeechResponse> responseStream = elevenLabsTextToSpeechModel.stream(speechPrompt);
// Process the stream, e.g., play the audio chunks
responseStream.subscribe(speechResponse -> {
byte[] audioChunk = speechResponse.getResult().getOutput();
// Play the audioChunk
});
语音 API
ElevenLabs Voices API 允许您检索有关可用语音、其设置和默认语音设置的信息。可以使用此 API 来发现要在语音请求中使用的“voiceId”。
要使用 Voices API,您需要创建一个ElevenLabsVoicesApi
:
ElevenLabsVoicesApi voicesApi = ElevenLabsVoicesApi.builder()
.apiKey(System.getenv("ELEVEN_LABS_API_KEY"))
.build();
然后,您可以使用以下方法:
-
getVoices()
:检索所有可用声音的列表。 -
getDefaultVoiceSettings()
:获取语音的默认设置。 -
getVoiceSettings(String voiceId)
:返回特定声音的设置。 -
getVoice(String voiceId)
:返回有关特定语音的元数据。
例:
// Get all voices
ResponseEntity<ElevenLabsVoicesApi.Voices> voicesResponse = voicesApi.getVoices();
List<ElevenLabsVoicesApi.Voice> voices = voicesResponse.getBody().voices();
// Get default voice settings
ResponseEntity<ElevenLabsVoicesApi.VoiceSettings> defaultSettingsResponse = voicesApi.getDefaultVoiceSettings();
ElevenLabsVoicesApi.VoiceSettings defaultSettings = defaultSettingsResponse.getBody();
// Get settings for a specific voice
ResponseEntity<ElevenLabsVoicesApi.VoiceSettings> voiceSettingsResponse = voicesApi.getVoiceSettings(voiceId);
ElevenLabsVoicesApi.VoiceSettings voiceSettings = voiceSettingsResponse.getBody();
// Get details for a specific voice
ResponseEntity<ElevenLabsVoicesApi.Voice> voiceDetailsResponse = voicesApi.getVoice(voiceId);
ElevenLabsVoicesApi.Voice voiceDetails = voiceDetailsResponse.getBody();
示例代码
-
ElevenLabsTextToSpeechModelIT.java测试提供了一些如何使用库的一般示例。
-
ElevenLabsApiIT.java测试提供了使用低级
ElevenLabsApi
.