/*
* Copyright 2012 Greg Milette and Adam Stroud
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package root.gast.audio.interp;
import root.gast.audio.record.AudioClipListener;
import android.util.Log;
/**
* @author Greg Milette <<a
* href="mailto:gregorym@gmail.com">gregorym@gmail.com</a>>
*
*/
public class LoudNoiseDetector implements AudioClipListener
{
private static final String TAG = "LoudNoiseDetector";
private double volumeThreshold;
public static final int DEFAULT_LOUDNESS_THRESHOLD = 2000;
private static final boolean DEBUG = true;
public LoudNoiseDetector()
{
volumeThreshold = DEFAULT_LOUDNESS_THRESHOLD;
}
public LoudNoiseDetector(double volumeThreshold)
{
this.volumeThreshold = volumeThreshold;
}
@Override
public boolean heard(short[] data, int sampleRate)
{
boolean heard = false;
// use rms to take the entire audio signal into account
// and discount any one single high amplitude
double currentVolume = rootMeanSquared(data);
if (DEBUG)
{
Log.d(TAG, "current: " + currentVolume + " threshold: "
+ volumeThreshold);
}
if (currentVolume > volumeThreshold)
{
Log.d(TAG, "heard");
heard = true;
}
return heard;
}
private double rootMeanSquared(short[] nums)
{
double ms = 0;
for (int i = 0; i < nums.length; i++)
{
ms += nums[i] * nums[i];
}
ms /= nums.length;
return Math.sqrt(ms);
}
}