TextToSpeech extends BaseService { public ServiceCall<InputStream> synthesize(SynthesizeOptions synthesizeOptions) { com.ibm.cloud.sdk.core.util.Validator.notNull( synthesizeOptions, "synthesizeOptions cannot be null"); String[] pathSegments = {"v1/synthesize"}; RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getServiceUrl(), pathSegments)); Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("text_to_speech", "v1", "synthesize"); for (Entry<String, String> header : sdkHeaders.entrySet()) { builder.header(header.getKey(), header.getValue()); } if (synthesizeOptions.accept() != null) { builder.header("Accept", synthesizeOptions.accept()); } if (synthesizeOptions.voice() != null) { builder.query("voice", synthesizeOptions.voice()); } if (synthesizeOptions.customizationId() != null) { builder.query("customization_id", synthesizeOptions.customizationId()); } final JsonObject contentJson = new JsonObject(); contentJson.addProperty("text", synthesizeOptions.text()); builder.bodyJson(contentJson); ResponseConverter<InputStream> responseConverter = ResponseConverterUtils.getInputStream(); return createServiceCall(builder.build(), responseConverter); } TextToSpeech();  TextToSpeech(Authenticator authenticator);  TextToSpeech(String serviceName);  TextToSpeech(String serviceName, Authenticator authenticator); ServiceCall<Voices> listVoices(ListVoicesOptions listVoicesOptions); ServiceCall<Voices> listVoices(); ServiceCall<Voice> getVoice(GetVoiceOptions getVoiceOptions); ServiceCall<InputStream> synthesize(SynthesizeOptions synthesizeOptions); WebSocket synthesizeUsingWebSocket(
      SynthesizeOptions synthesizeOptions, SynthesizeCallback callback); ServiceCall<Pronunciation> getPronunciation(
      GetPronunciationOptions getPronunciationOptions); ServiceCall<VoiceModel> createVoiceModel(CreateVoiceModelOptions createVoiceModelOptions); ServiceCall<VoiceModels> listVoiceModels(ListVoiceModelsOptions listVoiceModelsOptions); ServiceCall<VoiceModels> listVoiceModels(); ServiceCall<Void> updateVoiceModel(UpdateVoiceModelOptions updateVoiceModelOptions); ServiceCall<VoiceModel> getVoiceModel(GetVoiceModelOptions getVoiceModelOptions); ServiceCall<Void> deleteVoiceModel(DeleteVoiceModelOptions deleteVoiceModelOptions); ServiceCall<Void> addWords(AddWordsOptions addWordsOptions); ServiceCall<Words> listWords(ListWordsOptions listWordsOptions); ServiceCall<Void> addWord(AddWordOptions addWordOptions); ServiceCall<Translation> getWord(GetWordOptions getWordOptions); ServiceCall<Void> deleteWord(DeleteWordOptions deleteWordOptions); ServiceCall<Void> deleteUserData(DeleteUserDataOptions deleteUserDataOptions);  }
@SuppressWarnings("resource") @Test public void testSynthesize() throws IOException, InterruptedException { final File audio = new File("src/test/resources/text_to_speech/sample1.wav"); final Buffer buffer = new Buffer().write(Files.toByteArray(audio)); server.enqueue( new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.AUDIO_WAV).setBody(buffer)); SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder() .text(text) .voice(SynthesizeOptions.Voice.EN_US_LISAVOICE) .accept(HttpMediaType.AUDIO_PCM + "; rate=16000") .build(); final InputStream in = service.synthesize(synthesizeOptions).execute().getResult(); final RecordedRequest request = server.takeRequest(); final HttpUrl requestUrl = HttpUrl.parse("http: assertEquals(request.getBody().readUtf8(), "{\"text\":\"" + text + "\"}"); assertEquals(SYNTHESIZE_PATH, requestUrl.encodedPath()); assertEquals(SynthesizeOptions.Voice.EN_US_LISAVOICE, requestUrl.queryParameter("voice")); assertEquals(HttpMediaType.AUDIO_PCM + "; rate=16000", request.getHeader("Accept")); assertNotNull(in); writeInputStreamToOutputStream(in, new FileOutputStream("build/output.wav")); }
@SuppressWarnings("resource") @Test public void testSynthesizeWebM() throws IOException, InterruptedException { final File audio = new File("src/test/resources/text_to_speech/sample1.webm"); final Buffer buffer = new Buffer().write(Files.toByteArray(audio)); server.enqueue( new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.AUDIO_WEBM).setBody(buffer)); SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder() .text(text) .voice(SynthesizeOptions.Voice.EN_US_LISAVOICE) .accept(HttpMediaType.AUDIO_WEBM) .build(); final InputStream in = service.synthesize(synthesizeOptions).execute().getResult(); final RecordedRequest request = server.takeRequest(); final HttpUrl requestUrl = HttpUrl.parse("http: assertEquals(request.getBody().readUtf8(), "{\"text\":\"" + text + "\"}"); assertEquals(SYNTHESIZE_PATH, requestUrl.encodedPath()); assertEquals(SynthesizeOptions.Voice.EN_US_LISAVOICE, requestUrl.queryParameter("voice")); assertEquals(HttpMediaType.AUDIO_WEBM, request.getHeader("Accept")); assertNotNull(in); writeInputStreamToOutputStream(in, new FileOutputStream("build/output.webm")); }