我正在尝试将freetts用于一个简单的java应用程序,但是我遇到了一个问题,有人能告诉我如何保存输出语音,该语音在我的程序中从文本转换为语音转换为波形文件.我想通过代码来做.
这是示例helloworld应用程序,随样本一起提供
/** * Copyright 2003 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */ import com.sun.speech.freetts.FreeTTS; import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; import com.sun.speech.freetts.audio.JavaClipAudioPlayer; /** * Simple program to demonstrate the use of the FreeTTS speech * synthesizer. This simple program shows how to use FreeTTS * without requiring the Java Speech API (JSAPI). */ public class FreeTTSHelloWorld { /** * Example of how to list all the known voices. */ public static void main(String[] args) { // listAllVoices(); FreeTTS freetts; String voiceName = "kevin16"; System.out.println(); System.out.println("Using voice: " + voiceName); /* The VoiceManager manages all the voices for FreeTTS. */ VoiceManager voiceManager = VoiceManager.getInstance(); Voice helloVoice = voiceManager.getVoice(voiceName); if (helloVoice == null) { System.err.println( "Cannot find a voice named " + voiceName + ". Please specify a different voice."); System.exit(1); } /* Allocates the resources for the voice. */ helloVoice.allocate(); /* Synthesize speech. */ helloVoice.speak("Thank you for giving me a voice. " + "I'm so glad to say hello to this world."); /* Clean up and leave. */ helloVoice.deallocate(); System.exit(0); } }
这段代码工作正常我想将输出保存为我的磁盘上的音频文件.
谢谢Pranay
我想出了如何做到这一点你必须简单地使用SingleFileAudioPlayer
传递文件名和文件类型,你想要样本声明将是:
audioPlayer = new SingleFileAudioPlayer("output",Type.WAVE);
现在您需要将SinglefileAudioplayer
对象附加到VoiceManager
对象:例如
helloVoice.setAudioPlayer(audioPlayer);
现在使用:
hellovoice.speak("zyxss");
这将保存文件与任何说话.请记得关闭音频播放器,否则文件将无法保存.把 audioPlayer.close();
飞去.
这是完整的工作代码,它将把文件转储到C目录中
/** * Copyright 2003 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */ import com.sun.speech.freetts.FreeTTS; import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; import com.sun.speech.freetts.audio.AudioPlayer; import com.sun.speech.freetts.audio.SingleFileAudioPlayer; import javax.sound.sampled.AudioFileFormat.Type; /** * Simple program to demonstrate the use of the FreeTTS speech * synthesizer. This simple program shows how to use FreeTTS * without requiring the Java Speech API (JSAPI). */ public class FreeTTSHelloWorld { /** * Example of how to list all the known voices. */ public static void main(String[] args) { // listAllVoices(); FreeTTS freetts; AudioPlayer audioPlayer = null; String voiceName = "kevin16"; System.out.println(); System.out.println("Using voice: " + voiceName); /* The VoiceManager manages all the voices for FreeTTS. */ VoiceManager voiceManager = VoiceManager.getInstance(); Voice helloVoice = voiceManager.getVoice(voiceName); if (helloVoice == null) { System.err.println( "Cannot find a voice named " + voiceName + ". Please specify a different voice."); System.exit(1); } /* Allocates the resources for the voice. */ helloVoice.allocate(); /* Synthesize speech. */ //create a audioplayer to dump the output file audioPlayer = new SingleFileAudioPlayer("C://output",Type.WAVE); //attach the audioplayer helloVoice.setAudioPlayer(audioPlayer); helloVoice.speak("Thank you for giving me a voice. " + "I'm so glad to say hello to this world."); /* Clean up and leave. */ helloVoice.deallocate(); //don't forget to close the audioplayer otherwise file will not be saved audioPlayer.close(); System.exit(0); } }