Question

I am trying to fetch noise level of recorded audio in decibals. I am using following code but it is not giving the correct output

byte[] audioData = new byte[bufferSize];
recorder.read(audioData, 0, bufferSize);
ByteBuffer bb = ByteBuffer.wrap(audioData);
int sampleSize = bb.getInt();

now if I log sampleSize then it gives very huge value like 956318464

Can anybody tell how to get correct noise level in decibals.

Was it helpful?

Solution

First off- decibels is a ratio. You can't just get decibels, you need to compare the volume to a baseline measurement. So the real equation in terms of amplitude is

db= 10* log10(amplitude/baseline_amplitude);

If you're recording the audio now, to get the amplitude use MediaRecorder.getMaxAmplitude. For a baseline amplitude, measure the expected background noise.

OTHER TIPS

 public int calculatePowerDb(short[] sdata, int off, int samples)
    {
        double sum = 0;
        double sqsum = 0;
        for (int i = 0; i < samples; i++)
        {
            final long v = sdata[off + i];
            sum += v;
            sqsum += v * v;
        }
        double power = (sqsum - sum * sum / samples) / samples;

        power /= MAX_16_BIT * MAX_16_BIT;

        double result = Math.log10(power) * 10f + FUDGE;
        return (int)result;
    }
    private static final float MAX_16_BIT = 32768;
private static final float FUDGE = 0.6f;

its works fine this methode

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top