9 Commits
v42 ... v50

Author SHA1 Message Date
Andrew Stein
388d059af7 Updated requests / models with new voice parameters. Preparing for Projects integration 2023-11-15 14:34:49 -05:00
Andrew Stein
1f9c48eac6 Merge remote-tracking branch 'origin/main' 2023-09-03 13:55:48 -04:00
Andrew Stein
b3ea0e2a09 Updated POM.xml 2023-09-03 13:55:35 -04:00
Andrew Stein
75eb64a077 Update README.md 2023-09-03 13:55:18 -04:00
Andrew Stein
c5706317aa Updated build name. 2023-06-11 19:36:05 -04:00
Andrew Stein
0a2f6d1869 Reorganized files. 2023-06-11 19:35:13 -04:00
Andrew Stein
b753769a53 Updated POM version. 2023-06-11 19:33:09 -04:00
Andrew Stein
8162cada71 Reorganized directories 2023-06-11 19:32:49 -04:00
Andrew Stein
fdcc12ee41 Updated README 2023-06-11 19:07:28 -04:00
45 changed files with 577 additions and 178 deletions

View File

@@ -1,4 +1,4 @@
name: Dev Build
name: Build
on:
push:

View File

@@ -14,9 +14,9 @@ I downgraded the repo from JDK 17 to JDK 11 as well.
To add `elevenlabs-api` to your Maven project, use:
```xml
<dependency>
<groupId>net.andrewcpu.elevenlabs</groupId>
<groupId>net.andrewcpu</groupId>
<artifactId>elevenlabs-api</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.1</version>
</dependency>
```
The most up-to date package information can be found on the [Packages tab](https://github.com/AndrewCPU/elevenlabs-api/packages/)
@@ -279,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 🥂

95
pom.xml
View File

@@ -6,6 +6,7 @@
<name>Unofficial Java ElevenLabs Voice API</name>
<description>An API level interaction between Java and the ElevenLabs Voice Generation Web API.</description>
<url>https://github.com/AndrewCPU/elevenlabs-api</url>
<developers>
<developer>
<id>Andrewcpu</id>
@@ -24,17 +25,42 @@
<tag>HEAD</tag>
</scm>
<distributionManagement>
<repository>
<profiles>
<profile>
<id>github</id>
<name>GitHub AndrewCPU Apache Maven Packages</name>
<url>https://maven.pkg.github.com/AndrewCPU/elevenlabs-api</url>
</repository>
</distributionManagement>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub AndrewCPU Apache Maven Packages</name>
<url>https://maven.pkg.github.com/AndrewCPU/elevenlabs-api</url>
</repository>
</distributionManagement>
</profile>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<distributionManagement>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</profile>
</profiles>
<groupId>net.andrewcpu</groupId>
<artifactId>elevenlabs-api</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.2</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
@@ -94,6 +120,20 @@
<build>
<finalName>elevenlabs-api-${version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
@@ -114,6 +154,47 @@
</execution>
</executions>
</plugin>
<!-- Generates JAR's source file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<!-- Generates Javadoc JAR file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,5 +1,7 @@
package net.andrewcpu.elevenlabs;
import net.andrewcpu.elevenlabs.enums.GeneratedAudioOutputFormat;
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
import net.andrewcpu.elevenlabs.model.history.History;
import net.andrewcpu.elevenlabs.model.history.HistoryItem;
import net.andrewcpu.elevenlabs.model.request.TextToSpeechRequest;
@@ -9,16 +11,16 @@ import net.andrewcpu.elevenlabs.model.user.Subscription;
import net.andrewcpu.elevenlabs.model.user.User;
import net.andrewcpu.elevenlabs.model.voice.Voice;
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.net.ElevenRequest;
import net.andrewcpu.elevenlabs.net.history.*;
import net.andrewcpu.elevenlabs.net.models.GetModelsRequest;
import net.andrewcpu.elevenlabs.net.samples.DeleteSampleRequest;
import net.andrewcpu.elevenlabs.net.samples.GetSampleRequest;
import net.andrewcpu.elevenlabs.net.tts.PostTextToSpeechRequest;
import net.andrewcpu.elevenlabs.net.tts.PostTextToSpeechStreamedRequest;
import net.andrewcpu.elevenlabs.net.user.GetSubscriptionRequest;
import net.andrewcpu.elevenlabs.net.user.GetUserRequest;
import net.andrewcpu.elevenlabs.net.voices.*;
import net.andrewcpu.elevenlabs.requests.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.requests.history.*;
import net.andrewcpu.elevenlabs.requests.models.GetModelsRequest;
import net.andrewcpu.elevenlabs.requests.samples.DeleteSampleRequest;
import net.andrewcpu.elevenlabs.requests.samples.GetSampleRequest;
import net.andrewcpu.elevenlabs.requests.tts.PostTextToSpeechRequest;
import net.andrewcpu.elevenlabs.requests.tts.PostTextToSpeechStreamedRequest;
import net.andrewcpu.elevenlabs.requests.user.GetSubscriptionRequest;
import net.andrewcpu.elevenlabs.requests.user.GetUserRequest;
import net.andrewcpu.elevenlabs.requests.voices.*;
import net.andrewcpu.elevenlabs.util.ElevenNetworkUtil;
import java.io.File;
@@ -37,7 +39,7 @@ public class ElevenLabs {
API_KEY = apiKey;
}
private static <T> T sendRequest(ElevenRequest<T> request) {
private static <T> T sendRequest(ElevenLabsRequest<T> request) {
return ElevenNetworkUtil.sendRequest(request.getType(),request.getEndpoint(), request.getPayload(),request.getResponseClass());
}
@@ -120,6 +122,13 @@ public class ElevenLabs {
return sendRequest(new PostTextToSpeechRequest(voiceId, new TextToSpeechRequest(text, modelId, voiceSettings)));
}
public static File generateTextToSpeech(String voiceId, String text, String modelId, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization, VoiceSettings voiceSettings) {
return sendRequest(new PostTextToSpeechRequest(voiceId, new TextToSpeechRequest(text, modelId, voiceSettings), streamLatencyOptimization, outputFormat));
}
public static InputStream generateTextToSpeechStreamed(String voiceId, String text, String modelId, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization, VoiceSettings voiceSettings) {
return sendRequest(new PostTextToSpeechStreamedRequest(voiceId, new TextToSpeechRequest(text, modelId, voiceSettings), streamLatencyOptimization, outputFormat));
}
public static InputStream generateTextToSpeechStreamed(String voiceId, String text, String modelId, VoiceSettings voiceSettings) {
return sendRequest(new PostTextToSpeechStreamedRequest(voiceId, new TextToSpeechRequest(text, modelId, voiceSettings)));
}

View File

@@ -0,0 +1,19 @@
package net.andrewcpu.elevenlabs.enums;
public enum GeneratedAudioOutputFormat {
MP3_44100_64,
MP3_44100_96,
MP3_44100_128,
MP3_44100_192,
PCM_16000,
PCM_22050,
PCM_24000,
PCM_44100,
ULAW_8000;
// Method to get the default value
public static GeneratedAudioOutputFormat getDefault() {
return MP3_44100_128;
}
}

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs;
package net.andrewcpu.elevenlabs.enums;
public enum HttpRequestType {
POST, GET, PUT, DELETE;

View File

@@ -0,0 +1,27 @@
package net.andrewcpu.elevenlabs.enums;
public enum StreamLatencyOptimization {
//0 - default mode (no latency optimizations)
NONE(0),
//1 - normal latency optimizations (about 50% of possible latency improvement of option 3)
NORMAL(1),
//2 - strong latency optimizations (about 75% of possible latency improvement of option 3)
STRONG(2),
// 3 - max latency optimizations
MAX_TEXT_NORMALIZATION(3),
// 3 - max latency optimizations with text normalizer turned off
MAX_NO_TEXT_NORMALIZATION(4);
private final int value;
StreamLatencyOptimization(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static StreamLatencyOptimization getDefault() {
return NONE;
}
}

View File

@@ -24,23 +24,46 @@ public class GenerationTypeModel extends ElevenModel {
@JsonProperty("can_do_voice_conversion")
private boolean canDoVoiceConversion;
@JsonProperty("can_use_style")
private boolean canUseStyle;
@JsonProperty("can_use_speaker_boost")
private boolean canUseSpeakerBoost;
@JsonProperty("serves_pro_voices")
private boolean servesProVoices;
@JsonProperty("token_cost_factor")
private int tokenCostFactor;
@JsonProperty("description")
private String description;
@JsonProperty("requires_alpha_access")
private boolean requiresAlphaAccess;
@JsonProperty("max_characters_request_free_user")
private int maxCharactersRequestFreeUser;
@JsonProperty("max_characters_request_subscribed_user")
private int maxCharactersRequestSubscribedUser;
@JsonProperty("languages")
private List<Language> languages;
public GenerationTypeModel(String modelId, String name, boolean canBeFinetuned, boolean canDoTextToSpeech, boolean canDoVoiceConversion, int tokenCostFactor, String description, List<Language> languages) {
public GenerationTypeModel(String modelId, String name, boolean canBeFinetuned, boolean canDoTextToSpeech, boolean canDoVoiceConversion, boolean canUseStyle, boolean canUseSpeakerBoost, boolean servesProVoices, int tokenCostFactor, String description, boolean requiresAlphaAccess, int maxCharactersRequestFreeUser, int maxCharactersRequestSubscribedUser, List<Language> languages) {
this.modelId = modelId;
this.name = name;
this.canBeFinetuned = canBeFinetuned;
this.canDoTextToSpeech = canDoTextToSpeech;
this.canDoVoiceConversion = canDoVoiceConversion;
this.canUseStyle = canUseStyle;
this.canUseSpeakerBoost = canUseSpeakerBoost;
this.servesProVoices = servesProVoices;
this.tokenCostFactor = tokenCostFactor;
this.description = description;
this.requiresAlphaAccess = requiresAlphaAccess;
this.maxCharactersRequestFreeUser = maxCharactersRequestFreeUser;
this.maxCharactersRequestSubscribedUser = maxCharactersRequestSubscribedUser;
this.languages = languages;
}
@@ -87,17 +110,54 @@ public class GenerationTypeModel extends ElevenModel {
return languages;
}
@JsonIgnore
public boolean isCanUseStyle() {
return canUseStyle;
}
@JsonIgnore
public boolean isCanUseSpeakerBoost() {
return canUseSpeakerBoost;
}
@JsonIgnore
public boolean isServesProVoices() {
return servesProVoices;
}
@JsonIgnore
public boolean isRequiresAlphaAccess() {
return requiresAlphaAccess;
}
@JsonIgnore
public int getMaxCharactersRequestFreeUser() {
return maxCharactersRequestFreeUser;
}
@JsonIgnore
public int getMaxCharactersRequestSubscribedUser() {
return maxCharactersRequestSubscribedUser;
}
@Override
@JsonIgnore
public String toString() {
return "ModelResponse{" +
return "GenerationTypeModel{" +
"modelId='" + modelId + '\'' +
", name='" + name + '\'' +
", canBeFinetuned=" + canBeFinetuned +
", canDoTextToSpeech=" + canDoTextToSpeech +
", canDoVoiceConversion=" + canDoVoiceConversion +
", canUseStyle=" + canUseStyle +
", canUseSpeakerBoost=" + canUseSpeakerBoost +
", servesProVoices=" + servesProVoices +
", tokenCostFactor=" + tokenCostFactor +
", description='" + description + '\'' +
", requiresAlphaAccess=" + requiresAlphaAccess +
", maxCharactersRequestFreeUser=" + maxCharactersRequestFreeUser +
", maxCharactersRequestSubscribedUser=" + maxCharactersRequestSubscribedUser +
", languages=" + languages +
'}';
}

View File

@@ -3,6 +3,8 @@ 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.enums.GeneratedAudioOutputFormat;
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
import net.andrewcpu.elevenlabs.model.ElevenModel;
import net.andrewcpu.elevenlabs.model.tuning.FineTuning;
@@ -58,6 +60,9 @@ public class Voice extends ElevenModel {
@JsonProperty("sharing")
private Sharing sharing;
@JsonProperty("high_quality_base_model_ids")
private List<String> highQualityBaseModelIds;
@JsonIgnore
public String getVoiceId() {
return voiceId;
@@ -143,6 +148,7 @@ public class Voice extends ElevenModel {
this.sharing = refreshedData.sharing;
this.previewUrl = refreshedData.previewUrl;
this.category = refreshedData.category;
this.highQualityBaseModelIds = refreshedData.highQualityBaseModelIds;
return this;
}
@@ -154,6 +160,29 @@ public class Voice extends ElevenModel {
return ElevenLabs.generateTextToSpeech(voiceId, text, model, settings);
}
public File generate(String text, String model, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeech(voiceId,text, model, outputFormat,streamLatencyOptimization, settings);
}
public File generate(String text, String model, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat) {
return ElevenLabs.generateTextToSpeech(voiceId,text, model, outputFormat,StreamLatencyOptimization.getDefault(), settings);
}
public File generate(String text, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", outputFormat,StreamLatencyOptimization.getDefault(), settings);
}
public File generate(String text, VoiceSettings settings, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", GeneratedAudioOutputFormat.getDefault(),streamLatencyOptimization, settings);
}
public File generate(String text, VoiceSettings settings, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", outputFormat,streamLatencyOptimization, settings);
}
public File generate(String text, GeneratedAudioOutputFormat outputFormat, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", outputFormat,streamLatencyOptimization, settings);
}
public File generate(String text, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", GeneratedAudioOutputFormat.getDefault(), streamLatencyOptimization, settings);
}
public File generate(String text, VoiceSettings settings) {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", settings);
}
@@ -162,6 +191,8 @@ public class Voice extends ElevenModel {
return ElevenLabs.generateTextToSpeech(voiceId, text, "eleven_monolingual_v1", settings);
}
public InputStream generateStream(String text, String model) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, model, settings);
}
@@ -179,8 +210,44 @@ public class Voice extends ElevenModel {
}
@JsonIgnore
public InputStream generateStream(String text, String model, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, model, generatedAudioOutputFormat, streamLatencyOptimization, settings);
}
public InputStream generateStream(String text, String model, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization, VoiceSettings settings) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, model, generatedAudioOutputFormat, streamLatencyOptimization, settings);
}
public InputStream generateStream(String text, VoiceSettings settings, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, "eleven_monolingual_v1", generatedAudioOutputFormat, streamLatencyOptimization, settings);
}
public InputStream generateStream(String text, GeneratedAudioOutputFormat generatedAudioOutputFormat, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, "eleven_monolingual_v1", generatedAudioOutputFormat, streamLatencyOptimization, settings);
}
public InputStream generateStream(String text, String model, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, model, GeneratedAudioOutputFormat.getDefault(), streamLatencyOptimization, settings);
}
public InputStream generateStream(String text, String model, StreamLatencyOptimization streamLatencyOptimization, VoiceSettings settings) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, model, GeneratedAudioOutputFormat.getDefault(), streamLatencyOptimization, settings);
}
public InputStream generateStream(String text, VoiceSettings settings, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, "eleven_monolingual_v1", GeneratedAudioOutputFormat.getDefault(), streamLatencyOptimization, settings);
}
public InputStream generateStream(String text, StreamLatencyOptimization streamLatencyOptimization) {
return ElevenLabs.generateTextToSpeechStreamed(voiceId, text, "eleven_monolingual_v1", GeneratedAudioOutputFormat.getDefault(), streamLatencyOptimization, settings);
}
@Override
@JsonIgnore
public String toString() {
return "Voice{" +
"voiceId='" + voiceId + '\'' +
@@ -194,6 +261,7 @@ public class Voice extends ElevenModel {
", availableForTiers=" + availableForTiers +
", settings=" + settings +
", sharing=" + sharing +
", highQualityBaseModelIds=" + highQualityBaseModelIds +
'}';
}
}

