8

単純な Java アプリケーションに freetts を使用しようとしていますが、問題に直面しています。テキストから音声に変換された出力音声をプログラムで 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);
    }
}

このコードは正常に動作しています。出力をオーディオ ファイルとしてディスクに保存したいと考えています。

ありがとうプラネー

4

2 に答える 2

14

SingleFileAudioPlayerサンプル宣言が必要なファイル名とファイルタイプを渡すだけで、次のようになります。

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

SinglefileAudioplayer次に、オブジェクトをオブジェクトにアタッチする必要がありVoiceManagerます。例:

helloVoice.setAudioPlayer(audioPlayer);

今使用します:

hellovoice.speak("zyxss"); 

これにより、speakにあるものが何であれファイルが保存されます。オーディオプレーヤーを閉じることを忘れないでください。そうしないと、ファイルが保存されません。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-28T06:36:01.427 に答える
0

私は FreeTTS を使用したことがありませんが、JavaDocs をざっとスキャンするとVoice.setWaveDumpFile(String)が明らかになります。それは必要なことをしますか?

于 2010-10-27T00:19:34.123 に答える