Android录音时,根据PCM数据获取音量值(单位分贝)


Android录音时,根据PCM数据获取音量值(单位分贝)

采样值为16bit时,根据pcm数据获取分贝,可以按如下方法进行计算


private void calcDecibelLevel(short[] buffer, int readSize) {

    double sum = 0;

    for (short rawSample : buffer) {
        double sample = rawSample / 32768.0;
        sum += sample * sample;
    }

    double rms = Math.sqrt(sum / readSize);
    final double db = 20 * Math.log10(rms);

    mVolume = (int)db;
    Log.e(TAG, "calcDecibelLevel:volume = " + mVolume + ", readSize = " + readSize);
}

你可能感兴趣的:(Java,Android)