View File

@@ -15,9 +15,24 @@ public class VoiceSettings extends ElevenModel {
@JsonProperty("similarity_boost")
private double similarityBoost;
@JsonProperty("style")
private double style;
@JsonProperty("use_speaker_boost")
private boolean useSpeakerBoost;
public VoiceSettings(double stability, double similarityBoost, double style, boolean useSpeakerBoost) {
this.stability = stability;
this.similarityBoost = similarityBoost;
this.style = style;
this.useSpeakerBoost = useSpeakerBoost;
}
public VoiceSettings(double stability, double similarityBoost) {
this.stability = stability;
this.similarityBoost = similarityBoost;
this.style = 0;
this.useSpeakerBoost = true;
}
public VoiceSettings() {
@@ -34,11 +49,23 @@ public class VoiceSettings extends ElevenModel {
}
@JsonIgnore
public double getStyle() {
return style;
}
@JsonIgnore
public boolean isUseSpeakerBoost() {
return useSpeakerBoost;
}
@Override
@JsonIgnore
public String toString() {
return "VoiceSettings{" +
"stability=" + stability +
", similarityBoost=" + similarityBoost +
", style=" + style +
", useSpeakerBoost=" + useSpeakerBoost +
'}';
}
}

View File

@@ -1,9 +0,0 @@
package net.andrewcpu.elevenlabs.net;
import net.andrewcpu.elevenlabs.HttpRequestType;
public abstract class DeleteRequest<T> extends ElevenRequest<T> {
public DeleteRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.DELETE, endpoint, clazz);
}
}

