2011/07/10

Calculate Audio Level

How to calculate audio level from PCM sample data:

http://en.wikipedia.org/wiki/Root_mean_square
Sample Code
protected void updateLevelAndPeak(ByteBuffer bytebuffer, int bufferLen) { int numFrame = bufferLen / frameSizeInBytes; long sampleSqureTotal = 0L; long peak = 0L; ShortBuffer shortbuffer = bytebuffer.asShortBuffer(); for (int indexFrame = 0; indexFrame < numFrame; indexFrame++) { int sampleValue = 0; for (int indexChannel = 0; indexChannel < channels; indexChannel++) { sampleValue += shortbuffer.get(channels * indexFrame + indexChannel); } sampleValue /= channels; int k = Math.abs(sampleValue); sampleSqureTotal += sampleValue * sampleValue; if ((long) k > peak) { peak = k; } } currentLevel = numFrame <= 0 ? 0.0D : Math.sqrt(sampleSqureTotal / (long) numFrame); currentPeak = peak; }


Reference:
http://wiki.hydrogenaudio.org/index.php?title=Replaygain

No comments:

Post a Comment

Post Code on Blogger

Simplest way to post code to blogger for me: <pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black;overflow-x:...