Skip to content

Commit bebb634

Browse files
committed
Updated metering modes for MeterGraph and ScaledMeterGraph
1 parent 71612a7 commit bebb634

File tree

3 files changed

+244
-166
lines changed

3 files changed

+244
-166
lines changed

include/lsp-plug.in/dsp-units/util/MeterGraph.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ namespace lsp
3232
{
3333
enum meter_method_t
3434
{
35-
MM_MAXIMUM,
36-
MM_MINIMUM
35+
MM_ABS_MAXIMUM,
36+
MM_ABS_MINIMUM,
37+
MM_SIGN_MAXIMUM,
38+
MM_SIGN_MINIMUM,
3739
};
3840

3941
class LSP_DSP_UNITS_PUBLIC MeterGraph
4042
{
4143
protected:
4244
ShiftBuffer sBuffer;
4345
float fCurrent;
44-
size_t nCount;
45-
size_t nPeriod;
46-
bool bMinimize;
46+
uint32_t nCount;
47+
uint32_t nPeriod;
48+
meter_method_t enMethod;
4749

4850
public:
4951
explicit MeterGraph();
@@ -83,13 +85,13 @@ namespace lsp
8385
*
8486
* @param m metering method
8587
*/
86-
inline void set_method(meter_method_t m) { bMinimize = (m == MM_MINIMUM); }
88+
inline void set_method(meter_method_t m) { enMethod = m; }
8789

8890
/** Get data stored in buffer
8991
*
9092
* @return pointer to the first element of the buffer
9193
*/
92-
inline float *data() { return sBuffer.head(); }
94+
inline float *data() { return sBuffer.head(); }
9395

9496
/** Set strobe period
9597
*

src/main/util/MeterGraph.cpp

+121-118
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <lsp-plug.in/dsp/dsp.h>
2323
#include <lsp-plug.in/dsp-units/util/MeterGraph.h>
24+
#include <lsp-plug.in/stdlib/math.h>
2425

2526
namespace lsp
2627
{
@@ -43,7 +44,7 @@ namespace lsp
4344
fCurrent = 0.0f;
4445
nCount = 0;
4546
nPeriod = 1;
46-
bMinimize = false;
47+
enMethod = MM_ABS_MAXIMUM;
4748
}
4849

4950
void MeterGraph::destroy()
@@ -67,21 +68,31 @@ namespace lsp
6768

6869
void MeterGraph::process(float sample)
6970
{
70-
// Make sample positive
71-
if (sample < 0.0f)
72-
sample = - sample;
73-
74-
if (bMinimize)
75-
{
76-
// Update current sample
77-
if ((nCount == 0) || (fCurrent < sample))
78-
fCurrent = sample;
79-
}
80-
else
71+
// Update current sample
72+
switch (enMethod)
8173
{
82-
// Update current sample
83-
if ((nCount == 0) || (fCurrent > sample))
84-
fCurrent = sample;
74+
case MM_SIGN_MINIMUM:
75+
if ((nCount == 0) || (fabsf(fCurrent) > fabsf(sample)))
76+
fCurrent = sample;
77+
break;
78+
79+
case MM_SIGN_MAXIMUM:
80+
if ((nCount == 0) || (fabsf(fCurrent) < fabsf(sample)))
81+
fCurrent = sample;
82+
break;
83+
84+
case MM_ABS_MINIMUM:
85+
sample = fabsf(sample);
86+
if ((nCount == 0) || (fCurrent > sample))
87+
fCurrent = sample;
88+
break;
89+
90+
default:
91+
case MM_ABS_MAXIMUM:
92+
sample = fabsf(sample);
93+
if ((nCount == 0) || (fCurrent < sample))
94+
fCurrent = sample;
95+
break;
8596
}
8697

8798
// Increment number of samples processed
@@ -95,128 +106,120 @@ namespace lsp
95106

96107
void MeterGraph::process(const float *s, size_t n)
97108
{
98-
if (bMinimize)
109+
while (n > 0)
99110
{
100-
while (n > 0)
101-
{
102-
// Determine amount of samples to process
103-
ssize_t can_do = lsp_min(ssize_t(n), ssize_t(nPeriod - nCount));
111+
// Determine amount of samples to process
112+
ssize_t can_do = lsp_min(ssize_t(n), ssize_t(nPeriod - nCount));
104113

105-
// Process the samples
106-
if (can_do > 0)
114+
// Process the samples
115+
if (can_do > 0)
116+
{
117+
// Process samples
118+
switch (enMethod)
107119
{
108-
// Get maximum sample
109-
float sample = dsp::abs_min(s, can_do);
110-
if ((nCount == 0) || (fCurrent > sample))
111-
fCurrent = sample;
112-
113-
// Update counters and pointers
114-
nCount += can_do;
115-
n -= can_do;
116-
s += can_do;
120+
case MM_SIGN_MINIMUM:
121+
{
122+
const float sample = dsp::sign_min(s, can_do);
123+
if ((nCount == 0) || (fabsf(fCurrent) > fabsf(sample)))
124+
fCurrent = sample;
125+
break;
126+
}
127+
case MM_SIGN_MAXIMUM:
128+
{
129+
const float sample = dsp::sign_max(s, can_do);
130+
if ((nCount == 0) || (fabsf(fCurrent) < fabsf(sample)))
131+
fCurrent = sample;
132+
break;
133+
}
134+
case MM_ABS_MINIMUM:
135+
{
136+
const float sample = dsp::abs_min(s, can_do);
137+
if ((nCount == 0) || (fCurrent > sample))
138+
fCurrent = sample;
139+
break;
140+
}
141+
default:
142+
case MM_ABS_MAXIMUM:
143+
{
144+
const float sample = dsp::abs_max(s, can_do);
145+
if ((nCount == 0) || (fCurrent > sample))
146+
fCurrent = sample;
147+
break;
148+
}
117149
}
118150

119-
// Check that need to switch to next sample
120-
if (nCount >= nPeriod)
121-
{
122-
// Append current sample to buffer
123-
sBuffer.process(fCurrent);
124-
nCount = 0;
125-
}
151+
// Update counters and pointers
152+
nCount += can_do;
153+
n -= can_do;
154+
s += can_do;
126155
}
127-
}
128-
else
129-
{
130-
while (n > 0)
131-
{
132-
// Determine amount of samples to process
133-
ssize_t can_do = lsp_min(ssize_t(n), ssize_t(nPeriod - nCount));
134156

135-
// Process the samples
136-
if (can_do > 0)
137-
{
138-
// Get maximum sample
139-
float sample = dsp::abs_max(s, can_do);
140-
if ((nCount == 0) || (fCurrent < sample))
141-
fCurrent = sample;
142-
143-
// Update counters and pointers
144-
nCount += can_do;
145-
n -= can_do;
146-
s += can_do;
147-
}
148-
149-
// Check that need to switch to next sample
150-
if (nCount >= nPeriod)
151-
{
152-
// Append current sample to buffer and update counter
153-
sBuffer.process(fCurrent);
154-
nCount = 0;
155-
}
157+
// Check that need to switch to next sample
158+
if (nCount >= nPeriod)
159+
{
160+
// Append current sample to buffer
161+
sBuffer.process(fCurrent);
162+
nCount = 0;
156163
}
157164
}
158165
}
159166

160167
void MeterGraph::process(const float *s, float gain, size_t n)
161168
{
162-
if (bMinimize)
169+
while (n > 0)
163170
{
164-
while (n > 0)
165-
{
166-
// Determine amount of samples to process
167-
ssize_t can_do = lsp_min(ssize_t(n), ssize_t(nPeriod - nCount));
171+
// Determine amount of samples to process
172+
ssize_t can_do = lsp_min(ssize_t(n), ssize_t(nPeriod - nCount));
168173

169-
// Process the samples
170-
if (can_do > 0)
174+
// Process the samples
175+
if (can_do > 0)
176+
{
177+
// Process samples
178+
switch (enMethod)
171179
{
172-
// Get maximum sample
173-
float sample = dsp::abs_min(s, can_do) * gain;
174-
if ((nCount == 0) || (fCurrent > sample))
175-
fCurrent = sample;
176-
177-
// Update counters and pointers
178-
nCount += can_do;
179-
n -= can_do;
180-
s += can_do;
180+
case MM_SIGN_MINIMUM:
181+
{
182+
const float sample = dsp::sign_min(s, can_do) * gain;
183+
if ((nCount == 0) || (fabsf(fCurrent) > fabsf(sample)))
184+
fCurrent = sample;
185+
break;
186+
}
187+
case MM_SIGN_MAXIMUM:
188+
{
189+
const float sample = dsp::sign_max(s, can_do) * gain;
190+
if ((nCount == 0) || (fabsf(fCurrent) < fabsf(sample)))
191+
fCurrent = sample;
192+
break;
193+
}
194+
case MM_ABS_MINIMUM:
195+
{
196+
const float sample = dsp::abs_min(s, can_do) * gain;
197+
if ((nCount == 0) || (fCurrent > sample))
198+
fCurrent = sample;
199+
break;
200+
}
201+
default:
202+
case MM_ABS_MAXIMUM:
203+
{
204+
const float sample = dsp::abs_max(s, can_do) * gain;
205+
if ((nCount == 0) || (fCurrent > sample))
206+
fCurrent = sample;
207+
break;
208+
}
181209
}
182210

183-
// Check that need to switch to next sample
184-
if (nCount >= nPeriod)
185-
{
186-
// Append current sample to buffer
187-
sBuffer.process(fCurrent);
188-
nCount = 0;
189-
}
211+
// Update counters and pointers
212+
nCount += can_do;
213+
n -= can_do;
214+
s += can_do;
190215
}
191-
}
192-
else
193-
{
194-
while (n > 0)
195-
{
196-
// Determine amount of samples to process
197-
ssize_t can_do = lsp_min(ssize_t(n), ssize_t(nPeriod - nCount));
198-
199-
// Process the samples
200-
if (can_do > 0)
201-
{
202-
// Get maximum sample
203-
float sample = dsp::abs_max(s, can_do) * gain;
204-
if ((nCount == 0) || (fCurrent < sample))
205-
fCurrent = sample;
206-
207-
// Update counters and pointers
208-
nCount += can_do;
209-
n -= can_do;
210-
s += can_do;
211-
}
212216

213-
// Check that need to switch to next sample
214-
if (nCount >= nPeriod)
215-
{
216-
// Append current sample to buffer and update counter
217-
sBuffer.process(fCurrent);
218-
nCount = 0;
219-
}
217+
// Check that need to switch to next sample
218+
if (nCount >= nPeriod)
219+
{
220+
// Append current sample to buffer
221+
sBuffer.process(fCurrent);
222+
nCount = 0;
220223
}
221224
}
222225
}
@@ -227,7 +230,7 @@ namespace lsp
227230
v->write("fCurrent", fCurrent);
228231
v->write("nCount", nCount);
229232
v->write("nPeriod", nPeriod);
230-
v->write("bMinimize", bMinimize);
233+
v->write("enMethod", enMethod);
231234
}
232235
} /* namespace dspu */
233236
} /* namespace lsp */

0 commit comments

Comments
 (0)