Coders Packet

Convert Java Text to speech using the FreeTTS Jar file

By Kirti Sandip Phegade

Hello friends, Welcome to my new tutorial during this tutorial, we are going to create an application that converts Java Text into Speech using the FreeTTS library.

The basic idea of text-to-speech (TTS) technology is to convert written input to real human speech. 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. To develop this project, we use Some Swing classes like JButton, JLable, JFrame.

Method:-

1) Download FreeTTS Jar files  freetts-1.2.2-tst.zip

2)Extract freetts zip file 

3)after extraction of the file you can see Some jar files

  • cmu_time_awb
  • cmu_us_kal
  • cmudict40
  • cmulex
  • cmutimelex
  • en_us
  • freetts
  • freetts-jsapi10
  • mbrola

4)now create a new project in your IDE and add all jar files.

5) after adding all jar files we have to write the programming logic of this project.

Program to convert Java Text to Speech

import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import com.sun.speech.freetts.*;
import javax.swing.border.Border;
public class TextToSpeech extends JFrame
{
    JTextArea text;
    JButton button,buttonExit,buttonReset;
    TextToSpeech()
    {
        setBounds(500,60,600,400);//left,upperside,length,height
        getContentPane().setBackground(Color.LIGHT_GRAY);

        ImageIcon background = new ImageIcon(ClassLoader.getSystemResource("image/bg.jpg"));
        JLabel imagelable = new JLabel(background);
        imagelable.setBounds(0,0,600,400);//left =0,right = 0,length,height

        text = new JTextArea();
        text.setBounds(50,50,480, 200);
        text.setFont(new Font("Mongolian Baiti",Font.BOLD,20));
        text.setForeground(Color.BLACK);
        text.setLineWrap(true);
        text.setWrapStyleWord(true);

        button = new JButton("Start");
        button.setBounds(50,290,100,50);
        button.setFocusable(false);
        button.setFont(new Font("Mongolian Baiti",Font.BOLD,20));
        button.addActionListener(this::speech_Button_Action);

        buttonExit = new JButton("Exit");
        buttonExit.setBounds(250,290,100,50);
        buttonExit.setFocusable(false);
        buttonExit.setFont(new Font("Mongolian Baiti",Font.BOLD,20));
        buttonExit.addActionListener(this::Exit_Button_Action);

        buttonReset = new JButton("Reset");
        buttonReset.setBounds(450,290,100,50);
        buttonReset.setFocusable(false);
        buttonReset.setFont(new Font("Mongolian Baiti",Font.BOLD,20));
        buttonReset.addActionListener(this::Reset_Button_Action);

        add(buttonReset);
        add(buttonExit);
        add(button);
        add(text);
        add(imagelable);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void Reset_Button_Action(ActionEvent actionEvent)
    {
        text.setText(null);
    }

    private void Exit_Button_Action(ActionEvent actionEvent) {
        System.exit(0);
    }

    
    private void speech_Button_Action(ActionEvent actionEvent)
    {
        System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice voice = voiceManager.getVoice("kevin16");
        voice.allocate();
        voice.speak(text.getText());
    }

    public static void main(String[] args)
    {
        new TextToSpeech();
    }

}

In the program We use some methods:

1) System.setProperty():- The setProperty() method of the Java system class sets the property of the system which is indicated by a key. It has two attributes which are propertyName, value. It sets the system property ‘property name to have the value 'value'.

2)VoiceManager.getInstance():-VoiceManager Provides access to voices for all of FreeTTS. There is only one instance of the VoiceManager and getInstance() Gets the instance of the VoiceManager.

3)getVoice:-getVoice Provide an array of all voices available to FreeTTS in this program we use kevin16 voice and pass it to the Voice class object.

4)allocate():- now allocate voice using allocate method.

5)speak(text.getText):- Now to speak the voice, we will call the method speak() and pass the textfiled.

Output

output

By clicking the start button user can convert text into speech. If a user wants to reset textarea then by clicking the Reset button user can reset textarea and then the user can Exit from an application by clicking the Exit button

Download project

Reviews Report

Submitted by Kirti Sandip Phegade (KirtiPhegade)

Download packets of source code on Coders Packet