How can I store the output voice in an audio file in freetts

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

This is an example helloworld application that is defined with a 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 an audio file on my disk.

Thanks Pranay

+7
java voice-recognition
source share
2 answers

I figured out how to do this, you just need to use SingleFileAudioPlayer to pass the file name and file type for which you want the sample declaration to be as follows:

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

Now you need to attach the SingleFileAudioPlayer object to your VoiceManager object: for example.

 helloVoice.setAudioPlayer(audioPlayer); 

Now use:

 hellovoice.speak("zyxss"); 

This will save the file with what they say there. Do not forget to close the audio player, otherwise the file will not be saved. Put audioPlayer.close(); before leaving.

Here is the full working code that will upload the file to the 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); } } 
+10
source share

I never used FreeTTS, but a quick scan of JavaDocs shows Voice.setWaveDumpFile (String) . Does this do what is required?

0
source share

All Articles