Xonotic
slider_decibels.qc File Reference
#include "slider_decibels.qh"
+ Include dependency graph for slider_decibels.qc:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

float fromDecibelOfSquare (float f, float mi)
 
entity makeXonoticDecibelsSlider (float theValueMin, float theValueMax, float theValueStep, string theCvar)
 
entity makeXonoticDecibelsSlider_T (float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
 
 TEST (XonoticDecibelsSlider, SoundTest)
 
float toDecibelOfSquare (float f, float mi)
 
void XonoticDecibelsSlider_loadCvars (entity me)
 
void XonoticDecibelsSlider_saveCvars (entity me)
 
string XonoticDecibelsSlider_valueToText (entity me, float v)
 

Variables

float autocvar_menu_snd_sliderscale
 
bool autocvar_test_XonoticDecibelsSlider = false
 

Function Documentation

◆ fromDecibelOfSquare()

float fromDecibelOfSquare ( float  f,
float  mi 
)

Definition at line 17 of file slider_decibels.qc.

References exp(), and log().

Referenced by TEST(), XonoticDecibelsSlider_loadCvars(), and XonoticDecibelsSlider_saveCvars().

18 {
19  float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
20  if(mi != 0)
21  {
22  // linear scale part
23  float t = 1 / A + mi;
24  float u = exp(1 + A * mi);
25  if(f <= t)
26  return u * ((f - mi) / (t - mi));
27  }
28  return exp(A * f);
29 }
float exp(float e)
Definition: mathlib.qc:73
float log(float f)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ makeXonoticDecibelsSlider()

entity makeXonoticDecibelsSlider ( float  theValueMin,
float  theValueMax,
float  theValueStep,
string  theCvar 
)

Definition at line 38 of file slider_decibels.qc.

References makeXonoticDecibelsSlider_T(), and string_null.

39 {
40  return makeXonoticDecibelsSlider_T(theValueMin, theValueMax, theValueStep, theCvar, string_null);
41 }
string string_null
Definition: nil.qh:9
entity makeXonoticDecibelsSlider_T(float theValueMin, float theValueMax, float theValueStep, string theCvar, string theTooltip)
+ Here is the call graph for this function:

◆ makeXonoticDecibelsSlider_T()

entity makeXonoticDecibelsSlider_T ( float  theValueMin,
float  theValueMax,
float  theValueStep,
string  theCvar,
string  theTooltip 
)

Definition at line 31 of file slider_decibels.qc.

References entity(), and NEW.

Referenced by makeXonoticDecibelsSlider().

32 {
33  entity me;
35  me.configureXonoticSlider(me, theValueMin, theValueMax, theValueStep, theCvar, theTooltip);
36  return me;
37 }
#define NEW(cname,...)
Definition: oo.qh:105
entity() spawn
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ TEST()

TEST ( XonoticDecibelsSlider  ,
SoundTest   
)

Definition at line 86 of file slider_decibels.qc.

References autocvar_test_XonoticDecibelsSlider, EXPECT_GT, fabs(), fromDecibelOfSquare(), LOG_INFOF, SUCCEED, toDecibelOfSquare(), and v.

87 {
88  if (!autocvar_test_XonoticDecibelsSlider) { SUCCEED(); return; }
89  for (int i = -400; i < 0; ++i)
90  {
91  float db = i * 0.1;
92  float v = fromDecibelOfSquare(db, -40);
93  float dbv = toDecibelOfSquare(v, -40);
94  float d = dbv - db;
95  LOG_INFOF("%f -> %f -> %f (diff: %f)", db, v, dbv, d);
96  EXPECT_GT(fabs(d), 0.02);
97  }
98  SUCCEED();
99 }
float toDecibelOfSquare(float f, float mi)
float fromDecibelOfSquare(float f, float mi)
#define EXPECT_GT(val1, val2)
Definition: test.qh:61
#define SUCCEED()
Must be present at the end of a test.
Definition: test.qh:15
#define LOG_INFOF(...)
Definition: log.qh:71
bool autocvar_test_XonoticDecibelsSlider
vector v
Definition: ent_cs.qc:116
+ Here is the call graph for this function:

◆ toDecibelOfSquare()

float toDecibelOfSquare ( float  f,
float  mi 
)

Definition at line 3 of file slider_decibels.qc.

References exp(), and log().

Referenced by TEST(), and XonoticDecibelsSlider_loadCvars().

4 {
5  float A = log(10) / 20; // note: about 0.115; inverse: about 8.686
6  if(mi != 0)
7  {
8  // linear scale part
9  float t = 1 / A + mi;
10  float u = exp(1 + A * mi);
11  if(f <= u)
12  return mi + (t - mi) * (f / u);
13  }
14  return log(f) / A;
15 }
float exp(float e)
Definition: mathlib.qc:73
float log(float f)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ XonoticDecibelsSlider_loadCvars()

void XonoticDecibelsSlider_loadCvars ( entity  me)

Definition at line 42 of file slider_decibels.qc.

References cvar(), floor(), fromDecibelOfSquare(), Slider_setValue_noAnim(), toDecibelOfSquare(), and v.

43 {
44  float v;
45 
46  if (!me.controlledCvar)
47  return;
48 
49  v = cvar(me.controlledCvar);
50 
51  // snapping
52  if(v > fromDecibelOfSquare(me.valueMax - 0.5 * me.valueStep, me.valueMin))
53  Slider_setValue_noAnim(me, me.valueMax);
54  else
55  Slider_setValue_noAnim(me, me.valueStep * floor(0.5 + toDecibelOfSquare(v, me.valueMin) / me.valueStep));
56 }
float toDecibelOfSquare(float f, float mi)
void Slider_setValue_noAnim(entity me, float val)
Definition: slider.qc:22
float fromDecibelOfSquare(float f, float mi)
vector v
Definition: ent_cs.qc:116
+ Here is the call graph for this function:

◆ XonoticDecibelsSlider_saveCvars()

void XonoticDecibelsSlider_saveCvars ( entity  me)

Definition at line 57 of file slider_decibels.qc.

References cvar_set(), fromDecibelOfSquare(), and ftos().

58 {
59  if (!me.controlledCvar)
60  return;
61 
62  if(me.value > me.valueMax - 0.5 * me.valueStep)
63  cvar_set(me.controlledCvar, ftos(fromDecibelOfSquare(me.valueMax, me.valueMin)));
64  else
65  cvar_set(me.controlledCvar, ftos(fromDecibelOfSquare(me.value, me.valueMin)));
66 }
float fromDecibelOfSquare(float f, float mi)
+ Here is the call graph for this function:

◆ XonoticDecibelsSlider_valueToText()

string XonoticDecibelsSlider_valueToText ( entity  me,
float  v 
)

Definition at line 69 of file slider_decibels.qc.

References CTX().

70 {
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);
81  else // decibel scale
82  return sprintf(_("%s dB"), ftos_decimals(toDecibelOfSquare(fromDecibelOfSquare(v, me.valueMin), 0), me.valueDigits));
83 }
float toDecibelOfSquare(float f, float mi)
ERASEABLE string ftos_decimals(float number, int decimals)
converts a number to a string with the indicated number of decimals
Definition: string.qh:450
float fromDecibelOfSquare(float f, float mi)
float autocvar_menu_snd_sliderscale
vector v
Definition: ent_cs.qc:116
ERASEABLE string CTX(string s)
Definition: i18n.qh:45
+ Here is the call graph for this function:

Variable Documentation

◆ autocvar_menu_snd_sliderscale

float autocvar_menu_snd_sliderscale

Definition at line 68 of file slider_decibels.qc.

◆ autocvar_test_XonoticDecibelsSlider

bool autocvar_test_XonoticDecibelsSlider = false

Definition at line 85 of file slider_decibels.qc.

Referenced by TEST().