Chris Pioline
Chris Pioline

Reputation: 199

Watson Text to speech not producing Audio output

This is my java code:

public static void main(String[] args) {

    TextToSpeech textService = new TextToSpeech(IBM_WATSON_USERNAME, IBM_WATSON_PASSWORD);

    //String voice = "en-US_AllisonVoice";
    String text = "This is Just awesome And i am going to experience the effect";
    //String format = "audio/mp3";

    try {
        InputStream in = textService.synthesize(text, Voice.EN_ALLISON, AudioFormat.OGG_VORBIS)
                .execute();
        System.out.println(in.available());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

When i execute the code in eclipse, i am getting:

Dec 12, 2017 3:05:08 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/ogg;%20codecs%3Dvorbis http/1.1 (71-byte body)
Dec 12, 2017 3:05:09 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/ogg;%20codecs%3Dvorbis (588ms, unknown-length body)

Output for in.available() is: 0

Why am i not getting any audio? I can see that my text is not getting POSTED, as per the POST request.. What is that i am missing?

Upvotes: 0

Views: 82

Answers (1)

German Attanasio
German Attanasio

Reputation: 23663

The available() method from InputStream will always returns 0 because it depends on the implementation of the InputStream. See the InputStream Javadoc.

The InputStream you get when calling synthesize() it's a byteStream() from the okHttp library.

You need to read from the InputStream into a file or somewhere else. Here is a code snippet that can be used for that:

inputStream = textToSpeech.synthesize(/* parameters*/).execute();
outputStream = new FileOutputStream(new File("audio.ogg"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
  outputStream.write(bytes, 0, read);
}
System.out.println("Done!");

Note: The snippet above doesn't have any try{}catch and it doesn't close the streams. I'll leave that to you =)

Upvotes: 1

Related Questions