freetts初探

freetts初探

FreeTTS是一个语音合成库,今天进行了相关的试用。
1、下载完毕之后,构建工程,拷贝到LIB中的JAR有:en_us.jar、freetts.jar、jsapi.jar、freetts-jsapi10.jar
2、jsapi.jar因为采用的授权不同于freetts,所以需要运行jsapi.exe并同意后来获取
3、需要将speech.properties拷贝到user.home或者java.home/lib下
4、编写基于JSAPI的HelloWorld程序:
import  java.util.Locale;

import  javax.speech.Central;
import  javax.speech.EngineList;
import  javax.speech.synthesis.Synthesizer;
import  javax.speech.synthesis.SynthesizerModeDesc;
import  javax.speech.synthesis.Voice;

public   class  HelloWorld {

    
public  HelloWorld() {
    }

    
public   static   void  listAllVoices(String modeName) {
        System.out.println(
" All  "   +  modeName  +   "  Mode JSAPI Synthesizers and Voices: " );
        SynthesizerModeDesc required 
=   new  SynthesizerModeDesc( null , modeName, Locale.US,  null null );
        EngineList engineList 
=  Central.availableSynthesizers(required);
        
for  ( int  i  =   0 ; i  <  engineList.size(); i ++ ) {
            SynthesizerModeDesc desc 
=  (SynthesizerModeDesc) engineList.get(i);
            System.out.println(
"      "   +  desc.getEngineName()  +   "  (mode= "   +  desc.getModeName()  +   " , locale= "
                    
+  desc.getLocale()  +   " ): " );
            Voice voices[] 
=  desc.getVoices();
            
for  ( int  j  =   0 ; j  <  voices.length; j ++ )
                System.out.println(
"          "   +  voices[j].getName());
        }
    }

    
public   static   void  main(String args[]) {
        
//  利用 FreeTTS 读出Good job
         try  {
            SynthesizerModeDesc desc 
=   new  SynthesizerModeDesc( " FreeTTS en_US general synthesizer " " general " ,
                    Locale.US, 
null null );
            Synthesizer synthesizer 
=  Central.createSynthesizer(desc);
            
if  (synthesizer  ==   null ) {
                System.exit(
1 );
            }
            synthesizer.allocate();
            synthesizer.resume();
            desc 
=  (SynthesizerModeDesc) synthesizer.getEngineModeDesc();
            Voice voices[] 
=  desc.getVoices();
            
for  (Voice v : voices) {
                synthesizer.getSynthesizerProperties().setVoice(v);
                synthesizer.speakPlainText(
" good job " null );
                synthesizer.waitEngineState(
0x10000L );
            }
            synthesizer.deallocate();
        } 
catch  (Exception ex) {
            ex.printStackTrace();
        }
    }
}

5、编写基于FreeTTS的测试程序:
import  com.sun.speech.freetts.Voice;
import  com.sun.speech.freetts.VoiceManager;

public   class  FreeTTSHelloWorld {

    
public  FreeTTSHelloWorld() {
    }

    
public   static   void  listAllVoices() {
        System.out.println();
        System.out.println(
" All voices available: " );
        VoiceManager voiceManager 
=  VoiceManager.getInstance();
        Voice voices[] 
=  voiceManager.getVoices();
        
for  ( int  i  =   0 ; i  <  voices.length; i ++ )
            System.out.println(
"      "   +  voices[i].getName()  +   "  ( "   +  voices[i].getDomain()  +   "  domain) " );

    }

    
public   static   void  main(String args[]) {
        listAllVoices();
        System.out.println();
        VoiceManager voiceManager 
=  VoiceManager.getInstance();
        Voice helloVoice 
=  voiceManager.getVoice( " kevin16 " );
        
if  (helloVoice  ==   null ) {
            System.exit(
1 );
        }
        helloVoice.allocate();
        helloVoice.speak(
" GOOD JOB KINKDING " );
        helloVoice.deallocate();
        System.exit(
0 );
    }
}

你可能感兴趣的:(freetts初探)