Amplitude from AudioRecord
Asked Answered
A

1

5

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;
    }
}
Arronarrondissement answered 14/1, 2014 at 17:22 Comment(1)
Can I ask what you are doing with this line: int amplitude = (buffer[0] & 0xff) << 8 | buffer[1]; Also, how are you reporting db-amplitude without first using FFT?Epigeal
P
8
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);
Planet answered 14/1, 2014 at 17:52 Comment(4)
Good job. I finally have some numbers to work with. They are all mostly negative, but at least I can see what is going on. Thanks for your time.Arronarrondissement
I am happy that I could help. :]Planet
why the int amplitude = (buffer[0] & 0xff) << 8 | buffer[1]; ? Thank youTurd
amplitude always negative. Are you sure amplitude which is calculated is correct.Telegony

© 2022 - 2024 — McMap. All rights reserved.