كيف يمكنني تخزين صوت الإخراج إلى ملف صوتي في فريتس

StackOverflow https://stackoverflow.com/questions/4027853

  •  26-09-2019
  •  | 
  •  

سؤال

أحاول استخدام Freetts لتطبيق Java بسيط ، لكنني أواجه مشكلة ، هل يمكن لأي شخص أن يخبرني كيف يمكنني حفظ صوت الإخراج الذي يتم تحويله من نص إلى خطاب إلى ملف موجة في برنامجي. أريد أن أفعل ذلك عبر الكود.

هذا هو تطبيق 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 وإلا فلن يتم حفظ الملف. وضع 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);
        }
    }

نصائح أخرى

لم أستخدم Freetts مطلقًا ، لكن فحصًا سريعًا لـ Javadocs يكشف Voice.SetWavedumpfile (سلسلة). هل هذا يفعل ما هو مطلوب؟

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top