Ok, I thought this was simple and maybe I'm doing something dumb. I want to show the approximate / average volume of a sound. I did the following -
sampleTotal += Math.abs(recordBuffer * 256) + Math.abs(recordBuffer);
sampleTotal -= averageSampleBuffer[leadingSample];
averageSampleBuffer[leadingSample] = Math.abs(recordBuffer * 256) + Math.abs(recordBuffer);
averageVolume = sampleTotal / averageSampleBuffer.length;
leadingSample = leadingSample % averageSampleBuffer.length;
What I thought I could do was sample the first 2 bytes of a signed 16 bit audio stream every so often. The sampling would occur as fast as my polling loop. This doesn't seem to work though. If I normalize the averageVolume, I don't seem to get above the half way point. There also seems to be too much volume, even when things are quite.
Whats the right way to do this? I want to show graphically the volume, but ultimately this is going to be used as a VOX, rather than having push a button to start and stop recording.