VCV Rack API v2
Loading...
Searching...
No Matches
vumeter.hpp
Go to the documentation of this file.
1#pragma once
2#include <dsp/common.hpp>
3
4
5namespace rack {
6namespace dsp {
7
8
10struct VuMeter {
12 float dBInterval = 3.0;
13 float dBScaled;
15 void setValue(float v) {
16 dBScaled = std::log10(std::fabs(v)) * 20.0 / dBInterval;
17 }
22 float getBrightness(int i) {
23 if (i == 0) {
24 return (dBScaled >= 0.0) ? 1.0 : 0.0;
25 }
26 else {
27 return math::clamp(dBScaled + i, 0.0, 1.0);
28 }
29 }
30};
31
33
34
49struct VuMeter2 {
50 enum Mode {
52 RMS
53 };
56 float v = 0.f;
58 float lambda = 30.f;
59
60 void reset() {
61 v = 0.f;
62 }
63
64 void process(float deltaTime, float value) {
65 if (mode == RMS) {
66 value = std::pow(value, 2);
67 v += (value - v) * lambda * deltaTime;
68 }
69 else {
70 value = std::fabs(value);
71 if (value >= v) {
72 v = value;
73 }
74 else {
75 v += (value - v) * lambda * deltaTime;
76 }
77 }
78 }
79
85 float getBrightness(float dbMin, float dbMax) {
86 float db = amplitudeToDb((mode == RMS) ? std::sqrt(v) : v);
87 if (db >= dbMax)
88 return 1.f;
89 else if (db <= dbMin)
90 return 0.f;
91 else
92 return math::rescale(db, dbMin, dbMax, 0.f, 1.f);
93 }
94};
95
96
97} // namespace dsp
98} // namespace rack
#define DEPRECATED
Attribute for deprecated functions and symbols.
Definition common.hpp:24
T amplitudeToDb(T amp)
Definition common.hpp:44
DEPRECATED typedef VuMeter VUMeter
Definition vumeter.hpp:32
int clamp(int x, int a, int b)
Limits x between a and b.
Definition math.hpp:32
float rescale(float x, float xMin, float xMax, float yMin, float yMax)
Rescales x from the range [xMin, xMax] to [yMin, yMax].
Definition math.hpp:151
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Models a VU meter with smoothing.
Definition vumeter.hpp:49
float lambda
Inverse time constant in 1/seconds.
Definition vumeter.hpp:58
Mode mode
Definition vumeter.hpp:54
void process(float deltaTime, float value)
Definition vumeter.hpp:64
void reset()
Definition vumeter.hpp:60
Mode
Definition vumeter.hpp:50
@ RMS
Definition vumeter.hpp:52
@ PEAK
Definition vumeter.hpp:51
float v
Either the smoothed peak or the mean-square of the brightness, depending on the mode.
Definition vumeter.hpp:56
float getBrightness(float dbMin, float dbMax)
Returns the LED brightness measuring tick marks between dbMin and dbMax.
Definition vumeter.hpp:85
Deprecated.
Definition vumeter.hpp:10
void setValue(float v)
Value should be scaled so that 1.0 is clipping.
Definition vumeter.hpp:15
float dBScaled
Definition vumeter.hpp:13
float dBInterval
Decibel level difference between adjacent meter lights.
Definition vumeter.hpp:12
float getBrightness(int i)
Returns the brightness of the light indexed by i.
Definition vumeter.hpp:22