1. 程式人生 > 程式設計 >Java 實現麥克風自動錄音

Java 實現麥克風自動錄音

  最近在研究語音識別,使用百度的sdk。發現只有識別的部分,而我需要儲存音訊檔案,並且實現當有聲音傳入時自動生成音訊檔案。

  先上程式碼:

public class EngineeCore {

  String filePath = "E:\\voice\\voice_cache.wav";

  AudioFormat audioFormat;
  TargetDataLine targetDataLine;
  boolean flag = true;
private void stopRecognize() {
    flag = false;
    targetDataLine.stop();
    targetDataLine.close();
  }private AudioFormat getAudioFormat() {
    float sampleRate = 16000;
    // 8000,11025,16000,22050,44100
    int sampleSizeInBits = 16;
    // 8,16
    int channels = 1;
    // 1,2
    boolean signed = true;
    // true,false
    boolean bigEndian = false;
    // true,false
    return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);
  }// end getAudioFormat


  private void startRecognize() {
    try {
      // 獲得指定的音訊格式
      audioFormat = getAudioFormat();
      DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class,audioFormat);
      targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);

      // Create a thread to capture the microphone
      // data into an audio file and start the
      // thread running. It will run until the
      // Stop button is clicked. This method
      // will return after starting the thread.
      flag = true;
      new CaptureThread().start();
    } catch (Exception e) {
      e.printStackTrace();
    } // end catch
  }// end captureAudio method

  class CaptureThread extends Thread {
    public void run() {
      AudioFileFormat.Type fileType = null;
      File audioFile = new File(filePath);

      fileType = AudioFileFormat.Type.WAVE;
      //聲音錄入的權值
      int weight = 2;
      //判斷是否停止的計數
      int downSum = 0;

      ByteArrayInputStream bais = null;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      AudioInputStream ais = null;
      try {
        targetDataLine.open(audioFormat);
        targetDataLine.start();
        byte[] fragment = new byte[1024];

        ais = new AudioInputStream(targetDataLine);
        while (flag) {

          targetDataLine.read(fragment,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();
        byte audioData[] = baos.toByteArray();
        bais = new ByteArrayInputStream(audioData);
        ais = new AudioInputStream(bais,audioFormat,audioData.length / audioFormat.getFrameSize());
        //定義最終儲存的檔名
        System.out.println("開始生成語音檔案");
        AudioSystem.write(ais,AudioFileFormat.Type.WAVE,audioFile);
        downSum = 0;
        stopRecognize();

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        //關閉流

        try {
          ais.close();
          bais.close();
          baos.reset();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

    }// end run
  }// end inner class CaptureThread

接下來測試

  public static void main(String args[]) {
    EngineeCore engineeCore = new EngineeCore();

      engineeCore.startRecognize();

  }

  當有較高的聲音傳入麥克風時,targetDataLine讀取的位元組陣列首位或末位絕對值會變大(位置取決於音訊格式中的一些引數,如bigEndian)。傳入音量低,絕對值會變小

錄音開始。從targetDataLine中讀取的音訊資料被儲存在ByteArrayOutputStream中。一段時間音量一直低於權值時,認為無聲音傳入,結束錄音。從ByteArrayOutputStream取出位元組陣列,

轉為音訊儲存在本地檔案中。

  注意:

從targetDataLine讀取的位元組陣列不能直接用於百度等語音識別,需要先轉為音訊檔案,然後讀取音訊檔案生成的位元組陣列,才可用於語音識別。

以上就是Java 實現麥克風自動錄音的詳細內容,更多關於Java 麥克風自動錄音的資料請關注我們其它相關文章!