]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/slider_decibels.qc
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / slider_decibels.qc
1 #include "slider_decibels.qh"
2
3 float toDecibelOfSquare(float f, float mi)
4 {
5         float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
6         if (mi != 0) {
7                 // linear scale part
8                 float t = 1 / A + mi;
9                 float u = exp(1 + A * mi);
10                 if (f <= u) {
11                         return mi + (t - mi) * (f / u);
12                 }
13         }
14         return log(f) / A;
15 }
16
17 float fromDecibelOfSquare(float f, float mi)
18 {
19         float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
20         if (mi != 0) {
21                 // linear scale part
22                 float t = 1 / A + mi;
23                 float u = exp(1 + A * mi);
24                 if (f <= t) {
25                         return u * ((f - mi) / (t - mi));
26                 }
27         }
28         return exp(A * f);
29 }
30
31 entity makeXonoticDecibelsSlider_T(float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
32 {
33         entity me;
34         me = NEW(XonoticDecibelsSlider);
35         me.configureXonoticSlider(me, theValueMin, theValueMax, theValueStep, theCvar, theTooltip);
36         return me;
37 }
38 entity makeXonoticDecibelsSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
39 {
40         return makeXonoticDecibelsSlider_T(theValueMin, theValueMax, theValueStep, theCvar, string_null);
41 }
42 void XonoticDecibelsSlider_loadCvars(entity me)
43 {
44         float v;
45
46         if (!me.cvarName) {
47                 return;
48         }
49
50         v = cvar(me.cvarName);
51
52         // snapping
53         if (v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep, me.valueMin)) {
54                 Slider_setValue_noAnim(me, me.valueMax);
55         } else {
56                 Slider_setValue_noAnim(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v, me.valueMin) / me.valueStep));
57         }
58 }
59 void XonoticDecibelsSlider_saveCvars(entity me)
60 {
61         if (!me.cvarName) {
62                 return;
63         }
64
65         if (me.value > me.valueMax - 0.5 * me.valueStep) {
66                 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.valueMax, me.valueMin)));
67         } else {
68                 cvar_set(me.cvarName, ftos(fromDecibelOfSquare(me.value, me.valueMin)));
69         }
70 }
71
72 float autocvar_menu_snd_sliderscale;
73 string XonoticDecibelsSlider_valueToText(entity me, float v)
74 {
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));
87         }
88 }
89
90 bool autocvar_test_XonoticDecibelsSlider = false;
91 TEST(XonoticDecibelsSlider, SoundTest)
92 {
93         if (!autocvar_test_XonoticDecibelsSlider) { SUCCEED(); return; }
94         for (int i = -400; i < 0; ++i) {
95                 float db = i * 0.1;
96                 float v = fromDecibelOfSquare(db, -40);
97                 float dbv = toDecibelOfSquare(v, -40);
98                 float d = dbv - db;
99                 LOG_INFOF("%f -> %f -> %f (diff: %f)", db, v, dbv, d);
100                 EXPECT_GT(fabs(d), 0.02);
101         }
102         SUCCEED();
103 }