6 Commits

Author SHA1 Message Date
Andrewcpu
fa4c8bf5db Removed speceh to speech documentation until ElevenLabs publishes official API 2023-12-25 01:04:30 -05:00
Andrew Stein
96fc45920d Merge remote-tracking branch 'origin/main'
# Conflicts:
#	README.md
2023-11-29 14:03:15 -05:00
Andrew Stein
b9c74c1bd7 Updated README, added new builders for generations. 2023-11-29 14:01:41 -05:00
Andrew Stein
f2f3915c6c Added enum of available voice models (that I know of) with their display name and value. 2023-11-29 13:40:00 -05:00
Andrew Stein
79e570f989 Added support for paginated HistoryItems. 2023-11-29 13:39:37 -05:00
Andrew Stein
0a4a221b5c Added support for paginated HistoryItems. 2023-11-29 13:39:34 -05:00
19 changed files with 487 additions and 17 deletions

134
README.md
View File

@@ -15,7 +15,7 @@ To add `elevenlabs-api` to your Maven project, use:
<dependency>
<groupId>net.andrewcpu</groupId>
<artifactId>elevenlabs-api</artifactId>
<version>2.7.7</version>
<version>2.7.8</version>
</dependency>
```
**JAR**
@@ -40,6 +40,94 @@ ElevenLabs.setDefaultModel("eleven_monolingual_v1"); // Optional, defaults to: "
### **ElevenLabs API Documentation**: https://api.elevenlabs.io/docs
- - -
## Simplified Generation Handling with Builders
### v2.7.8 now includes SpeechGenerationBuilder.java
[//]: # (### Speech to Speech)
[//]: # (```java)
[//]: # (//File output)
[//]: # (SpeechGenerationBuilder.speechToSpeech&#40;&#41;)
[//]: # ( .file&#40;&#41; // output type of file &#40;or use .streamed&#40;&#41; for an InputStream&#41;)
[//]: # ( .setInputFile&#40;File&#41;)
[//]: # ( .setGeneratedAudioOutputFormat&#40;GeneratedAudioOutputFormat.MP3_44100_128&#41;)
[//]: # ( .setVoiceId&#40;"voiceIdString"&#41;)
[//]: # ( .setVoiceSettings&#40;VoiceSettings&#41;)
[//]: # ( .setVoice&#40;Voice&#41; // or use a voice object, which will pull settings / ID out of the Voice)
[//]: # ( .setModelId&#40;"modelIdString"&#41;)
[//]: # ( .setModel&#40;ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2&#41;)
[//]: # ( .setLatencyOptimization&#40;StreamLatencyOptimization.NONE&#41;)
[//]: # ( .build&#40;&#41;;)
[//]: # (//Streamed output)
[//]: # (SpeechGenerationBuilder.speechToSpeech&#40;&#41;)
[//]: # ( .streamed&#40;&#41;)
[//]: # ( .setInputFile&#40;File&#41;)
[//]: # ( .setGeneratedAudioOutputFormat&#40;GeneratedAudioOutputFormat.MP3_44100_128&#41;)
[//]: # ( .setVoiceId&#40;"voiceIdString"&#41;)
[//]: # ( .setVoiceSettings&#40;VoiceSettings&#41;)
[//]: # ( .setVoice&#40;Voice&#41; // or use a voice object, which will pull settings / ID out of the Voice)
[//]: # ( .setModelId&#40;"modelIdString"&#41;)
[//]: # ( .setModel&#40;ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2&#41;)
[//]: # ( .setLatencyOptimization&#40;StreamLatencyOptimization.NONE&#41;)
[//]: # ( .build&#40;&#41;;)
[//]: # (```)
### Text to Speech
```java
//File output
SpeechGenerationBuilder.textToSpeech()
.file() // output type of file (or use .streamed() for an InputStream)
.setText(String text)
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
.setVoiceId("voiceIdString")
.setVoiceSettings(VoiceSettings)
.setVoice(Voice) // or use a voice object, which will pull settings / ID out of the Voice
.setModelId("modelIdString")
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
.setLatencyOptimization(StreamLatencyOptimization.NONE)
.build();
//Streamed output
SpeechGenerationBuilder.textToSpeech()
.streamed()
.setText(String text)
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
.setVoiceId("voiceIdString")
.setVoiceSettings(VoiceSettings)
.setVoice(Voice) // or use a voice object, which will pull settings / ID out of the Voice
.setModelId("modelIdString")
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
.setLatencyOptimization(StreamLatencyOptimization.NONE)
.build();
```
- - -
## Voices
### Accessing your List of Available Voices
To retrieve your list of accessible Voices, you can statically utilize `Voice#getVoices()`. This will return both ElevenLab's pregenerated `Voice` models, as well as any personal `Voices` you have generated.
@@ -178,19 +266,26 @@ public InputStream generateStream(String text, VoiceSettings settings, StreamLat
public InputStream generateStream(String text, StreamLatencyOptimization streamLatencyOptimization);
public File speechToSpeech(File audioFile, StreamLatencyOptimization latencyOptimization, String modelId, VoiceSettings voiceSettings);
public File speechToSpeech(File audioFile, StreamLatencyOptimization latencyOptimization, String modelId);
public File speechToSpeech(File audioFile, String modelId);
public InputStream speechToSpeechStream(File audioFile, StreamLatencyOptimization latencyOptimization, String modelId, VoiceSettings voiceSettings);
public InputStream speechToSpeechStream(File audioFile, StreamLatencyOptimization latencyOptimization, String modelId);
public InputStream speechToSpeechStream(File audioFile, String modelId);
```
[//]: # (public File speechToSpeech&#40;File audioFile, StreamLatencyOptimization latencyOptimization, String modelId, VoiceSettings voiceSettings&#41;;)
[//]: # ()
[//]: # (public File speechToSpeech&#40;File audioFile, StreamLatencyOptimization latencyOptimization, String modelId&#41;;)
[//]: # ()
[//]: # (public File speechToSpeech&#40;File audioFile, String modelId&#41;;)
[//]: # ()
[//]: # (public InputStream speechToSpeechStream&#40;File audioFile, StreamLatencyOptimization latencyOptimization, String modelId, VoiceSettings voiceSettings&#41;;)
[//]: # ()
[//]: # (public InputStream speechToSpeechStream&#40;File audioFile, StreamLatencyOptimization latencyOptimization, String modelId&#41;;)
[//]: # ()
[//]: # (public InputStream speechToSpeechStream&#40;File audioFile, String modelId&#41;;)
- - -
## Audio Native Projects
### Creating an Audio Native Project
@@ -349,6 +444,19 @@ To get your ElevenLabs generation `History`, you can utilize `History#get()`. (Y
History history = History.get();
```
### Getting all History Items
The History endpoint accepts page size parameters and a start-after-history-id parameter. We can use this to fetch all of our HistoryItems.
```java
History history = History.get(); // the latest history object
Optional<History> hist = Optional.of(history);
List<HistoryItem> items = new ArrayList();
do {
items.addAll(hist.get().getHistoryItems());
hist = hist.get().next();
} while(hist.isPresent() && hist.hasMore());
```
### Getting a History Item
To retrieve a `HistoryItem` from your `History`, you can use `History#getHistoryItem(String itemId)`.
```java

View File

@@ -60,7 +60,7 @@
<groupId>net.andrewcpu</groupId>
<artifactId>elevenlabs-api</artifactId>
<version>2.7.7-SNAPSHOT</version>
<version>2.7.8</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>

View File

@@ -13,6 +13,14 @@ public class HistoryAPI extends ElevenLabsAPI {
return sendRequest(new GetHistoryRequest());
}
public History getHistory(int pageSize, String afterHistoryId) {
return sendRequest(new GetHistoryRequest(pageSize, afterHistoryId));
}
public History getHistory(String afterHistoryId) {
return sendRequest(new GetHistoryRequest(afterHistoryId));
}
public HistoryItem getHistoryItem(String historyItemId) {
return sendRequest(new GetHistoryItemByIdRequest(historyItemId));
}

View File

@@ -1,6 +1,7 @@
package net.andrewcpu.elevenlabs.api.impl;
import net.andrewcpu.elevenlabs.api.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.enums.ElevenLabsVoiceModel;
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.requests.sts.PostSpeechToSpeechRequest;
@@ -25,4 +26,22 @@ public class SpeechToSpeechAPI extends ElevenLabsAPI {
public InputStream generateSpeechToSpeechStream(String voiceId, VoiceSettings voiceSettings, String modelId, File audio) {
return generateSpeechToSpeechStream(voiceId, voiceSettings, modelId, audio, StreamLatencyOptimization.getDefault());
}
public File generateSpeechToSpeech(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio) {
return generateSpeechToSpeech(voiceId, voiceSettings, model.getModelId(), audio, StreamLatencyOptimization.getDefault());
}
public File generateSpeechToSpeech(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio, StreamLatencyOptimization latencyOptimization) {
return sendRequest(new PostSpeechToSpeechRequest(voiceId, voiceSettings,audio, model.getModelId(), latencyOptimization));
}
public InputStream generateSpeechToSpeechStream(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio, StreamLatencyOptimization streamLatencyOptimization) {
return sendRequest(new PostSpeechToSpeechStreamedRequest(voiceId, voiceSettings, audio, model.getModelId(), streamLatencyOptimization));
}
public InputStream generateSpeechToSpeechStream(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio) {
return generateSpeechToSpeechStream(voiceId, voiceSettings, model.getModelId(), audio, StreamLatencyOptimization.getDefault());
}
}

View File

@@ -0,0 +1,11 @@
package net.andrewcpu.elevenlabs.builders;
public class SpeechGenerationBuilder {
public static SpeechToSpeechBuilder speechToSpeech() {
return new SpeechToSpeechBuilder();
}
public static TextToSpeechBuilder textToSpeech() {
return new TextToSpeechBuilder();
}
}

View File

@@ -0,0 +1,15 @@
package net.andrewcpu.elevenlabs.builders;
import net.andrewcpu.elevenlabs.builders.impl.s2s.SpeechToSpeechFileBuilder;
import net.andrewcpu.elevenlabs.builders.impl.s2s.SpeechToSpeechStreamedBuilder;
public class SpeechToSpeechBuilder {
public SpeechToSpeechStreamedBuilder streamed() {
return new SpeechToSpeechStreamedBuilder();
}
public SpeechToSpeechFileBuilder file() {
return new SpeechToSpeechFileBuilder();
}
}

View File

@@ -0,0 +1,15 @@
package net.andrewcpu.elevenlabs.builders;
import net.andrewcpu.elevenlabs.builders.impl.tts.TextToSpeechFileBuilder;
import net.andrewcpu.elevenlabs.builders.impl.tts.TextToSpeechStreamedBuilder;
public class TextToSpeechBuilder {
public TextToSpeechStreamedBuilder streamed() {
return new TextToSpeechStreamedBuilder();
}
public TextToSpeechFileBuilder file() {
return new TextToSpeechFileBuilder();
}
}

View File

@@ -0,0 +1,79 @@
package net.andrewcpu.elevenlabs.builders.abstracts;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.builders.SpeechToSpeechBuilder;
import net.andrewcpu.elevenlabs.builders.TextToSpeechBuilder;
import net.andrewcpu.elevenlabs.enums.ElevenLabsVoiceModel;
import net.andrewcpu.elevenlabs.enums.GeneratedAudioOutputFormat;
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
import net.andrewcpu.elevenlabs.model.voice.Voice;
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
public abstract class AbstractSpeechGenerationBuilder<T, F> {
private String voiceId;
private VoiceSettings voiceSettings;
private String modelId = ElevenLabs.getDefaultModel();
private StreamLatencyOptimization latencyOptimization = StreamLatencyOptimization.getDefault();
private GeneratedAudioOutputFormat generatedAudioOutputFormat = GeneratedAudioOutputFormat.getDefault();
public abstract T self();
public abstract F build();
public GeneratedAudioOutputFormat getGeneratedAudioOutputFormat() {
return generatedAudioOutputFormat;
}
public T setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat generatedAudioOutputFormat) {
this.generatedAudioOutputFormat = generatedAudioOutputFormat;
return self();
}
public String getVoiceId() {
return voiceId;
}
public T setVoiceId(String voiceId) {
this.voiceId = voiceId;
return self();
}
public VoiceSettings getVoiceSettings() {
return voiceSettings;
}
public T setVoiceSettings(VoiceSettings voiceSettings) {
this.voiceSettings = voiceSettings;
return self();
}
public String getModelId() {
return modelId;
}
public T setModelId(String modelId) {
this.modelId = modelId;
return self();
}
public StreamLatencyOptimization getLatencyOptimization() {
return latencyOptimization;
}
public T setLatencyOptimization(StreamLatencyOptimization latencyOptimization) {
this.latencyOptimization = latencyOptimization;
return self();
}
public T setVoice(Voice voice) {
this.voiceId = voice.getVoiceId();
if(voice.getSettings() == null) voice.fetchSettings();
this.voiceSettings = voice.getSettings();
return self();
}
public T setModel(ElevenLabsVoiceModel voiceModel) {
this.modelId = voiceModel.getModelId();
return self();
}
}

View File

@@ -0,0 +1,32 @@
package net.andrewcpu.elevenlabs.builders.impl.s2s;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
import java.io.File;
public class SpeechToSpeechFileBuilder extends AbstractSpeechGenerationBuilder<SpeechToSpeechFileBuilder, File> {
private File inputFile;
public File getInputFile() {
return inputFile;
}
public SpeechToSpeechFileBuilder setInputFile(File inputFile) {
this.inputFile = inputFile;
return this;
}
@Override
public SpeechToSpeechFileBuilder self() {
return this;
}
@Override
public File build() {
return ElevenLabs
.getSpeechToSpeechAPI()
.generateSpeechToSpeech(getVoiceId(), getVoiceSettings(),getModelId(),getInputFile(),getLatencyOptimization());
}
}

View File

@@ -0,0 +1,32 @@
package net.andrewcpu.elevenlabs.builders.impl.s2s;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
import java.io.File;
import java.io.InputStream;
public class SpeechToSpeechStreamedBuilder extends AbstractSpeechGenerationBuilder<SpeechToSpeechStreamedBuilder, InputStream> {
private File inputFile;
public File getInputFile() {
return inputFile;
}
public SpeechToSpeechStreamedBuilder setInputFile(File inputFile) {
this.inputFile = inputFile;
return this;
}
@Override
public SpeechToSpeechStreamedBuilder self() {
return this;
}
@Override
public InputStream build() {
return ElevenLabs
.getSpeechToSpeechAPI()
.generateSpeechToSpeechStream(getVoiceId(), getVoiceSettings(),getModelId(),getInputFile(),getLatencyOptimization());
}
}

View File

@@ -0,0 +1,31 @@
package net.andrewcpu.elevenlabs.builders.impl.tts;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
import java.io.File;
public class TextToSpeechFileBuilder extends AbstractSpeechGenerationBuilder<TextToSpeechFileBuilder, File> {
private String text;
public String getText() {
return text;
}
public TextToSpeechFileBuilder setText(String text) {
this.text = text;
return this;
}
@Override
public TextToSpeechFileBuilder self() {
return this;
}
@Override
public File build() {
return ElevenLabs
.getTextToSpeechAPI()
.generateTextToSpeech(getVoiceId(), getText(),getModelId(), getGeneratedAudioOutputFormat(),getLatencyOptimization(), getVoiceSettings());
}
}

View File

@@ -0,0 +1,31 @@
package net.andrewcpu.elevenlabs.builders.impl.tts;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
import java.io.InputStream;
public class TextToSpeechStreamedBuilder extends AbstractSpeechGenerationBuilder<TextToSpeechStreamedBuilder, InputStream> {
private String text;
public String getText() {
return text;
}
public TextToSpeechStreamedBuilder setText(String text) {
this.text = text;
return this;
}
@Override
public TextToSpeechStreamedBuilder self() {
return this;
}
@Override
public InputStream build() {
return ElevenLabs
.getTextToSpeechAPI()
.generateTextToSpeechStreamed(getVoiceId(), getText(),getModelId(), getGeneratedAudioOutputFormat(),getLatencyOptimization(), getVoiceSettings());
}
}

View File

@@ -0,0 +1,32 @@
package net.andrewcpu.elevenlabs.enums;
public enum ElevenLabsVoiceModel {
ELEVEN_MULTILINGUAL_V2("eleven_multilingual_v2", "Eleven Multilingual v2"),
ELEVEN_ENGLISH_V2("eleven_english_v2", "Eleven English v2"),
ELEVEN_MULTILINGUAL_V1("eleven_multilingual_v1", "Eleven Multilingual v1"),
ELEVEN_MONOLINGUAL_V1("eleven_monolingual_v1", "Eleven English v1"),
ELEVEN_TURBO_V2("eleven_turbo_v2", "Eleven Turbo v2"),
ELEVEN_ENGLISH_STS_V2("eleven_english_sts_v2", "Eleven English v2", true),
;
final String modelId;
final String modelLabel;
final boolean supportsSpeechToSpeech;
ElevenLabsVoiceModel(String modelId, String modelLabel, boolean supportsSpeechToSpeech) {
this.modelId = modelId;
this.modelLabel = modelLabel;
this.supportsSpeechToSpeech = supportsSpeechToSpeech;
}
ElevenLabsVoiceModel(String modelId, String modelLabel) {
this(modelId, modelLabel, false);
}
public String getModelId() {
return modelId;
}
public String getModelLabel() {
return modelLabel;
}
}

View File

@@ -8,12 +8,25 @@ import net.andrewcpu.elevenlabs.model.ElevenModel;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class History extends ElevenModel {
public static final int DEFAULT_HISTORY_PAGE_SIZE = 100;
public static History get() {
return ElevenLabs.getHistoryAPI().getHistory();
}
public static History get(String afterHistoryId) {
return ElevenLabs.getHistoryAPI().getHistory(afterHistoryId);
}
public static History get(int pageSize, String afterHistoryId) {
return ElevenLabs.getHistoryAPI().getHistory(pageSize, afterHistoryId);
}
public static History get(int pageSize) {
return ElevenLabs.getHistoryAPI().getHistory(pageSize, null);
}
@JsonProperty("history")
private List<HistoryItem> historyItems;
@@ -64,6 +77,13 @@ public class History extends ElevenModel {
return ElevenLabs.getHistoryAPI().getHistoryItemAudio(Arrays.stream(items).map(HistoryItem::getHistoryItemId).toArray(String[]::new));
}
public Optional<History> next(int pageSize) {
if(!hasMore) {
return Optional.empty();
}
return Optional.of(ElevenLabs.getHistoryAPI().getHistory(pageSize, lastHistoryItemId));
}
@JsonIgnore
@Override
public String toString() {

View File

@@ -3,8 +3,36 @@ package net.andrewcpu.elevenlabs.requests.history;
import net.andrewcpu.elevenlabs.model.history.History;
import net.andrewcpu.elevenlabs.requests.GetRequest;
import java.util.HashMap;
import java.util.Map;
import static net.andrewcpu.elevenlabs.model.history.History.DEFAULT_HISTORY_PAGE_SIZE;
public class GetHistoryRequest extends GetRequest<History> {
private final int pageSize;
private final String startAfterHistoryId;
public GetHistoryRequest() {
this(DEFAULT_HISTORY_PAGE_SIZE, null);
}
public GetHistoryRequest(String startAfterHistoryId) {
this(DEFAULT_HISTORY_PAGE_SIZE, startAfterHistoryId);
}
public GetHistoryRequest(int pageSize, String startAfterHistoryId) {
super("v1/history", History.class);
this.pageSize = pageSize;
this.startAfterHistoryId = startAfterHistoryId;
}
@Override
public Map<String, String> getQueryParameters() {
Map<String, String> params = new HashMap<>();
params.put("page_size", String.valueOf(pageSize));
if(startAfterHistoryId != null) {
params.put("start_after_history_item_id", startAfterHistoryId);
}
return params;
}
}

View File

@@ -4,7 +4,6 @@ import net.andrewcpu.elevenlabs.model.user.Subscription;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetSubscriptionRequest extends GetRequest<Subscription> {
public GetSubscriptionRequest() {
super("v1/user/subscription", Subscription.class);
}

View File

@@ -1,6 +1,12 @@
package net.andrewcpu.elevenlabs;
import net.andrewcpu.elevenlabs.builders.SpeechGenerationBuilder;
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
import org.junit.Test;
import java.io.File;
public class ElevenLabsTest {
public static final String ELEVEN_LABS_API_KEY = System.getenv("ELEVENLABS_API_KEY");
public static final String TEST_VOICE = "ZjJOFdM86g4E9U6OhzUo";

View File

@@ -8,7 +8,9 @@ import org.junit.Test;
import java.io.File;
import java.util.List;
import java.util.Optional;
import static net.andrewcpu.elevenlabs.model.history.History.DEFAULT_HISTORY_PAGE_SIZE;
import static org.junit.Assert.*;
public class HistoryTest extends ElevenLabsTest {
@@ -44,5 +46,7 @@ public class HistoryTest extends ElevenLabsTest {
output.delete();
}));
Optional<History> nextHistory = history.next(DEFAULT_HISTORY_PAGE_SIZE);
assertTrue(nextHistory.isPresent());
}
}

View File

@@ -18,7 +18,7 @@ public class AvailableModelsTest extends ElevenLabsTest {
GenerationTypeModel[] models = null;
try{
models = ElevenLabs.getModelsAPI().getAvailableModels();
System.out.println(Arrays.stream(models).map(Object::toString).collect(Collectors.toList()));
Arrays.stream(models).map(o -> o.getModelId() + "," + o.getName()).forEach(System.out::println);
}catch (Exception e) {
e.printStackTrace();
fail("Failed to get voice type models: " + e.getMessage());