Friday, January 28, 2011

Convert Text to Speech– Java – freetts1.2

When I was searching to convert the Text to Speech using Java, I got many options but most are not proper and little complex to understand and use. Finally I found a simple and good text to speech API freetts1.2, which is simple, easy to use and free. You can download the freetts1.2 in the below given link.  I have given a simple program for your easy understanding of this API. Using this program you can hear the voice of the text and also you can save the text as a .wav file. First download and add the freetts1.2 API into your project. Use the below code 
Simple program,
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.SingleFileAudioPlayer;
import javax.sound.sampled.AudioFileFormat.Type;

public class Example {
            static String voiceName = "kevin16"; // Voice type "alan","kevin","kevin16"
            static String voiceMsg = "Hi How are you?"; // The text to Speech
            static String fileLocation = "D://select"; // filename without extension

            public static void main(String[] args) throws Exception {
                        VoiceManager voiceManager = VoiceManager.getInstance();
                        Voice voice = voiceManager.getVoice(voiceName);
                        voice.setStyle("casual"); //Voice style "business", "casual", "robotic", "breathy"
                        voice.allocate();            
                        SingleFileAudioPlayer audioPlayer = new SingleFileAudioPlayer(
                                                fileLocation, Type.WAVE);     //To Create the wav file for the text
                        voice.setAudioPlayer(audioPlayer);
                        voice.setVolume(100);
                        voice.speak(voiceMsg); //Speaks the text
                        voice.deallocate();
                        audioPlayer.close();
            }
}
 
The above program will convert the Text “Hi how are you?” to voice and save it in as “D:/select.wav”. If you would like to hear the audio in your system without saving, Comment the following lines in the program and run the application.
SingleFileAudioPlayer audioPlayer = new SingleFileAudioPlayer(fileLocation, Type.WAVE);
voice.setAudioPlayer(audioPlayer);
audioPlayer.close();

Now you can hear the voice.


Links you can download.
Hope it is useful!!!!! Have Fun:) 

3 comments:

  1. System property "mbrola.base" is undefined. Will not use MBROLA voices.

    this is the output which I am getting, please help me to get out of this,

    ReplyDelete
  2. System property "mbrola.base" is undefined. Will not use MBROLA voices.

    this is the output which I am getting, please help me to get out of this,
    I cant find any WAVE formats in TYPE formats

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete