Search in sources :

Example 1 with Marks

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

the class TextToSpeechWebSocketListener method onMessage.

/*
   * (non-Javadoc)
   * @see okhttp3.WebSocketListener#onMessage(okhttp3.WebSocket, java.lang.String)
   */
@Override
public void onMessage(WebSocket webSocket, String message) {
    JsonObject json = new JsonParser().parse(message).getAsJsonObject();
    if (json.has(ERROR)) {
        String error = json.get(ERROR).getAsString();
        callback.onError(new RuntimeException(error));
    } else if (json.has(WARNINGS)) {
        String warning = json.get(WARNINGS).getAsString();
        callback.onWarning(new RuntimeException(warning));
    } else if (json.has(BINARY_STREAMS)) {
        String contentType = json.get(BINARY_STREAMS).getAsJsonArray().get(0).getAsJsonObject().get(CONTENT_TYPE).getAsString();
        callback.onContentType(contentType);
    } else if (json.has(WORDS)) {
        callback.onTimings(GSON.fromJson(message, Timings.class));
    } else if (json.has(MARKS)) {
        callback.onMarks(GSON.fromJson(message, Marks.class));
    }
}
Also used : JsonObject(com.google.gson.JsonObject) ByteString(okio.ByteString) Marks(com.ibm.watson.text_to_speech.v1.model.Marks) JsonParser(com.google.gson.JsonParser)

Example 2 with Marks

use of com.ibm.watson.text_to_speech.v1.model.Marks 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

Marks (com.ibm.watson.text_to_speech.v1.model.Marks)2 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)1 MarkTiming (com.ibm.watson.text_to_speech.v1.model.MarkTiming)1 SynthesizeOptions (com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)1 BaseSynthesizeCallback (com.ibm.watson.text_to_speech.v1.websocket.BaseSynthesizeCallback)1 ArrayList (java.util.ArrayList)1 ByteString (okio.ByteString)1 Test (org.junit.Test)1