天天看点

FreeTTS Demo

package ttsdemo;

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

/*

It's a simple demo based on FreeTTS to synthetize voice and save to audio file.

The freetts/lib/*.jar should be added to the build path of this project.

*/


public class TTSDemo {
/*

@voice_name: the built in voice or other voice integrated with FreeTTS

@text: the text to be converted to speech

*/

    private String voice_name;
    private String text;
    
    private TTSDemo(){
        this.voice_name = "kevin";  /*set default voice*/
    }

    public void setText(String text) {
        this.text = text; 
    }


/*

@name = kevin||kevin16||alan : built in voice of FreeTTS

*/

    public void setVoice(String name) {
        this.voice_name = name;
    }

    public void speak() {
        Voice voice;
        VoiceManager voiceManager = VoiceManager.getInstance();
        voice = voiceManager.getVoice(voice_name);
        voice.allocate();
        voice.speak(text);
        voice.deallocate();
    }

    public void createAudioFile(String path) {
        Voice voice;
        VoiceManager voiceManager = VoiceManager.getInstance();
        voice = voiceManager.getVoice(voice_name);
        FreeTTS freeTTS = new FreeTTS(voice);
        freeTTS.setAudioFile(path);
        freeTTS.startup();
        voice.allocate();
        voice.speak(text);
        voice.deallocate();
        freeTTS.shutdown();
    }

    public static void main(String[] args) {
        String text = "Hello, World";
        TTSDemo demo = new TTSDemo();
        demo.setText(text);
        demo.speak();
        demo.createAudioFile("freeTTS_demo.wav");
    }
}