⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 freetts.java

📁 FreeTTS is a speech synthesis system written entirely in the Java programming language. It is based
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * Portions Copyright 2001-2005 Sun Microsystems, Inc. * Portions Copyright 1999-2001 Language Technologies Institute,  * Carnegie Mellon University. * All Rights Reserved.  Use is subject to license terms. *  * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL  * WARRANTIES. */package com.sun.speech.freetts;import com.sun.speech.freetts.audio.AudioPlayer;import com.sun.speech.freetts.audio.JavaClipAudioPlayer;import com.sun.speech.freetts.audio.JavaStreamingAudioPlayer;import com.sun.speech.freetts.audio.MultiFileAudioPlayer;import com.sun.speech.freetts.audio.NullAudioPlayer;import com.sun.speech.freetts.audio.SingleFileAudioPlayer;import com.sun.speech.freetts.audio.RawFileAudioPlayer;import com.sun.speech.freetts.en.us.CMUDiphoneVoice;import java.io.BufferedReader;import java.io.FileReader;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.IOException;import java.io.Reader;import java.net.URL;import javax.sound.sampled.AudioFileFormat.Type;import javax.sound.sampled.AudioFileFormat;import javax.sound.sampled.AudioSystem;/** * Standalone utility that directly interacts with a CMUDiphoneVoice. */public class FreeTTS {    private final static String VERSION = "FreeTTS 1.2.1 March 10, 2005";    private Voice voice;    private static AudioPlayer audioPlayer = null;    private boolean silent = false;    private String audioFile = null;    private boolean multiAudio = false;    private boolean streamingAudio = false;    private InputMode inputMode = InputMode.INTERACTIVE;                /**     * Constructs a default FreeTTS with the kevin16 voice.     */    public FreeTTS() {        VoiceManager voiceManager = VoiceManager.getInstance();        voiceManager.getVoice("kevin16");    }    /**     * Creates a FreeTTS object with the given Voice.     *     * @param voice the voice to use     */    public FreeTTS(Voice voice) {	this.voice = voice;    }    /**     * Starts this FreeTTS Synthesizer by loading the void and creating     * a new AudioPlayer.     */    public void startup() {	voice.allocate();	if (!getSilentMode()) {	    if (audioFile != null) {		AudioFileFormat.Type type = getAudioType(audioFile);		if (type != null) {		    if (multiAudio) {			audioPlayer = new			    MultiFileAudioPlayer(getBasename(audioFile), type);		    } else 		    audioPlayer = new			SingleFileAudioPlayer(getBasename(audioFile), type);		} else {		    try {			audioPlayer = new RawFileAudioPlayer(audioFile);		    } catch (IOException ioe) {			System.out.println("Can't open " + audioFile +				" " + ioe);		    }		}            } else if (!streamingAudio) {		audioPlayer = new JavaClipAudioPlayer();	    } else {                try {                    audioPlayer = voice.getDefaultAudioPlayer();                } catch (InstantiationException e) {                    e.printStackTrace();                }	    }	}	if (audioPlayer == null) {	    audioPlayer = new NullAudioPlayer();	}                if (false) {            System.out.println("Using " + audioPlayer);        }        	voice.setAudioPlayer(audioPlayer);    }    /**     * Returns the audio type based upon the extension of the given     * file     *     * @param file the file of interest     *      * @return the audio type of the file or null if it is a     *     non-supported type     */    private AudioFileFormat.Type getAudioType(String file) {	AudioFileFormat.Type[] types =	    AudioSystem.getAudioFileTypes();       String extension = getExtension(file);	for (int i = 0; i < types.length; i++) {	    if (types[i].getExtension().equals(extension)) {		return types[i];	    }	}	return null;    }    /**     * Given a filename returns the extension for the file     *     * @param path the path to extract the extension from     *      * @return the extension or <code>null</code> if none     */    private static String getExtension(String path) {	int index = path.lastIndexOf(".");	if (index == -1) {	    return null;	} else {	    return path.substring(index + 1);	}    }    /**     * Given a filename returns the basename for the file     *     * @param path the path to extract the basename from     *      * @return the basename of the file     */    private static String getBasename(String path) {	int index = path.lastIndexOf(".");	if (index == -1) {	    return path;	} else {	    return path.substring(0, index);	}    }    /**     * Shuts down this FreeTTS synthesizer by closing the AudioPlayer     * and voice.     */    public void shutdown() {	audioPlayer.close();	voice.deallocate();    }        /**     * Converts the given text to speech based using processing     * options currently set in FreeTTS.     *     * @param text the text to speak     *     * @return true if the utterance was played properly     */    public boolean textToSpeech(String text) {	return voice.speak(text);    }    /**     * Converts the given text to speech based using processing     * options currently set in FreeTTS.     *     * @param text the text to speak     *     * @return true if the utterance was played properly     */    private boolean batchTextToSpeech(String text) {	boolean ok;	voice.startBatch();	ok = textToSpeech(text);	voice.endBatch();	return ok;    }    /**     * Reads the file pointed to by the given path and     * renders each line as speech individually.     */    private boolean lineToSpeech(String path) {	boolean ok = true;	voice.startBatch();	try {	    BufferedReader reader =  new BufferedReader(new FileReader(path));	    String line;	    while ((line = reader.readLine()) != null && ok) {		ok = textToSpeech(line);	    }	    reader.close();	} catch (IOException ioe) {	    voice.error("can't read " + path);	}	voice.endBatch();	return ok;	    }    /**     * Returns the voice used by FreeTTS.     *     * @return the voice used by freetts     */    protected Voice getVoice() {	return voice;    }        /**     * Converts the text contained in the given stream to speech.     *     * @param is the stream containing the text to speak     */    public boolean streamToSpeech(InputStream is) {	boolean ok;	voice.startBatch();	ok = voice.speak(is);	voice.endBatch();	return ok;    }    /**     * Converts the text contained in the given path to speech.     *     * @param urlPath the file containing the text to speak     *     * @return true if the utterance was played properly     */    public boolean urlToSpeech(String urlPath) {	boolean ok = false;	try {	    URL url = new URL(urlPath);	    InputStream is = url.openStream();	    ok = streamToSpeech(is);	} catch (IOException ioe) {	    System.err.println("Can't read data from " + urlPath);	}	return ok;    }    /**     * Converts the text contained in the given path to speech.     *     * @param filePath the file containing the text to speak     *     * @return true if the utterance was played properly     */    public boolean fileToSpeech(String filePath) {	boolean ok = false;	try {	    InputStream is = new FileInputStream(filePath);	    ok = streamToSpeech(is);	} catch (IOException ioe) {	    System.err.println("Can't read data from " + filePath);	}	return ok;    }    /**     * Turns audio playing on and off.     *     * @param silent if true, don't play audio     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -