Search in sources :

Example 6 with SynthesizeOptions

use of com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class TextToSpeechTest method testSynthesize.

/**
 * Test synthesize.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@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();
    final RecordedRequest request = server.takeRequest();
    final HttpUrl requestUrl = HttpUrl.parse("http://www.example.com" + request.getPath());
    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"));
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HttpUrl(okhttp3.HttpUrl) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 7 with SynthesizeOptions

use of com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class TextToSpeechTest method testWithVoiceAsWav.

/**
 * Test with voice as AudioFormat.WAV.
 */
// @Test
public void testWithVoiceAsWav() {
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(text).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(SynthesizeOptions.Accept.AUDIO_WAV).build();
    final InputStream is = service.synthesize(synthesizeOptions).execute();
    assertNotNull(is);
    try {
        writeInputStreamToOutputStream(is, new FileOutputStream("target/output.wav"));
    } catch (final FileNotFoundException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions)

Example 8 with SynthesizeOptions

use of com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions in project java-sdk by watson-developer-cloud.

the class TextToSpeechTest method testSynthesizeWebM.

/**
 * Test synthesize for WebM.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@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(SynthesizeOptions.Accept.AUDIO_WEBM).build();
    final InputStream in = service.synthesize(synthesizeOptions).execute();
    final RecordedRequest request = server.takeRequest();
    final HttpUrl requestUrl = HttpUrl.parse("http://www.example.com" + request.getPath());
    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"));
}
Also used : Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HttpUrl(okhttp3.HttpUrl) SynthesizeOptions(com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

SynthesizeOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions)8 InputStream (java.io.InputStream)8 File (java.io.File)5 Test (org.junit.Test)5 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 AddWordOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordOptions)2 Word (com.ibm.watson.developer_cloud.text_to_speech.v1.model.Word)2 HttpUrl (okhttp3.HttpUrl)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Buffer (okio.Buffer)2 TranslationResult (com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult)1 TranslateOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslateOptions)1 AddWordsOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.AddWordsOptions)1 CreateVoiceModelOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.CreateVoiceModelOptions)1 DeleteVoiceModelOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteVoiceModelOptions)1 DeleteWordOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.DeleteWordOptions)1