X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fxonotic-data.pk3dir.git;a=blobdiff_plain;f=qcsrc%2Fmenu%2Fxonotic%2Fslider_decibels.c;h=0ea9d161f1e166f358ea53867522eab681ed136e;hp=729bec00693e28f1707c8c12868cfe90d1bbb974;hb=23ae96654035b8cda3da563f346b4507d04d73e8;hpb=7cf65fc959abac34d7791793d8d10e091416df2f diff --git a/qcsrc/menu/xonotic/slider_decibels.c b/qcsrc/menu/xonotic/slider_decibels.c index 729bec006..0ea9d161f 100644 --- a/qcsrc/menu/xonotic/slider_decibels.c +++ b/qcsrc/menu/xonotic/slider_decibels.c @@ -9,14 +9,32 @@ entity makeXonoticDecibelsSlider(float, float, float, string); #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) @@ -36,12 +54,10 @@ void XonoticDecibelsSlider_loadCvars(entity me) 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) { @@ -49,20 +65,41 @@ 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 == 3) // fake percent scale + return sprintf(_("%d %%"), (v - me.valueMin) / (me.valueMax - me.valueMin) * 100); + 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) // real percent scale + return sprintf(_("%.2f %%"), fromDecibelOfSquare(v, me.valueMin) * 100); + else // decibel scale + return sprintf(_("%s dB"), ftos_decimals(toDecibelOfSquare(fromDecibelOfSquare(v, me.valueMin), 0), me.valueDigits)); +} + +void _TEST_XonoticDecibelsSlider() +{ + float i; + for(i = -400; i < 0; ++i) + { + float db = i * 0.1; + float v = fromDecibelOfSquare(db, -40); + float dbv = toDecibelOfSquare(v, -40); + float d = dbv - db; + print(sprintf("%f -> %f -> %f (diff: %f)\n", db, v, dbv, d)); + TEST_Check(fabs(d) > 0.02); + } + TEST_OK(); } #endif