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
9 float u = exp(1 + A * mi);
11 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
23 float u = exp(1 + A * mi);
25 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)
50 v = cvar(me.cvarName);
53 if (v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep, me.valueMin)) {
54 Slider_setValue_noAnim(me, me.valueMax);
56 Slider_setValue_noAnim(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v, me.valueMin) / me.valueStep));
59 void XonoticDecibelsSlider_saveCvars(entity me)
65 if (me.value > me.valueMax - 0.5 * me.valueStep) {
66 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.valueMax, me.valueMin)));
68 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.value, me.valueMin)));
72 float autocvar_menu_snd_sliderscale;
73 string XonoticDecibelsSlider_valueToText(entity me, float v)
75 if (v > me.valueMax - 0.5 * me.valueStep) {
76 return CTX(_("VOL^MAX"));
77 } else if (v <= me.valueMin) {
78 return CTX(_("VOL^OFF"));
79 } else if (autocvar_menu_snd_sliderscale == 3) { // fake percent scale
80 return sprintf("%d %%", (v - me.valueMin) / (me.valueMax - me.valueMin) * 100);
81 } else if (autocvar_menu_snd_sliderscale == 2) { // 0..10 scale
82 return sprintf("%.1f", (v - me.valueMin) / (me.valueMax - me.valueMin) * 10);
83 } else if (autocvar_menu_snd_sliderscale == 1) { // real percent scale
84 return sprintf("%.2f %%", fromDecibelOfSquare(v, me.valueMin) * 100);
85 } else { // decibel scale
86 return sprintf(_("%s dB"), ftos_decimals(toDecibelOfSquare(fromDecibelOfSquare(v, me.valueMin), 0), me.valueDigits));
90 bool autocvar_test_XonoticDecibelsSlider = false;
91 TEST(XonoticDecibelsSlider, SoundTest)
93 if (!autocvar_test_XonoticDecibelsSlider) { SUCCEED(); return; }
94 for (int i = -400; i < 0; ++i) {
96 float v = fromDecibelOfSquare(db, -40);
97 float dbv = toDecibelOfSquare(v, -40);
99 LOG_INFOF("%f -> %f -> %f (diff: %f)", db, v, dbv, d);
100 EXPECT_GT(fabs(d), 0.02);