View File

@@ -1,32 +0,0 @@
package net.andrewcpu.elevenlabs.net;
import net.andrewcpu.elevenlabs.HttpRequestType;
import net.andrewcpu.elevenlabs.util.ElevenNetworkUtil;
public abstract class ElevenRequest<T> {
private HttpRequestType type;
private String endpoint;
private Class<T> responseClass;
public ElevenRequest(HttpRequestType type, String endpoint, Class<T> clazz) {
this.type = type;
this.endpoint = endpoint;
this.responseClass = clazz;
}
public HttpRequestType getType() {
return type;
}
public String getEndpoint() {
return endpoint;
}
public Class<T> getResponseClass() {
return responseClass;
}
public abstract Object getPayload();
}

View File

@@ -1,9 +0,0 @@
package net.andrewcpu.elevenlabs.net;
import net.andrewcpu.elevenlabs.HttpRequestType;
public abstract class GetRequest<T> extends ElevenRequest<T> {
public GetRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.GET, endpoint, clazz);
}
}

View File

@@ -1,9 +0,0 @@
package net.andrewcpu.elevenlabs.net;
import net.andrewcpu.elevenlabs.HttpRequestType;
public abstract class PostRequest<T> extends ElevenRequest<T> {
public PostRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.POST, endpoint, clazz);
}
}

