2011-12-15 143 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)的探测器。

祝你好运!