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.
http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/f/project/fr/freetts/FreeTTS/FreeTTS%201.2.2/
http://freetts.sourceforge.net/docs/index.php
http://freetts.sourceforge.net/docs/index.php
Hope it is useful!!!!! Have Fun:)