6 Commits
v37 ... v43

Author SHA1 Message Date
Andrew Stein
fdcc12ee41 Updated README 2023-06-11 19:07:28 -04:00
Andrew Stein
8c526a1539 Updated README and added new functions for parity. 2023-06-11 19:02:16 -04:00
Andrew Stein
95cf38c620 Updated action 2023-06-11 18:48:53 -04:00
Andrew Stein
6968219c55 Updated action 2023-06-11 18:28:05 -04:00
Andrew Stein
e6279b689f Updated action 2023-06-11 18:26:50 -04:00
Andrew Stein
3462385f3d Updated models to include static retrieval & action functions. 2023-06-11 18:19:25 -04:00
9 changed files with 275 additions and 85 deletions

114
README.md
View File

@@ -4,6 +4,10 @@
## Getting Started
So you wanna make custom voices, huh? Well you've come to the right place.
### Note: This repo is undergoing an upgrade to v2.0
If any of the documentation is out of place or issues occur, please submit a PR or create an issue.
I downgraded the repo from JDK 17 to JDK 11 as well.
### Installation
**Maven**
@@ -12,7 +16,7 @@ To add `elevenlabs-api` to your Maven project, use:
<dependency>
<groupId>net.andrewcpu.elevenlabs</groupId>
<artifactId>elevenlabs-api</artifactId>
<version>1.0-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>
</dependency>
```
The most up-to date package information can be found on the [Packages tab](https://github.com/AndrewCPU/elevenlabs-api/packages/)
@@ -27,11 +31,10 @@ Compiled JARs are available via the [Releases tab](https://github.com/AndrewCPU/
To access your ElevenLabs API key, head to the [official website](https://elevenlabs.io/), you can view your `xi-api-key` using the 'Profile' tab on the website.
To set up your ElevenLabs API key, you must register it with the ElevenLabsAPI Java API like below:
```java
ElevenLabsAPI.getInstance().setAPIKey("YOUR_API_KEY_HERE");
ElevenLabs.setApiKey("YOUR_API_KEY_HERE");
```
*For any public repository security, you should store your API key in an environment variable, or external from your source code.*
Once you've injected your API Key, you can safely assume that you will not receive a `ElevenLabsAPINotInitiatedException`.
- - -
<!-- TOC -->
@@ -66,9 +69,6 @@ Once you've injected your API Key, you can safely assume that you will not recei
* [Exceptions](#exceptions)
* [*ElevenLabsAPINotInitiatedException*](#elevenlabsapinotinitiatedexception)
* [*ElevenLabsValidationException*](#elevenlabsvalidationexception)
* [Built in Types](#built-in-types)
* [`Voice` Related Types](#voice-related-types)
* [`User` Related Types](#user-related-types)
* [Misc](#misc)
* [Links to ElevenLabs](#links-to-elevenlabs)
@@ -168,8 +168,15 @@ To generate an audio file with a given `Voice`, you can utilize the `Voice#gener
Depending on how you access your `Voice`, (with or without settings), will decide whether you can use the implicit `voiceSettings` or if you have to specify the `VoiceSettings` object to use. Unless explicitly requesting the `Voice` without settings, every `Voice` object SHOULD contain its default `VoiceSettings`.
```java
Voice voice;
File outputFile = voice.generate(String text, VoiceSettings voiceSettings, File output);
File outputFile = voice.generate(String text, File output); // Uses default voice settings
File file = voice.generate(String text);
File file = voice.generate(String text, VoiceSettings settings);
File file = voice.generate(String text, String model, VoiceSettings settings);
File file = voice.generate(String text, String model);
InputStream inputStream = voice.generateStream(String text);
InputStream inputStream = voice.generateStream(String text, VoiceSettings settings);
InputStream inputStream = voice.generateStream(String text, String model, VoiceSettings settings);
InputStream inputStream = voice.generateStream(String text, String model);
```
- - -
@@ -186,7 +193,7 @@ You can download a `Sample` via the `Sample#downloadAudio(File outputFile)` func
The `File` parameter of `downloadAudio()` is the location of where you want to locally download the sample.
```java
Voice voice;
File file = voice.getSamples().get(0).downloadAudio(File outputFile);
File file = voice.getSamples().get(0).downloadAudio();
```
### Deleting a Sample
@@ -214,11 +221,11 @@ HistoryItem item = history.getHistoryItem("itemId");
### Downloading History
The official API of ElevenLabs provides an endpoint for downloading multiple `HistoryItem`'s as a ZIP file. To download such items, you can pass a `String[]` containing the `HistoryItem` IDs, OR you can provide a `List<HistoryItem>` parameter.
The second parameter is the path in which you would like to save the ZIP file.
```java
History history;
File download = history.downloadHistory(new String[]{"item-id1", "item-id2"}, new File("outputFile.zip"));
File download = history.downloadHistory(List<HistoryItem> historyItems, File outputFile);
File download = history.downloadHistory(new String[]{"item-id1", "item-id2"});
File download = history.downloadHistory(List<HistoryItem> historyItems);
```
### Deleting a HistoryItem
@@ -236,10 +243,10 @@ Voice voice = item.getVoice();
```
### Downloading a HistoryItem Audio
A `HistoryItem` is a previous TTS generation. You can download the generation as an MP3 file by providing the `downloadAudio(File file)` function with the target location for the downloaded file. The return value is the same `File` provided as a parameter.
A `HistoryItem` is a previous TTS generation. You can download the generation as an MP3 file by executing the `downloadAudio()` function. The return value is the tmp `File` location of your download.
```java
HistoryItem item;
File file = item.downloadAudio(File outputFile);
File file = item.downloadAudio();
```
- - -
@@ -259,77 +266,8 @@ User user = User.get();
- - -
## Exceptions
You'll find most actions that make network requests also will throw `IOException`, `ElevenLabsAPINotInitiatedException`, and `ElevenLabsValidationException`.
*The only function that will make a network request **without throwing an exception is*** `HistoryItem#getVoice()`.
### *ElevenLabsAPINotInitiatedException*
This exception will be thrown if you attempt to use the library without setting an API key.
### *ElevenLabsValidationException*
This error indicates a malformed request to the ElevenLabs API. The exception should provide the location of any syntactically incorrect parameters within the request.
- - -
# Built in Types
There are a few objects and enums defined for this API.
## `Voice` Related Types
```java
Voice(String voiceId, String name, List<Sample> samples, String category,
Map<String, String> labels, String previewUrl, List<String> availableForTiers,
VoiceSettings settings)
```
```java
VoiceSettings(double stability, double similarityBoost)
```
```java
Sample(String sampleId, String fileName, String mimeType, long sizeBytes, String hash)
```
```java
History(List<HistoryItem> history)
```
```java
History.HistoryItem(String historyItemId, String voiceId, String voiceName, String text,
long dateUnix, int characterCountChangeFrom, int characterCountChangeTo,
String contentType, GenerationState state)
```
```java
public enum GenerationState {
CREATED,
DELETED,
PROCESSING;
}
```
- - -
## `User` Related Types
```java
User(Subscription subscription, boolean isNewUser, String xiApiKey)
```
```java
Subscription(String tier, int characterCount, int characterLimit, boolean canExtendCharacterLimit,
boolean allowedToExtendCharacterLimit, long nextCharacterCountResetUnix, int voiceLimit,
boolean canExtendVoiceLimit, boolean canUseInstantVoiceCloning, List<AvailableModel> availableModels,
AccountStatus status, NextInvoice nextInvoice)
```
```java
AvailableModel(String modelId, String displayName, List<SupportedLanguage> supportedLanguages)
```
```java
SupportedLanguage(String isoCode, String displayName)
```
```java
NextInvoice(int amountDueCents, long nextPaymentAttemptUnix)
```
```java
public enum AccountStatus {
TRIALING,
ACTIVE,
INCOMPLETE,
INCOMPLETE_EXPIRED,
PAST_DUE,
CANCELED,
UNPAID,
FREE;
}
```
- - -
@@ -341,7 +279,15 @@ If you like what you see, give it a star! :)
- - -
#### Unit Testing
*Todo*
Unit tests have been created, these endpoints are destructive and not included in the testing:
* deleteHistoryItem - WONT TEST
* deleteSample - WONT TEST
* deleteVoice - WONT TEST
* editVoiceSettings - WONT TEST
* createVoice - WONT TEST
* editVoice - WONT TEST
To run the unit tests yourself, you have to clone this repo and update the ElevenLabsTest.java file with your API key and your voice to test on.
Thanks to ElevenLabs for making an awesome tool 🥂

