Convert Text to Speech using FreeTTS API

Hello everyone, I am attaching simple program about Convert Text to Speech using FreeTTS API. The programs are shard in the same post.

To convert text to speech, I am using the third party library called FreeTTS 1.2.2. Which you can download from the following link. Download Here

After downloading the FreeTTS API extract the zip file and navigate to lib folder, then copy and paste it into your eclipse project.

Folder Structure

Convert Text to Speech using FreeTTS API Example in Java

TextToSpeechConvertor.java

package com.javatraineronline;

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

public class TextToSpeechConvertor {

	// Some available voices are (kevin, kevin16, alan)
	private static final String VOICE_NAME_KEVIN = "kevin16";
	private final Voice voice;
	
	public TextToSpeechConvertor() {

		VoiceManager vm = VoiceManager.getInstance();		
		voice = vm.getVoice(VOICE_NAME_KEVIN);
		voice.allocate();
	}

	public void speak(String inputText) {

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

}

Application

package com.javatraineronline;

import java.util.Scanner;

public class Application {

	public static void main(String[] args) {

		// Defining Scanner Object to read data from console
		Scanner inputScanner = new Scanner(System.in);
		
		TextToSpeechConvertor ttsc = new TextToSpeechConvertor();

		System.out.println("Enter the Text : (type 'exit' to terminate)");

		// Reading the text
		String inputText = inputScanner.nextLine();

		while (true) {

			if("exit".equalsIgnoreCase(inputText)) {
				
				inputText = "Bye, See you later";
				ttsc.speak(inputText);
				break;
			}
			
			ttsc.speak(inputText);
			
			System.out.println("Enter the Text : (type 'exit' to terminate)");
			inputText = inputScanner.nextLine();
		}

		inputScanner.close();
		
	}
}

Output

—————
Enter the Text : (type ‘exit’ to terminate)
hello everyone
Enter the Text : (type ‘exit’ to terminate)
exit

References

1. How to Convert Text to Speech using FreeTTS API
2. FreeTTS API