Skip to content

Commit f5cdcad

Browse files
authored
Add voice gender to Audio translation
1 parent 05dcc82 commit f5cdcad

4 files changed

Lines changed: 72 additions & 1 deletion

File tree

src/main/java/com/translated/lara/net/LaraClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import com.translated.lara.net.json.AudioStatusTypeAdapter;
1616
import com.translated.lara.net.json.DocumentStatusTypeAdapter;
1717
import com.translated.lara.net.json.TextResultValueTypeAdapter;
18+
import com.translated.lara.net.json.VoiceGenderTypeAdapter;
1819
import com.translated.lara.translator.Audio;
1920
import com.translated.lara.translator.Document;
2021
import com.translated.lara.translator.TextResult;
22+
import com.translated.lara.translator.VoiceGender;
2123

2224
import javax.crypto.Mac;
2325
import javax.crypto.spec.SecretKeySpec;
@@ -46,6 +48,7 @@ public class LaraClient {
4648
.registerTypeAdapter(TextResult.Value.class, new TextResultValueTypeAdapter())
4749
.registerTypeAdapter(Document.Status.class, new DocumentStatusTypeAdapter())
4850
.registerTypeAdapter(Audio.Status.class, new AudioStatusTypeAdapter())
51+
.registerTypeAdapter(VoiceGender.class, new VoiceGenderTypeAdapter())
4952
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
5053
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
5154
.create();
@@ -384,7 +387,7 @@ private AuthToken authenticate(AccessKey accessKey) throws LaraException {
384387
AuthenticationResponse authResponse = response.as(AuthenticationResponse.class);
385388

386389
if (authResponse == null || authResponse.getToken() == null || authResponse.getToken().isEmpty()) {
387-
throw new LaraApiConnectionException("Missing access token in authentication response: " + response.toString());
390+
throw new LaraApiConnectionException("Missing access token in authentication response: " + response);
388391
}
389392

390393
String refreshToken = connection.getHeaderField("x-lara-refresh-token");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.translated.lara.net.json;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonParseException;
7+
import com.translated.lara.translator.VoiceGender;
8+
9+
import java.lang.reflect.Type;
10+
11+
public class VoiceGenderTypeAdapter implements JsonDeserializer<VoiceGender> {
12+
@Override
13+
public VoiceGender deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
14+
if (jsonElement != null && !jsonElement.isJsonNull()) {
15+
return VoiceGender.valueOf(jsonElement.getAsString().toUpperCase());
16+
} else {
17+
return null;
18+
}
19+
}
20+
}

src/main/java/com/translated/lara/translator/AudioOptions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class AudioOptions {
1515
private String[] glossaries = null;
1616
private Boolean noTrace = null;
1717
private TranslationStyle style = null;
18+
private VoiceGender voiceGender = null;
1819

1920
/**
2021
* Returns the list of adaptation targets (for example customer IDs or domains).
@@ -111,6 +112,24 @@ public AudioOptions setStyle(TranslationStyle style) {
111112
return this;
112113
}
113114

115+
/**
116+
* Returns the voice gender for the translated audio synthesis.
117+
*/
118+
public VoiceGender getVoiceGender() {
119+
return voiceGender;
120+
}
121+
122+
/**
123+
* Sets the voice gender for the translated audio synthesis.
124+
*
125+
* @param voiceGender voice gender; may be {@code null}
126+
* @return this {@code AudioOptions} instance for method chaining
127+
*/
128+
public AudioOptions setVoiceGender(VoiceGender voiceGender) {
129+
this.voiceGender = voiceGender;
130+
return this;
131+
}
132+
114133
/**
115134
* Converts the configured options into HTTP parameters
116135
* to be sent to the LARA audio translation endpoint.
@@ -122,6 +141,7 @@ public HttpParams<Object> toParams() {
122141
params.set("adapt_to", adaptTo);
123142
params.set("glossaries", glossaries);
124143
params.set("style", TranslationStyle.toString(style));
144+
params.set("voice_gender", VoiceGender.toString(voiceGender));
125145

126146
return params;
127147
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.translated.lara.translator;
2+
3+
/**
4+
* Voice gender for audio translation synthesis.
5+
*/
6+
public enum VoiceGender {
7+
8+
MALE("male"),
9+
FEMALE("female");
10+
11+
private final String value;
12+
13+
VoiceGender(String value) {
14+
this.value = value;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return value;
20+
}
21+
22+
/**
23+
* Returns the API string value for the given gender, or null if null.
24+
*/
25+
public static String toString(VoiceGender gender) {
26+
return gender != null ? gender.toString() : null;
27+
}
28+
}

0 commit comments

Comments
 (0)