Making noise

Now that we have a small collection of voices to choose from, see our previous article, let us turn our attention to making some noise from our Java application.

Just to get us going we will select a voice and use it to play a quote.  The quote we will use for now is “Believe in yourself.  You are braver than you think, more talented than you know, and capable of more than you imagine.”  The voice we will use is the 8-bit Kevin.

So, if we go back to our code in the previous example we can add the following lines:

 

// use the Kevin voice
		Voice voice = vm.getVoice("kevin");
		//Voice voice = vm.getVoice("mbrola_us3");
		voice.allocate();
		
		voice.speak("Believe in yourself.  You are braver than you "
				+ "think, more talented than you know, and capable "
				+ "of more than you imagine.");
		voice.deallocate();

 

When we run the code our program will now speak the words from our sample phrase. You can select some of the other voices to see how things change.

The final code for the 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
 * Making some sound with a selected voice
 * @author jmcneil
 * (c) copyright Software Pulse 2019
 *
 */
public class MakeSound {
	private static Voice[] voices;

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

		System.setProperty("mbrola.base", "c:\\mbrola");
		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();

		// use the Kevin voice
		Voice voice = vm.getVoice("kevin");
		//Voice voice = vm.getVoice("mbrola_us3");
		voice.allocate();
		
		voice.speak("Believe in yourself.  You are braver than you "
				+ "think, more talented than you know, and capable "
				+ "of more than you imagine.");
		voice.deallocate();

	}

}

 

There is no download for this post as the code for this is only a few lines different from the first posting and all the external files were provided as well. Take a look at the JavaFX and adding basic speech post for the download link.

Leave a Reply

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

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