#ifdef IMPLEMENTATION
-float toDecibelOfSquare(float f)
+float toDecibelOfSquare(float f, float mi)
{
- return 20.0 * log10(f);
+ float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
+ if(mi != 0)
+ {
+ // linear scale part
+ float t = 1 / A + mi;
+ float y = exp(1 + A * mi);
+ if(f <= y)
+ return mi + (t - mi) * (f / y);
+ }
+ return log(f) / A;
}
-float fromDecibelOfSquare(float f)
+float fromDecibelOfSquare(float f, float mi)
{
- return pow(10, f / 20.0);
+ float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
+ if(mi != 0)
+ {
+ // linear scale part
+ float t = 1 / A + mi;
+ float y = exp(1 + A * mi);
+ if(f <= t)
+ return y * ((f - mi) / (t - mi));
+ }
+ return exp(A * f);
}
entity makeXonoticDecibelsSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
v = cvar(me.cvarName);
// snapping
- if(v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep))
+ if(v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep, me.valueMin))
Slider_setValue(me, me.valueMax);
- else if(v < fromDecibelOfSquare(me.valueMin - 0.5 * me.valueStep))
- Slider_setValue(me, -1000000); // virtually infinite
else
- Slider_setValue(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v) / me.valueStep) );
+ Slider_setValue(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v, me.valueMin) / me.valueStep) );
}
void XonoticDecibelsSlider_saveCvars(entity me)
{
return;
if(me.value > me.valueMax - 0.5 * me.valueStep)
- cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.valueMax)));
- else if(me.value < me.valueMin - 0.5 * me.valueStep)
- cvar_set(me.cvarName, "0");
+ cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.valueMax, me.valueMin)));
else
- cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.value)));
+ cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.value, me.valueMin)));
}
+float autocvar_menu_snd_sliderscale;
string XonoticDecibelsSlider_valueToText(entity me, float v)
{
if(v > me.valueMax - 0.5 * me.valueStep)
return CTX(_("VOL^MAX"));
- else if(v < me.valueMin - 0.5 * me.valueStep)
+ else if(v <= me.valueMin)
return CTX(_("VOL^OFF"));
- return sprintf(_("%s dB"), SUPER(XonoticDecibelsSlider).valueToText(me, v));
+ else if(autocvar_menu_snd_sliderscale == 2) // 0..10 scale
+ return sprintf(_("%.1f"), (v - me.valueMin) / (me.valueMax - me.valueMin) * 10);
+ else if(autocvar_menu_snd_sliderscale == 1) // percent scale
+ return sprintf(_("%.2f %%"), fromDecibelOfSquare(v, me.valueMin));
+ else // decibel scale
+ return sprintf(_("%s dB"), ftos_decimals(toDecibelOfSquare(fromDecibelOfSquare(v, me.valueMin), 0), me.valueDigits));
}
#endif