Introduction
This is a simple Talking Clock application which basically demonstrates 3 things:
- TPI concept - True Platform Independence - That means this application will run anywhere where JRE is installed. Like Linux Flavors, MacOS or Windows platform.
- 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.
- 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.
public MainForm()
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e){}
initComponents();
Timer tt = new Timer(1000, taskPerformer);
tt.start();
setDefaultCloseOperation(EXIT_ON_CLOSE);
jButton1.setText(new Date().toString());
}
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
jButton1.setText(new Date().toString());
}
};
private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
{
String sText;
Date nulls=new Date();
sText="The Time is :"
+(DateFormat.getTimeInstance(DateFormat.SHORT).format(nulls));
if (sText != null && sText.trim().length() > 0)
{
VoiceManager voiceManager = null;
Voice syntheticVoice = null;
try
{
voiceManager = VoiceManager.getInstance();
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.