基於語音測試須要,須要將文字數據轉換成語音數據,文章未經原做者容許轉載,只用於記錄。java
package com.thinkgem.jeesite.common.utils; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class MSTTSSpeech { private int volume=100;// 聲音:1到100 private int rate=0;// 頻率:-10到10 private int voice=0;// 語音庫序號 private int audio=0;// 輸出設備序號 private ActiveXComponent ax=null; private Dispatch spVoice=null;// 聲音對象 private Dispatch spFileStream=null;// 音頻文件輸出流對象,在讀取或保存音頻文件時使用 private Dispatch spAudioFormat=null;// 音頻格式對象 private Dispatch spMMAudioOut=null;// 音頻輸出對象 private int formatType=22;// 音頻的輸出格式,默認爲:SAFT22kHz16BitMono public MSTTSSpeech() { ComThread.InitSTA(); if(ax==null) { ax=new ActiveXComponent("Sapi.SpVoice"); spVoice=ax.getObject(); } } /** * 改變語音庫 * @param voice 語音庫序號 */ public void changeVoice(int voice) { if(this.voice != voice) { this.voice=voice; } try { Dispatch voiceItems=Dispatch.call(spVoice,"GetVoices").toDispatch(); int count=Integer.valueOf(Dispatch.call(voiceItems,"Count").toString()); if(count>0) { Dispatch voiceItem=Dispatch.call(voiceItems,"Item",new Variant(this.voice)).toDispatch(); Dispatch.put(spVoice,"Voice",voiceItem); } } catch(Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } /** * 改變音頻輸出設備 * @param audio 音頻設備序號 */ public void changeAudioOutput(int audio) { if(this.audio != audio) { this.audio=audio; } try { Dispatch audioOutputs=Dispatch.call(spVoice,"GetAudioOutputs").toDispatch(); int count=Integer.valueOf(Dispatch.call(audioOutputs,"Count").toString()); if(count > 0) { Dispatch audioOutput=Dispatch.call(audioOutputs,"Item",new Variant(this.audio)).toDispatch(); Dispatch.put(spVoice,"AudioOutput",audioOutput); } } catch(Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } /** * 播放語音 * @param text 要轉換成語音的文本 */ public void speak(String text) { this.speak(text,0); } /** * 中止播放語音 */ public void stop() { // this.speak("", 1); Dispatch.call(spVoice,"Pause"); } /** * 播放語音 * @param text 要轉換成語音的文本 * @param type 類型0:播放,1:中止 */ private void speak(String text,int type) { switch(type) { case 0: try { // 調整音量和讀的速度 Dispatch.put(spVoice,"Volume",new Variant(this.volume));// 設置音量 Dispatch.put(spVoice,"Rate",new Variant(this.rate));// 設置速率 // 設置音頻格式類型 if(spAudioFormat==null) { ax=new ActiveXComponent("Sapi.SpAudioFormat"); spAudioFormat=ax.getObject(); ax=new ActiveXComponent("Sapi.SpMMAudioOut"); spMMAudioOut=ax.getObject(); } Dispatch.put(spAudioFormat,"Type",new Variant(this.formatType)); Dispatch.putRef(spMMAudioOut,"Format",spAudioFormat); Dispatch.put(spVoice,"AllowAudioOutputFormatChangesOnNextSet",new Variant(false)); Dispatch.putRef(spVoice,"AudioOutputStream",spMMAudioOut); // 開始朗讀 Dispatch.call(spVoice,"Speak",new Variant(text)); } catch(Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } break; case 1: try { Dispatch.call(spVoice,"Speak",new Variant(text),new Variant(2)); } catch(Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } break; default: break; } } /** * 獲取系統中全部的語音庫名稱數組 * @return String[] */ public String[] getVoices() { String[] voices=null; try { Dispatch voiceItems=Dispatch.call(spVoice,"GetVoices").toDispatch(); int count=Integer.valueOf(Dispatch.call(voiceItems,"Count").toString()); if(count > 0) { voices=new String[count]; for(int i=0;i<count;i++) { Dispatch voiceItem=Dispatch.call(voiceItems,"Item",new Variant(i)).toDispatch(); String voice=Dispatch.call(voiceItem,"GetDescription").toString(); voices[i]=voice; } } } catch(Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } return voices; } /** * 獲取音頻輸出設備名稱數組 * @return String[] */ public String[] getAudioOutputs() { String[] result=null; try { Dispatch audioOutputs=Dispatch.call(spVoice,"GetAudioOutputs").toDispatch(); int count=Integer.valueOf(Dispatch.call(audioOutputs,"Count").toString()); if(count > 0) { result=new String[count]; for(int i=0;i<count;i++) { Dispatch voiceItem=Dispatch.call(audioOutputs,"Item",new Variant(i)).toDispatch(); String voice=Dispatch.call(voiceItem,"GetDescription").toString(); result[i]=voice; } } } catch(Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } return result; } /** * 將文字轉換成音頻信號,而後輸出到.WAV文件 * @param text 文本字符串 * @param filePath 輸出文件路徑 */ public void saveToWav(String text,String filePath) { // 建立輸出文件流對象 ax=new ActiveXComponent("Sapi.SpFileStream"); spFileStream=ax.getObject(); // 建立音頻流格式對象 if(spAudioFormat==null) { ax=new ActiveXComponent("Sapi.SpAudioFormat"); spAudioFormat=ax.getObject(); } // 設置音頻流格式類型 Dispatch.put(spAudioFormat,"Type",new Variant(this.formatType)); // 設置文件輸出流的格式 Dispatch.putRef(spFileStream,"Format",spAudioFormat); // 調用輸出文件流對象的打開方法,建立一個.wav文件 Dispatch.call(spFileStream,"Open",new Variant(filePath),new Variant(3),new Variant(true)); // 設置聲音對象的音頻輸出流爲輸出文件流對象 Dispatch.putRef(spVoice,"AudioOutputStream",spFileStream); // 調整音量和讀的速度 Dispatch.put(spVoice,"Volume",new Variant(this.volume));// 設置音量 Dispatch.put(spVoice,"Rate",new Variant(this.rate));// 設置速率 // 開始朗讀 Dispatch.call(spVoice,"Speak",new Variant(text)); // 關閉輸出文件流對象,釋放資源 Dispatch.call(spFileStream,"Close"); Dispatch.putRef(spVoice,"AudioOutputStream",null); } /** * @return the volume */ public int getVolume() { return volume; } /** * @param volume * the volume to set */ public void setVolume(int volume) { this.volume = volume; } /** * @return the rate */ public int getRate() { return rate; } /** * @param rate * the rate to set */ public void setRate(int rate) { this.rate = rate; } /** * @return the voice */ public int getVoice() { return voice; } /** * @param voice * the voice to set */ public void setVoice(int voice) { this.voice = voice; } /** * @return the audio */ public int getAudio() { return audio; } /** * @param audio * the audio to set */ public void setAudio(int audio) { this.audio=audio; } /** * @return the ax */ public ActiveXComponent getAx() { return ax; } /** * @param ax * the ax to set */ public void setAx(ActiveXComponent ax) { this.ax=ax; } /** * @return the formatType */ public int getFormatType() { return formatType; } /** * 設置音頻輸出格式類型<br> * SAFTDefault = -1<br> * SAFTNoAssignedFormat = 0<br> * SAFTText = 1<br> * SAFTNonStandardFormat = 2<br> * SAFTExtendedAudioFormat = 3<br> * // Standard PCM wave formats<br> * SAFT8kHz8BitMono = 4<br> * SAFT8kHz8BitStereo = 5<br> * SAFT8kHz16BitMono = 6<br> * SAFT8kHz16BitStereo = 7<br> * SAFT11kHz8BitMono = 8<br> * SAFT11kHz8BitStereo = 9<br> * SAFT11kHz16BitMono = 10<br> * SAFT11kHz16BitStereo = 11<br> * SAFT12kHz8BitMono = 12<br> * SAFT12kHz8BitStereo = 13<br> * SAFT12kHz16BitMono = 14<br> * SAFT12kHz16BitStereo = 15<br> * SAFT16kHz8BitMono = 16<br> * SAFT16kHz8BitStereo = 17<br> * SAFT16kHz16BitMono = 18<br> * SAFT16kHz16BitStereo = 19<br> * SAFT22kHz8BitMono = 20<br> * SAFT22kHz8BitStereo = 21<br> * SAFT22kHz16BitMono = 22<br> * SAFT22kHz16BitStereo = 23<br> * SAFT24kHz8BitMono = 24<br> * SAFT24kHz8BitStereo = 25<br> * SAFT24kHz16BitMono = 26<br> * SAFT24kHz16BitStereo = 27<br> * SAFT32kHz8BitMono = 28<br> * SAFT32kHz8BitStereo = 29<br> * SAFT32kHz16BitMono = 30<br> * SAFT32kHz16BitStereo = 31<br> * SAFT44kHz8BitMono = 32<br> * SAFT44kHz8BitStereo = 33<br> * SAFT44kHz16BitMono = 34<br> * SAFT44kHz16BitStereo = 35<br> * SAFT48kHz8BitMono = 36<br> * SAFT48kHz8BitStereo = 37<br> * SAFT48kHz16BitMono = 38<br> * SAFT48kHz16BitStereo = 39<br> * <br> * // TrueSpeech format<br> * SAFTTrueSpeech_8kHz1BitMono = 40<br> * // A-Law formats<br> * SAFTCCITT_ALaw_8kHzMono = 41<br> * SAFTCCITT_ALaw_8kHzStereo = 42<br> * SAFTCCITT_ALaw_11kHzMono = 43<br> * SAFTCCITT_ALaw_11kHzStereo = 4<br> * SAFTCCITT_ALaw_22kHzMono = 44<br> * SAFTCCITT_ALaw_22kHzStereo = 45<br> * SAFTCCITT_ALaw_44kHzMono = 46<br> * SAFTCCITT_ALaw_44kHzStereo = 47<br> * <br> * // u-Law formats<br> * SAFTCCITT_uLaw_8kHzMono = 48<br> * SAFTCCITT_uLaw_8kHzStereo = 49<br> * SAFTCCITT_uLaw_11kHzMono = 50<br> * SAFTCCITT_uLaw_11kHzStereo = 51<br> * SAFTCCITT_uLaw_22kHzMono = 52<br> * SAFTCCITT_uLaw_22kHzStereo = 53<br> * SAFTCCITT_uLaw_44kHzMono = 54<br> * SAFTCCITT_uLaw_44kHzStereo = 55<br> * SAFTADPCM_8kHzMono = 56<br> * SAFTADPCM_8kHzStereo = 57<br> * SAFTADPCM_11kHzMono = 58<br> * SAFTADPCM_11kHzStereo = 59<br> * SAFTADPCM_22kHzMono = 60<br> * SAFTADPCM_22kHzStereo = 61<br> * SAFTADPCM_44kHzMono = 62<br> * SAFTADPCM_44kHzStereo = 63<br> * <br> * // GSM 6.10 formats<br> * SAFTGSM610_8kHzMono = 64<br> * SAFTGSM610_11kHzMono = 65<br> * SAFTGSM610_22kHzMono = 66<br> * SAFTGSM610_44kHzMono = 67<br> * // Other formats<br> * SAFTNUM_FORMATS = 68<br> * * @param formatType * 音頻輸出格式類型 */ public void setFormatType(int formatType) { this.formatType=formatType; } public static void main(String[] args) { MSTTSSpeech speech=new MSTTSSpeech(); String text="這是個人測試,物理內存至少須要512MB,建議2GB以上,虛擬內存是主機物理內存的兩倍,不要設到系統盤,硬盤空閒空間大於4.77GB."; speech.setFormatType(6); // speech.setRate(-1); speech.saveToWav(text,"E://test.wav"); //speech.speak(text); SoundUtil.wavToPcm("E://test.wav"); } }
把wav語音文件轉換爲pcm,其中用的Jar包爲sound.jar,該jar包自帶於jdk裏,不用處處找了。windows
package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; /** * 與聲音有關的工具類 * @author suyunlong * */ public class SoundUtil { /** * 將WAV格式的音頻文件轉換爲PCM格式的文件 * @param wavFilePath WAV文件路徑 * @throws UnsupportedAudioFileException * @throws IOException */ public static void wavToPcm(String wavFilePath) { try { File wavFile=new File(wavFilePath); AudioInputStream sourceAudioInputStream=AudioSystem.getAudioInputStream(wavFile); // 此處的轉換必需是16bit的音頻文件 AudioInputStream targetAudioInputStream=AudioSystem.getAudioInputStream(AudioFormat.Encoding.ULAW, sourceAudioInputStream); String newFilePath=wavFilePath.substring(0,wavFilePath.lastIndexOf(".")+1)+"pcm"; AudioSystem.write(targetAudioInputStream,AudioFileFormat.Type.WAVE,new File(newFilePath)); } catch(UnsupportedAudioFileException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch(IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } /** * 獲取音頻文件的編碼格式 * @param wavFilePath 音頻文件格式 * @return String */ public static String getWavFormat(String wavFilePath) { File wavFile=new File(wavFilePath); AudioInputStream ais; String result=""; try { ais=AudioSystem.getAudioInputStream(wavFile); AudioFormat af=ais.getFormat(); result=af.toString(); } catch(UnsupportedAudioFileException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch(IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } return result; } public static void main(String[] args) { System.out.println(SoundUtil.getWavFormat("E://syl//C//test.wav")); SoundUtil.wavToPcm("E://syl//C//test.wav"); } }