I'm trying to implement Replay Gain (that is, responding to replaygain tags in music files) into the stock Android/AOSP music player app. This is just a personal project but down the line I could offer it as a patch or maybe make a lib or player of my own. I'm only looking at Track Gain in the first instance. The nature of my question is probably down to the fact I flunked maths ;)
The task is to do two things: A) Before playback starts, clamp the volume to device maximum -14dB (or maybe more, some sources advise -23dB). B) Before playback of each track begins, read its tag and adjust by that number of dB.
I see that adjusting the playback volume in Android involves logarithm scaling as well, but since I'm new to logarithms I'm not sure if that makes things easier or twice (10x?) as hard.
Can anyone please give me the clue I lack on how to translate the decibel values to usable params for MediaPlayer.setVolume()? Thanks for any tips.
EDIT: I have some (probably shaky) assertions that might make the connection needed, but I badly need to be told if any of them are harebrained.
- ReplayGain works on an assumption that the target level of -14 dBFS == 89dB SPL. I therefore pretend these scales are equivalent all the way up and down (I know this is inaccurate, but how inaccurate?)
- I treat MediaPlayer.setVolume(1.0f,1.0f) (the maximum volume) as 0dBFS.
- Based on (1), I equate this to 89+14 = 103dBSPL.
- I treat MediaPlayer.setVolume(0.0f,0.0f) (mute) as 0dBSPL, so I have an abstracted linear volume scale of 0-103.
If these hold water, I'd turn a Track Gain value of -1.3 into the appropriate parameter for MediaPlayer.setVolume() as follows:
- Target loudness = 89 - 1.3 = 87.7
- float newVol = 1.0f - (Math.log(103 - 87.7 / Math.log(103)) = 0.41143207395167
Is this remotely on the right track?