iPhone - AVAudioPlayer - convert decibel level into percent

旧巷老猫 提交于 2019-12-06 05:31:22

问题


I like to update an existing iPhone application which is using AudioQueue for playing audio files. The levels (peakPowerForChannel, averagePowerForChannel) were linear form 0.0f to 1.0f.

Now I like to use the simpler class AVAudioPlayer which works fine, the only issue is that the levels which are now in decibel, not linear from -120.0f to 0.0f.

Has anyone a formula to convert it back to the linear values between 0.0f and 1.0f?

Thanks

Tom


回答1:


Several Apple examples use the following formula to convert the decibels into a linear range (from 0.0 to 1.0):

double percentage = pow (10, (0.05 * power));

where power is the value you get from one of the various level meter methods or functions, such as AVAudioPlayer's averagePowerForChannel:




回答2:


Math behind the Linear and Logarithmic value conversion:

1. Linear to Decibel (logarithmic):

decibelValue = 20.0f * log10(linearValue)

Note: log is base 10

Suppose the linear value in the form of percentage range from [ 0 (min vol) to 100 (max vol)] then the decibelValue for half of the volume (50%) is

decibelValue = 20.0f * log10(50.0f/100.0f) = -6 dB

Full volume:

decibelValue = 20.0f * log10(100.0f/100.0f) = 0 dB

Complete mute:

decibelValue = 20.0f * log10(0/100.0f) = -infinity 

2. Decibel(logarithmic) to Linear:

LinearValue = pow(10.0f, decibelValue/20.0f)



回答3:


Apple uses a lookup table in their SpeakHere sample that converts from dB to a linear value displayed on a level meter.

I moulded their calculation in a small routine; see here.



来源:https://stackoverflow.com/questions/1512131/iphone-avaudioplayer-convert-decibel-level-into-percent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!