Use a C++ bool constant
[ardour.git] / gtk2_ardour / logmeter.h
1 /*
2     Copyright (C) 2000-2007 Paul Davis
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_gtk_log_meter_h__
21 #define __ardour_gtk_log_meter_h__
22
23 #include "ardour/dB.h"
24
25 #if 1
26 static inline float
27 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
28 {
29         return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
30 }
31
32 static inline float
33 alt_log_meter (float power)
34 {
35         return _log_meter (power, -192.0, 0.0, 8.0);
36 }
37 #endif
38
39 /* prototypes - avoid compiler warning */
40 static inline float log_meter (float db);
41 static inline float meter_deflect_ppm (float);
42 static inline float meter_deflect_din (float);
43 static inline float meter_deflect_nordic (float);
44 static inline float meter_deflect_vu (float);
45 static inline float meter_deflect_k (float, float);
46
47
48
49 static inline float
50 log_meter (float db)
51 {
52          gfloat def = 0.0f; /* Meter deflection %age */
53
54          if (db < -70.0f) {
55                  def = 0.0f;
56          } else if (db < -60.0f) {
57                  def = (db + 70.0f) * 0.25f;
58          } else if (db < -50.0f) {
59                  def = (db + 60.0f) * 0.5f + 2.5f;
60          } else if (db < -40.0f) {
61                  def = (db + 50.0f) * 0.75f + 7.5f;
62          } else if (db < -30.0f) {
63                  def = (db + 40.0f) * 1.5f + 15.0f;
64          } else if (db < -20.0f) {
65                  def = (db + 30.0f) * 2.0f + 30.0f;
66          } else if (db < 6.0f) {
67                  def = (db + 20.0f) * 2.5f + 50.0f;
68          } else {
69                  def = 115.0f;
70          }
71
72          /* 115 is the deflection %age that would be
73             when db=6.0. this is an arbitrary
74             endpoint for our scaling.
75          */
76
77          return def/115.0f;
78 }
79
80 static inline float
81 log_meter0dB (float db)
82 {
83          gfloat def = 0.0f; /* Meter deflection %age */
84
85          if (db < -70.0f) {
86                  def = 0.0f;
87          } else if (db < -60.0f) {
88                  def = (db + 70.0f) * 0.25f;
89          } else if (db < -50.0f) {
90                  def = (db + 60.0f) * 0.5f + 2.5f;
91          } else if (db < -40.0f) {
92                  def = (db + 50.0f) * 0.75f + 7.5f;
93          } else if (db < -30.0f) {
94                  def = (db + 40.0f) * 1.5f + 15.0f;
95          } else if (db < -20.0f) {
96                  def = (db + 30.0f) * 2.0f + 30.0f;
97          } else if (db < 0.0f) {
98                  def = (db + 20.0f) * 2.5f + 50.0f;
99          } else {
100                  def = 100.0f;
101          }
102         return def/100.0f;
103 }
104
105 static inline float
106 meter_deflect_ppm (float db)
107 {
108         if (db < -30) {
109                 // 2.258 == ((-30 + 32.0)/ 28.0) / 10^(-30 / 20);
110                 return (dB_to_coefficient(db) * 2.258769757f);
111         } else {
112                 const float rv = (db + 32.0f) / 28.0f;
113                 if (rv < 1.0) {
114                         return rv;
115                 } else {
116                         return 1.0;
117                 }
118         }
119 }
120
121 static inline float
122 meter_deflect_din (float db)
123 {
124         float rv = dB_to_coefficient(db);
125         rv = sqrtf (sqrtf (2.3676f * rv)) - 0.1803f;
126         if (rv >= 1.0) {
127                 return 1.0;
128         } else {
129                 return (rv > 0 ? rv : 0.0);
130         }
131 }
132
133 static inline float
134 meter_deflect_nordic (float db)
135 {
136         if (db < -60) {
137                 return 0.0;
138         } else {
139                 const float rv = (db + 60.0f) / 54.0f;
140                 if (rv < 1.0) {
141                         return rv;
142                 } else {
143                         return 1.0;
144                 }
145         }
146 }
147
148 static inline float
149 meter_deflect_vu (float db)
150 {
151         const float rv = 6.77165f * dB_to_coefficient(db);
152         if (rv > 1.0) return 1.0;
153         return rv;
154 }
155
156 static inline float
157 meter_deflect_k (float db, float krange)
158 {
159         db+=krange;
160         if (db < -40.0f) {
161                 return (dB_to_coefficient(db) * 500.0f / (krange + 45.0f));
162         } else {
163                 const float rv = (db + 45.0f) / (krange + 45.0f);
164                 if (rv < 1.0) {
165                         return rv;
166                 } else {
167                         return 1.0;
168                 }
169         }
170 }
171
172 #endif /* __ardour_gtk_log_meter_h__ */