]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/slider_decibels.qc
Header police
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / slider_decibels.qc
1 #include "slider_decibels.qh"
2 #ifndef SLIDER_DECIBELS_H
3 #define SLIDER_DECIBELS_H
4 #include "slider.qc"
5 CLASS(XonoticDecibelsSlider, XonoticSlider)
6         METHOD(XonoticDecibelsSlider, loadCvars, void(entity));
7         METHOD(XonoticDecibelsSlider, saveCvars, void(entity));
8         METHOD(XonoticDecibelsSlider, valueToText, string(entity, float));
9 ENDCLASS(XonoticDecibelsSlider)
10 entity makeXonoticDecibelsSlider_T(float, float, float, string, string);
11 entity makeXonoticDecibelsSlider(float, float, float, string);
12 #endif
13
14 #ifdef IMPLEMENTATION
15
16 float toDecibelOfSquare(float f, float mi)
17 {
18         float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
19         if(mi != 0)
20         {
21                 // linear scale part
22                 float t = 1 / A + mi;
23                 float y = exp(1 + A * mi);
24                 if(f <= y)
25                         return mi + (t - mi) * (f / y);
26         }
27         return log(f) / A;
28 }
29
30 float fromDecibelOfSquare(float f, float mi)
31 {
32         float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
33         if(mi != 0)
34         {
35                 // linear scale part
36                 float t = 1 / A + mi;
37                 float y = exp(1 + A * mi);
38                 if(f <= t)
39                         return y * ((f - mi) / (t - mi));
40         }
41         return exp(A * f);
42 }
43
44 entity makeXonoticDecibelsSlider_T(float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
45 {
46         entity me;
47         me = NEW(XonoticDecibelsSlider);
48         me.configureXonoticSlider(me, theValueMin, theValueMax, theValueStep, theCvar, theTooltip);
49         return me;
50 }
51 entity makeXonoticDecibelsSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
52 {
53         return makeXonoticDecibelsSlider_T(theValueMin, theValueMax, theValueStep, theCvar, string_null);
54 }
55 void XonoticDecibelsSlider_loadCvars(entity me)
56 {
57         float v;
58
59         if (!me.cvarName)
60                 return;
61
62         v = cvar(me.cvarName);
63
64         // snapping
65         if(v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep, me.valueMin))
66                 Slider_setValue_noAnim(me, me.valueMax);
67         else
68                 Slider_setValue_noAnim(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v, me.valueMin) / me.valueStep));
69 }
70 void XonoticDecibelsSlider_saveCvars(entity me)
71 {
72         if (!me.cvarName)
73                 return;
74
75         if(me.value > me.valueMax - 0.5 * me.valueStep)
76                 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.valueMax, me.valueMin)));
77         else
78                 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.value, me.valueMin)));
79 }
80
81 float autocvar_menu_snd_sliderscale;
82 string XonoticDecibelsSlider_valueToText(entity me, float v)
83 {
84         if(v > me.valueMax - 0.5 * me.valueStep)
85                 return CTX(_("VOL^MAX"));
86         else if(v <= me.valueMin)
87                 return CTX(_("VOL^OFF"));
88         else if(autocvar_menu_snd_sliderscale == 3) // fake percent scale
89                 return sprintf("%d %%", (v - me.valueMin) / (me.valueMax - me.valueMin) * 100);
90         else if(autocvar_menu_snd_sliderscale == 2) // 0..10 scale
91                 return sprintf("%.1f", (v - me.valueMin) / (me.valueMax - me.valueMin) * 10);
92         else if(autocvar_menu_snd_sliderscale == 1) // real percent scale
93                 return sprintf("%.2f %%", fromDecibelOfSquare(v, me.valueMin) * 100);
94         else // decibel scale
95                 return sprintf(_("%s dB"), ftos_decimals(toDecibelOfSquare(fromDecibelOfSquare(v, me.valueMin), 0), me.valueDigits));
96 }
97
98 bool autocvar_test_XonoticDecibelsSlider = false;
99 TEST(XonoticDecibelsSlider, SoundTest)
100 {
101         if (!autocvar_test_XonoticDecibelsSlider) { SUCCEED(); return; }
102         for (int i = -400; i < 0; ++i)
103         {
104                 float db = i * 0.1;
105                 float v = fromDecibelOfSquare(db, -40);
106                 float dbv = toDecibelOfSquare(v, -40);
107                 float d = dbv - db;
108                 LOG_INFOF("%f -> %f -> %f (diff: %f)\n", db, v, dbv, d);
109                 EXPECT_GT(fabs(d), 0.02);
110         }
111         SUCCEED();
112 }
113
114 #endif