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