HAAS Stereo Enhancer
[calf.git] / src / calf / vumeter.h
blob4e1417140c50975ac9253ada9fb3011f557acf10
1 /* Calf DSP Library
2 * Peak metering facilities.
4 * Copyright (C) 2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
21 #ifndef __CALF_VUMETER_H
22 #define __CALF_VUMETER_H
24 #include <math.h>
26 namespace dsp {
28 /// Peak meter class
29 struct vumeter
31 /// Measured signal level
32 float level;
33 /// Falloff of signal level (b1 coefficient of a 1-pole filter)
34 float falloff;
35 /// Clip indicator (set to 1 when |value| >= 1, fading otherwise)
36 float clip;
37 /// Falloff of clip indicator (b1 coefficient of a 1-pole filter); set to 1 if no falloff is required (manual reset of clip indicator)
38 float clip_falloff;
39 /// Amount of samples > 1.f; Clipping occurs if 3 samples are over 0dB
40 int count_over;
41 /// reverse VU meter
42 bool reverse;
44 vumeter()
46 falloff = 0.999f;
47 clip_falloff = 0.999f;
48 reverse = false;
49 reset();
52 void reset()
54 level = reverse ? 1 : 0;
55 clip = 0;
58 /// Set falloff so that the meter falls 20dB in time_20dB seconds, assuming sample rate of sample_rate
59 /// @arg time_20dB time for the meter to move by 20dB (default 300ms if <= 0)
60 void set_falloff(double time_20dB, double sample_rate)
62 if (time_20dB <= 0)
63 time_20dB = 0.3;
64 // 20dB = 10x +/- --> 0.1 = pow(falloff, sample_rate * time_20dB) = exp(sample_rate * ln(falloff))
65 // ln(0.1) = sample_rate * ln(falloff)
66 falloff = pow(0.1, 1 / (sample_rate * time_20dB));
67 clip_falloff = falloff;
70 void set_reverse(bool rev) {
71 reverse = rev;
72 reset();
74 /// Copy falloff from another object
75 void copy_falloff(const vumeter &src)
77 falloff = src.falloff;
78 clip_falloff = src.clip_falloff;
81 /// Update peak meter based on input signal
82 inline void update(const float *src, unsigned int len)
84 update_stereo(src, NULL, len);
86 /// Update peak meter based on louder of two input signals
87 inline void update_stereo(const float *src1, const float *src2, unsigned int len)
89 fall(len);
90 // Process input samples - to get peak value, take a max of all values in the input signal and "aged" old peak
91 // Clip is set to 1 if any sample is out-of-range, if no clip occurs, the "aged" value is assumed
92 if (src1)
93 run_sample_loop(src1, len);
94 if (src2)
95 run_sample_loop(src2, len);
97 inline void run_sample_loop(const float *src, unsigned int len)
99 for (unsigned int i = 0; i < len; i++)
100 process(src[i]);
102 inline void process(const float value)
104 level = reverse ? std::min(level, (float)fabs(value)) : std::max(level, (float)fabs(value));
105 if (level > 1.f)
106 count_over ++;
107 else
108 count_over = 0;
109 if (count_over >= 3)
110 clip = 1.f;
112 void fall(unsigned int len) {
113 // "Age" the old level by falloff^length
114 if (reverse)
115 level /= pow(falloff, len);
116 else
117 level *= pow(falloff, len);
118 // Same for clip level (using different fade constant)
119 clip *= pow(clip_falloff, len);
120 dsp::sanitize(level);
121 dsp::sanitize(clip);
123 /// Update clip meter as if update was called with all-zero input signal
124 inline void update_zeros(unsigned int len)
126 level *= pow((double)falloff, (double)len);
127 clip *= pow((double)clip_falloff, (double)len);
128 dsp::sanitize(level);
129 dsp::sanitize(clip);
135 #endif