I have some code that is supposed to be getting the amplitude from an AudioRecord. Problem is that the math is only returning -Infinity. Can I get some more eyes to look at it with me please:
private class measureSnoreAudio extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
Log.d(TAG, "Creating the buffer of size " + BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
Log.d(TAG, "Creating the AudioRecord");
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDING_RATE, CHANNEL, FORMAT, BUFFER_SIZE * 10);
Log.d(TAG, "AudioRecord recording...");
recorder.startRecording();
while (isRecordingSnore) {
// read the data into the buffer
int read = recorder.read(buffer, 0, buffer.length);
int amplitude = (buffer[0] & 0xff) << 8 | buffer[1];
// Determine amplitude
double amplitudeDb = 20 * Math
.log10(Math.abs(amplitude) / 32768);
String dbString = String.valueOf(amplitudeDb);
Log.d("Snore DB", "dB " + dbString);
//TextView textAmplitude = (TextView) findViewById(R.id.tvAmplitude);
//textAmplitude.setText(dbString);
}
Log.d(TAG, "AudioRecord finished recording");
return null;
}
}
double amplitudeDb = 20 * Math.log10(Math.abs(amplitude) / 32768);
I think maybe the problem is from Math.abs(amplitude) / 32768, amplitude is integer, so Math.abs(amplitude) will also return integer, as Math.abs(amplitude) is less than 32768 (perhaps I am not correct, byte is maximum 2^7 - 1, can here amplitude bigger than 32768? ). So Math.abs(amplitude) / 32768 is equal to 0. Log10(0) is -Infinity, I have tested with a Java project in Eclipse. You can change to
double amplitudeDb = 20 * Math.log10((double)Math.abs(amplitude) / 32768);