ios - How can I normalized decibel value and make it between 0 and 1 -
i trying power of audio looping on audio samples , getting average power per channel.
i want value between 0 , 1 reflect intensity of audio being played. right getting db (decibel value) float value -160 (near silent) 0 (very loud).
here code written in swift:
func configureaudio() { audiocontroller.play() avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, error: nil) audiocontroller.numberofloops = 0 audiocontroller.meteringenabled = true var audtimer = cadisplaylink(target: self, selector: "monitoraudio") audtimer.addtorunloop(nsrunloop.currentrunloop(), formode: nsrunloopcommonmodes) } func monitoraudio() { audiocontroller.updatemeters() var db = float(0) in 0..<audiocontroller.numberofchannels { db += audiocontroller.averagepowerforchannel(i) } db /= float(audiocontroller.numberofchannels) //code copied website not normalization var power = (int(log10(db+161)/log10(1.5) * 100) - 1220) * 6 if power < 0 { power = 0 } println(power) }
i not sure if db value getting accurate or doing wrong?
right getting values between 0-190 using code found doesn't make sense me.
what need normalize db values getting between 0 - 1
edit - feel free write code in obective-c
averagepowerforchannel
returns db value 0 db
represents digital full scale. if want value between 0 , 1 db not right scale you.
as code kind of hinting @ function turning digital level db
db = 20 * log10(ffs);
so when ffs = 1.0, db 0 , when ffs == 0 db -inf.
you need go other way.
var power = exp(10, db/20);
Comments
Post a Comment