use of com.ibm.watson.text_to_speech.v1.model.Timings 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");
}
}
Aggregations