View File

@@ -2,11 +2,19 @@ package net.andrewcpu.elevenlabs.model.history;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.ElevenModel;
import java.io.File;
import java.util.Arrays;
import java.util.List;
public class History extends ElevenModel {
public static History get() {
return ElevenLabs.getHistory();
}
@JsonProperty("history")
private List<HistoryItem> historyItems;
@@ -23,6 +31,16 @@ public class History extends ElevenModel {
public History() {
}
@JsonIgnore
public HistoryItem getHistoryItem(String id) {
for (HistoryItem item : historyItems) {
if (item.getHistoryItemId().equals(id)) {
return item;
}
}
return null;
}
@JsonIgnore
public List<HistoryItem> getHistoryItems() {
return historyItems;
@@ -38,6 +56,14 @@ public class History extends ElevenModel {
return hasMore;
}
public File downloadHistory(String... historyIds) {
return ElevenLabs.getHistoryItemAudio(historyIds);
}
public File downloadHistory(HistoryItem... items) {
return ElevenLabs.getHistoryItemAudio(Arrays.stream(items).map(HistoryItem::getHistoryItemId).toArray(String[]::new));
}
@JsonIgnore
@Override
public String toString() {

View File

@@ -2,8 +2,10 @@ package net.andrewcpu.elevenlabs.model.history;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.ElevenModel;
import java.io.File;
import java.util.Map;
public class HistoryItem extends ElevenModel {
@@ -121,6 +123,14 @@ public class HistoryItem extends ElevenModel {
return feedback;
}
public String delete() {
return ElevenLabs.deleteHistoryItem(historyItemId);
}
public File downloadAudio() {
return ElevenLabs.getHistoryItemAudio(historyItemId);
}
@JsonIgnore
@Override
public String toString() {

View File

@@ -2,9 +2,15 @@ package net.andrewcpu.elevenlabs.model.user;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.ElevenModel;
public class Subscription extends ElevenModel {
@JsonIgnore
public static Subscription get() {
return ElevenLabs.getSubscription();
}
@JsonProperty("tier")
private String tier;

View File

@@ -2,9 +2,15 @@ package net.andrewcpu.elevenlabs.model.user;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.ElevenModel;
public class User extends ElevenModel {
public static User get() {
return ElevenLabs.getUser();
}
@JsonProperty("subscription")
private Subscription subscription;

View File

@@ -2,8 +2,11 @@ package net.andrewcpu.elevenlabs.model.voice;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.ElevenModel;
import java.io.File;
public class Sample extends ElevenModel {
@JsonProperty("sample_id")
private String sampleId;
@@ -20,6 +23,9 @@ public class Sample extends ElevenModel {
@JsonProperty("hash")
private String hash;
@JsonIgnore
public Voice voice;
public Sample(String sampleId, String fileName, String mimeType, long sizeBytes, String hash) {
this.sampleId = sampleId;
this.fileName = fileName;
@@ -56,6 +62,17 @@ public class Sample extends ElevenModel {
return hash;
}
public File downloadAudio() {
return ElevenLabs.getAudioSample(voice.getVoiceId(), sampleId);
}
/**
* Warning! This will delete the sample.
*/
public String delete() {
return ElevenLabs.deleteSample(voice.getVoiceId(), sampleId);
}
@Override
@JsonIgnore
public String toString() {

View File

@@ -2,13 +2,29 @@ package net.andrewcpu.elevenlabs.model.voice;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.ElevenModel;
import net.andrewcpu.elevenlabs.model.tuning.FineTuning;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Voice extends ElevenModel {
public static List<Voice> getVoices() {
return ElevenLabs.getVoices();
}
public static Voice getVoice(String voiceId) {
return ElevenLabs.getVoice(voiceId);
}
public static Voice getVoice(String voiceId, boolean withSettings) {
return ElevenLabs.getVoice(voiceId, withSettings);
}
@JsonProperty("voice_id")
private String voiceId;
@@ -54,7 +70,7 @@ public class Voice extends ElevenModel {
@JsonIgnore
public List<Sample> getSamples() {
return samples;
return samples.stream().peek(s -> s.voice = this).collect(Collectors.toList());
}
@JsonIgnore
@@ -97,6 +113,72 @@ public class Voice extends ElevenModel {
return sharing;
}
/**
* Warning! This will delete the voice.
*/
public String delete() {
return ElevenLabs.deleteVoice(voiceId);
}
public VoiceSettings fetchSettings() {
this.settings = ElevenLabs.getVoiceSettings(voiceId);
return settings;
}
public VoiceSettings updateVoiceSettings(VoiceSettings voiceSettings) {
ElevenLabs.editVoiceSettings(voiceId, voiceSettings);
this.settings = voiceSettings;
return settings;
}
public Voice refresh() {
Voice refreshedData = Voice.getVoice(voiceId, true);
this.name = refreshedData.name;
this.settings = refreshedData.settings;
this.voiceId = refreshedData.voiceId;
this.labels = refreshedData.labels;
this.description = refreshedData.description;
this.samples = refreshedData.samples;
this.fineTuning = refreshedData.fineTuning;
this.availableForTiers = refreshedData.availableForTiers;
this.sharing = refreshedData.sharing;
this.previewUrl = refreshedData.previewUrl;
this.category = refreshedData.category;
return this;
}
public File generate(String text, String model) {
return ElevenLabs.generateTextToSpeech(voiceId, text, model, settings);
}
public File generate(String text, String model, VoiceSettings settings) {
return ElevenLabs.generateTextToSpeech(voiceId, text, model, settings);
}
public File generate(String text, VoiceSettings settings) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", settings);
}
public File generate(String text) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", settings);
}
public InputStream generateStream(String text, String model) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, model, settings);
}
public InputStream generateStream(String text, String model, VoiceSettings settings) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, model, settings);
}
public InputStream generateStream(String text, VoiceSettings settings) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, "eleven_monolingual_v1", settings);
}
public InputStream generateStream(String text) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, "eleven_monolingual_v1", settings);
}
@JsonIgnore
@Override
public String toString() {

View File

@@ -0,0 +1,93 @@
package net.andrewcpu.elevenlabs.model.voice;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.response.CreateVoiceResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class VoiceBuilder {
public static VoiceBuilder fromVoice(Voice voice) {
VoiceBuilder voiceBuilder = new VoiceBuilder(voice);
return voiceBuilder;
}
private Voice voice;
private String name;
private String description;
private Map<String, String> labels;
private List<File> files;
public VoiceBuilder() {
this.labels = new HashMap<>();
this.files = new ArrayList<>();
}
public VoiceBuilder(Voice voice) {
this();
this.voice = voice;
this.name = voice.getName();
this.description = voice.getDescription();
this.labels = voice.getLabels();
}
public String getName() {
return name;
}
public VoiceBuilder withName(String name) {
this.name = name;
return this;
}
public String getDescription() {
return description;
}
public VoiceBuilder withDescription(String description) {
this.description = description;
return this;
}
public Map<String, String> getLabels() {
return labels;
}
public VoiceBuilder withLabels(Map<String, String> labels) {
this.labels = labels;
return this;
}
public VoiceBuilder withLabel(String key, String value) {
this.labels.put(key, value);
return this;
}
public List<File> getFiles() {
return files;
}
public VoiceBuilder withFile(File file) {
this.files.add(file);
return this;
}
public VoiceBuilder withFiles(List<File> files) {
this.files = files;
return this;
}
public Voice create() {
CreateVoiceResponse createVoiceResponse = ElevenLabs.createVoice(name, files.toArray(File[]::new), description, labels);
return Voice.getVoice(createVoiceResponse.getVoiceId(), true);
}
public Voice edit() {
ElevenLabs.editVoice(voice.getVoiceId(), name, files.toArray(File[]::new), description, labels);
voice = voice.refresh();
return voice;
}
}

View File

@@ -2,9 +2,13 @@ package net.andrewcpu.elevenlabs.model.voice;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.model.ElevenModel;
public class VoiceSettings extends ElevenModel {
public static VoiceSettings getDefaultVoiceSettings() {
return ElevenLabs.getDefaultVoiceSettings();
}
@JsonProperty("stability")
private double stability;