View File

@@ -1,9 +0,0 @@
package net.andrewcpu.elevenlabs.net;
import net.andrewcpu.elevenlabs.HttpRequestType;
public abstract class PutRequest<T> extends ElevenRequest<T> {
public PutRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.PUT, endpoint, clazz);
}
}

View File

@@ -1,19 +0,0 @@
package net.andrewcpu.elevenlabs.net.tts;
import net.andrewcpu.elevenlabs.model.request.TextToSpeechRequest;
import net.andrewcpu.elevenlabs.net.PostRequest;
import java.io.File;
public class PostTextToSpeechRequest extends PostRequest<File> {
private TextToSpeechRequest request;
public PostTextToSpeechRequest(String voiceId, TextToSpeechRequest request) {
super("v1/text-to-speech/" + voiceId, File.class);
this.request = request;
}
@Override
public Object getPayload() {
return request;
}
}

View File

@@ -1,20 +0,0 @@
package net.andrewcpu.elevenlabs.net.tts;
import net.andrewcpu.elevenlabs.model.request.TextToSpeechRequest;
import net.andrewcpu.elevenlabs.net.PostRequest;
import java.io.File;
import java.io.InputStream;
public class PostTextToSpeechStreamedRequest extends PostRequest<InputStream> {
private TextToSpeechRequest request;
public PostTextToSpeechStreamedRequest(String voiceId, TextToSpeechRequest request) {
super("v1/text-to-speech/" + voiceId, InputStream.class);
this.request = request;
}
@Override
public Object getPayload() {
return request;
}
}

