Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
signalmath.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_SIGNALUTILS_HH__
3#define __ASE_SIGNALUTILS_HH__
4
5#include <ase/mathutils.hh>
6
7namespace Ase {
8
10template<typename Float> Float hz_changed (Float a, Float b);
11
13template<typename Float> Float voltage2hz (Float x);
14
16float fast_voltage2hz (float x);
17
19template<typename Float> Float hz2voltage (Float x);
20
22float fast_hz2voltage (float x);
23
25template<typename Float> Float db_changed (Float a, Float b);
26
28template<typename Float> Float voltage2db (Float x);
29
31float fast_voltage2db (float x);
32
34template<typename Float> Float db2voltage (Float x);
35
37float fast_db2voltage (float x);
38
40template<typename Float> Float voltage_changed (Float a, Float b);
41
43struct Logscale {
44 double b2 = 0; // log2 (begin)
45 double r2 = 1, ir = 1; // range
47 void
48 setup (double min, double max)
49 {
50 b2 = ::log2l (min);
51 auto range = ::log2l (max) - b2;
52 ir = 1.0 / range;
53 r2 = range;
54 }
56 double
57 scale (double normalized) const
58 {
59 return ::exp2 (b2 + normalized * r2);
60 }
62 double
63 iscale (double mmvalue) const
64 {
65 return (::log2 (mmvalue) - b2) * ir;
66 }
67};
68
69// == Implementations ==
70static constexpr const long double c3_hertz = 261.6255653005986346778499935233; // 440 * 2^(-9/12)
71static constexpr const long double c3_hertz_inv = 0.0038222564329714297410505703465146; // 1 / c3_hertz
72
73template<typename Float> extern inline ASE_CONST Float
74voltage2hz (Float x)
75{
76 return std::exp2 (x * Float (10.0)) * Float (c3_hertz);
77}
78
79extern inline ASE_CONST float
81{
82 return fast_exp2 (x * 10.0f) * float (c3_hertz);
83}
84
85template<typename Float> extern inline ASE_CONST Float
86hz2voltage (Float x)
87{
88 return std::log2 (x * Float (c3_hertz_inv)) * Float (0.1);
89}
90
91extern inline ASE_CONST float
93{
94 return fast_log2 (x * float (c3_hertz_inv)) * 0.1f;
95}
96
97template<typename Float> extern inline ASE_CONST Float
98voltage2db (Float x)
99{
100 return std::log10 (std::abs (x)) * Float (20);
101}
102
103extern inline ASE_CONST float
105{
106 return fast_log2 (__builtin_fabsf (x)) * 6.02059991327962390427477789448986f;
107}
108
109template<typename Float> extern inline ASE_CONST Float
110db2voltage (Float x)
111{
112 return std::pow (10, x * Float (0.05));
113}
114
115extern inline ASE_CONST float
117{
118 return fast_exp2 (x * 0.1660964047443681173935159714744695f);
119}
120
121template<typename Float> extern inline ASE_CONST Float
122hz_changed (Float a, Float b)
123{
124 return __builtin_fabsf (a - b) > 1e-3;
125}
126
127template<typename Float> extern inline ASE_CONST Float
128db_changed (Float a, Float b)
129{
130 return __builtin_fabsf (a - b) > 1e-3;
131}
132
133template<typename Float> extern inline ASE_CONST Float
134voltage_changed (Float a, Float b)
135{
136 return __builtin_fabsf (a - b) > 1e-7;
137}
138
139} // Ase
140
141#endif // __ASE_SIGNALUTILS_HH__
T exp2(T... args)
T log10(T... args)
T log2(T... args)
typedef float
The Anklang C++ API namespace.
Definition api.hh:9
float fast_voltage2hz(float x)
Float precision variant of voltage2hz using fast_exp2().
Definition signalmath.hh:80
Float voltage2hz(Float x)
Convert synthesizer value (Voltage) to Hertz.
Definition signalmath.hh:74
float fast_exp2(float x)
Definition mathutils.hh:82
float fast_hz2voltage(float x)
Float precision variant of hz2voltage using fast_log2().
Definition signalmath.hh:92
Float db_changed(Float a, Float b)
Determine a significant Decibel change.
Float hz2voltage(Float x)
Convert Hertz to synthesizer value (Voltage).
Definition signalmath.hh:86
float fast_db2voltage(float x)
Float precision variant of db2voltage using fast_exp2().
float fast_voltage2db(float x)
Float precision variant of voltage2db using fast_log2().
Float voltage_changed(Float a, Float b)
Determine a significant synthesizer value (Voltage) change.
Float hz_changed(Float a, Float b)
Determine a significant frequency change (audible Hertz).
Float voltage2db(Float x)
Convert synthesizer value (Voltage) to Decibel.
Definition signalmath.hh:98
float fast_log2(float x)
Definition mathutils.hh:101
Float db2voltage(Float x)
Convert Decibel to synthesizer value (Voltage).
T pow(T... args)
Logarithmically map (and invert) a range onto 0…+1.
Definition signalmath.hh:43
double iscale(double mmvalue) const
Calculate normalized from a scale() result within [min … max].
Definition signalmath.hh:63
double scale(double normalized) const
Calculate scale value within [min … max] from normalized x.
Definition signalmath.hh:57
void setup(double min, double max)
Provide minimum and maximum values to be mapped.
Definition signalmath.hh:48