Ardour  9.0-pre0-350-gf17a656217
control_math.h
Go to the documentation of this file.
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 #pragma once
20 
21 #include <assert.h>
22 #include <math.h>
23 #include <stdint.h>
24 
25 /* these numbers ar arbitrary; we use them to keep floats well out of the denormal range */
26 #define TINY_NUMBER (0.0000001) /* (-140dB) */
27 
28 /* map gain-coeff [0..2] to position [0..1] */
29 static inline double
30 gain_to_position (double g)
31 {
32  if (g == 0) {
33  return 0;
34  }
35  return pow ((6.0 * log (g) / log (2.0) + 192.0) / 198.0, 8.0);
36 }
37 
38 /* map position [0..1] to gain-coeff [0..2] */
39 static inline double
40 position_to_gain (double pos)
41 {
42  if (pos == 0.0) {
43  return 0.0;
44  }
45  return exp (((pow (pos, 1.0 / 8.0) * 198.0) - 192.0) / 6.0 * log (2.0));
46 }
47 
48 /* map position [0..1] to parameter [lower..upper] on a logarithmic scale */
49 static inline double
50 position_to_logscale (double pos, double lower, double upper)
51 {
52  assert (upper > lower && lower * upper > 0);
53  assert (pos >= 0.0 && pos <= 1.0);
54  return lower * pow (upper / lower, pos);
55 }
56 
57 /* map parameter [lower..upper] to position [0..1] on a logarithmic scale*/
58 static inline double
59 logscale_to_position (double val, double lower, double upper)
60 {
61  assert (upper > lower && lower * upper > 0);
62  assert (val >= lower && val <= upper);
63  return log (val / lower) / log (upper / lower);
64 }
65 
66 static inline double
67 logscale_to_position_with_steps (double val, double lower, double upper, uint32_t steps)
68 {
69  assert (steps > 1);
70  double v = logscale_to_position (val, lower, upper) * (steps - 1.0);
71  return round (v) / (steps - 1.0);
72 }
73 
74 static inline double
75 position_to_logscale_with_steps (double pos, double lower, double upper, uint32_t steps)
76 {
77  assert (steps > 1);
78  double p = round (pos * (steps - 1.0)) / (steps - 1.0);
79  return position_to_logscale (p, lower, upper);
80 }
81 
82 
83 static inline double
84 interpolate_linear (double from, double to, double fraction)
85 {
86  return from + (fraction * (to - from));
87 }
88 
89 static inline double
90 interpolate_logarithmic (double from, double to, double fraction, double /*lower*/, double /*upper*/)
91 {
92 #if 0
93  /* this is expensive, original math incl. range-check assertions */
94  double l0 = logscale_to_position (from, lower, upper);
95  double l1 = logscale_to_position (to, lower, upper);
96  return position_to_logscale (l0 + fraction * (l1 - l0), lower, upper);
97 #else
98  assert (from > 0 && from * to > 0);
99  assert (fraction >= 0 && fraction <= 1);
100  return from * pow (to / from, fraction);
101 #endif
102 }
103 
104 static inline double
105 interpolate_gain (double f, double t, double fraction, double upper)
106 {
107  double from = f + TINY_NUMBER; //kill denormals before we use them for anything
108  double to = t + TINY_NUMBER; //kill denormals before we use them for anything
109  if ( fabs(to-from) < TINY_NUMBER ){
110  return to;
111  }
112 
113  // this is expensive -- optimize
114  double g0 = gain_to_position (from * 2. / upper);
115  double g1 = gain_to_position (to * 2. / upper);
116  double diff = g1 - g0;
117 
118  return position_to_gain (g0 + fraction * (diff)) * upper / 2.;
119 }
120 
static double gain_to_position(double g)
Definition: control_math.h:30
static double position_to_logscale_with_steps(double pos, double lower, double upper, uint32_t steps)
Definition: control_math.h:75
static double interpolate_gain(double f, double t, double fraction, double upper)
Definition: control_math.h:105
static double logscale_to_position_with_steps(double val, double lower, double upper, uint32_t steps)
Definition: control_math.h:67
#define TINY_NUMBER
Definition: control_math.h:26
static double position_to_gain(double pos)
Definition: control_math.h:40
static double position_to_logscale(double pos, double lower, double upper)
Definition: control_math.h:50
static double logscale_to_position(double val, double lower, double upper)
Definition: control_math.h:59
static double interpolate_linear(double from, double to, double fraction)
Definition: control_math.h:84
static double interpolate_logarithmic(double from, double to, double fraction, double, double)
Definition: control_math.h:90