JavaFX and adding basic speech

In the last post we took a look at a small application to demonstrate making our program speak and the various voices and settings that can be used to alter the sound.

In this post, we are going to build a small proof of concept project to load the FreeTTS synthesiser and start to use it.

External Files

Before we go too far, we need to download some external files. I like to get this out of the way early because without these the rest of the project will not work.

For this project, we will be using FreeTTS speech synthesiser.  To access information about FreeTTS go here https://freetts.sourceforge.io/, where you can also find the download link to download the zip file containing the library.

The version we use here is freetts-1.2.2 (FreeTTS-1.2.2-bin.zip).  If you are looking to follow the code presented in this series of blogs and want to make sure you use the same versions, then you can also download the files at the bottom of this post.

We will also use the voices that come with Comedia’s cspeech Java Library.  To get this go to https://sourceforge.net/projects/comedia/files/ and select Comedia Speech. We are using verison 1.1 so select that folder and then click to download cspeech-1.1.0-full.zip

The other external files you will need are the MBROLA binary and voices files http://tcts.fpms.ac.be/synthesis/mbrola.html.  The MBROLA project cites its aims as follows:

“The aim of the MBROLA project, initiated by the TCTS Lab of the Faculté Polytechnique de Mons (Belgium), is to obtain a set of speech synthesizers for as many languages as possible and provide them free for non-commercial applications. The ultimate goal is to boost academic research on speech synthesis, and particularly on prosody generation, known as one of the biggest challenges taken up by Text-To-Speech synthesizers for the years to come.”

The files we need from this site are the binary engine of which we need the MS-DOS version. If you are using Mac or Linux there are versions for these but I have not tried them.  We also need some extra voices.  I selected the US1, US2 and US3 voices.  I tried the EN1 British voice but that did not work and many other people have also commented that they have been unable to get it to work.  If you do or you try any other language then let me know in the comments below.

Proof of Concept

Let us now turn our attention to how we put this application together and through this see how we could go about adding speech into our own applications.

I always like to conduct a little proof of concept before sinking too much time in a certain direction.  I like to know I can get the core part of the application working.  I can then worry about the design and the little details afterwards.

Our proof of concept will be to create a small JavaFX application and add the FreeTTS library with the Comdedia voices to the project.  We can then get an instance of the Voice Manager and see what voices we have available.  If we can get this far then we know we can use the FreeTTS library.

Creating the Project

So, down to our proof of concept.  We will start off by creating a plain old Java project.  We will create a JavaFX project later once we know we can get things working.  I will call mine “Speech” but you can call yours whatever best suits.  To our project, we need to add the FreeTTS JAR.

If we take a look at the “FreeTTS-1.2.2-bin.zip” file we downloaded earlier we can see within it there are a whole list of files.  To get our application working we only need to add a few of these to our project and that is what we will do.  Unzip the contents of the zip file to any location you want.  Then navigate to the location “freetts-1.2\lib”.  In this directory, you will find a number of Jar files.  The ones we will be using are:

  • cmulex.jar
  • en_us.jar
  • freetts.jar
  • freetts-jsapi10.jar
  • cmu_us_kal.jar

Go to the build path of the project properties and select “Add External JARS”.  Navigate to where you extracted the zip files and select the jar files listed above.

We now have access to the Java-based voice synthesiser library and basic voices.

The Code

Next, we can create a new class with a main method.  I will call mine BasicSound.  All we are going to do here is get an instance of the FreeTTS VoiceManager, cycle through the available voices and write them out to the console.  So long as there are some voices available to use, we should be able to make some noise.

The code for our class looks like this:

package uk.co.softwarepulse.speech.app;

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

/**
 * Getting started with text to speech
 * What voices do I have?
 * @author jmcneil
 * (c) copyright Software Pulse 2019
 *
 */
public class BasicSound {


	private static Voice[] voices;

	public static void main (String[]args) throws Exception{

		VoiceManager vm;
		// VoiceManager uses the singleton approach to creating and 
		// providing an instance
		vm = VoiceManager.getInstance();
		// Get all the voices which the VoiceManager knows about
		voices = vm.getVoices();

		for( Voice voice:voices ) {
			// Find out what voices are available.
			System.out.println(voice.getName() + " - " + 
			voice.getDescription());
		}
	}
}

The Output

And when we run it, we get the following output:

System property "mbrola.base" is undefined.  Will not use MBROLA voices.
alan - default time-domain cluster unit voice
kevin - default 8-bit diphone voice
kevin16 - default 16-bit diphone voice

If you are interested in downloading the source code and the libraries used in this “Adding basic speech” example you can get them from here.  Please be aware we do ask for your email address so we can send you a link to the download.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.