Pregunta

Estoy intentando utilizar freetts para una aplicación Java sencilla pero estoy frente a un problema, puede alguien decirme ¿Cómo puedo guardar la voz de salida que se convierte de texto a voz en un archivo de onda en mi programa. Quiero hacerlo a través de código.

Esta es la aplicación holamundo muestra que se da con la muestra

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

Este código está trabajando muy bien quiero guardar el resultado como un archivo de audio en mi disco.

Gracias Pranay

¿Fue útil?

Solución

Me di cuenta de cómo hacer que usted tiene que utilizar simplemente SingleFileAudioPlayer pasar el nombre y tipo de archivo que desea ejemplo de declaración será como:

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

Ahora tiene que asociar el objeto SinglefileAudioplayer a su objeto VoiceManager: por ejemplo,

helloVoice.setAudioPlayer(audioPlayer);

Ahora utilice:

hellovoice.speak("zyxss"); 

Esto guarda el archivo con lo que hay en el habla. Recuerde cerrar la audioplayer de lo contrario no se guardará el archivo. Ponga audioPlayer.close(); antes de salir.

Este es el código completo de trabajo que volcar el archivo en el directorio 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);
        }
    }

Otros consejos

Nunca he utilizado FreeTTS, pero un análisis rápido de los JavaDocs revela

scroll top