AudioDispatcher dispatcher;
dispatcher= AudioDispatcher.fromDefaultMicrophone(1024, 0);
dispatcher.addAudioProcessor(new AudioProcessor() {
float threshold = -70;
//dB @Override
public boolean process(AudioEvent audioEvent) {
float[] buffer = audioEvent.getFloatBuffer();
double level = soundPressureLevel(buffer);
if(level > threshold){
System.out.println("Sound detected.");
} return true;
}
@Override
public void processingFinished() {}
/**
* Returns the dBSPL for a buffer.
*/
private double soundPressureLevel(final float[] buffer) {
double power = 0.0D; for (float element : buffer) {
power += element * element; }
double value = Math.pow(power, 0.5)/ buffer.length;
return 20.0 * Math.log10(value);
}
});
Comments
Leave a comment