Skip to main content

Java Program which speaks


Today you will see a java program which could convert text to speech .The program will ask you to enter  string and then it will spell the string in a natural human voice.

NOTE:- For this app i am using a third party library named FreeTTS 1.2.2 you could download it from here .Here i am assuming that you know how to use the jars of third party libraries if not then you could have a look on here.
If you are using Blue J IDE then see it here.
If you are using Ellipse then look here.

So below is the complete code of our Application


Spell.java



 
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import java.util.Scanner;

public class Spell {
private static final String VOICE_NAME_KEVIN = "kevin16";
    private final Voice voice;
    
    public Spell() {

        VoiceManager vm = VoiceManager.getInstance();       
        voice = vm.getVoice(VOICE_NAME_KEVIN);
        voice.allocate();
    }

    public void speak(String inputText) {

        if(inputText != null) {
            
            voice.speak(inputText);
        }
else          {
                inputText="Atleast enter something to speak";     
                voice.speak(inputText);
         }
}

    public static void main(String[] args) {

        // Defining Scanner Object to read data from console
        Scanner inputScanner = new Scanner(System.in);
        
        Spell tsc = new Spell();

        System.out.println("Enter the Text : (type 'exit' to terminate)");

        // Reading the text
        String inputText = inputScanner.nextLine();

        while (true) {

            if("exit".equalsIgnoreCase(inputText)) {
                
                inputText = "Good Bye, we will talk later";
                tsc.speak(inputText);
                break;
            }
            
            tsc.speak(inputText);
            
            System.out.println("Enter the Text : (type 'exit' to terminate)");
            inputText = inputScanner.nextLine();
        }

        inputScanner.close();
        
    }
}



Comments

Popular posts from this blog

Create a simple virus using java

What's my idea Before we start talking how we will create a simple flooder virus using java we must be clear about what is  the idea and how to implement it. So the idea is to create a program which will create  thousands of blank files in a drive you will don't believe but it could create file with  a speed of   1060+ files per second . How to implement it Now the problem is how we would do this?? So here is the answer .  First of all we will design a loop which will run  N  number of times where  N  is the number of files which we want to create. Then we will create a new File with the help of  createNewFile()  function of  java.io.File  class. The name of each file is the value of the variable which is used in the loop. Here is the sample virus program. /**  * **********This virus program is just for educational purpose   * author will not be re...

Capturing Screen Shot using java

Here i will tell you how to write a program using java which is capable of capturing screenshots(either whole or partial).And save it as an image. Which java API ? The java.awt.Robot class provides a useful method for capturing a screenshot.Here is the method prototype. BufferedImage createScreenCapture(Rectangle screenRect) It is clear from the prototype that this function returns a BufferedImage and takes a Rectangle class object (portion of the screen to capture) as the argument.The BufferedImage which is returned could be saved using ImageIO class write method.Lets begin with the program. 1) Capturing full screen In order to capture full screen using  createScreenCapture(Rectangle screenRect) screenRect should have the full screen size and in order to do that we will use Toolkit class of java. Rectangle screenRect =  new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() ); Now here begins the whole program. /* ******...

How to setup a .jar file in Eclipse IDE

How to set a new package in Eclipse IDE In this tutorial you will learn how you can setup a jar file in Eclipse IDE. Follow the given steps and you could easily set it up. Let me specify that in my case we will install a jar file created my me that is utility.jar .If you are downloading other packages like Free TTS,open cv, j3d or any other then you just need to find the jar files in their folder and rest of the tutorial remain the same. Step 1:- Open Eclipse Ide Open your Eclipse ide in my case it is  Version: Luna Service Release 2 (4.4.2) Step 2:- In the top menu bar click on  WINDOW  menu item. And then you will see a pop up menu will appear.In the pop up menu click on  PREFERENCES  . Step 3:- Then in the next window's left hand side click on JAVA and then on  BUILD PATH   .Under build path you will see two options   classpath variables   and  User Libraries   you need to click on user...