Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - AVAudioPlayer - convert decibel level into percent

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

like image 552
crashtesttommy Avatar asked Oct 02 '09 22:10

crashtesttommy


3 Answers

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:

like image 152
invalidname Avatar answered Nov 04 '22 20:11

invalidname


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)
like image 44
SridharKritha Avatar answered Nov 04 '22 20:11

SridharKritha


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.

like image 37
meaning-matters Avatar answered Nov 04 '22 19:11

meaning-matters