Sound Level

java sound

To know the level of noise one calculates the Root-Mean-Square (RMS) of a sample of audio data.

This code is for 16 bit audio.

private static int calculateRMSLevel(byte[] audioData) { long lSum = 0; for (int i = 0; i < audioData.length; i += 2) { int val = (short) (((audioData[i + 1] & 0xff) << 8) | (audioData[i] & 0xff)); lSum += val; } double dAvg = lSum / audioData.length; double sumMeanSquare = 0d; for (int i = 0; i < audioData.length; i += 2) { int val = (short) (((audioData[i + 1] & 0xff) << 8) | (audioData[i] & 0xff)); sumMeanSquare += Math.pow(val - dAvg, 2d); } double averageMeanSquare = sumMeanSquare / audioData.length / 2; return (int) (Math.pow(averageMeanSquare, 0.5d) + 0.5); }

Reference

No known

History Apr 06, 2018