Search in sources :

Example 1 with BaseSynthesizeCallback

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

the class TextToSpeechIT method testSynthesizeUsingWebSocket.

/**
 * Test synthesize using web socket.
 *
 * @throws InterruptedException the interrupted exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testSynthesizeUsingWebSocket() throws InterruptedException, IOException {
    String basicText = "One taught me love. One taught me patience, and one taught me pain. Now, I'm so amazing. Say " + "I've loved and I've lost, but that's not what I see. So, look what I got." + " Look what you taught me. And for that, I say... thank u, next.";
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(basicText).voice(SynthesizeOptions.Voice.EN_US_ALLISONVOICE).accept(HttpMediaType.AUDIO_OGG).timings(Collections.singletonList("words")).build();
    service.synthesizeUsingWebSocket(synthesizeOptions, new BaseSynthesizeCallback() {

        @Override
        public void onContentType(String contentType) {
            returnedContentType = contentType;
        }

        @Override
        public void onAudioStream(byte[] bytes) {
            // build byte array of synthesized text
            try {
                byteArrayOutputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onTimings(Timings timings) {
            returnedTimings.add(timings);
        }
    });
    // wait for synthesis to complete
    lock.await(5, TimeUnit.SECONDS);
    String filename = "synthesize_websocket_test.ogg";
    OutputStream fileOutputStream = new FileOutputStream(filename);
    byteArrayOutputStream.writeTo(fileOutputStream);
    File createdFile = new File(filename);
    assertTrue(createdFile.exists());
    assertTrue(returnedContentType.contains("audio/ogg"));
    for (Timings t : returnedTimings) {
        List<WordTiming> wordTimings = t.getWords();
        for (WordTiming wordTiming : wordTimings) {
            assertTrue(basicText.contains(wordTiming.getWord()));
        }
    }
    // clean up
    byteArrayOutputStream.close();
    fileOutputStream.close();
    if (createdFile.delete()) {
        System.out.println("File deleted successfully!");
    } else {
        System.out.println("File could not be deleted");
    }
}
Also used : Timings(com.ibm.watson.text_to_speech.v1.model.Timings) BaseSynthesizeCallback(com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) WordTiming(com.ibm.watson.text_to_speech.v1.model.WordTiming) File(java.io.File) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 2 with BaseSynthesizeCallback

use of com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback 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)

Aggregations

WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)2 SynthesizeOptions (com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)2 BaseSynthesizeCallback (com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback)2 Test (org.junit.Test)2 MarkTiming (com.ibm.watson.text_to_speech.v1.model.MarkTiming)1 Marks (com.ibm.watson.text_to_speech.v1.model.Marks)1 Timings (com.ibm.watson.text_to_speech.v1.model.Timings)1 WordTiming (com.ibm.watson.text_to_speech.v1.model.WordTiming)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1