-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGainMeter.h
247 lines (217 loc) · 7.65 KB
/
GainMeter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef COLIN_GAINMETER_H
#define COLIN_GAINMETER_H
#include <JuceHeader.h>
#include <vector>
/*
==============================================================================
GainMeter.h
Created: 10 Nov 2022 10:30:49am
Author: Colin Raab
very WIP effect, at proof of concept stage
==============================================================================
*/
namespace Colin {
class GainMeter {
protected:
float magnitude;
float RMS;
float gain;
float release;
int imageIdx;
int imageCache;
int imagesMax;
float imagesMaxInv;
bool isReady;
juce::Image img1;
std::vector<juce::Image> images;
std::vector<juce::Colour> colors;
juce::ColourGradient gradient;
float distortion = 0;
int x, y, width, height;
public:
GainMeter() {
magnitude = 0;
gain = 0;
release = .2f;
imageIdx = 0;
imageCache = 0;
imagesMax = 0;
imagesMaxInv = 0.f;
isReady = false;
}
~GainMeter() = default;
void setDistortion(float distortion) {
this->distortion = distortion;
}
void setGradient(juce::ColourGradient gradient) {
for(int i=0; i<colors.size(); i++) {
double pos = double(i);
pos /= colors.size();
juce::Colour color = gradient.getColourAtPosition(pos);
colors[i] = color;
}
this->gradient = gradient;
isReady = true;
}
void setBuffer(juce::AudioBuffer<float>& buffer, int skipSamples = 0) {
magnitude = 0.f;
/// alternative code to get magnitude of the buffer
skipSamples++;
for (auto i = 0; i < buffer.getNumSamples(); i+=skipSamples)
{
auto sampleL = buffer.getReadPointer(0,i);
auto sampleR = buffer.getReadPointer(1,i);
float absSample = std::abs(*sampleL + *sampleR) / 2;
if(magnitude < absSample) magnitude = absSample;
}
//magnitude = buffer.getMagnitude(0, 0, buffer.getNumSamples());
if(gain < magnitude) gain = magnitude;
else gain = gain + release * (magnitude - gain);
if(magnitude < 0.05) imageIdx = 0;
else imageIdx = int(gain*(imagesMax-1) + 1);
}
void setBufferRMS(juce::AudioBuffer<float>& buffer) {
float newRMS = (buffer.getRMSLevel(0, 0, buffer.getNumSamples()) + buffer.getRMSLevel(1, 0, buffer.getNumSamples())) / 2;
RMS = (newRMS + RMS) / 2;
}
void draw(juce::Graphics& g) {
const int frames = img1.getHeight() / img1.getWidth() - 80;
//const float dB = linearToDB(RMS);
const int frameId = (int)ceil(RMS * ((double)frames - 1.0)) + 50;
const float radius = juce::jmin(width / 2.0f, height / 2.0f);
const float centerX = x + width * 0.5f;
const float centerY = y + height * 0.5f;
const float rx = centerX - radius - 1.0f;
const float ry = centerY - radius;
g.drawImage(img1,
(int)rx,
(int)ry,
3 * (int)radius,
2 * (int)radius,
0,
frameId*img1.getWidth(),
img1.getWidth(),
img1.getWidth());
}
bool shouldRepaint() {
return true;
/*
if(imageCache == imageIdx) {
return false;
}
else {
imageCache = imageIdx;
return true;
}
*/
}
void setImages(int x, int y, int width, int height) {
img1 = juce::ImageFileFormat::loadFrom(BinaryData::VU_png, BinaryData::VU_pngSize);
this->x = x;
this->y = y;
this->width = width;
this->height = height;
isReady = true;
/*
for(int i=0; i<images.size(); i++) {
//auto iF = float(i);
//auto iRel = iF * imagesMaxInv;
for(int y=0; y<images[i].getHeight(); y++) {
for(int x=0; x<images[i].getWidth(); x++) {
images[i] = image;
//images[i].setPixelAt(x, y, image.getPixelAt(x, y));
}
}
}
*/
}
};
class Bulb : public juce::Component
{
public:
Bulb(const juce::Colour& c) : colour(c) {}
void paint(juce::Graphics& g) override
{
const auto delta = 4.f;
const auto bounds = getLocalBounds().toFloat().reduced(delta);
//const auto side = juce::jmin(bounds.getWidth(), bounds.getHeight());
const auto height = 10;
const auto width = 8;
const auto bulbFillBounds = juce::Rectangle<float>{ bounds.getX(), bounds.getY(), width, height };
if (isOn)
g.setColour(colour);
else
g.setColour(juce::Colours::black);
g.fillRect(bulbFillBounds);
g.setColour(juce::Colours::black);
g.drawRect(bulbFillBounds, 1.f);
if (isOn)
{
g.setGradientFill(
juce::ColourGradient{
colour.withAlpha(0.3f),
bulbFillBounds.getCentre(),
colour.withLightness(1.5f).withAlpha(0.f),
{},
true
});
g.fillEllipse(bulbFillBounds.expanded(delta));
}
}
void setState(const bool state) { isOn = state; }
private:
bool isOn = false;
juce::Colour colour{};
};
class VerticalDiscreteMeter : public juce::Component, public juce::Timer
{
public:
VerticalDiscreteMeter(std::function<float()>&& valueFunction) : valueSupplier(std::move(valueFunction))
{
startTimerHz(24);
}
void paint(juce::Graphics& g) override
{
const auto level = juce::jmap(valueSupplier(), -60.f, 6.f, 0.f, 1.f);
for (auto i = 0 ; i < totalNumberOfBulbs; i++)
{
if (level >= static_cast<float>(i + 1) / totalNumberOfBulbs)
bulbs[i]->setState(true);
else
bulbs[i]->setState(false);
}
}
void resized() override
{
const auto bounds = getLocalBounds().toFloat();
gradient = juce::ColourGradient{
juce::Colours::green,
bounds.getBottomLeft(),
juce::Colours::red,
bounds.getTopLeft(),
false
};
gradient.addColour(0.5, juce::Colours::yellow);
auto bulbBounds = getLocalBounds();
const auto bulbWidth = bulbBounds.getWidth() / totalNumberOfBulbs;
bulbs.clear();
for (auto i = 0; i < totalNumberOfBulbs; i++)
{
auto bulb = std::make_unique<Bulb>(gradient.getColourAtPosition(static_cast<double>(i) / totalNumberOfBulbs));
addAndMakeVisible(bulb.get());
bulb->setBounds(bulbBounds.removeFromLeft(bulbWidth));
bulbs.push_back(std::move(bulb));
}
}
void timerCallback() override
{
repaint();
}
private:
std::function<float()> valueSupplier;
std::vector<std::unique_ptr<Bulb>> bulbs;
juce::ColourGradient gradient{};
const int totalNumberOfBulbs = 20;
};
}
#endif