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