如何将输出语音存储到freetts中的音频文件中 [英] how can i store output voice to an audio file in freetts

查看:146
本文介绍了如何将输出语音存储到freetts中的音频文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将freetts用于一个简单的java应用程序,但是我遇到了一个问题,任何人都可以告诉我如何保存输出语音,该语音在我的程序中从文本转换为语音转换为波形文件。我想通过代码来实现。

I am trying to use freetts for a simple java application but i am facing a problem, can anyone tell me how can i save the output voice which is converted from text to speech into a wave file in my program. I want to do it via code.

这是样本helloworld应用程序,它带有样本

This is the sample helloworld application which is given with the sample

/**
 * 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);
    }
}

此代码工作正常我想保存输出作为我磁盘上的音频文件。

This code is working fine I want to save the output as a audio file on my disk.

谢谢
Pranay

Thanks Pranay

推荐答案

我想出了如何做到这一点你必须简单地使用 SingleFileAudioPlayer 传递你想要样本声明的文件名和文件类型:

I figured out how to do that you have to simply use SingleFileAudioPlayer pass the file name and file type which you want sample declaration will be like:

audioPlayer = new SingleFileAudioPlayer("output",Type.WAVE);

现在你需要附上 SinglefileAudioplayer 对象到您的 VoiceManager 对象:例如

Now you need to attach the SinglefileAudioplayer object to your VoiceManager object: e.g.

helloVoice.setAudioPlayer(audioPlayer);

现在使用:

hellovoice.speak("zyxss"); 

这将保存文件中的内容。请记得关闭音频播放器,否则将无法保存文件。在退出之前输入 audioPlayer.close();

This will save the file with whatever there in speak. Remember to close the audioplayer otherwise the file will not be saved. Put audioPlayer.close(); before exiting.

以下是将文件转储到您的完整工作代码C目录

Here is the complete working code which will dump file in your C directory

    /**
     * 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);
        }
    }

这篇关于如何将输出语音存储到freetts中的音频文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