|
62 | 62 | import com.ibm.watson.text_to_speech.v1.model.Voice;
|
63 | 63 | import com.ibm.watson.text_to_speech.v1.model.Voices;
|
64 | 64 | 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; |
65 | 67 | import java.io.InputStream;
|
66 | 68 | import java.util.HashMap;
|
67 | 69 | import java.util.Map;
|
68 | 70 | import java.util.Map.Entry;
|
| 71 | +import okhttp3.HttpUrl; |
69 | 72 | import okhttp3.MultipartBody;
|
| 73 | +import okhttp3.OkHttpClient; |
| 74 | +import okhttp3.Request; |
| 75 | +import okhttp3.WebSocket; |
70 | 76 |
|
71 | 77 | /**
|
72 | 78 | * 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) {
|
154 | 160 | this.configureService(serviceName);
|
155 | 161 | }
|
156 | 162 |
|
| 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 | + |
157 | 210 | /**
|
158 | 211 | * List voices.
|
159 | 212 | *
|
|
0 commit comments