百度语音java代码 百度语音接口api( 二 )


二、实现语音引擎 语音引擎的实现只包括一个类,四个方法 。它利用了J2SE 1.3包含的Java Sound API 。在这里,我不准备全面地介绍这个API , 但你可以通过实例学习它的用法 。Java Sound API并不是一个特别复杂的API , 代码中的注释将告诉你必须了解的知识 。下面是Talker类的基本定义: package com.lotontech.speech; import javax.sound.sampled.*; import java.io.*; import java.util.*; import java.net.*; public class Talker { private SourceDataLine line=null; } 如果从命令行执行Talker,下面的main()方法将作为入口点运行 。main()方法获取第一个命令行参数,然后把它传递给sayPhoneWord()方法: /* * 读出在命令行中指定的表示读音的字符串 */ public static void main(String args[]) { Talker player=new Talker(); if (args.length0) player.sayPhoneWord(args[0]); System.exit(0); }
sayPhoneWord()方法既可以通过上面的main()方法调用,也可以在Java程序中直接调用 。从表面上看,sayPhoneWord()方法比较复杂,其实并非如此 。实际上,它简单地遍历所有单词的语音元素(在输入字符串中语音元素以“|”分隔),通过一个声音输出通道一个元素一个元素地播放出来 。为了让声音更自然一些 , 我把每一个声音样本的结尾和下一个声音样本的开头合并了起来: /* * 读出指定的语音字符串 */ public void sayPhoneWord(String word) { // 为上一个声音构造的模拟byte数组 byte[] previousSound=null; // 把输入字符串分割成单独的音素 StringTokenizer st=new StringTokenizer(word,"|",false); while (st.hasMoreTokens()) { // 为音素构造相应的文件名字 String thisPhoneFile=st.nextToken(); thisPhoneFile="/allophones/"+thisPhoneFile+".au"; // 从声音文件读取数据 byte[] thisSound=getSound(thisPhoneFile); if (previousSound!=null) { // 如果可能的话,把前一个音素和当前音素合并 int mergeCount=0; if (previousSound.length=500thisSound.length=500) mergeCount=500; for (int i=0; i { previousSound[previousSound.length-mergeCount+i] =(byte)((previousSound[previousSound.length -mergeCount+i]+thisSound[i])/2); } // 播放前一个音素 playSound(previousSound); // 把经过截短的当前音素作为前一个音素 byte[] newSound=new byte[thisSound.length-mergeCount]; for (int ii=0; ii newSound[ii]=thisSound[ii+mergeCount]; previousSound=newSound; } else previousSound=thisSound; } // 播放最后一个音素 , 清理声音通道 playSound(previousSound); drain(); } 在sayPhoneWord()的后面,你可以看到它调用playSound()输出单个声音样本(即一个音素),然后调用drain()清理声音通道 。下面是playSound()的代码: /* * 该方法播放一个声音样本 */ private void playSound(byte[] data) { if (data.length0) line.write(data, 0, data.length); } 下面是drain()的代码: /* * 该方法清理声音通道 */ private void drain() { if (line!=null) line.drain(); try {Thread.sleep(100);} catch (Exception e) {} }
现在回过头来看sayPhoneWord(),这里还有一个方法我们没有分析,即getSound()方法 。getSound()方法从一个au文件以字节数据的形式读入预先录制的声音样本 。要了解读取数据、转换音频格式、初始化声音输出行(SouceDataLine)以及构造字节数据的详细过程,请参考下面代码中的注释: /* * 该方法从文件读取一个音素 ,  * 并把它转换成byte数组 */ private byte[] getSound(String fileName) { try { URL url=Talker.class.getResource(fileName); AudioInputStream stream = AudioSystem.getAudioInputStream(url); AudioFormat format = stream.getFormat(); // 把一个ALAW/ULAW声音转换成PCM以便回放 if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW)) { AudioFormat tmpFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2, format.getFrameRate(), true); stream = AudioSystem.getAudioInputStream(tmpFormat, stream); format = tmpFormat; } DataLine.Info info = new DataLine.Info( Clip.class, format, ((int) stream.getFrameLength() * format.getFrameSize())); if (line==null) { // 输出线还没有实例化 // 是否能够找到合适的输出线类型? DataLine.Info outInfo = new DataLine.Info(SourceDataLine.class, format); if (!AudioSystem.isLineSupported(outInfo)) { System.out.println("不支持匹配" + outInfo + "的输出线"); throw new Exception("不支持匹配" + outInfo + "的输出线"); } // 打开输出线 line = (SourceDataLine) AudioSystem.getLine(outInfo); line.open(format, 50000); line.start(); } int frameSizeInBytes = format.getFrameSize(); int bufferLengthInFrames = line.getBufferSize() / 8; int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; byte[] data=https://www.04ip.com/post/new byte[bufferLengthInBytes]; // 读取字节数据,并计数 int numBytesRead = 0; if ((numBytesRead = stream.read(data)) != -1) { int numBytesRemaining = numBytesRead; } // 把字节数据切割成合适的大小 byte[] newData=new byte[numBytesRead]; for (int i=0; i newData[i]=data[i]; return newData; } catch (Exception e) { return new byte[0]; } } 这就是全部的代码,包括注释在内,一个大约150行代码的语音合成器 。

推荐阅读