Home > Java > Implementing Text to Speech quickly with FreeTTS

Implementing Text to Speech quickly with FreeTTS

I had a quick play with the FreeTTS Text to Speech API yesterday. I needed a voice alert for a little Swing app I was running up. Outwardly this looked like hard work, perhaps more work than I cared for, but nevertheless I persisted and came up with this as a quick first shot. I have to do some more work on it to resolve issues with threadedness, but that’s just a matter of time and, like Marcel Proust I will probably get round to it… someday anyway… Let me say that the TTS voices are horrible and work needs doing on these (for which the FreeTTS guys are not responsible) more than the API itself which works pretty much out of the box. The API, when boiled down, does what it says on the label.

You can get FreeTTS from here on Sourceforge.

You need to unzip it, head to the unzipped bin directory and run the JSAPI.exe to make the jars, then put the jars where you need them and make them available to your application. And put the speech.properties file somewhere your app can find it like your {jre}/lib. You will probably end up using the kevin voice. I have it hardcoded but you can easily extend and modify the code to take a String with the name of the voice you want to use or hardcode your own in place of nasty kevin. I’ve put the ShowVoices routine in there to show you the available voices just in case… But kevin works, almost. All you need to do is instantiate TTSReader, and call consume, passing it the String you want it to speak.


import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class TTSReader {

public void showVoices() {
        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice[] v = voiceManager.getVoices();
        for (int i = 0; i > v.length; i++) {
            System.out.println(v[i].getName() + "  : " + v[i].getDomain() );
        }
    }
 
    public void consume(String input)
    {
    VoiceManager voiceManager = VoiceManager.getInstance();
        Voice myVoice = voiceManager.getVoice("kevin");
        myVoice.allocate();
        myVoice.speak(input);
        myVoice.deallocate();
    }
}
Categories: Java Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment