1 #ifndef SLIDER_DECIBELS_H
2 #define SLIDER_DECIBELS_H
4 CLASS(XonoticDecibelsSlider, XonoticSlider)
5 METHOD(XonoticDecibelsSlider, loadCvars, void(entity));
6 METHOD(XonoticDecibelsSlider, saveCvars, void(entity));
7 METHOD(XonoticDecibelsSlider, valueToText, string(entity, float));
8 ENDCLASS(XonoticDecibelsSlider)
9 entity makeXonoticDecibelsSlider_T(float, float, float, string, string);
10 entity makeXonoticDecibelsSlider(float, float, float, string);
15 float toDecibelOfSquare(float f, float mi)
17 float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
22 float y = exp(1 + A * mi);
24 return mi + (t - mi) * (f / y);
29 float fromDecibelOfSquare(float f, float mi)
31 float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
36 float y = exp(1 + A * mi);
38 return y * ((f - mi) / (t - mi));
43 entity makeXonoticDecibelsSlider_T(float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
46 me = NEW(XonoticDecibelsSlider);
47 me.configureXonoticSlider(me, theValueMin, theValueMax, theValueStep, theCvar, theTooltip);
50 entity makeXonoticDecibelsSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
52 return makeXonoticDecibelsSlider_T(theValueMin, theValueMax, theValueStep, theCvar, string_null);
54 void XonoticDecibelsSlider_loadCvars(entity me)
61 v = cvar(me.cvarName);
64 if(v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep, me.valueMin))
65 Slider_setValue_noAnim(me, me.valueMax);
67 Slider_setValue_noAnim(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v, me.valueMin) / me.valueStep));
69 void XonoticDecibelsSlider_saveCvars(entity me)
74 if(me.value > me.valueMax - 0.5 * me.valueStep)
75 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.valueMax, me.valueMin)));
77 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.value, me.valueMin)));
80 float autocvar_menu_snd_sliderscale;
81 string XonoticDecibelsSlider_valueToText(entity me, float v)
83 if(v > me.valueMax - 0.5 * me.valueStep)
84 return CTX(_("VOL^MAX"));
85 else if(v <= me.valueMin)
86 return CTX(_("VOL^OFF"));
87 else if(autocvar_menu_snd_sliderscale == 3) // fake percent scale
88 return sprintf("%d %%", (v - me.valueMin) / (me.valueMax - me.valueMin) * 100);
89 else if(autocvar_menu_snd_sliderscale == 2) // 0..10 scale
90 return sprintf("%.1f", (v - me.valueMin) / (me.valueMax - me.valueMin) * 10);
91 else if(autocvar_menu_snd_sliderscale == 1) // real percent scale
92 return sprintf("%.2f %%", fromDecibelOfSquare(v, me.valueMin) * 100);
94 return sprintf(_("%s dB"), ftos_decimals(toDecibelOfSquare(fromDecibelOfSquare(v, me.valueMin), 0), me.valueDigits));
97 bool autocvar_test_XonoticDecibelsSlider = false;
98 TEST(XonoticDecibelsSlider)
100 if (!autocvar_test_XonoticDecibelsSlider) { TEST_OK(); return; }
101 for (int i = -400; i < 0; ++i)
104 float v = fromDecibelOfSquare(db, -40);
105 float dbv = toDecibelOfSquare(v, -40);
107 LOG_INFOF("%f -> %f -> %f (diff: %f)\n", db, v, dbv, d);
108 TEST_Check(fabs(d) > 0.02);