Hello friends, Welcome to my new tutorial and in this tutorial, we will learn about how we can convert Java Text to Speech using the FreeTTS Jar file.
So Let’s start our tutorial freetts speech to text example and convert Java text to Speech using the Eclipse IDE. To know how to download Eclipse IDE, you can click here.
Also Read – How to Play Mp3 File in Java
Converting Java Text to Speech Using Eclipse IDE
What is FreeTTS?
- FreeTTS is entirely written in Java programming language, which is nothing but an open-source Speech Synthesis system by which we can make our computer speak.
- In simple words, we can say that it is an artificial production of human speech which converts normal language text into speech. So in this tutorial, We will learn about how to convert text to speech in Java using the Eclipse IDE.
Downloading FreeTTS Jar file
- In the first step, we need to download the FreeTTS JAR file to include it in our program.
- You can download the FreeTTS JAR file from the download link given below.
- After downloading this ZIP file, extract the files and navigate to the lib folder.
- After going to the lib folder for your convenience, copy all the JAR files and store them in any new folder on your PC.
- As we have finally downloaded and extracted our JAR files now, the next thing we have to do is create a Java project in Eclipse IDE.
So let’s get started with our tutorial about text to speech in Java.
Creating a Java Project in Eclipse
- Open Eclipse IDE and click on the Java Project under the new section of File Menu (File>>New>>Java Project).
- Now give a name to your project (TextToAudio in this example) and click on “Finish”.
- Now right click on the project and create a new Java class (New>>Class).
- Now give a name to your class (TextToSpeech in this Example), tick mark on the public static void main(String[] args), and then click on the Finish button as shown in the figure below.
1 2 3 4 5 6 7 8 |
public class TextToSpeech { public static void main(String[] args) { } } |
Adding FreeTTS JAR Files to Eclipse
- To convert Java text to speech in Eclipse IDE, you need to include FreeTTS Jar files to the Eclipse.
- I have given the download link of the zip file in the above.
- Extract the Zip Archive and navigate to the lib folder.
- After going to the lib folder for your convenience, copy all the JAR files and store them in any new folder on your PC.
- Right-click on the project, go to the properties section, select Java Build Path, and click on the Add External JARs button.
- After clicking on the button, a pop-up window will appear to select and open the Jar files.
- You can see the added Jar files as shown in the figure below. Now click on the Apply and Close button.
Adding Voice to Text in Java
- First of all, we need to create an object of the Voice class.
- Now we have to get the voice of the person using the getVoice() method and it takes a String value in its parameter(in this example I am using kevin).
- Next, we have to allocate the voice using the allocate() method.
- Now to speak the voice, we will call the method speak() using the object of the Voice class and it takes the text value that we want to be spoken in its argument.
- We can also set the rate, pitch and volume of the voice according to our requirements.
- The programming example is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; public class TextToSpeech { public static void main(String[] args) { Voice voice;//Creating object of Voice class voice = VoiceManager.getInstance().getVoice("kevin");//Getting voice if (voice != null) { voice.allocate();//Allocating Voice } try { voice.setRate(190);//Setting the rate of the voice voice.setPitch(150);//Setting the Pitch of the voice voice.setVolume(3);//Setting the volume of the voice voice.speak("Hello this is Tutorials Field");//Calling speak() method } catch(Exception e) { e.printStackTrace(); } } } |
- Now run your program.
- As soon as you run your program, the written texts with the speak() method will be spoken. You can try it by yourself. It will act fine.
- You can also download the source code of this project from the link given below.
Java Text to Speech Source Code Download
- The link of the file is given below
So Friends, this was all from this tutorial. If you have any queries regarding this post then you can comment below and you can also check my previous post about how to create Login Form in Java Swing. Thank You
People are also Reading…..
Super
Thank you Vimalraj
Thank you sir for this easy to follow example.
You are welcome Doug and thank you for the appreciation
Thanks.Your tutorial is easy to understand .