2010-10-26 14 views
7

J'essaie d'utiliser freetts pour une application java simple mais je suis confronté à un problème, quelqu'un peut-il me dire comment puis-je enregistrer la voix de sortie qui est convertie du texte à discours dans un fichier wave dans mon programme. Je veux le faire via le code.comment puis-je stocker la voix de sortie dans un fichier audio en freetts

Voici l'exemple d'application HelloWorld qui est donné avec l'échantillon

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

Ce code fonctionne bien, je veux enregistrer la sortie en tant que fichier audio sur mon disque.

Merci Pranay

Répondre

10

je compris comment faire, vous devez utiliser simplement SingleFileAudioPlayer passer le nom de fichier et le type de fichier que vous voulez déclaration de l'échantillon sera comme:

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

Maintenant, vous devez attacher l'objet SinglefileAudioplayer à votre VoiceManager objet: par exemple

helloVoice.setAudioPlayer(audioPlayer); 

utiliser maintenant:

hellovoice.speak("zyxss"); 

Cela enregistrera le fichier avec tout ce qu'il y en parler. N'oubliez pas de fermer l'audioplieur sinon le fichier ne sera pas sauvegardé. Mettez audioPlayer.close(); avant de quitter.

est ici le code de travail complet qui déchargera fichier dans votre répertoire 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

belles solution..thanks beaucoup – hemant

0

Je ne l'ai jamais utilisé FreeTTS, mais une analyse rapide des JavaDocs révèle Voice.setWaveDumpFile(String). Est-ce que cela fait ce qui est requis?

+0

Salut, merci pour la réponse, mais cela ne fonctionne pas –