Java 实现麦克风自动录音
最近在研究语音识别,使用百度的sdk。发现只有识别的部分,而我需要保存音频文件,并且实现当有声音传入时自动生成音频文件。
先上代码:
publicclassEngineeCore{
StringfilePath="E:\\voice\\voice_cache.wav";
AudioFormataudioFormat;
TargetDataLinetargetDataLine;
booleanflag=true;
privatevoidstopRecognize(){
flag=false;
targetDataLine.stop();
targetDataLine.close();
}privateAudioFormatgetAudioFormat(){
floatsampleRate=16000;
//8000,11025,16000,22050,44100
intsampleSizeInBits=16;
//8,16
intchannels=1;
//1,2
booleansigned=true;
//true,false
booleanbigEndian=false;
//true,false
returnnewAudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);
}//endgetAudioFormat
privatevoidstartRecognize(){
try{
//获得指定的音频格式
audioFormat=getAudioFormat();
DataLine.InfodataLineInfo=newDataLine.Info(TargetDataLine.class,audioFormat);
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
//Createathreadtocapturethemicrophone
//dataintoanaudiofileandstartthe
//threadrunning.Itwillrununtilthe
//Stopbuttonisclicked.Thismethod
//willreturnafterstartingthethread.
flag=true;
newCaptureThread().start();
}catch(Exceptione){
e.printStackTrace();
}//endcatch
}//endcaptureAudiomethod
classCaptureThreadextendsThread{
publicvoidrun(){
AudioFileFormat.TypefileType=null;
FileaudioFile=newFile(filePath);
fileType=AudioFileFormat.Type.WAVE;
//声音录入的权值
intweight=2;
//判断是否停止的计数
intdownSum=0;
ByteArrayInputStreambais=null;
ByteArrayOutputStreambaos=newByteArrayOutputStream();
AudioInputStreamais=null;
try{
targetDataLine.open(audioFormat);
targetDataLine.start();
byte[]fragment=newbyte[1024];
ais=newAudioInputStream(targetDataLine);
while(flag){
targetDataLine.read(fragment,0,fragment.length);
//当数组末位大于weight时开始存储字节(有声音传入),一旦开始不再需要判断末位
if(Math.abs(fragment[fragment.length-1])>weight||baos.size()>0){
baos.write(fragment);
System.out.println("守卫:"+fragment[0]+",末尾:"+fragment[fragment.length-1]+",lenght"+fragment.length);
//判断语音是否停止
if(Math.abs(fragment[fragment.length-1])<=weight){
downSum++;
}else{
System.out.println("重置奇数");
downSum=0;
}//计数超过20说明此段时间没有声音传入(值也可更改)
if(downSum>20){
System.out.println("停止录入");
break;
}
}
}
//取得录音输入流
audioFormat=getAudioFormat();
byteaudioData[]=baos.toByteArray();
bais=newByteArrayInputStream(audioData);
ais=newAudioInputStream(bais,audioFormat,audioData.length/audioFormat.getFrameSize());
//定义最终保存的文件名
System.out.println("开始生成语音文件");
AudioSystem.write(ais,AudioFileFormat.Type.WAVE,audioFile);
downSum=0;
stopRecognize();
}catch(Exceptione){
e.printStackTrace();
}finally{
//关闭流
try{
ais.close();
bais.close();
baos.reset();
}catch(IOExceptione){
e.printStackTrace();
}
}
}//endrun
}//endinnerclassCaptureThread
接下来测试
publicstaticvoidmain(Stringargs[]){
EngineeCoreengineeCore=newEngineeCore();
engineeCore.startRecognize();
}
当有较高的声音传入麦克风时,targetDataLine读取的字节数组首位或末位绝对值会变大(位置取决于音频格式中的一些参数,如bigEndian)。传入音量低,绝对值会变小
录音开始。从targetDataLine中读取的音频数据被保存在ByteArrayOutputStream中。一段时间音量一直低于权值时,认为无声音传入,结束录音。从ByteArrayOutputStream取出字节数组,
转为音频保存在本地文件中。
注意:
从targetDataLine读取的字节数组不能直接用于百度等语音识别,需要先转为音频文件,然后读取音频文件生成的字节数组,才可用于语音识别。
以上就是Java实现麦克风自动录音的详细内容,更多关于Java麦克风自动录音的资料请关注毛票票其它相关文章!
