Frage

Ich versuche freetts für eine einfache Java-Anwendung zu verwenden, aber ich bin mit einem Problem konfrontiert, kann mir jemand sagen, wie kann ich spart die Ausgabe Stimme, die von Text zu Sprache in eine Wave-Datei in meinem Programm umgewandelt wird. Ich möchte es über Code tun.

Dies ist die Probe Hello World-Anwendung, die mit der Probe gegeben wird

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

Dieser Code funktioniert gut Ich möchte auf meiner Festplatte die Ausgabe als Audiodatei speichern.

Danke Pranay

War es hilfreich?

Lösung

habe ich herausgefunden, wie zu tun, dass man einfach haben verwenden SingleFileAudioPlayer den Dateinamen und Dateityp übergeben, die Sie Beispieldeklaration wollen sein wie:

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

Nun müssen Sie das SinglefileAudioplayer Objekt zu Ihrem VoiceManager Objekt befestigen: z.B.

helloVoice.setAudioPlayer(audioPlayer);

Jetzt verwenden:

hellovoice.speak("zyxss"); 

Dadurch wird die Datei speichert mit dem, was es in sprechen. Denken Sie daran, den Audioplayer sonst wird die Datei zu schließen, wird nicht gespeichert. Setzen Sie audioPlayer.close(); vor dem Beenden.

Dies ist der komplette Arbeitscode, die Datei in Ihrem Verzeichnis C-Dump wird

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

Andere Tipps

Ich habe noch nie FreeTTS verwendet, aber eine schnelle Abtastung der JavaDocs enthüllt Voice.setWaveDumpFile (String) . Heißt das tun, was erforderlich ist?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top