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
|
/*
==============================================================================
This file is part of the IEM plug-in suite.
Author: Daniel Rudrich
Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
https://iem.at
The IEM plug-in suite is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The IEM plug-in suite is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software. If not, see <https://www.gnu.org/licenses/>.
==============================================================================
*/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
//==============================================================================
/*
*/
class LevelMeter : public juce::Component
{
class Overlay : public juce::Component
{
public:
Overlay() { setBufferedToImage (true); }
~Overlay() override = default;
const float decibelsToY (const float dB)
{
return offset - scale * std::tanh (dB / minLevel * -2.0f);
}
void setMinLevel (float newMinLevel)
{
minLevel = newMinLevel;
repaint();
}
const float getOffset() { return offset; }
const float getMeterHeight() { return getHeight() - 2; }
const juce::Rectangle<int> getMeterArea() { return meterArea; }
private:
void paint (juce::Graphics& g) override
{
juce::Path bg;
juce::Rectangle<int> meterArea (0, 0, getWidth(), getHeight());
meterArea.reduce (2, 2);
int width = meterArea.getWidth();
int xPos = meterArea.getX();
bg.addRoundedRectangle (meterArea, 2);
g.setColour (juce::Colour (0xFF212121));
g.strokePath (bg, juce::PathStrokeType (2.f));
g.setColour (juce::Colours::white);
auto currentFont = juce::FontOptions (getLookAndFeel().getTypefaceForFont (
juce::FontOptions (9.0f, 0)))
.withHeight (9.0f);
g.setFont (currentFont);
int lastTextDrawPos = -1;
drawLevelMark (g, xPos, width, 0, "0");
drawLevelMark (g, xPos, width, -3, "3");
drawLevelMark (g, xPos, width, -6, "6");
for (float dB = -10.0f; dB >= minLevel; dB -= 5.0f)
lastTextDrawPos = drawLevelMark (g,
xPos,
width,
dB,
juce::String (juce::roundToInt (-dB)),
lastTextDrawPos);
}
void resized() override
{
offset = 0.1 * getHeight();
scale = getHeight() - offset;
meterArea = juce::Rectangle<int> (0, 0, getWidth(), getHeight()).reduced (2, 2);
}
const int inline drawLevelMark (juce::Graphics& g,
int x,
int width,
const int level,
const juce::String& label,
int lastTextDrawPos = -1)
{
float yPos = decibelsToY (level);
x = x + 1.0f;
width = width - 2.0f;
g.drawLine (x, yPos, x + 2, yPos);
g.drawLine (x + width - 2, yPos, x + width, yPos);
if (yPos - 4 > lastTextDrawPos)
{
g.drawText (label,
x + 2,
yPos - 4,
width - 4,
9,
juce::Justification::centred,
false);
return yPos + 5;
}
return lastTextDrawPos;
}
juce::Rectangle<int> meterArea;
float minLevel = -60.0f;
float scale = 0.0f;
float offset = 0.0f;
};
public:
LevelMeter() { addAndMakeVisible (overlay); }
~LevelMeter() {}
void setGainReductionMeter (bool isGainReductionMeter)
{
isGRmeter = isGainReductionMeter;
repaint();
}
void paint (juce::Graphics& g) override
{
const int height = overlay.getMeterHeight();
auto meterArea = overlay.getMeterArea();
g.setColour (juce::Colours::black);
g.fillRect (meterArea);
juce::Rectangle<int> lvlRect;
if (isGRmeter)
lvlRect = juce::Rectangle<int> (
juce::Point<int> (meterArea.getX(), overlay.getOffset()),
juce::Point<int> (meterArea.getRight(), overlay.decibelsToY (level)));
else
lvlRect = juce::Rectangle<int> (
juce::Point<int> (meterArea.getX(), height),
juce::Point<int> (meterArea.getRight(), overlay.decibelsToY (level)));
g.setColour (levelColour);
g.fillRect (lvlRect);
}
void setColour (juce::Colour newColour)
{
levelColour = newColour;
repaint();
}
void setLevel (const float newLevel)
{
if (level != newLevel)
{
level = newLevel;
repaint();
}
}
void setMinLevel (float newMinLevel)
{
overlay.setMinLevel (newMinLevel);
repaint();
}
void resized() override { overlay.setBounds (getLocalBounds()); }
private:
Overlay overlay;
juce::Colour levelColour = juce::Colour (juce::Colours::green);
bool isGRmeter = false;
float level = 0.0f;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LevelMeter)
};
|