How to Convert Text to Speech using FreeTTS API

Convert Text to Speech using FreeTTS API

In this tutorial, I am sharing simple program about How to Convert Text to Speech using FreeTTS API. The attached program were tested with environment and output has been shared in the same post.

Folder Structure

Convert Text to Speech using FreeTTS API

Resource

Download FreeTTS 1.2.2 Here

TextToSpeech.java

package com.dineshkrish.freetts;

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

public class TextToSpeech {

	// Voice Name There are few more voice are available (ie: kevin, kevin16, alan)
	private static final String VOICE_NAME = "kevin16";
	private final Voice voice;
	
	public TextToSpeech() {

		VoiceManager voiceManager = VoiceManager.getInstance();		
		voice = voiceManager.getVoice(VOICE_NAME);
		voice.allocate();
	}

	public void speak(String text) {

		if(text != null && !text.isEmpty()) {
			
			voice.speak(text);
		}
	}

}

Application.java

package com.dineshkrish.freetts;

import java.util.Scanner;

public class Application {

	public static void main(String[] args) {

		// Defining Scanner Object to read data from console
		Scanner scanner = new Scanner(System.in);
		
		TextToSpeech tts = new TextToSpeech();

		System.out.println("Enter the Text to Convert to Speech and type 'exit' to terminate !!!");

		// Reading the text
		String text = scanner.nextLine();

		while (!"exit".equalsIgnoreCase(text)) {

			tts.speak(text);
			
			System.out.println("Enter the Text to Convert to Speech and type 'exit' to terminate !!!");
			text = scanner.nextLine();
		}

		scanner.close();
	}
}

Output

—————–
Enter the Text to Convert to Speech and type ‘exit’ to terminate !!!
Hello World
Enter the Text to Convert to Speech and type ‘exit’ to terminate !!!
exit

References

1. FreeTTS
2. FreeTTS API