Skip to content

Commit b9c74c1

Browse files
committed
Updated README, added new builders for generations.
1 parent f2f3915 commit b9c74c1

17 files changed

+372
-12
lines changed

README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ To add `elevenlabs-api` to your Maven project, use:
1313
<dependency>
1414
<groupId>net.andrewcpu</groupId>
1515
<artifactId>elevenlabs-api</artifactId>
16-
<version>2.7.2</version>
16+
<version>2.7.8</version>
1717
</dependency>
1818
```
1919
**JAR**
@@ -38,6 +38,68 @@ ElevenLabs.setDefaultModel("eleven_monolingual_v1"); // Optional, defaults to: "
3838
### **ElevenLabs API Documentation**: https://api.elevenlabs.io/docs
3939

4040
- - -
41+
42+
## Simplified Generation Handling with Builders
43+
### v2.7.8 now includes SpeechGenerationBuilder.java
44+
45+
### Speech to Speech
46+
```java
47+
//File output
48+
SpeechGenerationBuilder.speechToSpeech()
49+
.file() // output type of file (or use .streamed() for an InputStream)
50+
.setInputFile(File)
51+
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
52+
.setVoiceId("voiceIdString")
53+
.setVoiceSettings(VoiceSettings)
54+
.setVoice(Voice) // or use a voice object, which will pull settings / ID out of the Voice
55+
.setModelId("modelIdString")
56+
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
57+
.setLatencyOptimization(StreamLatencyOptimization.NONE)
58+
.build();
59+
//Streamed output
60+
SpeechGenerationBuilder.speechToSpeech()
61+
.streamed()
62+
.setInputFile(File)
63+
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
64+
.setVoiceId("voiceIdString")
65+
.setVoiceSettings(VoiceSettings)
66+
.setVoice(Voice) // or use a voice object, which will pull settings / ID out of the Voice
67+
.setModelId("modelIdString")
68+
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
69+
.setLatencyOptimization(StreamLatencyOptimization.NONE)
70+
.build();
71+
```
72+
73+
### Text to Speech
74+
```java
75+
//File output
76+
SpeechGenerationBuilder.textToSpeech()
77+
.file() // output type of file (or use .streamed() for an InputStream)
78+
.setText(String text)
79+
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
80+
.setVoiceId("voiceIdString")
81+
.setVoiceSettings(VoiceSettings)
82+
.setVoice(Voice) // or use a voice object, which will pull settings / ID out of the Voice
83+
.setModelId("modelIdString")
84+
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
85+
.setLatencyOptimization(StreamLatencyOptimization.NONE)
86+
.build();
87+
//Streamed output
88+
SpeechGenerationBuilder.textToSpeech()
89+
.streamed()
90+
.setText(String text)
91+
.setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat.MP3_44100_128)
92+
.setVoiceId("voiceIdString")
93+
.setVoiceSettings(VoiceSettings)
94+
.setVoice(Voice) // or use a voice object, which will pull settings / ID out of the Voice
95+
.setModelId("modelIdString")
96+
.setModel(ElevenLabsVoiceModel.ELEVEN_ENGLISH_STS_V2)
97+
.setLatencyOptimization(StreamLatencyOptimization.NONE)
98+
.build();
99+
```
100+
101+
- - -
102+
41103
## Voices
42104
### Accessing your List of Available Voices
43105
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.
@@ -347,6 +409,19 @@ To get your ElevenLabs generation `History`, you can utilize `History#get()`. (Y
347409
History history = History.get();
348410
```
349411

412+
413+
### Getting all History Items
414+
The History endpoint accepts page size parameters and a start-after-history-id parameter. We can use this to fetch all of our HistoryItems.
415+
```java
416+
History history = History.get(); // the latest history object
417+
Optional<History> hist = Optional.of(history);
418+
List<HistoryItem> items = new ArrayList();
419+
do {
420+
items.addAll(hist.get().getHistoryItems());
421+
hist = hist.get().next();
422+
} while(hist.isPresent() && hist.hasMore());
423+
```
424+
350425
### Getting a History Item
351426
To retrieve a `HistoryItem` from your `History`, you can use `History#getHistoryItem(String itemId)`.
352427
```java

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
<groupId>net.andrewcpu</groupId>
6262
<artifactId>elevenlabs-api</artifactId>
63-
<version>2.7.7-SNAPSHOT</version>
63+
<version>2.7.8</version>
6464

6565
<properties>
6666
<maven.compiler.source>11</maven.compiler.source>

src/main/java/net/andrewcpu/elevenlabs/api/impl/SpeechToSpeechAPI.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.andrewcpu.elevenlabs.api.impl;
22

33
import net.andrewcpu.elevenlabs.api.ElevenLabsAPI;
4+
import net.andrewcpu.elevenlabs.enums.ElevenLabsVoiceModel;
45
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
56
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
67
import net.andrewcpu.elevenlabs.requests.sts.PostSpeechToSpeechRequest;
@@ -25,4 +26,22 @@ public InputStream generateSpeechToSpeechStream(String voiceId, VoiceSettings vo
2526
public InputStream generateSpeechToSpeechStream(String voiceId, VoiceSettings voiceSettings, String modelId, File audio) {
2627
return generateSpeechToSpeechStream(voiceId, voiceSettings, modelId, audio, StreamLatencyOptimization.getDefault());
2728
}
29+
30+
31+
32+
public File generateSpeechToSpeech(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio) {
33+
return generateSpeechToSpeech(voiceId, voiceSettings, model.getModelId(), audio, StreamLatencyOptimization.getDefault());
34+
}
35+
36+
public File generateSpeechToSpeech(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio, StreamLatencyOptimization latencyOptimization) {
37+
return sendRequest(new PostSpeechToSpeechRequest(voiceId, voiceSettings,audio, model.getModelId(), latencyOptimization));
38+
}
39+
40+
public InputStream generateSpeechToSpeechStream(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio, StreamLatencyOptimization streamLatencyOptimization) {
41+
return sendRequest(new PostSpeechToSpeechStreamedRequest(voiceId, voiceSettings, audio, model.getModelId(), streamLatencyOptimization));
42+
}
43+
44+
public InputStream generateSpeechToSpeechStream(String voiceId, VoiceSettings voiceSettings, ElevenLabsVoiceModel model, File audio) {
45+
return generateSpeechToSpeechStream(voiceId, voiceSettings, model.getModelId(), audio, StreamLatencyOptimization.getDefault());
46+
}
2847
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.andrewcpu.elevenlabs.builders;
2+
3+
public class SpeechGenerationBuilder {
4+
public static SpeechToSpeechBuilder speechToSpeech() {
5+
return new SpeechToSpeechBuilder();
6+
}
7+
8+
public static TextToSpeechBuilder textToSpeech() {
9+
return new TextToSpeechBuilder();
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.andrewcpu.elevenlabs.builders;
2+
3+
import net.andrewcpu.elevenlabs.builders.impl.s2s.SpeechToSpeechFileBuilder;
4+
import net.andrewcpu.elevenlabs.builders.impl.s2s.SpeechToSpeechStreamedBuilder;
5+
6+
public class SpeechToSpeechBuilder {
7+
8+
public SpeechToSpeechStreamedBuilder streamed() {
9+
return new SpeechToSpeechStreamedBuilder();
10+
}
11+
12+
public SpeechToSpeechFileBuilder file() {
13+
return new SpeechToSpeechFileBuilder();
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.andrewcpu.elevenlabs.builders;
2+
3+
4+
import net.andrewcpu.elevenlabs.builders.impl.tts.TextToSpeechFileBuilder;
5+
import net.andrewcpu.elevenlabs.builders.impl.tts.TextToSpeechStreamedBuilder;
6+
7+
public class TextToSpeechBuilder {
8+
public TextToSpeechStreamedBuilder streamed() {
9+
return new TextToSpeechStreamedBuilder();
10+
}
11+
12+
public TextToSpeechFileBuilder file() {
13+
return new TextToSpeechFileBuilder();
14+
}
15+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package net.andrewcpu.elevenlabs.builders.abstracts;
2+
3+
import net.andrewcpu.elevenlabs.ElevenLabs;
4+
import net.andrewcpu.elevenlabs.builders.SpeechToSpeechBuilder;
5+
import net.andrewcpu.elevenlabs.builders.TextToSpeechBuilder;
6+
import net.andrewcpu.elevenlabs.enums.ElevenLabsVoiceModel;
7+
import net.andrewcpu.elevenlabs.enums.GeneratedAudioOutputFormat;
8+
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
9+
import net.andrewcpu.elevenlabs.model.voice.Voice;
10+
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
11+
12+
public abstract class AbstractSpeechGenerationBuilder<T, F> {
13+
private String voiceId;
14+
private VoiceSettings voiceSettings;
15+
private String modelId = ElevenLabs.getDefaultModel();
16+
private StreamLatencyOptimization latencyOptimization = StreamLatencyOptimization.getDefault();
17+
private GeneratedAudioOutputFormat generatedAudioOutputFormat = GeneratedAudioOutputFormat.getDefault();
18+
public abstract T self();
19+
20+
public abstract F build();
21+
22+
23+
public GeneratedAudioOutputFormat getGeneratedAudioOutputFormat() {
24+
return generatedAudioOutputFormat;
25+
}
26+
27+
public T setGeneratedAudioOutputFormat(GeneratedAudioOutputFormat generatedAudioOutputFormat) {
28+
this.generatedAudioOutputFormat = generatedAudioOutputFormat;
29+
return self();
30+
}
31+
32+
public String getVoiceId() {
33+
return voiceId;
34+
}
35+
36+
public T setVoiceId(String voiceId) {
37+
this.voiceId = voiceId;
38+
return self();
39+
}
40+
41+
public VoiceSettings getVoiceSettings() {
42+
return voiceSettings;
43+
}
44+
45+
public T setVoiceSettings(VoiceSettings voiceSettings) {
46+
this.voiceSettings = voiceSettings;
47+
return self();
48+
}
49+
50+
public String getModelId() {
51+
return modelId;
52+
}
53+
54+
public T setModelId(String modelId) {
55+
this.modelId = modelId;
56+
return self();
57+
}
58+
59+
public StreamLatencyOptimization getLatencyOptimization() {
60+
return latencyOptimization;
61+
}
62+
63+
public T setLatencyOptimization(StreamLatencyOptimization latencyOptimization) {
64+
this.latencyOptimization = latencyOptimization;
65+
return self();
66+
}
67+
68+
public T setVoice(Voice voice) {
69+
this.voiceId = voice.getVoiceId();
70+
if(voice.getSettings() == null) voice.fetchSettings();
71+
this.voiceSettings = voice.getSettings();
72+
return self();
73+
}
74+
75+
public T setModel(ElevenLabsVoiceModel voiceModel) {
76+
this.modelId = voiceModel.getModelId();
77+
return self();
78+
}
79+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.andrewcpu.elevenlabs.builders.impl.s2s;
2+
3+
import net.andrewcpu.elevenlabs.ElevenLabs;
4+
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
5+
6+
import java.io.File;
7+
8+
public class SpeechToSpeechFileBuilder extends AbstractSpeechGenerationBuilder<SpeechToSpeechFileBuilder, File> {
9+
private File inputFile;
10+
11+
public File getInputFile() {
12+
return inputFile;
13+
}
14+
15+
public SpeechToSpeechFileBuilder setInputFile(File inputFile) {
16+
this.inputFile = inputFile;
17+
return this;
18+
}
19+
20+
@Override
21+
public SpeechToSpeechFileBuilder self() {
22+
return this;
23+
}
24+
25+
@Override
26+
public File build() {
27+
return ElevenLabs
28+
.getSpeechToSpeechAPI()
29+
.generateSpeechToSpeech(getVoiceId(), getVoiceSettings(),getModelId(),getInputFile(),getLatencyOptimization());
30+
}
31+
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.andrewcpu.elevenlabs.builders.impl.s2s;
2+
3+
import net.andrewcpu.elevenlabs.ElevenLabs;
4+
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
5+
6+
import java.io.File;
7+
import java.io.InputStream;
8+
9+
public class SpeechToSpeechStreamedBuilder extends AbstractSpeechGenerationBuilder<SpeechToSpeechStreamedBuilder, InputStream> {
10+
private File inputFile;
11+
12+
public File getInputFile() {
13+
return inputFile;
14+
}
15+
16+
public SpeechToSpeechStreamedBuilder setInputFile(File inputFile) {
17+
this.inputFile = inputFile;
18+
return this;
19+
}
20+
21+
@Override
22+
public SpeechToSpeechStreamedBuilder self() {
23+
return this;
24+
}
25+
26+
@Override
27+
public InputStream build() {
28+
return ElevenLabs
29+
.getSpeechToSpeechAPI()
30+
.generateSpeechToSpeechStream(getVoiceId(), getVoiceSettings(),getModelId(),getInputFile(),getLatencyOptimization());
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.andrewcpu.elevenlabs.builders.impl.tts;
2+
3+
import net.andrewcpu.elevenlabs.ElevenLabs;
4+
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
5+
6+
import java.io.File;
7+
8+
public class TextToSpeechFileBuilder extends AbstractSpeechGenerationBuilder<TextToSpeechFileBuilder, File> {
9+
private String text;
10+
11+
public String getText() {
12+
return text;
13+
}
14+
15+
public TextToSpeechFileBuilder setText(String text) {
16+
this.text = text;
17+
return this;
18+
}
19+
20+
@Override
21+
public TextToSpeechFileBuilder self() {
22+
return this;
23+
}
24+
25+
@Override
26+
public File build() {
27+
return ElevenLabs
28+
.getTextToSpeechAPI()
29+
.generateTextToSpeech(getVoiceId(), getText(),getModelId(), getGeneratedAudioOutputFormat(),getLatencyOptimization(), getVoiceSettings());
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.andrewcpu.elevenlabs.builders.impl.tts;
2+
3+
import net.andrewcpu.elevenlabs.ElevenLabs;
4+
import net.andrewcpu.elevenlabs.builders.abstracts.AbstractSpeechGenerationBuilder;
5+
6+
import java.io.InputStream;
7+
8+
public class TextToSpeechStreamedBuilder extends AbstractSpeechGenerationBuilder<TextToSpeechStreamedBuilder, InputStream> {
9+
private String text;
10+
11+
public String getText() {
12+
return text;
13+
}
14+
15+
public TextToSpeechStreamedBuilder setText(String text) {
16+
this.text = text;
17+
return this;
18+
}
19+
20+
@Override
21+
public TextToSpeechStreamedBuilder self() {
22+
return this;
23+
}
24+
25+
@Override
26+
public InputStream build() {
27+
return ElevenLabs
28+
.getTextToSpeechAPI()
29+
.generateTextToSpeechStreamed(getVoiceId(), getText(),getModelId(), getGeneratedAudioOutputFormat(),getLatencyOptimization(), getVoiceSettings());
30+
}
31+
}

0 commit comments

Comments
 (0)