1 #include "slider_decibels.qh"
3 float toDecibelOfSquare(float f, float mi)
5 float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
10 float u = exp(1 + A * mi);
12 return mi + (t - mi) * (f / u);
17 float fromDecibelOfSquare(float f, float mi)
19 float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
24 float u = exp(1 + A * mi);
26 return u * ((f - mi) / (t - mi));
31 entity makeXonoticDecibelsSlider_T(float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
34 me = NEW(XonoticDecibelsSlider);
35 me.configureXonoticSlider(me, theValueMin, theValueMax, theValueStep, theCvar, theTooltip);
38 entity makeXonoticDecibelsSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
40 return makeXonoticDecibelsSlider_T(theValueMin, theValueMax, theValueStep, theCvar, string_null);
42 void XonoticDecibelsSlider_loadCvars(entity me)
46 if (!me.controlledCvar)
49 v = cvar(me.controlledCvar);
52 if(v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep, me.valueMin))
53 Slider_setValue_noAnim(me, me.valueMax);
55 Slider_setValue_noAnim(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v, me.valueMin) / me.valueStep));
57 void XonoticDecibelsSlider_saveCvars(entity me)
59 if (!me.controlledCvar)
62 if(me.value > me.valueMax - 0.5 * me.valueStep)
63 cvar_set(me.controlledCvar, ftos(fromDecibelOfSquare(me.valueMax, me.valueMin)));
65 cvar_set(me.controlledCvar, ftos(fromDecibelOfSquare(me.value, me.valueMin)));
68 float autocvar_menu_snd_sliderscale;
69 string XonoticDecibelsSlider_valueToText(entity me, float v)
71 if(v > me.valueMax - 0.5 * me.valueStep)
72 return CTX(_("VOL^MAX"));
73 else if(v <= me.valueMin)
74 return CTX(_("VOL^OFF"));
75 else if(autocvar_menu_snd_sliderscale == 3) // fake percent scale
76 return sprintf("%d %%", (v - me.valueMin) / (me.valueMax - me.valueMin) * 100);
77 else if(autocvar_menu_snd_sliderscale == 2) // 0..10 scale
78 return sprintf("%.1f", (v - me.valueMin) / (me.valueMax - me.valueMin) * 10);
79 else if(autocvar_menu_snd_sliderscale == 1) // real percent scale
80 return sprintf("%.2f %%", fromDecibelOfSquare(v, me.valueMin) * 100);
82 return sprintf(_("%s dB"), ftos_decimals(toDecibelOfSquare(fromDecibelOfSquare(v, me.valueMin), 0), me.valueDigits));
85 bool autocvar_test_XonoticDecibelsSlider = false;
86 TEST(XonoticDecibelsSlider, SoundTest)
88 if (!autocvar_test_XonoticDecibelsSlider) { SUCCEED(); return; }
89 for (int i = -400; i < 0; ++i)
92 float v = fromDecibelOfSquare(db, -40);
93 float dbv = toDecibelOfSquare(v, -40);
95 LOG_INFOF("%f -> %f -> %f (diff: %f)", db, v, dbv, d);
96 EXPECT_GT(fabs(d), 0.02);