Oct 13, 2022

Android Microphone Data

Android is giving me data I don't understand from AudioRecord.read() and I wanted to document this here incase someone else finds it useful. It took me a while to figure out why the audio data graph (image 1) doesn't look like audio data, the lines are jumps because the signal jumps from -127 to 128 and if you erase those jump lines (image 2) and then center the data (image 3) you can see what I think is the intended audio wave (or an inversion of it).

So when you might expect an audio wave to have values 0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1.

What it's actually outputting is 128, 127, 126, 125, 126, 127, 128, -127, -126, -125, -126, -127 (remember Java stores Byte from -127 to 128 instead of from 0 to 255).

The data in image 3 in the 0-255 range can be calculated with the following code:

int amplitude = audioBytes[i];
if (amplitude > 0)
{
    amplitude = (Byte.MAX_VALUE - amplitude) + 128;
}
else
{
    amplitude = Math.abs(amplitude);
}

Android feels like a train wreck to work with and I seem to run into stuff like this frequently. This may be the intended behaviour but the docs are so sparse I wasn't able to find any comments either way.

Note: Results for Google Pixel 6.




contact@hernblog.com