How to Convert File Text to Speech using FreeTTS API

Convert File Text to Speech using FreeTTS API

In this tutorial, I am sharing simple program about, How to Convert File Text to Speech using FreeTTS API. The program has been test and output is shared in the same post.

File Structure

Convert File Text to Speech using FreeTTS API

Resource

Download FreeTTS 1.2.2 API Here

input.txt

FileTextToSpeechConvertor.java

package com.dineshkrish.freetts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class FileTextToSpeechConvertor {

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

		VoiceManager vm = VoiceManager.getInstance();		
		voice = vm.getVoice(VOICE_NAME_KEVIN);
		voice.allocate();
	}
	
	public void speak(File file) {
		
		if(file != null) {
			
			FileInputStream inputStream = null;
			try {
			
				inputStream = new FileInputStream(file);
				voice.speak(inputStream);
				
			} catch (FileNotFoundException e) {
				
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
	}

}

Application.java

package com.dineshkrish.freetts;

import java.io.File;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class Application {

	public static void main(String[] args) {

		// Defining File Object 
		File file = new File("input.txt");
		
		FileTextToSpeechConvertor fttsc = new FileTextToSpeechConvertor();

		// Passing file object as argument
		fttsc.speak(file);
		
		System.out.println("Reading is over!!!!");
		
	}
}

Output

——————

Reading is over!!!!

References

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