# LSTM
**Repository Path**: BestJohnny/LSTM
## Basic Information
- **Project Name**: LSTM
- **Description**: lstm
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-05-29
- **Last Updated**: 2025-12-05
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
🗣️🔊 elevenlabs-api
image
非官方的 ElevenLabs AI 语音生成 Java API
快速入门
你想制作自定义语音?那来对地方了。本库涵盖了截至 2023 年 11 月 15 日的所有 ElevenLabs API 端点。更新说明我似乎操之过急(也可能是碰巧遇到了语音转语音 API 文档的意外推送),但我实现代码所基于的原始文档现已被移除或隐藏。该文档可能会原样重新发布,也可能需要调整后才会重新上线。
安装方式
Maven 依赖
要将 elevenlabs-api 引入你的 Maven 项目,请添加以下配置:
```
xml
net.andrewcpu
elevenlabs-api
2.7.8
```
JAR 包下载
已编译的 JAR 包可通过 Releases 标签页 获取。
API 密钥配置
要获取你的 ElevenLabs API 密钥,请访问 官方网站,在网站的「个人资料(Profile)」标签页中可查看你的 xi-api-key。需按如下方式在 ElevenLabsAPI Java 接口中注册 API 密钥:
```
java
ElevenLabs.setApiKey("YOUR_API_KEY_HERE");
ElevenLabs.setDefaultModel("eleven_monolingual_v1"); // 可选配置,默认值为:"eleven_monolingual_v1"
```
出于公共代码仓库的安全考虑,建议将 API 密钥存储在环境变量中,或放在源代码外部。
ElevenLabs 相关链接
ElevenLabs 官网:https://elevenlabs.io
ElevenLabs API 文档:https://api.elevenlabs.io/docs
借助构建器简化生成流程
v2.7.8 版本新增 SpeechGenerationBuilder.java
文本转语音
```
java
//文件输出
SpeechGenerationBuilder.textToSpeech()
.file() // 输出类型为文件(也可使用 .streamed() 获取 InputStream 流)
.setText(String text)
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
.setVoiceId("voiceIdString")
.setVoiceSettings(VoiceSettings)
.setVoice(Voice) // 也可传入语音对象,会自动从 Voice 中提取配置/ID
.setModelId("modelIdString")
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
.setLatencyOptimization(StreamLatencyOptimization.NONE)
.build();
//流式输出
SpeechGenerationBuilder.textToSpeech()
.streamed()
.setText(String text)
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
.setVoiceId("voiceIdString")
.setVoiceSettings(VoiceSettings)
.setVoice(Voice) // 也可传入语音对象,会自动从 Voice 中提取配置/ID
.setModelId("modelIdString")
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
.setLatencyOptimization(StreamLatencyOptimization.NONE)
.build();
```
语音管理
获取可用语音列表
要获取所有可访问的语音列表,可静态调用 Voice#getVoices() 方法。该方法会返回 ElevenLabs 预生成的 Voice 模型,以及你自己创建的所有自定义语音。
```
java
List voices = Voice.getVoices();
```
获取默认语音配置
ElevenLabs 提供了默认的 VoiceSettings 配置,可通过 VoiceSettings#getDefaultVoiceSettings() 静态获取该操作会发起网络请求。
```
plaintext
VoiceSettings.getDefaultVoiceSettings();
```
通过 ID 获取语音
可通过多种方式根据 voiceId 检索语音:
第一种方式:根据 voiceId 获取 Voice 对象,默认包含语音配置。
```
java
Voice.get(String voiceId);
```
若不想获取包含默认配置的 Voice 模型,可使用 Voice#get(String voiceId, boolean withSettings) 方法。将 withSettings 设置为 false 时,返回的语音对象将不包含默认的 VoiceSettings(后续可通过 Voice#fetchSettings() 加载配置,或在生成 TTS 时手动传入 VoiceSettings 对象)。
```
java
Voice.get(String voiceId, boolean withSettings);
```
删除语音
要删除语音,可调用 Voice#delete() 方法,该操作会从 ElevenLabs API 中移除对应的语音。
```
java
Voice voice;
voice.delete();
```
获取语音的最新配置
若默认的 VoiceSettings 参数在 API 外部被修改(比如通过官网或其他集成系统),可调用 Voice#fetchSettings() 方法获取并应用最新的 VoiceSettings 配置到语音对象该操作会发起网络请求,并更新当前操作的对象。
```
java
Voice voice;
voice.fetchSettings(); // 从 ElevenLabs 请求最新配置
```
更新语音配置
可修改并更新 Voice 对象中的 VoiceSettings 配置:Voice#updateVoiceSettings(VoiceSettings settings) 方法用于更新生成语音时使用的默认语音参数该操作会发起网络请求,并更新当前操作的对象。
```
java
Voice voice;
voice.updateVoiceSettings(VoiceSettings settings);
```
编辑语音
要编辑已有的 Voice 模型,可通过 VoiceBuilder#fromVoice(Voice voice) 方法将语音加载到构建器中。fromVoice(Voice voice) 方法会将语音对象中存储的所有当前值直接导入 VoiceBuilder(无需重新定义已有配置)。
「创建语音」章节同样使用 VoiceBuilder 类,区别在于前者使用 VoiceBuilder#create() 方法,而编辑语音需使用 VoiceBuilder#edit() 方法。
编辑语音时,必须调用 VoiceBuilder.edit():
```
java
Voice voice;
VoiceBuilder builder = VoiceBuilder.fromVoice(voice); // 将现有语音加载到构建器
builder.withLabel("key", "value"); // 添加新标签
builder.removeLabel("oldKey"); // 移除旧标签
builder.withFile(new File("someAudioFile.mp3")); // 添加新的音频样本
voice = builder.edit(); // 编辑语音并返回更新后的语音对象
```
创建语音
要通过 API 生成新的 Voice 模型,可使用 VoiceBuilder 类组装所需参数。注意事项:
所有 Voice 对象必须设置名称(每个语音只能有一个唯一名称);
至少需通过 VoiceBuilder#withFile() 提供一个音频文件(语音样本);
标签为可选配置,但建议添加,一个语音可绑定任意数量的标签。
```
java
Voice voice;
VoiceBuilder builder = new VoiceBuilder();
builder.withName("Voice name");
builder.withFile(new File("sample1.mp3"));
builder.withFile(new File("sample2.mp3"));
builder.withLabel("accent", "American");
voice = builder.create();
```
生成音频(TTS + STS)
要使用指定语音生成音频文件,可调用 Voice#generate(...) 系列方法。根据获取语音的方式(是否包含配置),决定是否能使用隐式的 voiceSettings,或是否需要手动指定 VoiceSettings 对象。除非明确请求不包含配置的语音,否则每个 Voice 对象都应包含默认的 VoiceSettings。
```
java
Voice voice;
File file = voice.generate("Hello world!", "my_favorite_model");
// 可用方法列表:
public File generate(String text, String model);
public File generate(String text, String model, VoiceSettings settings);
public File generate(String text, String model, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization);
public File generate(String text, String model, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat);
public File generate(String text, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat);
public File generate(String text, VoiceSettings settings, StreamLatencyOptimization streamLatencyOptimization);
public File generate(String text, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization);
public File generate(String text, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization);
public File generate(String text, StreamLatencyOptimization streamLatencyOptimization);
public File generate(String text, VoiceSettings settings);
public File generate(String text);
public InputStream generateStream(String text, String model);
public InputStream generateStream(String text, String model, VoiceSettings settings);
public InputStream generateStream(String text, VoiceSettings settings);
public InputStream generateStream(String text);
public InputStream generateStream(String text, String model, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization);
public InputStream generateStream(String text, String model, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization, VoiceSettings settings);
public InputStream generateStream(String text, VoiceSettings settings, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization);
public InputStream generateStream(String text, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization);
public InputStream generateStream(String text, String model, StreamLatencyOptimization streamLatencyOptimization);
public InputStream generateStream(String text, String model, StreamLatencyOptimization streamLatencyOptimization, VoiceSettings settings);
public InputStream generateStream(String text, VoiceSettings settings, StreamLatencyOptimization streamLatencyOptimization);
public InputStream generateStream(String text, StreamLatencyOptimization streamLatencyOptimization);
```
音频原生项目
创建音频原生项目
可通过 AudioNative API 创建音频原生项目:
```
java
CreateAudioEnabledProjectRequest request = new CreateAudioEnabledProjectRequest()
.setName("Project name")
.setImage("https://...com/img.png")
.setAuthor("Andrew")
.setSmall(true)
.setTextColor("red")
.setBackgroundColor("black")
.setSessionization(3)
.setVoiceId("aso23809")
.setModelId("my_favorite_model")
.setFile(new File("input.dat"))
.setAutoConvert(true);
CreateAudioEnabledProjectModelResponse response = ElevenLabs.getAudioNativeAPI()
.createAudioEnabledProject(request);
```
项目管理
创建项目
可通过 AddProjectRequest 构建器创建新项目:
```
java
AddProjectRequest request = new AddProjectRequest()
.setName("name")
.setFromUrl("...")
.setFromDocument(new File("file.dat"))
.setDefaultTitleVoiceId("voiceA")
.setDefaultParagraphVoiceId("voiceB")
.setDefaultModelId("the_default_model_of_your_dreams")
.setProjectOutputQuality(ProjectOutputQuality.STANDARD)
.setTitle("Big Title")
.setAuthor("Best Author")
.setIsbnNumber("THE. ISBN.")
.setAcxVolumeNormalization(true);
Project project = Project.addProject(request);
```
获取单个项目
通过项目 ID 获取指定项目:
```
java
Project project = Project.getProjectById(projectId);
```
获取所有项目
获取与当前账户关联的所有项目:
```
java
List projects = Project.getProjects();
```
项目交互操作
```
java
Project project;
// 删除项目
String deleteResult = project.delete();
// 转换项目
String conversionResult = project.convertProject();
// 获取项目快照
List snapshots = project.getSnapshots();
// 从内存中获取章节列表
List chapters = project.getChapters();
// 从 API 刷新本地项目的章节数据
chapters = project.fetchUpdatedChapters(); // 该操作会更新本地项目对象,并返回新章节列表
// 通过 ID 获取章节
Chapter chapter = project.getChapterById(chapterId);
// 删除章节
String result = project.deleteChapter(chapter);
// 转换章节
String result = project.convertChapter(chapter);
// 获取章节快照
List snapshots = project.getChapterSnapshots(chapter);
```
章节交互操作
```
java
Project project;
Chapter chapter = project.getChapterById("chapter_id");
// 删除章节
chapter.deleteChapter(project.getProjectId());
// 转换章节
chapter.convertChapter(project.getProjectId());
// 获取章节快照
List snapshots = chapter.getChapterSnapshots(project.getProjectId());
```
访问快照音频
访问 ProjectSnapshot 音频流:
```
java
Project project;
List projectSnapshots = project.getSnapshots();
ProjectSnapshot first = projectSnapshots.get(0);
InputStream audio = first.getAudioStream();
```
访问 ChapterSnapshot 音频流:
```
java
Project project;
Chapter chapter;
List chapterSnapshots = project.getChapterSnapshots(chapter);
ChapterSnapshot first = chapterSnapshots.get(0);
InputStream audio = first.getAudioStream();
```
ProjectSnapshot 和 ChapterSnapshot 均属于 Snapshot 类型。
语音样本
Sample 是用于训练指定 Voice 模型的训练数据。
获取语音样本
要获取指定语音的样本列表,可调用 Voice#getSamples() 方法:
```
java
Voice voice;
List samples = voice.getSamples();
```
下载样本
可通过 Sample#downloadAudio(File outputFile) 方法下载样本,其中 File 参数指定样本的本地下载路径:
```
java
Voice voice;
File file = voice.getSamples().get(0).downloadAudio();
```
删除样本
删除样本操作简单,该操作会发起网络请求:
```
java
Sample sample;
sample.delete();
```
历史记录
ElevenLabs 的 History 包含你所有过往的 TTS 生成记录。
获取生成历史
要获取生成历史,可调用 History#get() 方法(也可通过 User 对象的 User#getHistory() 方法获取):
```
java
History history = History.get();
```
获取所有历史记录项
历史记录接口支持分页参数和起始 ID 参数,可通过以下方式获取所有历史记录项:
```
java
History history = History.get(); // 获取最新的历史记录对象
Optional hist = Optional.of(history);
List items = new ArrayList();
do {
items.addAll(hist.get().getHistoryItems());
hist = hist.get().next();
} while(hist.isPresent() && hist.hasMore());
```
获取单个历史记录项
通过 ID 从历史记录中检索指定的 HistoryItem:
```
java
History history;
HistoryItem item = history.getHistoryItem("itemId");
```
下载历史记录
ElevenLabs 官方 API 提供了批量下载历史记录项的接口(打包为 ZIP 文件)。可传入包含 HistoryItem ID 的 String[] 数组,或 List 列表作为参数:
```
java
History history;
File download = history.downloadHistory(new String[]{"item-id1", "item-id2"});
File download = history.downloadHistory(List historyItems);
```
删除历史记录项
可调用 HistoryItem#delete() 方法从 ElevenLabs 中删除指定的历史记录项:
```
java
HistoryItem item;
item.delete();
```
获取历史记录项对应的语音
默认情况下,HistoryItem 包含生成该记录所使用的语音 voiceId。getVoice() 方法会向 ElevenLabs API 发起请求,获取对应的语音对象(另可参考 Voice.get() 和 Voice.getVoices() 方法):
```
java
HistoryItem item;
Voice voice = item.getVoice();
```
下载历史记录项音频
HistoryItem 代表一条过往的 TTS 生成记录,可通过 downloadAudio() 方法将生成的音频下载为 MP3 文件,返回值为下载文件的临时路径:
```
java
HistoryItem item;
File file = item.downloadAudio();
```
项目补充说明
Projects 模块相关补充说明待完善。
用户管理
获取订阅信息
Subscription 包含管理 API 使用所需的所有核心数据(字符使用量、下一个计费周期等):
```
java
Subscription subscription = Subscription.get();
```
获取用户信息
该接口会返回与当前 API 密钥关联的 User 对象:
```
java
User user = User.get();
```
异常说明
ElevenLabsValidationException
该异常表示向 ElevenLabs API 发起的请求格式错误,异常信息会指明请求中语法错误的参数位置。
其他说明
ElevenLabs 官方文档注明,其 API 仍处于实验阶段,所有接口均可能发生变更。API 的调整可能导致本库无法正常使用,若你发现 API 变更或库存在错误,欢迎提交 issue 或 PR。
如果你觉得本库对你有帮助,不妨点个星标支持一下!:)
待办事项
在添加项目支持时新增的两个构建器,我可能会重新设计优化,使其用法更清晰(尽管现有文档已覆盖相关使用场景)。
单元测试
已编写单元测试,但以下具有破坏性的接口未纳入测试范围:
- deleteHistoryItem - 不测试
- deleteSample - 不测试
- deleteVoice - 不测试
- editVoiceSettings - 不测试
- createVoice - 不测试
- editVoice - 不测试
若需自行运行单元测试,需克隆本仓库,并在 ElevenLabsTest.java 文件中填入你的 API 密钥和用于测试的语音信息。
感谢 ElevenLabs 打造了如此出色的工具 🥂