Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Swing

Talking Clock Implemented in Java using Swing UI and FreeTTS

4.27/5 (5 votes)
23 Jul 2010CPOL2 min read 40.8K   2.1K  
A platform independent application that runs on Windows, Mac and Linux
Screenshot-1.png

Introduction

This is a simple Talking Clock application which basically demonstrates 3 things:

  1. TPI concept - True Platform Independence - That means this application will run anywhere where JRE is installed. Like Linux Flavors, MacOS or Windows platform.
  2. Nimbus UI- This is a new age platform independent UI to keep the visual design of a software based on Swing as it is between the various Platforms.
  3. Using FreeTTS - an open source speech synthesis system written entirely in Java. It is a must to run this app and can be found HERE (Could not upload here due to size limit of 6MB).

Background

During development of platform independent applications, I often wondered about JSAPI (Java Speech API) to create voice aided applications and eventually found this Flite based implementation of JSAPI - FreeTTS (Free Text To Speech).

Using the Code

The first part is Constructor work. It applies the Nimbus UI, then creates a timer 'tt' with tick of a second calling 'taskperformer' and starts it and finally applies initial text to the button, i.e., current date and time.

The second part is a taskperformer itself, whatever is coded here will execute every second. In our case, it is applying the current date and time.

The third part is the actual core of the program. Using variable 'nulls', we hold an instance of date time, then format it using SHORT formatting so that it only returns the time in 12 hour format to be ultimately stored in string 'sText' along with other text required to be spoken. After checking that text is not empty, we create Father-"VoiceManager", and child "Voice" with respective instances 'voiceManager' (note the lower letter) and 'syntheticVoice', both assigned to null.

Next, we do assignment to voiceManager and syntheticVoice using methods getinstance() and getvoice() respectively. The getvoice() is parameterized to apply the desired voice to our speech, details of which can be obtained in the FreeTTS documentation. Finally we use allocate() and speak() with parameter sText to actually speak the time.

Java
public MainForm()
    {
        // The below try block is used to apply the platform independent 
        // nimbus look to Application.
         try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch(Exception e){}
        initComponents();
        // Creation of timer that ticks after 1000 milli seconds = 1 second.
        Timer tt = new Timer(1000, taskPerformer);
        tt.start();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //Button initially set to display current date and time.
        jButton1.setText(new Date().toString());
    }

    ActionListener taskPerformer = new ActionListener()
    {

        public void actionPerformed(ActionEvent evt)
        {
            // action performed at every tick, i.e. refresh the text with current time.
            jButton1.setText(new Date().toString());
        }
    };

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
    {                                          
        // string to hold the text to be spoken.
        String sText;
        //nulls is a temporary variable to hold datetime for further formatting.
        Date nulls=new Date();
        // Creation of final output using SHORT format of datetime.
        sText="The Time is :"
		+(DateFormat.getTimeInstance(DateFormat.SHORT).format(nulls));
        // Checking though unnecessary, is used avoid exceptions.
        if (sText != null && sText.trim().length() > 0)
        {
            
            VoiceManager voiceManager = null;
            Voice syntheticVoice = null;
            try
            {
                voiceManager = VoiceManager.getInstance();
                // other voices are also supported, 
                // like alan,kevin etc see freeTTS documentation on sourceforge.
                syntheticVoice = voiceManager.getVoice("kevin16");
                syntheticVoice.allocate();
                syntheticVoice.speak(sText);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                syntheticVoice.deallocate();
            }
        }
    }

Note: Using source under NetBeans would be a breeze (File->Open Project->Extracted Folder), but manually compiling MainForm.java under src->Regular_App with -jar switch would do the same. The latest JRE is recommended to run the application without any problems and JDK is mandatory to compile the source.

Points of Interest

Being really new to Java, I initially used 'string split' using regular expressions to format the Long version of date and time. But when Googling for date format, I finally found this simple one line code to format the Date.

History

The older(dumb) version without using standard formatting may be obtained by PM.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)