Search in sources :

Example 11 with SynthesizeOptions

use of com.ibm.watson.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 12 with SynthesizeOptions

use of com.ibm.watson.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)

Example 13 with SynthesizeOptions

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

the class TranslateAndSynthesizeExample method main.

public static void main(String[] args) throws IOException {
    Authenticator ltAuthenticator = new IamAuthenticator("<iam_api_key>");
    LanguageTranslator translator = new LanguageTranslator("2019-11-22", ltAuthenticator);
    Authenticator ttsAuthenticator = new IamAuthenticator("<iam_api_key>");
    TextToSpeech synthesizer = new TextToSpeech(ttsAuthenticator);
    String text = "Greetings from Watson Developer Cloud";
    // translate
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).source(Language.ENGLISH).target(Language.SPANISH).build();
    TranslationResult translationResult = translator.translate(translateOptions).execute().getResult();
    String translation = translationResult.getTranslations().get(0).getTranslation();
    // synthesize
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(translation).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(HttpMediaType.AUDIO_WAV).build();
    InputStream in = synthesizer.synthesize(synthesizeOptions).execute().getResult();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
}
Also used : LanguageTranslator(com.ibm.watson.language_translator.v3.LanguageTranslator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) InputStream(java.io.InputStream) TranslateOptions(com.ibm.watson.language_translator.v3.model.TranslateOptions) TranslationResult(com.ibm.watson.language_translator.v3.model.TranslationResult) File(java.io.File) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)

Example 14 with SynthesizeOptions

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

the class TextToSpeechIT method testSynthesizeUsingWebSocketWithSsml.

/**
 * Test synthesize using web socket with ssml.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSynthesizeUsingWebSocketWithSsml() throws InterruptedException {
    List<String> ssmlMarks = new ArrayList<>();
    ssmlMarks.add("sean");
    ssmlMarks.add("ricky");
    String ssmlText = String.format("Thought I'd end up with <mark name=\"%s\" />Sean, <express-as type=\"Apology\"> " + "but he wasn't a match. </express-as> Wrote some songs " + "about <mark name=\"%s\" />Ricky, now I listen and " + "laugh", ssmlMarks.get(0), ssmlMarks.get(1));
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(ssmlText).voice(SynthesizeOptions.Voice.EN_US_ALLISONVOICE).accept(HttpMediaType.AUDIO_OGG).build();
    service.synthesizeUsingWebSocket(synthesizeOptions, new BaseSynthesizeCallback() {

        @Override
        public void onMarks(Marks marks) {
            returnedMarks.add(marks);
        }
    });
    // wait for synthesis to complete
    lock.await(5, TimeUnit.SECONDS);
    for (Marks m : returnedMarks) {
        List<MarkTiming> markList = m.getMarks();
        for (MarkTiming markTiming : markList) {
            assertTrue(ssmlMarks.contains(markTiming.getMark()));
        }
    }
}
Also used : MarkTiming(com.ibm.watson.text_to_speech.v1.model.MarkTiming) BaseSynthesizeCallback(com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback) ArrayList(java.util.ArrayList) Marks(com.ibm.watson.text_to_speech.v1.model.Marks) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 15 with SynthesizeOptions

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

the class TextToSpeechTest method testSynthesizeWOptions.

// Test the synthesize operation with a valid options model parameter
@Test
public void testSynthesizeWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "This is a mock binary response.";
    String synthesizePath = "/v1/synthesize";
    server.enqueue(new MockResponse().setHeader("Content-type", "audio/basic").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the SynthesizeOptions model
    SynthesizeOptions synthesizeOptionsModel = new SynthesizeOptions.Builder().text("testString").accept("audio/ogg;codecs=opus").voice("en-US_MichaelV3Voice").customizationId("testString").build();
    // Invoke synthesize() with a valid options model and verify the result
    Response<InputStream> response = textToSpeechService.synthesize(synthesizeOptionsModel).execute();
    assertNotNull(response);
    InputStream responseObj = response.getResult();
    assertNotNull(responseObj);
    responseObj.close();
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, synthesizePath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("voice"), "en-US_MichaelV3Voice");
    assertEquals(query.get("customization_id"), "testString");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) InputStream(java.io.InputStream) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions) Test(org.testng.annotations.Test)

Aggregations

InputStream (java.io.InputStream)13 File (java.io.File)9 Test (org.junit.Test)9 SynthesizeOptions (com.ibm.watson.developer_cloud.text_to_speech.v1.model.SynthesizeOptions)8 SynthesizeOptions (com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)7 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)4 FileOutputStream (java.io.FileOutputStream)4 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 FileInputStream (java.io.FileInputStream)3 HttpUrl (okhttp3.HttpUrl)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 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 BaseSynthesizeCallback (com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback)2 Buffer (okio.Buffer)2 TranslationResult (com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult)1