2011-12-15 139 views
8

我想讓我的Android應用識別聲音。例如,我想知道麥克風發出的聲音是拍手還是敲門聲或別的東西。Android中的聲音識別

我需要使用數學,還是我可以使用一些庫?

如果有任何聲音分析庫,請告訴我。謝謝。

+0

看看這個帖子:http://stackoverflow.com/questions/2257075/real-time-audio-processing-in-android – coder 2011-12-15 17:53:47

+0

是的,我讀過關於AudioRecord類。該類的Read()方法返回原始數據,需要使用數學進行分析。但是我在問是否有第三方API可以在沒有數學的情況下分析聲音? – Elephant 2011-12-15 19:51:20

回答

2

你不需要數學,你不需要AudioRecord。每隔1000毫秒檢查MediaRecorder.getMaxAmplitude()。

this codethis code可能有幫助。

以下是您需要的一些代碼。

public class Clapper 
{ 
    private static final String TAG = "Clapper"; 

    private static final long DEFAULT_CLIP_TIME = 1000; 
    private long clipTime = DEFAULT_CLIP_TIME; 
    private AmplitudeClipListener clipListener; 

    private boolean continueRecording; 

    /** 
    * how much louder is required to hear a clap 10000, 18000, 25000 are good 
    * values 
    */ 
    private int amplitudeThreshold; 

    /** 
    * requires a little of noise by the user to trigger, background noise may 
    * trigger it 
    */ 
    public static final int AMPLITUDE_DIFF_LOW = 10000; 
    public static final int AMPLITUDE_DIFF_MED = 18000; 
    /** 
    * requires a lot of noise by the user to trigger. background noise isn't 
    * likely to be this loud 
    */ 
    public static final int AMPLITUDE_DIFF_HIGH = 25000; 

    private static final int DEFAULT_AMPLITUDE_DIFF = AMPLITUDE_DIFF_MED; 

    private MediaRecorder recorder; 

    private String tmpAudioFile; 

    public Clapper() throws IOException 
    { 
     this(DEFAULT_CLIP_TIME, "/tmp.3gp", DEFAULT_AMPLITUDE_DIFF, null, null); 
    } 

    public Clapper(long snipTime, String tmpAudioFile, 
      int amplitudeDifference, Context context, AmplitudeClipListener clipListener) 
      throws IOException 
    { 
     this.clipTime = snipTime; 
     this.clipListener = clipListener; 
     this.amplitudeThreshold = amplitudeDifference; 
     this.tmpAudioFile = tmpAudioFile; 
    } 

    public boolean recordClap() 
    { 
     Log.d(TAG, "record clap"); 
     boolean clapDetected = false; 

     try 
     { 
      recorder = AudioUtil.prepareRecorder(tmpAudioFile); 
     } 
     catch (IOException io) 
     { 
      Log.d(TAG, "failed to prepare recorder ", io); 
      throw new RecordingFailedException("failed to create recorder", io); 
     } 

     recorder.start(); 
     int startAmplitude = recorder.getMaxAmplitude(); 
     Log.d(TAG, "starting amplitude: " + startAmplitude); 

     do 
     { 
      Log.d(TAG, "waiting while recording..."); 
      waitSome(); 
      int finishAmplitude = recorder.getMaxAmplitude(); 
      if (clipListener != null) 
      { 
       clipListener.heard(finishAmplitude); 
      } 

      int ampDifference = finishAmplitude - startAmplitude; 
      if (ampDifference >= amplitudeThreshold) 
      { 
       Log.d(TAG, "heard a clap!"); 
       clapDetected = true; 
      } 
      Log.d(TAG, "finishing amplitude: " + finishAmplitude + " diff: " 
        + ampDifference); 
     } while (continueRecording || !clapDetected); 

     Log.d(TAG, "stopped recording"); 
     done(); 

     return clapDetected; 
    } 

    private void waitSome() 
    { 
     try 
     { 
      // wait a while 
      Thread.sleep(clipTime); 
     } catch (InterruptedException e) 
     { 
      Log.d(TAG, "interrupted"); 
     } 
    } 

    /** 
    * need to call this when completely done with recording 
    */ 
    public void done() 
    { 
     Log.d(TAG, "stop recording"); 
     if (recorder != null) 
     { 
      if (isRecording()) 
      { 
       stopRecording(); 
      } 
      //now stop the media player 
      recorder.stop(); 
      recorder.release(); 
     } 
    } 

    public boolean isRecording() 
    { 
     return continueRecording; 
    } 

    public void stopRecording() 
    { 
     continueRecording = false; 
    } 
} 
+4

您示例中的代碼將對任何響亮的聲音做出反應(不僅鼓掌)。它無法識別聲音的性質。我對嗎? – Elephant 2012-03-12 19:49:01

1

我意識到這是一個古老的,但我偶然發現它。我很確定,一般的開放域聲音識別不是一個解決的問題。所以,不,你不會找到任何類型的庫去做你想要的東西,因爲這樣的代碼在任何地方都不存在。如果你挑選一些受限制的領域,你可以訓練一個分類器來識別你感興趣的各種聲音,但這需要大量的數學,以及每個潛在聲音的大量例子。如果你想要的圖書館存在,這將是非常酷的,但據我所知,這項技術還沒有出現。

10

Musicg庫對於哨子檢測很有用。關於拍手,我不會推薦使用它,因爲它會對每一個響亮的聲音(甚至是演講)作出反應。

對於拍手和其他敲擊聲音的檢測,我推薦TarsosDSP。它有一個簡單的API和豐富的功能(音高檢測等)。對於拍手檢測,你可以使用類似(如果你使用TarsosDSPAndroid-V3):

MicrophoneAudioDispatcher mDispatcher = new MicrophoneAudioDispatcher((int) SAMPLE_RATE, BUFFER_SIZE, BUFFER_OVERLAP); 
double threshold = 8; 
double sensitivity = 20; 
mPercussionDetector = new PercussionOnsetDetector(22050, 1024, 
     new OnsetHandler() { 

      @Override 
      public void handleOnset(double time, double salience) { 
       Log.d(TAG, "Clap detected!"); 
      } 
     }, sensitivity, threshold); 
mDispatcher.addAudioProcessor(mPercussionDetector); 
new Thread(mDispatcher).start(); 

您可以調整,通過調整靈敏度(0-100)和閾值(0-20)的探測器。

祝你好運!