gpt4 book ai didi

java - 如何将输出语音存储到 freetts 中的音频文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:16:43 24 4
gpt4 key购买 nike

我正在尝试将 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);
}
}

此代码运行良好我想将输出保存为磁盘上的音频文件。

谢谢呼吸机

最佳答案

我想出了如何做到这一点,你必须简单地使用 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);
}
}

关于java - 如何将输出语音存储到 freetts 中的音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4027853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com