1 /*
2  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef __pbd_control_math_h__
20 #define __pbd_control_math_h__
21 
22 #include <assert.h>
23 #include <math.h>
24 #include <stdint.h>
25 
26 /* these numbers ar arbitrary; we use them to keep floats well out of the denormal range */
27 #define TINY_NUMBER (0.0000001)  /* (-140dB) */
28 
29 /* map gain-coeff [0..2] to position [0..1] */
30 static inline double
gain_to_position(double g)31 gain_to_position (double g)
32 {
33 	if (g == 0) {
34 		return 0;
35 	}
36 	return pow ((6.0 * log (g) / log (2.0) + 192.0) / 198.0, 8.0);
37 }
38 
39 /* map position [0..1] to gain-coeff [0..2] */
40 static inline double
position_to_gain(double pos)41 position_to_gain (double pos)
42 {
43 	if (pos == 0.0) {
44 		return 0.0;
45 	}
46 	return exp (((pow (pos, 1.0 / 8.0) * 198.0) - 192.0) / 6.0 * log (2.0));
47 }
48 
49 /* map position [0..1] to parameter [lower..upper] on a logarithmic scale */
50 static inline double
position_to_logscale(double pos,double lower,double upper)51 position_to_logscale (double pos, double lower, double upper)
52 {
53 	assert (upper > lower && lower * upper > 0);
54 	assert (pos >= 0.0 && pos <= 1.0);
55 	return lower * pow (upper / lower, pos);
56 }
57 
58 /* map parameter [lower..upper] to position [0..1] on a logarithmic scale*/
59 static inline double
logscale_to_position(double val,double lower,double upper)60 logscale_to_position (double val, double lower, double upper)
61 {
62 	assert (upper > lower && lower * upper > 0);
63 	assert (val >= lower && val <= upper);
64 	return log (val / lower) / log (upper / lower);
65 }
66 
67 static inline double
logscale_to_position_with_steps(double val,double lower,double upper,uint32_t steps)68 logscale_to_position_with_steps (double val, double lower, double upper, uint32_t steps)
69 {
70 	assert (steps > 1);
71 	double v = logscale_to_position (val, lower, upper) * (steps - 1.0);
72 	return round (v) / (steps - 1.0);
73 }
74 
75 static inline double
position_to_logscale_with_steps(double pos,double lower,double upper,uint32_t steps)76 position_to_logscale_with_steps (double pos, double lower, double upper, uint32_t steps)
77 {
78 	assert (steps > 1);
79 	double p = round (pos * (steps - 1.0)) / (steps - 1.0);
80 	return position_to_logscale (p, lower, upper);
81 }
82 
83 
84 static inline double
interpolate_linear(double from,double to,double fraction)85 interpolate_linear (double from, double to, double fraction)
86 {
87 	return from + (fraction * (to - from));
88 }
89 
90 static inline double
interpolate_logarithmic(double from,double to,double fraction,double,double)91 interpolate_logarithmic (double from, double to, double fraction, double /*lower*/, double /*upper*/)
92 {
93 #if 0
94 	/* this is expensive, original math incl. range-check assertions */
95 	double l0 = logscale_to_position (from, lower, upper);
96 	double l1 = logscale_to_position (to, lower, upper);
97 	return position_to_logscale (l0 + fraction * (l1 - l0), lower, upper);
98 #else
99 	assert (from > 0 && from * to > 0);
100 	assert (fraction >= 0 && fraction <= 1);
101 	return from * pow (to / from, fraction);
102 #endif
103 }
104 
105 static inline double
interpolate_gain(double f,double t,double fraction,double upper)106 interpolate_gain (double f, double t, double fraction, double upper)
107 {
108 	double from = f + TINY_NUMBER; //kill denormals before we use them for anything
109 	double to = t + TINY_NUMBER; //kill denormals before we use them for anything
110 	if ( fabs(to-from) < TINY_NUMBER ){
111 		 return to;
112 	}
113 
114 	// this is expensive -- optimize
115 	double g0 = gain_to_position (from * 2. / upper);
116 	double g1 = gain_to_position (to * 2. / upper);
117 	double diff = g1 - g0;
118 
119 	return position_to_gain (g0 + fraction * (diff)) * upper / 2.;
120 }
121 
122 #endif
123