View File

@@ -0,0 +1,9 @@
package net.andrewcpu.elevenlabs.requests;
import net.andrewcpu.elevenlabs.enums.HttpRequestType;
public abstract class DeleteRequest<T> extends ElevenLabsRequest<T> {
public DeleteRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.DELETE, endpoint, clazz);
}
}

View File

@@ -0,0 +1,40 @@
package net.andrewcpu.elevenlabs.requests;
import net.andrewcpu.elevenlabs.enums.HttpRequestType;
import java.util.HashMap;
import java.util.Map;
import static net.andrewcpu.elevenlabs.util.ElevenNetworkUtil.buildQueryParameters;
public abstract class ElevenLabsRequest<T> {
private HttpRequestType type;
private String endpoint;
private Class<T> responseClass;
public ElevenLabsRequest(HttpRequestType type, String endpoint, Class<T> clazz) {
this.type = type;
this.endpoint = endpoint;
this.responseClass = clazz;
}
public HttpRequestType getType() {
return type;
}
public String getEndpoint() {
return endpoint + "?" + buildQueryParameters(getQueryParameters());
}
public Map<String, String> getQueryParameters() {
return new HashMap<>();
}
public Class<T> getResponseClass() {
return responseClass;
}
public abstract Object getPayload();
}

View File

