Skip to content

Commit 7205523

Browse files
jeff-arnapaparazzi0329
authored andcommitted
chore(hand edits): apply text to speech hand edits
1 parent 63883b0 commit 7205523

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

text-to-speech/src/main/java/com/ibm/watson/text_to_speech/v1/TextToSpeech.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@
6262
import com.ibm.watson.text_to_speech.v1.model.Voice;
6363
import com.ibm.watson.text_to_speech.v1.model.Voices;
6464
import com.ibm.watson.text_to_speech.v1.model.Words;
65+
import com.ibm.watson.text_to_speech.v1.websocket.SynthesizeCallback;
66+
import com.ibm.watson.text_to_speech.v1.websocket.TextToSpeechWebSocketListener;
6567
import java.io.InputStream;
6668
import java.util.HashMap;
6769
import java.util.Map;
6870
import java.util.Map.Entry;
71+
import okhttp3.HttpUrl;
6972
import okhttp3.MultipartBody;
73+
import okhttp3.OkHttpClient;
74+
import okhttp3.Request;
75+
import okhttp3.WebSocket;
7076

7177
/**
7278
* The IBM Watson™ Text to Speech service provides APIs that use IBM's speech-synthesis
@@ -154,6 +160,53 @@ public TextToSpeech(String serviceName, Authenticator authenticator) {
154160
this.configureService(serviceName);
155161
}
156162

163+
/**
164+
* Synthesize audio.
165+
*
166+
* <p>Synthesizes text to audio that is spoken in the specified voice. The service bases its
167+
* understanding of the language for the input text on the specified voice. Use a voice that
168+
* matches the language of the input text.
169+
*
170+
* <p>The method accepts a maximum of 5 KB of input text in the body of the request, and 8 KB for
171+
* the URL and headers. The 5 KB limit includes any SSML tags that you specify. The service
172+
* returns the synthesized audio stream as an array of bytes.
173+
*
174+
* <p>### Audio formats (accept types)
175+
*
176+
* <p>For more information about specifying an audio format, including additional details about
177+
* some of the formats, see [Audio
178+
* formats](https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-audioFormats#audioFormats).
179+
*
180+
* @param synthesizeOptions the {@link SynthesizeOptions} containing the options for the call
181+
* @param callback the {@link SynthesizeCallback} callback
182+
* @return a {@link WebSocket} instance
183+
*/
184+
public WebSocket synthesizeUsingWebSocket(
185+
SynthesizeOptions synthesizeOptions, SynthesizeCallback callback) {
186+
com.ibm.cloud.sdk.core.util.Validator.notNull(
187+
synthesizeOptions, "synthesizeOptions cannot be null");
188+
com.ibm.cloud.sdk.core.util.Validator.notNull(callback, "callback cannot be null");
189+
190+
HttpUrl.Builder urlBuilder = HttpUrl.parse(getServiceUrl() + "/v1/synthesize").newBuilder();
191+
192+
if (synthesizeOptions.voice() != null) {
193+
urlBuilder.addQueryParameter("voice", synthesizeOptions.voice());
194+
}
195+
if (synthesizeOptions.customizationId() != null) {
196+
urlBuilder.addQueryParameter("customization_id", synthesizeOptions.customizationId());
197+
}
198+
199+
String url = urlBuilder.toString().replace("https://", "wss://");
200+
Request.Builder builder = new Request.Builder().url(url);
201+
202+
setAuthentication(builder);
203+
setDefaultHeaders(builder);
204+
205+
OkHttpClient client = configureHttpClient();
206+
return client.newWebSocket(
207+
builder.build(), new TextToSpeechWebSocketListener(synthesizeOptions, callback));
208+
}
209+
157210
/**
158211
* List voices.
159212
*

text-to-speech/src/main/java/com/ibm/watson/text_to_speech/v1/model/SynthesizeOptions.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package com.ibm.watson.text_to_speech.v1.model;
1414

1515
import com.ibm.cloud.sdk.core.service.model.GenericModel;
16+
import java.util.List;
1617

1718
/** The synthesize options. */
1819
public class SynthesizeOptions extends GenericModel {
@@ -157,6 +158,7 @@ public interface SpellOutMode {
157158
protected String spellOutMode;
158159
protected Long ratePercentage;
159160
protected Long pitchPercentage;
161+
protected List<String> timings;
160162

161163
/** Builder. */
162164
public static class Builder {
@@ -167,6 +169,7 @@ public static class Builder {
167169
private String spellOutMode;
168170
private Long ratePercentage;
169171
private Long pitchPercentage;
172+
private List<String> timings;
170173

171174
/**
172175
* Instantiates a new Builder from an existing SynthesizeOptions instance.
@@ -181,6 +184,7 @@ private Builder(SynthesizeOptions synthesizeOptions) {
181184
this.spellOutMode = synthesizeOptions.spellOutMode;
182185
this.ratePercentage = synthesizeOptions.ratePercentage;
183186
this.pitchPercentage = synthesizeOptions.pitchPercentage;
187+
this.timings = synthesizeOptions.timings;
184188
}
185189

186190
/** Instantiates a new builder. */
@@ -280,6 +284,17 @@ public Builder pitchPercentage(long pitchPercentage) {
280284
this.pitchPercentage = pitchPercentage;
281285
return this;
282286
}
287+
288+
/**
289+
* Set the timings.
290+
*
291+
* @param timings the list of timings
292+
* @return the SynthesizeOptions builder
293+
*/
294+
public Builder timings(List<String> timings) {
295+
this.timings = timings;
296+
return this;
297+
}
283298
}
284299

285300
protected SynthesizeOptions() {}
@@ -429,4 +444,21 @@ public Long ratePercentage() {
429444
public Long pitchPercentage() {
430445
return pitchPercentage;
431446
}
447+
448+
/**
449+
* Gets the timings.
450+
*
451+
* <p>An array that specifies whether the service is to return word timing information for all
452+
* strings of the input text. Specify `words` as the element of the array to request word timing
453+
* information. The service returns the start and end time of each word of the input. Specify an
454+
* empty array or omit the parameter to receive no word timing information. Not supported for
455+
* Japanese input text.
456+
*
457+
* <p>NOTE: This parameter only works for the `synthesizeUsingWebSocket` method.
458+
*
459+
* @return the timings
460+
*/
461+
public List<String> timings() {
462+
return timings;
463+
}
432464
}

0 commit comments

Comments
 (0)