我正在尝试使用freetts为一个简单的java应用程序,但我正面临一个问题,谁能告诉我如何才能保存输出的声音,这是从文本到语音转换成我的程序中的一个wave文件。我想通过代码做到这一点。
这是随示例一起提供的示例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
发布于 2010-10-28 14:36:01
我知道如何做到这一点,你必须简单地使用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);
}
}发布于 2010-10-27 08:19:34
我从来没有用过FreeTTS,但快速浏览一下JavaDocs就会发现Voice.setWaveDumpFile(String)。这是否完成了所需的工作?
https://stackoverflow.com/questions/4027853
复制相似问题