@@ -0,0 +1,9 @@
package net.andrewcpu.elevenlabs.requests;
import net.andrewcpu.elevenlabs.enums.HttpRequestType;
public abstract class GetRequest<T> extends ElevenLabsRequest<T> {
public GetRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.GET, endpoint, clazz);
}
}

View File

@@ -0,0 +1,9 @@
package net.andrewcpu.elevenlabs.requests;
import net.andrewcpu.elevenlabs.enums.HttpRequestType;
public abstract class PostRequest<T> extends ElevenLabsRequest<T> {
public PostRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.POST, endpoint, clazz);
}
}

View File

@@ -0,0 +1,9 @@
package net.andrewcpu.elevenlabs.requests;
import net.andrewcpu.elevenlabs.enums.HttpRequestType;
public abstract class PutRequest<T> extends ElevenLabsRequest<T> {
public PutRequest(String endpoint, Class<T> clazz) {
super(HttpRequestType.PUT, endpoint, clazz);
}
}

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.net.history;
package net.andrewcpu.elevenlabs.requests.history;
import net.andrewcpu.elevenlabs.net.DeleteRequest;
import net.andrewcpu.elevenlabs.requests.DeleteRequest;
public class DeleteHistoryItemRequest extends DeleteRequest<String> {
public DeleteHistoryItemRequest(String historyItemId) {

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.net.history;
package net.andrewcpu.elevenlabs.requests.history;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
import java.io.File;

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.history;
package net.andrewcpu.elevenlabs.requests.history;
import net.andrewcpu.elevenlabs.model.history.HistoryItem;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetHistoryItemByIdRequest extends GetRequest<HistoryItem> {
public GetHistoryItemByIdRequest(String historyId) {

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.history;
package net.andrewcpu.elevenlabs.requests.history;
import net.andrewcpu.elevenlabs.model.history.History;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetHistoryRequest extends GetRequest<History> {
public GetHistoryRequest() {

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.history;
package net.andrewcpu.elevenlabs.requests.history;
import net.andrewcpu.elevenlabs.model.history.HistoryItemList;
import net.andrewcpu.elevenlabs.net.PostRequest;
import net.andrewcpu.elevenlabs.requests.PostRequest;
import java.io.File;
import java.util.Arrays;

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.models;
package net.andrewcpu.elevenlabs.requests.models;
import net.andrewcpu.elevenlabs.model.response.GenerationTypeModel;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetModelsRequest extends GetRequest<GenerationTypeModel[]> {
public GetModelsRequest() {

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.net.samples;
package net.andrewcpu.elevenlabs.requests.samples;
import net.andrewcpu.elevenlabs.net.DeleteRequest;
import net.andrewcpu.elevenlabs.requests.DeleteRequest;
public class DeleteSampleRequest extends DeleteRequest<String> {
public DeleteSampleRequest(String voiceId, String sampleId) {

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.net.samples;
package net.andrewcpu.elevenlabs.requests.samples;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
import java.io.File;

View File

@@ -0,0 +1,58 @@
package net.andrewcpu.elevenlabs.requests.tts;
import net.andrewcpu.elevenlabs.enums.GeneratedAudioOutputFormat;
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
import net.andrewcpu.elevenlabs.model.request.TextToSpeechRequest;
import net.andrewcpu.elevenlabs.requests.PostRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class PostTextToSpeechRequest extends PostRequest<File> {
private TextToSpeechRequest request;
private StreamLatencyOptimization streamLatencyOptimization;
private GeneratedAudioOutputFormat outputFormat;
public PostTextToSpeechRequest(String voiceId, TextToSpeechRequest request) {
super("v1/text-to-speech/" + voiceId, File.class);
this.request = request;
this.streamLatencyOptimization = StreamLatencyOptimization.getDefault();
this.outputFormat = GeneratedAudioOutputFormat.getDefault();
}
public PostTextToSpeechRequest(String voiceId, TextToSpeechRequest request, StreamLatencyOptimization streamLatencyOptimization) {
super("v1/text-to-speech/" + voiceId, File.class);
this.request = request;
this.streamLatencyOptimization = streamLatencyOptimization;
this.outputFormat = GeneratedAudioOutputFormat.getDefault();
}
public PostTextToSpeechRequest(String voiceId, TextToSpeechRequest request, StreamLatencyOptimization streamLatencyOptimization, GeneratedAudioOutputFormat generatedAudioOutputFormat) {
super("v1/text-to-speech/" + voiceId, File.class);
this.request = request;
this.streamLatencyOptimization = streamLatencyOptimization;
this.outputFormat = generatedAudioOutputFormat;
}
public PostTextToSpeechRequest(String voiceId, TextToSpeechRequest request, GeneratedAudioOutputFormat generatedAudioOutputFormat) {
super("v1/text-to-speech/" + voiceId, File.class);
this.request = request;
this.streamLatencyOptimization = StreamLatencyOptimization.getDefault();
this.outputFormat = generatedAudioOutputFormat;
}
@Override
public Object getPayload() {
return request;
}
@Override
public Map<String, String> getQueryParameters() {
Map<String, String> map = new HashMap<>();
map.put("optimize_streaming_latency", String.valueOf(streamLatencyOptimization.getValue()));
map.put("output_format", outputFormat.name());
return map;
}
}

View File

@@ -0,0 +1,58 @@
package net.andrewcpu.elevenlabs.requests.tts;
import net.andrewcpu.elevenlabs.enums.GeneratedAudioOutputFormat;
import net.andrewcpu.elevenlabs.enums.StreamLatencyOptimization;
import net.andrewcpu.elevenlabs.model.request.TextToSpeechRequest;
import net.andrewcpu.elevenlabs.requests.PostRequest;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class PostTextToSpeechStreamedRequest extends PostRequest<InputStream> {
private TextToSpeechRequest request;
private StreamLatencyOptimization streamLatencyOptimization;
private GeneratedAudioOutputFormat outputFormat;
public PostTextToSpeechStreamedRequest(String voiceId, TextToSpeechRequest request) {
super("v1/text-to-speech/" + voiceId, InputStream.class);
this.request = request;
this.streamLatencyOptimization = StreamLatencyOptimization.getDefault();
this.outputFormat = GeneratedAudioOutputFormat.getDefault();
}
public PostTextToSpeechStreamedRequest(String voiceId, TextToSpeechRequest request, StreamLatencyOptimization streamLatencyOptimization) {
super("v1/text-to-speech/" + voiceId, InputStream.class);
this.request = request;
this.streamLatencyOptimization = streamLatencyOptimization;
this.outputFormat = GeneratedAudioOutputFormat.getDefault();
}
public PostTextToSpeechStreamedRequest(String voiceId, TextToSpeechRequest request, StreamLatencyOptimization streamLatencyOptimization, GeneratedAudioOutputFormat generatedAudioOutputFormat) {
super("v1/text-to-speech/" + voiceId, InputStream.class);
this.request = request;
this.streamLatencyOptimization = streamLatencyOptimization;
this.outputFormat = generatedAudioOutputFormat;
}
public PostTextToSpeechStreamedRequest(String voiceId, TextToSpeechRequest request, GeneratedAudioOutputFormat generatedAudioOutputFormat) {
super("v1/text-to-speech/" + voiceId, InputStream.class);
this.request = request;
this.streamLatencyOptimization = StreamLatencyOptimization.getDefault();
this.outputFormat = generatedAudioOutputFormat;
}
@Override
public Map<String, String> getQueryParameters() {
Map<String, String> map = new HashMap<>();
map.put("optimize_streaming_latency", String.valueOf(streamLatencyOptimization.getValue()));
map.put("output_format", outputFormat.name());
return map;
}
@Override
public Object getPayload() {
return request;
}
}

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.user;
package net.andrewcpu.elevenlabs.requests.user;
import net.andrewcpu.elevenlabs.model.user.Subscription;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetSubscriptionRequest extends GetRequest<Subscription> {

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.user;
package net.andrewcpu.elevenlabs.requests.user;
import net.andrewcpu.elevenlabs.model.user.User;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetUserRequest extends GetRequest<User> {
public GetUserRequest() {

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.net.DeleteRequest;
import net.andrewcpu.elevenlabs.requests.DeleteRequest;
public class DeleteVoiceRequest extends DeleteRequest<String> {
public DeleteVoiceRequest(String voiceId) {

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetDefaultVoiceSettingsRequest extends GetRequest<VoiceSettings> {
public GetDefaultVoiceSettingsRequest() {

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.model.voice.Voice;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
import java.util.HashMap;
import java.util.Map;

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetVoiceSettingsRequest extends GetRequest<VoiceSettings> {
public GetVoiceSettingsRequest(String voiceId) {

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.model.response.VoiceModelResponse;
import net.andrewcpu.elevenlabs.net.GetRequest;
import net.andrewcpu.elevenlabs.requests.GetRequest;
public class GetVoicesRequest extends GetRequest<VoiceModelResponse> {
public GetVoicesRequest() {

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.model.response.CreateVoiceResponse;
import net.andrewcpu.elevenlabs.net.PostRequest;
import net.andrewcpu.elevenlabs.requests.PostRequest;
import java.io.File;
import java.util.HashMap;

View File

@@ -1,7 +1,6 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.model.response.CreateVoiceResponse;
import net.andrewcpu.elevenlabs.net.PostRequest;
import net.andrewcpu.elevenlabs.requests.PostRequest;
import java.io.File;
import java.util.HashMap;

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.net.voices;
package net.andrewcpu.elevenlabs.requests.voices;
import net.andrewcpu.elevenlabs.model.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.net.PostRequest;
import net.andrewcpu.elevenlabs.requests.PostRequest;
public class PostEditVoiceSettingsRequest extends PostRequest<String> {
private VoiceSettings voiceSettings;

View File

@@ -3,12 +3,10 @@ package net.andrewcpu.elevenlabs.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.andrewcpu.elevenlabs.ElevenLabs;
import net.andrewcpu.elevenlabs.HttpRequestType;
import net.andrewcpu.elevenlabs.enums.HttpRequestType;
import net.andrewcpu.elevenlabs.exceptions.ValidationException;
import net.andrewcpu.elevenlabs.model.error.ValidationError;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.*;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
@@ -23,11 +21,11 @@ import org.apache.hc.core5.net.URIBuilder;
import java.io.*;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.interfaces.DSAParams;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
public class ElevenNetworkUtil {
private static final String BASE_URL = "https://api.elevenlabs.io/";
@@ -38,6 +36,23 @@ public class ElevenNetworkUtil {
request.setHeader("xi-api-key", ElevenLabs.getApiKey());
}
public static String buildQueryParameters(Map<String, String> parameters) {
StringJoiner queryParameters = new StringJoiner("&");
for (Map.Entry<String, String> entry : parameters.entrySet()) {
String encodedKey = encodeValue(entry.getKey());
String encodedValue = encodeValue(entry.getValue());
queryParameters.add(encodedKey + "=" + encodedValue);
}
return queryParameters.toString();
}
private static String encodeValue(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Encoding not supported", e);
}
}
public static HttpUriRequestBase getRequest(HttpRequestType type, String path) {
switch (type) {
@@ -141,6 +156,7 @@ public class ElevenNetworkUtil {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpUriRequestBase request = getRequest(method, path, payload);
return getRequestResult(responseType, objectMapper, httpclient, request);
} catch (IOException | ValidationException e) {
throw new RuntimeException(e);

View File

@@ -2,7 +2,7 @@ package net.andrewcpu.elevenlabs;
public class ElevenLabsTest {
public static final String ELEVEN_LABS_API_KEY = "";
public static final String ELEVEN_LABS_API_KEY = System.getenv("ELEVEN_LABS_API_KEY");
public static final String TEST_VOICE = "ZjJOFdM86g4E9U6OhzUo";
static {
ElevenLabs.setApiKey(ELEVEN_LABS_API_KEY);

View File

@@ -14,7 +14,7 @@ public class TextToSpeechTest extends ElevenLabsTest {
@Test
public void testDownloadResponse() {
assertFalse("Cannot download tts!", doesThrow(() -> {
File file = ElevenLabs.generateTextToSpeech(TEST_VOICE, "This is a test", "eleven_monolingual_v1", new VoiceSettings(0.7, 0.7));
File file = ElevenLabs.generateTextToSpeech(TEST_VOICE, "This is a test", "eleven_monolingual_v1", new VoiceSettings(0.7, 0.7, 0, true));
file.delete();
}));
}
@@ -22,7 +22,7 @@ public class TextToSpeechTest extends ElevenLabsTest {
@Test
public void testStreamedResponse() {
assertFalse("Cannot strem tts!", doesThrow(() -> {
InputStream inputStream = ElevenLabs.generateTextToSpeechStreamed(TEST_VOICE, "This is a test", "eleven_monolingual_v1", new VoiceSettings(0.7, 0.7));
InputStream inputStream = ElevenLabs.generateTextToSpeechStreamed(TEST_VOICE, "This is a test", "eleven_monolingual_v1", new VoiceSettings(0.7, 0.7, 0, true));
File tmp;
try {
tmp = File.createTempFile("test", "audio");