Saturday, 2 April 2016

Text to Speech in Java

Converting text-to-speech is very simple, just by using FreeTTS library.

Include library by maven dependency :

Download library (jar file) from here and include into project directly.

How to use FreeTTS API ?
Step - 1 : Get the instance of com.sun.speech.freetts.VoiceManager class.
               VoiceManager provides access to all voices available in FreeTTS
               VoiceManager voiceManager = VoiceManager.getInstance();

Step - 2 : Get com.sun.speech.freetts.Voice instance using voiceManager by supplying the voice name
               Voice voice = voiceManager.getVoice("kevin16");
               Here "kevin16" is one of the voice name available in FreeTTS library.
               You can also get all available voice names like below :
              Voice voices1[] = voiceManager.getVoices();  
              System.out.println("Available Voices");
              for (int i = 0; i < voices1.length; i++)

Step - 3 : Set voice rate (i.e no of words per minute)

Step - 4 : call speak( ) method by providing the text to speech
              voice.speak("<text to speech>");
              This speak( ) is overloaded to support various types of inputs like text, inputStream etc.
              You can also read a file by providing the fileInputStream as below : 
              FileInputStream fileInputStream = new FileInputStream("d:/ttsInput.txt");

Here is the complete code to convert a text file to speech : 
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class TextToSpeech {
    private static final String voiceName = "kevin16"; // this is one of the voice name available in default
    private static void doSpeak() {
        try {
            VoiceManager voiceManager = VoiceManager.getInstance();
            Voice voice = voiceManager.getVoice(voiceName);

            if (voice != null) {
                // Set 120 words per minute
                FileInputStream fileInputStream = new                                                                FileInputStream("d:/ttsInput.txt");
            } else {
                System.out.println("No Voice Available");
        } catch (Exception e) {

    public static void main(String[] args){

Here is a demonstration of a Speaking Alarm Clock application using this FreeTTS library.
Functionalities of application : 
1. class AlarmClockThis class creates the alarm clock UI using Java Swing API, having feature to set alarm for a specific time and triggers the TimeSpeaker to speak the time on the time for which alarm is set.
2. class TimeSpeaker : This class converts any time into word form
                            e.g  05:15 -> quarter past five
                                  03:30 -> half past three
                                  11:40 -> twenty minutes to twelve
3. class TextToSpeech : This is the class responsible of converting text to speech using freetts (Free Text To Speech) library

Download OR fork complete project from here .

Thank You for Visiting the blog. Please feel free to share yout comment/suggestion. 

No comments:

Post a Comment