I am using the Android Visualizer to obtain the waveform data of an audio stream and calculate the RMS of it.
public int[] getFormattedData(byte[] rawVizData) {
for (int i = 0; i < formattedVizData.length; i++) {
// convert from unsigned 8 bit to signed 16 bit
int tmp = ((int) rawVizData[i] & 0xFF) - 128;
formattedVizData[i] = tmp;
}
return formattedVizData;
}
//...
double rms = 0;
int[] formattedVizData = getFormattedData(buffer);
if (formattedVizData.length > 0) {
for (int i = 0; i < formattedVizData.length; i++) {
int val = formattedVizData[i];
rms += val * val;
}
rms = Math.sqrt(rms / formattedVizData.length);
}
I set the capture size to 1024 and expected the RMS value of the signal to grow if the playback volume is higher. I test with a 440Hz sine test tone and get the following results:
VOLUME RMS MIN VALUE MAX VALUE 6.67% 5.5 -8 7 13.33% 13.3 -19 18 20.00% 28.1 -40 39 26.67% 42.6 -60 59 33.33% 68 -96 95 40.00% 54 -77 76 46.67% 81 -115 114 53.33% 64 -91 90 60.00% 51 -91 90 66.67% 68 -97 96 73.33% 48 -69 68 80.00% 68 -97 96 86.67% 45 -65 64 93.33% 64 -91 90 100.00% 90 -128 127
Why does my assumption not hold anymore for volumes > 33.33%? What am I missing here? Is there some obvious bug in my code I just seem to be unable to find?
I tried for days now finding out why this is not working and so far couldn't come up with a solution.
Any help is greatly appreciated.