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:) 

Thursday, January 27, 2011

Java Decompiler – Convert java class file to java source file

There are many Java Decompilers available. The Best one I have found was the JAD Decompiler and sharing it to you people.
You can download the JAD Decompiler for different OS from the following website.
When you unzip, you can find two files JAD.exe and Readme.txt. It is easy to learn, understand how to use the JAD from the Readme.txt file. I will give simple information on how to use it. There are some simple commands you need to use to convert .class to .java.
For windows,
Place the JAD.exe at the location of the .class file or the folder contains .class files. Open the Command Prompt. Navigate to the location of the JAD.
If you don’t know how to use Command Prompt
{
If the .Class file location is at D:\Project\Sample. Open the command prompt by Start->Run->(Type  “cmd”) or Hold Windows Key and Press R Key. It will show some location on it. Type “d:” and hit enter. The D: location will be shown. Type “cd D:\Project\Sample” and hit enter. You will get the path on Command Prompt. Then continue with the decompilation on command prompt at D:\Project\Sample>
}
## To decompile a single JAVA class file 'example1.class', type the following:

jad -sjava example1.class

This command creates file 'example1.java'.

## If you want to decompile the whole tree of JAVA classes,use the following command:

jad -o -r -sjava -dsrc tree/**/*.class

This command decompiles all .class files located in all subdirectories of 'tree' and creates output files in subdirectories of 'src' according to package names of classes. For example, if file 'tree/a/b/c.class' contains class 'c' from package 'a.b', then output file will have a name 'src/a/b/c.java'.

Hope it is useful!!!
Have Fun:)