Audio output level in a form that can be converted to decibel

放肆的年华 提交于 2019-12-03 21:24:18

Be aware that there are many different meanings of the word 'deciBel'. It is a means of representing some quantity (such as intensity/power/loudness) relative to a reference point. For audio signals inside equipment, or in an audio application, there is a peak level of 0dB. When sound is emitted from a speaker, the perceived loudness is measured as a Sound Pressure Level, often described as 'dB (SPL)' (or weighted variants such as dBA). When you see the tables of values such as rock concerts at 100dB then this is the SPL that is being described. This measurement is itself relative to a reference level.

So what will have available in the API is the buffer of audio data from which you can easily obtain the audio level in terms of the raw signal (which has a maximum of 0dB). You can't however easily convert this to a physical loudness because this will be dependent on the hardware. It will be different between one model of phone and the next, and will depend on the headphones too. The only way of doing this will be to calibrate the phone by measuring with an SPL meter, but then this will give you a result which will only give reasonable results on this particular phone.

I'm doing it like this:

SLmillibel gain_to_attenuation(float volume)
{
    SLmillibel volume_mb;
    if(volume>=1.0f) volume_mb=SL_MILLIBEL_MAX;
    else if(volume<=0.02f) volume_mb=SL_MILLIBEL_MIN;
    else
    {
        volume_mb=M_LN2/log(1.0f/(1.0f-volume))*-1000.0f;
        if(volume_mb>0) volume_mb=SL_MILLIBEL_MIN;
    }
    return volume_mb;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!