JAVA做的语音识别 Java在这方面有一个开源的项目在做这个……我前不久帮别人用C++做了一个类似功能呢的 不过简单一些 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主可以了解下Linux下的语音识别软件 声音识别是一个专门的技术,其实就是对声音波形的判断。如果你想用Java实现的话,那你必须先对Java的声音输入输出的操作有基本的了解。下面是一个简单的录音并输出的程序。录音的时候,每次读取数据是存放在buffer这个字节数组之中的,再把它输出。而你,是要识别的话,就是从这个数组中提取信息。那么这个数组的结构是什么样的呢。对这个程序而言,采用16位双声道录音,那么这个数组,每2位拼起来,是一个采样值,这个值即是声音的响度。将2个字节视为一个单位的话,由于是双声道,就是说,两个单位一组,一个是左声道,一个是右声道,交替录入的。import java.io.*;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.SourceDataLine;import javax.sound.sampled.DataLine;import javax.sound.sampled.FloatControl;import javax.sound.sampled.TargetDataLine;public class RecordAndPlay { volatile int divider; public RecordAndPlay(){ Play(); } public static void main(String[] args) { new RecordAndPlay(); } //播放音频文件 public void Play() { try { AudioFormat audioFormat =// new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100F,// 8, 1, 1, 44100F, false); new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100F, 16, 2, 4, 44100F, true); DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(info); targetDataLine.open(audioFormat); SourceDataLine sourceDataLine; info = new DataLine.Info(SourceDataLine.class, audioFormat); sourceDataLine = (SourceDataLine) AudioSystem.getLine(info); sourceDataLine.open(audioFormat); targetDataLine.start(); sourceDataLine.start(); FloatControl fc=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); double value=0.2; float dB = (float)(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0); fc.setValue(dB); int nByte = 0; final int bufSize=1024; byte[] buffer = new byte[bufSize]; while (nByte != -1) { //System.in.read(); nByte = targetDataLine.read(buffer, 0, bufSize); sourceDataLine.write(buffer, 0, nByte); } sourceDataLine.stop(); } catch (Exception e) { e.printStackTrace(); } }} 先学习学习 因为原来没接触过 不知道如何下手呀。 你会用sphinx2-0.1 这个我看网上说事JAVA里面专门做语音的 但是下了也不知道该怎么用压 那针对上次给你的那个录音程序,我写了一个录音并显示波形的程序。功能如下:1.点击“开始”开始录音并显示,点击“暂停”即可暂停录音。2.在暂停录音的状态下,可以移动滚动条查看之前的波形,按“回放”可以回放并显示波形,回放的开始是在当前显示的最左边的波形所对应的声音(不过这里遇到一个问题,即最后一点声音回放不出来,比较奇怪,没找到原因)。程序如下:import java.awt.*;import java.awt.event.*;import java.awt.geom.Line2D;import javax.swing.*;import javax.swing.event.*;import java.util.*;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.SourceDataLine;import javax.sound.sampled.DataLine;import javax.sound.sampled.FloatControl;import javax.sound.sampled.TargetDataLine;/** * 2011-6-23 0:53:40 * @author Administrator */public class ShowWave { JFrame frame; JPanel pan; final int panHeight=400; JScrollBar timeLocationScrollBar; int point[]; int number; byte bufferAll[]; int bufferAllIndex; int vRate,hRate; JButton startButton,pauseButton; JButton replayButton,stopReplayButton; boolean continueRecorde; boolean continueReplay; JPanel centerPane,buttonPane; JSlider hSlider; boolean jsbActive; public ShowWave(){ initData(); frame=new JFrame("录音并显示波形"); pan=new JPanel(){ public void paint(Graphics g){ g.setColor(Color.WHITE); g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.setColor(Color.red); int x[]=new int[number]; for(int i=0;i<number;i++){ x[i]=i; point[i]=panHeight-point[i]; } g.drawPolyline(x, point, number);// Graphics2D g2d=(Graphics2D)g;// for(int i=0;i<number-1;i++){// g2d.draw(new Line2D.Double(x[i], point[i], x[i+1], point[i+1]));// } g.setColor(Color.blue); } }; pan.setPreferredSize(new Dimension(600,panHeight)); timeLocationScrollBar=new JScrollBar(); timeLocationScrollBar.setOrientation(JScrollBar.HORIZONTAL); timeLocationScrollBar.setMaximum(0); timeLocationScrollBar.setMinimum(0); timeLocationScrollBar.setValue(0); timeLocationScrollBar.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { if(jsbActive==false){ return; } synchronized(bufferAll){ int beginIndex=timeLocationScrollBar.getValue(); beginIndex=beginIndex*2*hRate; if(beginIndex==0){ number=bufferAllIndex/hRate/2; if(number>600){ number=600; } } else{ number=600; } for(int i=0;i<number;i++,beginIndex+=2*hRate){ int hBit=bufferAll[beginIndex]; int lBit=bufferAll[beginIndex+1]; point[i]=hBit<<8|lBit; point[i]/=vRate; point[i]+=panHeight/2; } pan.repaint(); } } }); centerPane=new JPanel(); centerPane.setLayout(new BorderLayout()); centerPane.add(pan); centerPane.add(timeLocationScrollBar,BorderLayout.SOUTH); frame.getContentPane().add(centerPane); startButton=new JButton("开始"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { continueRecorde=true; jsbActive=false; } }); pauseButton=new JButton("暂停"); pauseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { continueRecorde=false; jsbActive=true; } }); hSlider=new JSlider(); hSlider.setOrientation(JSlider.HORIZONTAL); hSlider.setMaximum(100); hSlider.setMinimum(1); hSlider.setValue(hRate); hSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { hRate=hSlider.getValue(); int length=bufferAllIndex/hRate/2; length-=600; int value=(int)((double)timeLocationScrollBar.getValue()/timeLocationScrollBar.getMaximum()*length); jsbActive=false; timeLocationScrollBar.setMaximum(length); jsbActive=true; timeLocationScrollBar.setValue(value); } }); replayButton=new JButton("回放"); replayButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(continueRecorde||continueReplay){ return; } new Thread(){ public void run(){ try{ continueReplay=true; AudioFormat audioFormat =new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100F, 16, 1, 2,44100F, true); DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); SourceDataLine sourceDataLine; sourceDataLine = (SourceDataLine) AudioSystem.getLine(info); sourceDataLine.open(audioFormat); sourceDataLine.start(); FloatControl fc=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); double value=2; float dB = (float)(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0); fc.setValue(dB); int beginIndex=timeLocationScrollBar.getValue(); beginIndex=beginIndex*2*hRate; int bufSize=1024; byte buffer[]=new byte[bufSize]; while (beginIndex < bufferAllIndex && continueReplay) { synchronized (bufferAll) { int nByte = bufferAllIndex - beginIndex > bufSize ? bufSize : bufferAllIndex - beginIndex; System.arraycopy(bufferAll, beginIndex, buffer, 0, nByte); sourceDataLine.write(buffer, 0, nByte);// System.out.println(beginIndex+" "+bufferAllIndex); beginIndex += nByte; if(beginIndex/2/hRate<=timeLocationScrollBar.getMaximum()){ timeLocationScrollBar.setValue(beginIndex/2/hRate); } } } sourceDataLine.flush(); sourceDataLine.stop(); sourceDataLine.close(); continueReplay=false; }catch(Exception ee){ee.printStackTrace();} } }.start(); } }); stopReplayButton=new JButton("停止回放"); stopReplayButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { continueReplay=false; } }); Box box=Box.createHorizontalBox(); box.add(Box.createHorizontalGlue()); box.add(startButton); box.add(Box.createHorizontalStrut(10)); box.add(pauseButton); box.add(Box.createHorizontalStrut(10)); box.add(hSlider); box.add(Box.createHorizontalStrut(10)); box.add(replayButton); box.add(Box.createHorizontalStrut(10)); box.add(stopReplayButton); box.add(Box.createHorizontalGlue()); box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); frame.getContentPane().add(box,BorderLayout.SOUTH); frame.pack(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); play(); } 代码过长,只能分开贴public void initData(){ point=new int[600]; Arrays.fill(point, 0); number=600; bufferAll=new byte[130*1024*1024]; bufferAllIndex=0; vRate=120; hRate=20;//1470 continueRecorde=false; continueReplay=false; jsbActive=true; } public void play() { try { AudioFormat audioFormat =// new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100F,// 8, 1, 1, 44100F, false); new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100F, 16, 1, 2, 44100F, true); DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat); TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(info); targetDataLine.open(audioFormat); SourceDataLine sourceDataLine; info = new DataLine.Info(SourceDataLine.class, audioFormat); sourceDataLine = (SourceDataLine) AudioSystem.getLine(info); sourceDataLine.open(audioFormat); targetDataLine.start(); sourceDataLine.start(); FloatControl fc=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN); double value=2; float dB = (float)(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0); fc.setValue(dB); int nByte = 0; final int bufSize=256; byte[] buffer = new byte[bufSize]; new Thread(){ public void run(){ while(true){ if(!continueRecorde){ try{ Thread.sleep(50); }catch(InterruptedException ie){} continue; } synchronized(bufferAll){ if(600*hRate*2<bufferAllIndex){ int beginIndex=bufferAllIndex-600*hRate*2; for(int i=0;i<600;i++,beginIndex+=2*hRate){ int hBit=bufferAll[beginIndex]; int lBit=bufferAll[beginIndex+1]; point[i]=hBit<<8|lBit; point[i]/=vRate; point[i]+=panHeight/2; } number=600; pan.repaint(); } else{ int beginIndex=0; number=bufferAllIndex/hRate/2; for(int i=0;i<number;i++,beginIndex+=2*hRate){ int hBit=bufferAll[beginIndex]; int lBit=bufferAll[beginIndex+1]; point[i]=hBit<<8|lBit; point[i]/=vRate; point[i]+=panHeight/2; } pan.repaint(); } int length=bufferAllIndex/hRate/2; if(length>600){ timeLocationScrollBar.setMaximum(length-600); timeLocationScrollBar.setValue(length-600); } } try{ Thread.sleep(10); }catch(InterruptedException ie){} } } }.start(); while (nByte != -1) { if(!continueRecorde){ try{ Thread.sleep(50); }catch(InterruptedException ie){} continue; } synchronized(bufferAll){ nByte = targetDataLine.read(buffer, 0, bufSize); System.arraycopy(buffer, 0, bufferAll, bufferAllIndex, nByte); bufferAllIndex+=nByte; sourceDataLine.write(buffer, 0, nByte); }// try{// Thread.sleep(10);// }catch(InterruptedException ie){} } sourceDataLine.stop(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String args[]){ new ShowWave(); }}真是没道理,三百行也叫长 我也关注一下、、、提取语音的特征(样本)----->存库------->(语音命令)----->库对比。(特定人识别)谁有idea 提取语音特性。 去除噪音----byte (也就是不是我们说的话的) abs(80)还是多少、、 sphinx4正在试,能调通demo,就是识别率很低,我觉得我哪个特征文件没有配置好,就是不知道是什么没配好,有做这块的吗,给个指导 大侠你好,想请教你一个问题。“录音的时候,每次读取数据是存放在buffer这个字节数组之中的,再把它输出。而你,是要识别的话,就是从这个数组中提取信息。那么这个数组的结构是什么样的呢。对这个程序而言,采用16位双声道录音,那么这个数组,每2位拼起来,是一个采样值,这个值即是声音的响度。将2个字节视为一个单位的话,由于是双声道,就是说,两个单位一组,一个是左声道,一个是右声道,交替录入的。”在论坛看过你的贴子,但不太明白。我现在要做一个计算心跳频率的程序,声音录到了,但得到的buffer这个字节数组后,不知道下一步怎么做,求指导,谢谢。 @wenbodong #22 22楼的大侠,你的语音的回放功能在底下可拉动的条拉到最头之后可以实现完整回放,你可以尝试着修改相应的部分,另,我觉着这个声音的播放和声波的产生还是有一定的滞后,不知能否烦劳大侠看看如何解决? 请问这位大侠如何得到buffer字节数组? 这个关机命令怎么用不了 万分紧急的问题... 谢谢各位前辈帮忙...??? 小弟想走J2EE的道路,请高人引路! java 求教..... >>和>>>区别的实例分析例题有二句没有看懂 求阳历转换为农历的方法。赠高分!谢谢 新手上路,Jb8的小问题 SQL:ResultSet is closed 这是什么问题 哪里可以下载到WEBLOGIC 谢谢各位!!! 新人求推荐,,,,一本能辅助我学习Java的书,, java基础中的简单算法,在线等 谁能把这段代码的内存讲清楚啊???
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.TargetDataLine;
public class RecordAndPlay {
volatile int divider;
public RecordAndPlay(){
Play();
}
public static void main(String[] args) {
new RecordAndPlay();
}
//播放音频文件
public void Play() { try {
AudioFormat audioFormat =
// new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100F,
// 8, 1, 1, 44100F, false);
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100F, 16, 2, 4,
44100F, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
audioFormat);
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
SourceDataLine sourceDataLine;
info = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
sourceDataLine.open(audioFormat);
targetDataLine.start();
sourceDataLine.start();
FloatControl fc=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
double value=0.2;
float dB = (float)(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0);
fc.setValue(dB);
int nByte = 0;
final int bufSize=1024;
byte[] buffer = new byte[bufSize];
while (nByte != -1) {
//System.in.read();
nByte = targetDataLine.read(buffer, 0, bufSize);
sourceDataLine.write(buffer, 0, nByte);
}
sourceDataLine.stop(); } catch (Exception e) {
e.printStackTrace();
}
}
}
1.点击“开始”开始录音并显示,点击“暂停”即可暂停录音。
2.在暂停录音的状态下,可以移动滚动条查看之前的波形,按“回放”可以回放并显示波形,回放的开始是在当前显示的最左边的波形所对应的声音(不过这里遇到一个问题,即最后一点声音回放不出来,比较奇怪,没找到原因)。
程序如下:import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.TargetDataLine;
/**
* 2011-6-23 0:53:40
* @author Administrator
*/
public class ShowWave {
JFrame frame;
JPanel pan;
final int panHeight=400;
JScrollBar timeLocationScrollBar;
int point[];
int number;
byte bufferAll[];
int bufferAllIndex;
int vRate,hRate;
JButton startButton,pauseButton;
JButton replayButton,stopReplayButton;
boolean continueRecorde;
boolean continueReplay;
JPanel centerPane,buttonPane;
JSlider hSlider;
boolean jsbActive;
public ShowWave(){
initData();
frame=new JFrame("录音并显示波形");
pan=new JPanel(){
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
int x[]=new int[number];
for(int i=0;i<number;i++){
x[i]=i;
point[i]=panHeight-point[i];
}
g.drawPolyline(x, point, number);
// Graphics2D g2d=(Graphics2D)g;
// for(int i=0;i<number-1;i++){
// g2d.draw(new Line2D.Double(x[i], point[i], x[i+1], point[i+1]));
// }
g.setColor(Color.blue);
}
};
pan.setPreferredSize(new Dimension(600,panHeight));
timeLocationScrollBar=new JScrollBar();
timeLocationScrollBar.setOrientation(JScrollBar.HORIZONTAL);
timeLocationScrollBar.setMaximum(0);
timeLocationScrollBar.setMinimum(0);
timeLocationScrollBar.setValue(0);
timeLocationScrollBar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
if(jsbActive==false){
return;
}
synchronized(bufferAll){
int beginIndex=timeLocationScrollBar.getValue();
beginIndex=beginIndex*2*hRate;
if(beginIndex==0){
number=bufferAllIndex/hRate/2;
if(number>600){
number=600;
}
}
else{
number=600;
}
for(int i=0;i<number;i++,beginIndex+=2*hRate){
int hBit=bufferAll[beginIndex];
int lBit=bufferAll[beginIndex+1];
point[i]=hBit<<8|lBit;
point[i]/=vRate;
point[i]+=panHeight/2;
}
pan.repaint();
}
}
});
centerPane=new JPanel();
centerPane.setLayout(new BorderLayout());
centerPane.add(pan);
centerPane.add(timeLocationScrollBar,BorderLayout.SOUTH);
frame.getContentPane().add(centerPane);
startButton=new JButton("开始");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
continueRecorde=true;
jsbActive=false;
}
});
pauseButton=new JButton("暂停");
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
continueRecorde=false;
jsbActive=true;
}
});
hSlider=new JSlider();
hSlider.setOrientation(JSlider.HORIZONTAL);
hSlider.setMaximum(100);
hSlider.setMinimum(1);
hSlider.setValue(hRate);
hSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
hRate=hSlider.getValue();
int length=bufferAllIndex/hRate/2;
length-=600;
int value=(int)((double)timeLocationScrollBar.getValue()/timeLocationScrollBar.getMaximum()*length);
jsbActive=false;
timeLocationScrollBar.setMaximum(length);
jsbActive=true;
timeLocationScrollBar.setValue(value);
}
});
replayButton=new JButton("回放");
replayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(continueRecorde||continueReplay){
return;
}
new Thread(){
public void run(){
try{
continueReplay=true;
AudioFormat audioFormat =new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
44100F, 16, 1, 2,44100F, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
SourceDataLine sourceDataLine;
sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
FloatControl fc=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
double value=2;
float dB = (float)(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0);
fc.setValue(dB);
int beginIndex=timeLocationScrollBar.getValue();
beginIndex=beginIndex*2*hRate;
int bufSize=1024;
byte buffer[]=new byte[bufSize];
while (beginIndex < bufferAllIndex && continueReplay) {
synchronized (bufferAll) {
int nByte = bufferAllIndex - beginIndex > bufSize ? bufSize : bufferAllIndex - beginIndex;
System.arraycopy(bufferAll, beginIndex, buffer, 0, nByte);
sourceDataLine.write(buffer, 0, nByte);
// System.out.println(beginIndex+" "+bufferAllIndex);
beginIndex += nByte;
if(beginIndex/2/hRate<=timeLocationScrollBar.getMaximum()){
timeLocationScrollBar.setValue(beginIndex/2/hRate);
}
}
}
sourceDataLine.flush();
sourceDataLine.stop();
sourceDataLine.close();
continueReplay=false;
}catch(Exception ee){ee.printStackTrace();}
}
}.start();
}
});
stopReplayButton=new JButton("停止回放");
stopReplayButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
continueReplay=false;
}
});
Box box=Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(startButton);
box.add(Box.createHorizontalStrut(10));
box.add(pauseButton);
box.add(Box.createHorizontalStrut(10));
box.add(hSlider);
box.add(Box.createHorizontalStrut(10));
box.add(replayButton);
box.add(Box.createHorizontalStrut(10));
box.add(stopReplayButton);
box.add(Box.createHorizontalGlue());
box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.getContentPane().add(box,BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
play();
}
point=new int[600];
Arrays.fill(point, 0);
number=600;
bufferAll=new byte[130*1024*1024];
bufferAllIndex=0;
vRate=120;
hRate=20;//1470
continueRecorde=false;
continueReplay=false;
jsbActive=true;
}
public void play() { try {
AudioFormat audioFormat =
// new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100F,
// 8, 1, 1, 44100F, false);
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100F, 16, 1, 2,
44100F, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
audioFormat);
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
SourceDataLine sourceDataLine;
info = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
sourceDataLine.open(audioFormat);
targetDataLine.start();
sourceDataLine.start();
FloatControl fc=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
double value=2;
float dB = (float)(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0);
fc.setValue(dB);
int nByte = 0;
final int bufSize=256;
byte[] buffer = new byte[bufSize];
new Thread(){
public void run(){
while(true){
if(!continueRecorde){
try{
Thread.sleep(50);
}catch(InterruptedException ie){}
continue;
}
synchronized(bufferAll){
if(600*hRate*2<bufferAllIndex){
int beginIndex=bufferAllIndex-600*hRate*2;
for(int i=0;i<600;i++,beginIndex+=2*hRate){
int hBit=bufferAll[beginIndex];
int lBit=bufferAll[beginIndex+1];
point[i]=hBit<<8|lBit;
point[i]/=vRate;
point[i]+=panHeight/2;
}
number=600;
pan.repaint();
}
else{
int beginIndex=0;
number=bufferAllIndex/hRate/2;
for(int i=0;i<number;i++,beginIndex+=2*hRate){
int hBit=bufferAll[beginIndex];
int lBit=bufferAll[beginIndex+1];
point[i]=hBit<<8|lBit;
point[i]/=vRate;
point[i]+=panHeight/2;
}
pan.repaint();
}
int length=bufferAllIndex/hRate/2;
if(length>600){
timeLocationScrollBar.setMaximum(length-600);
timeLocationScrollBar.setValue(length-600);
}
}
try{
Thread.sleep(10);
}catch(InterruptedException ie){}
}
}
}.start();
while (nByte != -1) {
if(!continueRecorde){
try{
Thread.sleep(50);
}catch(InterruptedException ie){}
continue;
}
synchronized(bufferAll){
nByte = targetDataLine.read(buffer, 0, bufSize);
System.arraycopy(buffer, 0, bufferAll, bufferAllIndex, nByte);
bufferAllIndex+=nByte;
sourceDataLine.write(buffer, 0, nByte);
}
// try{
// Thread.sleep(10);
// }catch(InterruptedException ie){}
}
sourceDataLine.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]){
new ShowWave();
}
}真是没道理,三百行也叫长
谁有idea 提取语音特性。
“
录音的时候,每次读取数据是存放在buffer这个字节数组之中的,再把它输出。而你,是要识别的话,就是从这个数组中提取信息。那么这个数组的结构是什么样的呢。对这个程序而言,采用16位双声道录音,那么这个数组,每2位拼起来,是一个采样值,这个值即是声音的响度。将2个字节视为一个单位的话,由于是双声道,就是说,两个单位一组,一个是左声道,一个是右声道,交替录入的。
”在论坛看过你的贴子,但不太明白。我现在要做一个计算心跳频率的程序,声音录到了,但得到的buffer这个字节数组后,不知道下一步怎么做,求指导,谢谢。
请问这位大侠如何得到buffer字节数组?