JAVA做的语音识别 (via TranXcode)解决思路

JAVA做的语音识别 (via TranXcode)我想用java实现一个 语音识别的软件。比如我说一句‘hello’,软件就会采集

JAVA做的语音识别 (via TranXcode)
我想用java实现一个 语音识别的软件。比如我说一句‘hello’,软件就会采集我说话的特征值,并记录下来,并输出相应值。当别人说‘hello’的时候,采集其特征值,并与我的比较,相同就通过,不同就继续输入。声音的采集主要通过麦克风实现。

[解决办法]
声音识别是一个专门的技术,其实就是对声音波形的判断。如果你想用Java实现的话,那你必须先对Java的声音输入输出的操作有基本的了解。下面是一个简单的录音并输出的程序。录音的时候,每次读取数据是存放在buffer这个字节数组之中的,再把它输出。而你,是要识别的话,就是从这个数组中提取信息。那么这个数组的结构是什么样的呢。对这个程序而言,采用16位双声道录音,那么这个数组,每2位拼起来,是一个采样值,这个值即是声音的响度。将2个字节视为一个单位的话,由于是双声道,就是说,两个单位一组,一个是左声道,一个是右声道,交替录入的。

Java code
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();        }    }}
[解决办法]
那针对上次给你的那个录音程序,我写了一个录音并显示波形的程序。功能如下:
1.点击“开始”开始录音并显示,点击“暂停”即可暂停录音。
2.在暂停录音的状态下,可以移动滚动条查看之前的波形,按“回放”可以回放并显示波形,回放的开始是在当前显示的最左边的波形所对应的声音(不过这里遇到一个问题,即最后一点声音回放不出来,比较奇怪,没找到原因)。
程序如下:
Java code
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();    }