Text To Speech using Java-Swing and Java Speech API

Hi, Today we are learning how to develop a text-to-speech application in Java. In this project, we are using Java-Swing and Java Speech API to develop this application.

Java-Swing is a lightweight and cross-platform toolkit that provides the graphical user interface(GUI) to java programs. Java Speech API is an application programming interface that provides features to develop cross-platform voice applications.

To develop this project we require to download FreeTTS(An open-source implementation of Java Speech API).

Download the Zip folder of FreeTTS from here. Extract file and open lib folder in it then run jsapi.exe if you are using windows or use jsapi.sh if you are using mac/Linux. After executing the jsapi file it will generate jsapi.jar.

Now we have to create a new java project in any IDE and name it TextToSpeech. Next, create a class file and name it TextToSpeech.

Now we have to add some jar files in this project from the freetts folder the path and list of the jar files are listed below.

File Name                                   Path

jsapi.jar                                     /freetts-1.2/lib/

cmudict04.jar                            /freetts-1.2/lib/

cmulex.jar                                   /freetts-1.2/lib/

cmu_time_awb.jar                     /freetts-1.2/lib/

cmutimelex.jar                            /freetts-1.2/lib/

cmu_us_kal.jar                           /freetts-1.2/lib/

en_us.jar                                     /freetts-1.2/lib/

freetts.jar                                     /freetts-1.2/lib/

freetts-jsapi10.jar                        /freetts-1.2/lib/

mbrola.jar                                     /freetts-1.2/mbrola/

After including all these jar file we have to program the TextToSpeech.java file just copy the given below code and paste it in that file.

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class TextToSpeech {

    public  void Speak(String text) {

        System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
        Voice textaudio;
        textaudio = VoiceManager.getInstance().getVoice("kevin16");
        if (textaudio != null) {
            textaudio.allocate();
        }
        try {
            textaudio.setRate(150);
            textaudio.setPitch(150);
            textaudio.setVolume(6);
            textaudio.speak(text);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

 

Now create a Java swing application and name it TextGui and copy the given code and paste it into that file.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JEditorPane;

public class TextGui {

  private JFrame frame;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          TextGui window = new TextGui();
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */
  public TextGui() {
    initialize();
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 512, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    
    JLabel lblTextToSpeech = new JLabel("Text To Speech");
    lblTextToSpeech.setFont(new Font("Dialog", Font.BOLD, 20));
    lblTextToSpeech.setHorizontalAlignment(SwingConstants.CENTER);
    lblTextToSpeech.setBounds(147, 12, 216, 33);
    frame.getContentPane().add(lblTextToSpeech);
    
    JLabel lblEnterTheText = new JLabel("Enter The Text Please");
    lblEnterTheText.setBounds(12, 85, 165, 33);
    frame.getContentPane().add(lblEnterTheText);
    JEditorPane editorPane = new JEditorPane();
    editorPane.setBounds(190, 85, 296, 99);
    frame.getContentPane().add(editorPane);
    JButton btnSpeak = new JButton("Speak");
    btnSpeak.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        TextToSpeech t=new TextToSpeech();
        t.Speak(editorPane.getText());
      }
    });
    btnSpeak.setBounds(195, 211, 117, 25);
    frame.getContentPane().add(btnSpeak);
    
    
  }
}

Now run this project and build your own Text To Speech Application.

Note: If you are facing some errors while executing this project then follow the given below steps.

  • Create a new project Text2Speech(use JDK grater than or equal to JDK-11).
  • Next, Create a new Swing Application and name it text2speech.
  • Now download a zip folder from here unzip it.
  • Now open this folder and you will find TextToSpeech.jar file
  • Now include this jar file in the project.
  • Now copy the give below code and paste it inside that file.
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import com.txt.TextToSpeech;


public class text2speech {

  private JFrame frame;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          text2speech window = new text2speech();
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */
  public text2speech() {
    initialize();
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 512, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    
    JLabel lblTextToSpeech = new JLabel("Text To Speech");
    lblTextToSpeech.setFont(new Font("Dialog", Font.BOLD, 20));
    lblTextToSpeech.setHorizontalAlignment(SwingConstants.CENTER);
    lblTextToSpeech.setBounds(147, 12, 216, 33);
    frame.getContentPane().add(lblTextToSpeech);
    
    JLabel lblEnterTheText = new JLabel("Enter The Text Please");
    lblEnterTheText.setBounds(12, 85, 165, 33);
    frame.getContentPane().add(lblEnterTheText);
    JEditorPane editorPane = new JEditorPane();
    editorPane.setBounds(190, 85, 296, 99);
    frame.getContentPane().add(editorPane);
    JButton btnSpeak = new JButton("Speak");
    btnSpeak.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        TextToSpeech t=new TextToSpeech();
        t.Speak(editorPane.getText());
      }
    });
    btnSpeak.setBounds(195, 211, 117, 25);
    frame.getContentPane().add(btnSpeak);
    
    
  }
}

 

Thank you for reading this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *