LMMS
Loading...
Searching...
No Matches
vumeter.h
Go to the documentation of this file.
1/* Calf DSP Library
2 * Peak metering facilities.
3 *
4 * Copyright (C) 2007 Krzysztof Foltman
5 *
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.
10 *
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.
15 *
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.
20 */
21#ifndef __CALF_VUMETER_H
22#define __CALF_VUMETER_H
23
24#include <math.h>
25
26namespace dsp {
27
29struct vumeter
30{
32 float level;
34 float falloff;
36 float clip;
42 bool reverse;
43
45 {
46 falloff = 0.999f;
47 clip_falloff = 0.999f;
48 reverse = false;
49 reset();
50 }
51
52 void reset()
53 {
54 level = reverse ? 1 : 0;
55 clip = 0;
56 }
57
60 void set_falloff(double time_20dB, double sample_rate)
61 {
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));
68 }
69
70 void set_reverse(bool rev) {
71 reverse = rev;
72 reset();
73 }
74
75 void copy_falloff(const vumeter &src)
76 {
77 falloff = src.falloff;
79 }
80
82 inline void update(const float *src, unsigned int len)
83 {
84 update_stereo(src, NULL, len);
85 }
86
87 inline void update_stereo(const float *src1, const float *src2, unsigned int len)
88 {
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);
96 }
97 inline void run_sample_loop(const float *src, unsigned int len)
98 {
99 for (unsigned int i = 0; i < len; i++)
100 process(src[i]);
101 }
102 inline void process(const float value)
103 {
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;
111 }
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);
122 }
123
124 inline void update_zeros(unsigned int len)
125 {
126 level *= pow((double)falloff, (double)len);
127 clip *= pow((double)clip_falloff, (double)len);
130 }
131};
132
134{
136
137 inline void update_stereo(const float *src1, const float *src2, unsigned int len)
138 {
139 left.update_stereo(src1, NULL, len);
140 right.update_stereo(NULL, src2, len);
141 }
142 inline void update_zeros(unsigned int len)
143 {
144 left.update_zeros(len);
145 right.update_zeros(len);
146 }
147 inline void reset()
148 {
149 left.reset();
150 right.reset();
151 }
152 inline void set_falloff(double time_20dB, double sample_rate)
153 {
154 left.set_falloff(time_20dB, sample_rate);
155 right.copy_falloff(left);
156 }
157 inline void copy_falloff(const dual_vumeter &src)
158 {
159 left.copy_falloff(src.left);
160 right.copy_falloff(src.right);
161 }
162
163};
164
165};
166
167#endif
#define NULL
Definition CarlaBridgeFormat.cpp:30
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
Definition audio_fx.h:36
void sanitize(float &value)
Definition primitives.h:354
Definition vumeter.h:134
void copy_falloff(const dual_vumeter &src)
Definition vumeter.h:157
vumeter left
Definition vumeter.h:135
vumeter right
Definition vumeter.h:135
void set_falloff(double time_20dB, double sample_rate)
Definition vumeter.h:152
void update_stereo(const float *src1, const float *src2, unsigned int len)
Definition vumeter.h:137
void reset()
Definition vumeter.h:147
void update_zeros(unsigned int len)
Definition vumeter.h:142
Peak meter class.
Definition vumeter.h:30
void set_falloff(double time_20dB, double sample_rate)
Definition vumeter.h:60
bool reverse
reverse VU meter
Definition vumeter.h:42
float level
Measured signal level.
Definition vumeter.h:32
void update_zeros(unsigned int len)
Update clip meter as if update was called with all-zero input signal.
Definition vumeter.h:124
float falloff
Falloff of signal level (b1 coefficient of a 1-pole filter).
Definition vumeter.h:34
void fall(unsigned int len)
Definition vumeter.h:112
void reset()
Definition vumeter.h:52
void run_sample_loop(const float *src, unsigned int len)
Definition vumeter.h:97
int count_over
Amount of samples > 1.f; Clipping occurs if 3 samples are over 0dB.
Definition vumeter.h:40
void copy_falloff(const vumeter &src)
Copy falloff from another object.
Definition vumeter.h:75
vumeter()
Definition vumeter.h:44
float clip_falloff
Falloff of clip indicator (b1 coefficient of a 1-pole filter); set to 1 if no falloff is required (ma...
Definition vumeter.h:38
void update_stereo(const float *src1, const float *src2, unsigned int len)
Update peak meter based on louder of two input signals.
Definition vumeter.h:87
float clip
Clip indicator (set to 1 when |value| >= 1, fading otherwise).
Definition vumeter.h:36
void process(const float value)
Definition vumeter.h:102
void update(const float *src, unsigned int len)
Update peak meter based on input signal.
Definition vumeter.h:82
void set_reverse(bool rev)
Definition vumeter.h:70