VOGONS


First post, by superfury

User metadata
Rank l33t++
Rank
l33t++

I want to convert 0.0-1.0 factors from and to decibel-like factors in the same range(so essentially turning a straight 0-1 line into a dB curve from 0-1 to be better for the human ear. So 0.5 is half as loud as 1.0 when inputted, with the output being in the range of 0.0-1.0).

I already have these two formulas:

//dB vs factor conversions!
#define dB2factor(dB, fMaxLevelDB) pow(10, (((dB) - (fMaxLevelDB)) / 20))
#define factor2dB(factor, fMaxLevelDB) ((fMaxLevelDB) + (20 * log(factor)))

I just want to apply them to convert to and from 0.0-1.0 scale (both logarithmic and linear).

Anyone knows how to do this?

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 1 of 5, by gdjacobs

User metadata
Rank l33t++
Rank
l33t++

The problem is that 0 is difficult in this context, as lim x->inf a^(1/x) = 0, therefore log 0 is not viable in a computing sense.

All hail the Great Capacitor Brand Finder

Reply 2 of 5, by superfury

User metadata
Rank l33t++
Rank
l33t++

So if I force input value 0 to become 0 beforehand, and use input value >0 to convert to 0.0-1.0 using the above functions, how should I do that?

I currently use:

soundchannels[n].volume_percent = p_volume?dB2factor(p_volume,100.0f)*dB2factor(SOUND_VOLUME,100.0f):0; //The volume in linear percent, with 0dB=silence!

To convert from 0.0-100.0(percentage of full volume to sound) to 0.0-1.0 to apply in volume. Anyone knows what's going wrong here?

I want it to:
100.0f => 1.0f (Full volume to the ears)
50.0f => half volume to the ears
25.0f => quarter volume to the ears

Anyone knows if this is the correct way? If not, then what is?

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 3 of 5, by Jepael

User metadata
Rank Oldbie
Rank
Oldbie
superfury wrote:
I want it to: 100.0f => 1.0f (Full volume to the ears) 50.0f => half volume to the ears 25.0f => quarter volume to the ears […]
Show full quote

I want it to:
100.0f => 1.0f (Full volume to the ears)
50.0f => half volume to the ears
25.0f => quarter volume to the ears

Anyone knows if this is the correct way? If not, then what is?

Well that is certainly possible, but most likely not what you want, unless you want to accept the limitations of it.

20*log(x) is for amplitude like sound voltage waveform, 10*log(x) is for power like ear pressure waveform.

100% power to ears = -0dB = 1.0 amplitude
50% power to ears = -3dB = 0.707 amplitude
25% power = -6dB = 0.5 amplitude

If you don't like decibels, another way to calculate directly is that power equals amplitude squared.

If you continue like this (please use LibreOffice or Excel or whatever), you'll see the trend:

at 10% of power, you know that is -10dB, so it's 0.316 in amplitude.
at 1% of power, you know that is -20dB, so it's 0.1 in amplitude.

If you only have your volume levels in integer percentage between 0 and 100%, and the 0% was already defined to be silence and 100% as full amplitude, then you only can control volume with 20dB range, which is pretty much useless for a volume control.

One way to solve is of course to allow the user to set at 0.1% steps, but then you still have 30dB of range. 0.01% steps, 40dB range.

Reply 4 of 5, by superfury

User metadata
Rank l33t++
Rank
l33t++

So I just use base 20 instead?

//Basic dB and factor convertions!
#define dB2factor(dB, fMaxLevelDB) (powf(20, (((dB) - (fMaxLevelDB)) / 20)))
#define factor2dB(factor, fMaxLevelDB) ((fMaxLevelDB) + (20 * log(factor)))
//Convert a volume in the range of 0=0, 100=1 to decibel factor to use with volume multiplication of signals!
#define convertVolume(vol) (factor2dB(((vol)*0.01f+1.0f),0.0f)/factor2dB(1.0f+1.0f,0.0f))

Is this correct? I just use convertVolume to convert the 0.0-1.0 (1.0 is 100%, 0.0 becomes 0.0 manually or directly usable by the normal call, 2.0 becomes double the volume, 3.0 becomes 3x as loud as 100% etc.)? Is that correct?

Author of the UniPCemu emulator.
UniPCemu Git repository
UniPCemu for Android, Windows, PSP, Vita and Switch on itch.io

Reply 5 of 5, by Azarien

User metadata
Rank Oldbie
Rank
Oldbie

You need special cases for 1.0 and 0.0, so that 1.0 will give you full volume exactly (and not almost full due to rounding errors) and 0.0 will give you silence, instead of some theoretical -inf dB that may be still audible in practice.