2010-10-26 51 views
7

我想使用freetts爲簡單的java應用程序,但我面臨一個問題,任何人都可以告訴我如何保存輸出語音從文本轉換爲在我的程序中將語音轉換爲波形文件。我想通過代碼來完成。我如何將輸出語音存儲到音頻文件的freetts

這是與樣品

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

此代碼工作正常我想輸出保存爲我的硬盤上的音頻文件中給出的示例HelloWorld應用程序。

感謝 Pranay

回答

10

我想通了,如何做到這一點,你必須簡單地使用SingleFileAudioPlayer傳遞要樣品宣言將是文件名和文件類型,如:

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

現在,您需要將SinglefileAudioplayer對象附加到您的VoiceManager對象:例如

helloVoice.setAudioPlayer(audioPlayer); 

現在用途:

hellovoice.speak("zyxss"); 

這將保存在講什麼那裏的文件。請記住關閉音頻播放器,否則文件將不會被保存。在退出之前放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); 
     } 
    } 
+0

不錯solution..thanks – hemant 2012-08-28 10:42:40

0

我從來沒有使用過的FreeTTS,但JavaDoc中的快速掃描顯示Voice.setWaveDumpFile(String)。這是做什麼需要?

+0

嗨感謝您的回答,但這並不很多工作 – 2010-10-28 06:21:04

相關問題