Tuesday, 16 August 2016

Text To Speech Using Voice RSS API

RSS Feeds are well known in the internet world. The new Voice RSS service brings the Speech Synthesis to a new level, where one can get the benefit of this feature with its Cloud Based Solution [SaaS] which requires no installation, no dependency on the technology, fast and accessible at all times.

Voice RSS exposes the Web Service / API to convert the given text into speech. 
Follow the below steps.



Step 1:

In order to use this service one has to get the API key from the voicerss.org website.
Register & get the Key from the below link.
          Get API Key

Step 2:

Create a Java Project in eclipse & there are no extra dependencies to be added.

Step 3:

Create a java class by name 'TextToSpeech' and a method by name 'speak'
Add the below code in 'speak' and the same in 'main'.

String speakText = "Voice RSS TTS API Test";
String APIKey = "*****getyourapifromstep1***";
byte[] stream;

//Form the URL with required information
URL url = new URL("http://api.voicerss.org/?" + "key="
    + URLEncoder.encode(APIKey, "UTF-8") + "&src="
    + URLEncoder.encode(speakText, "UTF-8") + "&hl="
    + URLEncoder.encode("en-us", "UTF-8") + "&f="
    + URLEncoder.encode("44khz_16bit_mono", "UTF-8"));

InputStream inps = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int i = 0;
while (-1 != (i = inps.read(buffer))) {
     out.write(buffer, 0, i);
}
out.close();
inps.close();

stream = out.toByteArray();
//Save the stream into audio file
FileOutputStream fos = new FileOutputStream("C:/TEST/testAudio.mp3");
fos.write(stream);
fos.close();


References:

http://www.voicerss.org/
https://community.perfectomobile.com/posts/1126665-about-mobile-text-to-speech

4 comments:

  1. Nice Blog :) :) Few Error in code:
    1. URLEncoder.encode(speaktext, "UTF-8") : Change speaktext to speakText
    2. stream = out.toByteArray(); Stream is not defined.
    Else it working absolutely fine

    ReplyDelete
  2. Thanks Saurabh. It was my oversight...:)
    I have updated the blog accordingly.

    ReplyDelete
  3. Great post Shiv. Does this cover foreign languages or the text-to-speech is only for English?

    ReplyDelete
    Replies
    1. Thank you.:)
      Currently VOICE RSS supports around 25+ language codes. Refer below link for more information.
      http://www.voicerss.org/api/documentation.aspx

      Delete