Skip to content

Instantly share code, notes, and snippets.

@h4ck4life
Created September 4, 2013 06:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h4ck4life/6433506 to your computer and use it in GitHub Desktop.
Save h4ck4life/6433506 to your computer and use it in GitHub Desktop.
Android blow detection via microphone. Source: http://stackoverflow.com/a/6186977
public boolean isBlowing()
{
boolean recorder=true;
int minSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);
short[] buffer = new short[minSize];
ar.startRecording();
while(recorder)
{
ar.read(buffer, 0, minSize);
for (short s : buffer)
{
if (Math.abs(s) > 27000) //DETECT VOLUME (IF I BLOW IN THE MIC)
{
blow_value=Math.abs(s);
System.out.println("Blow Value="+blow_value);
ar.stop();
recorder=false;
return true;
}
}
}
return false;
}
@isc30
Copy link

isc30 commented Dec 20, 2015

Nice code, thx

@ysharoiko
Copy link

What is AudioRecord ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment