Files
CrilleBassStrip/Source/PluginEditor.cpp
T

1138 lines
24 KiB
C++

#include "PluginEditor.h"
#include <cmath>
namespace
{
juce::String frequencyToNoteName(float frequency)
{
if (frequency <= 0.0f)
return "--";
const float midiNote =
69.0f
+ 12.0f
* std::log2(frequency / 440.0f);
const int roundedMidiNote =
static_cast<int>(std::round(midiNote));
static const juce::StringArray noteNames
{
"C",
"C#",
"D",
"Eb",
"E",
"F",
"F#",
"G",
"Ab",
"A",
"Bb",
"B"
};
const int noteIndex =
((roundedMidiNote % 12) + 12) % 12;
return noteNames[noteIndex];
}
float transposeFrequency(float frequency, int semitones)
{
if (frequency <= 0.0f)
return 0.0f;
return frequency
* std::pow(
2.0f,
static_cast<float>(semitones) / 12.0f
);
}
}
CrilleBassStripAudioProcessorEditor::LevelMeter::LevelMeter(
Mode meterMode)
: mode(meterMode)
{
setInterceptsMouseClicks(false, false);
}
void CrilleBassStripAudioProcessorEditor::LevelMeter::setLevel(
float newLevelDb)
{
displayedDb = newLevelDb;
repaint();
}
void CrilleBassStripAudioProcessorEditor::LevelMeter::setThreshold(
float newThresholdDb)
{
thresholdDb = newThresholdDb;
repaint();
}
void CrilleBassStripAudioProcessorEditor::LevelMeter::paint(
juce::Graphics& g)
{
auto bounds = getLocalBounds().toFloat();
auto titleArea = bounds.removeFromTop(18.0f);
auto valueArea = bounds.removeFromBottom(20.0f);
auto meterArea = bounds.reduced(5.0f, 3.0f);
g.setColour(juce::Colour(0xffaeb4bc));
g.setFont(juce::Font(10.0f, juce::Font::bold));
g.drawText(
mode == Mode::inputLevel ? "IN" : "GR",
titleArea,
juce::Justification::centred
);
g.setColour(juce::Colour(0xff0b0d10));
g.fillRoundedRectangle(meterArea, 3.0f);
g.setColour(juce::Colour(0xff3c424a));
g.drawRoundedRectangle(meterArea, 3.0f, 1.0f);
auto inner = meterArea.reduced(3.0f);
if (mode == Mode::inputLevel)
{
const float normalized =
juce::jmap(
juce::jlimit(
-80.0f,
0.0f,
displayedDb
),
-80.0f,
0.0f,
0.0f,
1.0f
);
const float litHeight =
inner.getHeight() * normalized;
auto lit =
inner.withTop(
inner.getBottom() - litHeight
);
juce::Colour colour =
juce::Colour(0xff45c95a);
if (displayedDb > -12.0f)
colour = juce::Colour(0xffffc247);
if (displayedDb > -3.0f)
colour = juce::Colour(0xffff5c5c);
g.setColour(colour);
g.fillRoundedRectangle(lit, 2.0f);
const float thresholdNorm =
juce::jmap(
juce::jlimit(
-80.0f,
0.0f,
thresholdDb
),
-80.0f,
0.0f,
0.0f,
1.0f
);
const float thresholdY =
inner.getBottom()
- inner.getHeight() * thresholdNorm;
g.setColour(
juce::Colours::white.withAlpha(0.9f)
);
g.drawLine(
inner.getX() - 2.0f,
thresholdY,
inner.getRight() + 2.0f,
thresholdY,
1.5f
);
}
else
{
const float normalized =
juce::jlimit(
0.0f,
1.0f,
displayedDb / 24.0f
);
const float litHeight =
inner.getHeight() * normalized;
auto lit =
inner.withHeight(litHeight);
g.setColour(juce::Colour(0xffd34a43));
g.fillRoundedRectangle(lit, 2.0f);
g.setColour(juce::Colour(0xff777f89));
g.setFont(9.0f);
for (float db : { 6.0f, 12.0f, 18.0f })
{
const float y =
inner.getY()
+ inner.getHeight() * (db / 24.0f);
g.drawLine(
inner.getX(),
y,
inner.getRight(),
y,
1.0f
);
}
}
g.setColour(juce::Colour(0xffd9dde3));
g.setFont(9.5f);
const auto value =
juce::String(displayedDb, 1) + " dB";
g.drawText(
value,
valueArea,
juce::Justification::centred
);
}
CrilleBassStripAudioProcessorEditor::TunerDisplay::TunerDisplay()
{
setInterceptsMouseClicks(false, false);
}
void CrilleBassStripAudioProcessorEditor::TunerDisplay::setFrequency(
float newFrequencyHz)
{
frequencyHz = newFrequencyHz;
repaint();
}
void CrilleBassStripAudioProcessorEditor::TunerDisplay::paint(
juce::Graphics& g)
{
auto bounds = getLocalBounds();
const bool hasSignal = frequencyHz > 0.0f;
g.setColour(juce::Colour(0xff777f89));
g.setFont(juce::Font(10.0f, juce::Font::bold));
g.drawText(
"INPUT",
bounds.removeFromTop(14),
juce::Justification::centred);
if (!hasSignal)
{
g.setColour(juce::Colour(0xfff0f1f2));
g.setFont(juce::Font(26.0f, juce::Font::bold));
g.drawText("--", 0, 14, getWidth(), 30, juce::Justification::centred);
g.setColour(juce::Colour(0xffaeb4bc));
g.setFont(10.0f);
g.drawText("-- Hz", 0, 43, getWidth(), 15, juce::Justification::centred);
g.setColour(juce::Colour(0xff454a51));
g.drawLine(18.0f, 70.0f, static_cast<float>(getWidth() - 18), 70.0f, 2.0f);
g.fillEllipse(static_cast<float>(getWidth()) * 0.5f - 4.0f, 66.0f, 8.0f, 8.0f);
g.setColour(juce::Colour(0xff686e76));
g.setFont(9.0f);
g.drawText("PLAY A NOTE", 0, 78, getWidth(), 16, juce::Justification::centred);
return;
}
const float midiNote =
69.0f + 12.0f * std::log2(frequencyHz / 440.0f);
const int nearestMidiNote =
static_cast<int>(std::round(midiNote));
const float targetFrequency =
440.0f * std::pow(
2.0f,
static_cast<float>(nearestMidiNote - 69) / 12.0f);
const float cents =
1200.0f * std::log2(frequencyHz / targetFrequency);
const bool inTune = std::abs(cents) <= 5.0f;
const auto noteName = frequencyToNoteName(targetFrequency);
g.setColour(inTune
? juce::Colour(0xff45c95a)
: juce::Colour(0xfff0f1f2));
g.setFont(juce::Font(26.0f, juce::Font::bold));
g.drawText(noteName, 0, 14, getWidth(), 30, juce::Justification::centred);
g.setColour(juce::Colour(0xffaeb4bc));
g.setFont(10.0f);
g.drawText(
juce::String(frequencyHz, 1) + " Hz",
0,
43,
getWidth(),
15,
juce::Justification::centred);
const float centreX = static_cast<float>(getWidth()) * 0.5f;
const float indicatorY = 70.0f;
const auto orange = juce::Colour(0xffe5a536);
const auto inactive = juce::Colour(0xff454a51);
const auto green = juce::Colour(0xff45c95a);
g.setColour(juce::Colour(0xff3c424a));
g.drawLine(20.0f, indicatorY, static_cast<float>(getWidth() - 20), indicatorY, 2.0f);
juce::Path leftArrow;
leftArrow.addTriangle(
12.0f, indicatorY,
28.0f, indicatorY - 9.0f,
28.0f, indicatorY + 9.0f);
juce::Path rightArrow;
rightArrow.addTriangle(
static_cast<float>(getWidth() - 12), indicatorY,
static_cast<float>(getWidth() - 28), indicatorY - 9.0f,
static_cast<float>(getWidth() - 28), indicatorY + 9.0f);
g.setColour(!inTune && cents > 5.0f ? orange : inactive);
g.fillPath(leftArrow);
g.setColour(!inTune && cents < -5.0f ? orange : inactive);
g.fillPath(rightArrow);
g.setColour(inTune ? green : juce::Colour(0xff686e76));
g.fillEllipse(centreX - 5.0f, indicatorY - 5.0f, 10.0f, 10.0f);
g.setColour(inTune ? green : orange);
g.setFont(juce::Font(9.0f, juce::Font::bold));
juce::String statusText;
if (inTune)
statusText = "IN TUNE";
else if (cents < 0.0f)
statusText = "TUNE UP " + juce::String(std::round(std::abs(cents)), 0) + " ct";
else
statusText = "TUNE DOWN " + juce::String(std::round(std::abs(cents)), 0) + " ct";
g.drawText(
statusText,
0,
80,
getWidth(),
16,
juce::Justification::centred);
}
CrilleBassStripAudioProcessorEditor::SectionToggleButton::
SectionToggleButton(
const juce::String& sectionName,
juce::Colour activeColour)
: juce::ToggleButton(sectionName),
sectionColour(activeColour)
{
setClickingTogglesState(true);
setMouseCursor(juce::MouseCursor::PointingHandCursor);
}
void CrilleBassStripAudioProcessorEditor::SectionToggleButton::paintButton(
juce::Graphics& g,
bool shouldDrawButtonAsHighlighted,
bool shouldDrawButtonAsDown)
{
const bool active = getToggleState();
const auto bounds = getLocalBounds().toFloat();
const float ledSize = 9.0f;
auto ledBounds = juce::Rectangle<float>(
5.0f,
(bounds.getHeight() - ledSize) * 0.5f,
ledSize,
ledSize);
auto ledColour = active
? sectionColour
: juce::Colour(0xff454a51);
if (shouldDrawButtonAsDown)
ledColour = ledColour.brighter(0.25f);
else if (shouldDrawButtonAsHighlighted)
ledColour = ledColour.brighter(0.12f);
g.setColour(ledColour.withAlpha(active ? 0.25f : 0.12f));
g.fillEllipse(ledBounds.expanded(3.0f));
g.setColour(ledColour);
g.fillEllipse(ledBounds);
g.setColour(active
? juce::Colour(0xfff0f1f2)
: juce::Colour(0xff686e76));
g.setFont(juce::Font(15.0f, juce::Font::bold));
g.drawText(
getButtonText(),
20,
0,
getWidth() - 25,
getHeight(),
juce::Justification::centred);
}
CrilleBassStripAudioProcessorEditor::
CrilleBassStripAudioProcessorEditor(
CrilleBassStripAudioProcessor& p)
: AudioProcessorEditor(&p),
processor(p)
{
downTuneButton.setButtonText(
"DOWN 1/2 STEP"
);
downTuneButton.setClickingTogglesState(true);
downTuneButton.setColour(
juce::ToggleButton::textColourId,
juce::Colour(0xffd9dde3)
);
downTuneButton.setColour(
juce::ToggleButton::tickColourId,
juce::Colour(0xffe5a536)
);
downTuneButton.setColour(
juce::ToggleButton::tickDisabledColourId,
juce::Colour(0xff555b63)
);
addAndMakeVisible(downTuneButton);
addAndMakeVisible(tunerToggleButton);
addAndMakeVisible(tunerDisplay);
downTuneAttachment =
std::make_unique<
juce::AudioProcessorValueTreeState::
ButtonAttachment
>(
processor.apvts,
"downTuneOn",
downTuneButton
);
tunerToggleAttachment =
std::make_unique<
juce::AudioProcessorValueTreeState::ButtonAttachment
>(
processor.apvts,
"tunerOn",
tunerToggleButton);
addAndMakeVisible(gateToggleButton);
addAndMakeVisible(compressorToggleButton);
addAndMakeVisible(distortionToggleButton);
addAndMakeVisible(eqToggleButton);
gateToggleAttachment =
std::make_unique<
juce::AudioProcessorValueTreeState::ButtonAttachment
>(
processor.apvts,
"gateOn",
gateToggleButton);
compressorToggleAttachment =
std::make_unique<
juce::AudioProcessorValueTreeState::ButtonAttachment
>(
processor.apvts,
"compressorOn",
compressorToggleButton);
distortionToggleAttachment =
std::make_unique<
juce::AudioProcessorValueTreeState::ButtonAttachment
>(
processor.apvts,
"distortionOn",
distortionToggleButton);
eqToggleAttachment =
std::make_unique<
juce::AudioProcessorValueTreeState::ButtonAttachment
>(
processor.apvts,
"eqOn",
eqToggleButton);
addKnob(
"gateThreshold",
"THRESHOLD"
);
addKnob(
"gateRelease",
"RELEASE"
);
addKnob(
"gateGain",
"GAIN"
);
addKnob(
"compThreshold",
"THRESHOLD"
);
addKnob(
"compRatio",
"RATIO"
);
addKnob(
"compGain",
"GAIN"
);
addKnob(
"drive",
"DRIVE"
);
addKnob(
"mix",
"MIX"
);
addKnob(
"distGain",
"GAIN"
);
addKnob(
"bass",
"BASS"
);
addKnob(
"lowMid",
"LOW MID"
);
addKnob(
"middle",
"MIDDLE"
);
addKnob(
"treble",
"TREBLE"
);
addKnob(
"output",
"OUTPUT"
);
addAndMakeVisible(gateInputMeter);
addAndMakeVisible(compressorInputMeter);
addAndMakeVisible(compressorReductionMeter);
updateSectionStates();
setSize(760, 720);
startTimerHz(30);
}
void CrilleBassStripAudioProcessorEditor::addKnob(
const juce::String& id,
const juce::String& caption)
{
auto k = std::make_unique<Knob>();
k->slider.setSliderStyle(
juce::Slider::RotaryHorizontalVerticalDrag
);
k->slider.setTextBoxStyle(
juce::Slider::TextBoxBelow,
false,
72,
20
);
k->slider.setColour(
juce::Slider::rotarySliderFillColourId,
juce::Colour(0xffe5a536)
);
k->slider.setColour(
juce::Slider::rotarySliderOutlineColourId,
juce::Colour(0xff30343a)
);
k->slider.setColour(
juce::Slider::thumbColourId,
juce::Colour(0xffffd275)
);
k->label.setText(
caption,
juce::dontSendNotification
);
k->label.setJustificationType(
juce::Justification::centred
);
k->label.setColour(
juce::Label::textColourId,
juce::Colour(0xffd9dde3)
);
k->label.setFont(
juce::Font(
12.0f,
juce::Font::bold
)
);
addAndMakeVisible(k->slider);
addAndMakeVisible(k->label);
attachments.push_back(
std::make_unique<
juce::AudioProcessorValueTreeState::
SliderAttachment
>(
processor.apvts,
id,
k->slider
)
);
knobs.push_back(std::move(k));
}
void CrilleBassStripAudioProcessorEditor::setKnobSectionEnabled(
int firstKnob,
int numberOfKnobs,
bool enabled)
{
const float alpha = enabled ? 1.0f : 0.35f;
for (int i = firstKnob;
i < firstKnob + numberOfKnobs;
++i)
{
knobs[static_cast<std::size_t>(i)]->slider.setEnabled(enabled);
knobs[static_cast<std::size_t>(i)]->slider.setAlpha(alpha);
knobs[static_cast<std::size_t>(i)]->label.setAlpha(alpha);
}
}
void CrilleBassStripAudioProcessorEditor::updateSectionStates()
{
const bool gateEnabled = gateToggleButton.getToggleState();
const bool compressorEnabled = compressorToggleButton.getToggleState();
const bool distortionEnabled = distortionToggleButton.getToggleState();
const bool eqEnabled = eqToggleButton.getToggleState();
setKnobSectionEnabled(0, 3, gateEnabled);
setKnobSectionEnabled(3, 3, compressorEnabled);
setKnobSectionEnabled(6, 3, distortionEnabled);
setKnobSectionEnabled(9, 5, eqEnabled);
gateInputMeter.setAlpha(gateEnabled ? 1.0f : 0.25f);
compressorInputMeter.setAlpha(compressorEnabled ? 1.0f : 0.25f);
compressorReductionMeter.setAlpha(compressorEnabled ? 1.0f : 0.25f);
}
float CrilleBassStripAudioProcessorEditor::smoothMeter(
float current,
float target,
float attack,
float release)
{
const float coefficient =
target > current ? attack : release;
return current
+ coefficient * (target - current);
}
void CrilleBassStripAudioProcessorEditor::timerCallback()
{
updateSectionStates();
const bool tunerEnabled = tunerToggleButton.getToggleState();
tunerDisplay.setVisible(tunerEnabled);
tunerDisplay.setFrequency(
tunerEnabled ? processor.getDetectedFrequencyHz() : 0.0f);
smoothedGateDb =
smoothMeter(
smoothedGateDb,
processor.getGateInputLevelDb(),
0.65f,
0.10f
);
smoothedCompressorDb =
smoothMeter(
smoothedCompressorDb,
processor.getCompressorInputLevelDb(),
0.65f,
0.10f
);
smoothedReductionDb =
smoothMeter(
smoothedReductionDb,
processor.getCompressorGainReductionDb(),
0.55f,
0.12f
);
gateInputMeter.setLevel(
smoothedGateDb
);
compressorInputMeter.setLevel(
smoothedCompressorDb
);
compressorReductionMeter.setLevel(
smoothedReductionDb
);
gateInputMeter.setThreshold(
processor.apvts
.getRawParameterValue("gateThreshold")
->load()
);
compressorInputMeter.setThreshold(
processor.apvts
.getRawParameterValue("compThreshold")
->load()
);
repaint();
}
void CrilleBassStripAudioProcessorEditor::paint(
juce::Graphics& g)
{
g.fillAll(juce::Colour(0xff111317));
g.setColour(juce::Colour(0xffe5a536));
g.setFont(
juce::Font(
28.0f,
juce::Font::bold
)
);
g.drawText(
"CRILLE BASS STRIP",
25,
15,
getWidth() - 50,
40,
juce::Justification::centredLeft
);
g.setColour(juce::Colour(0xff777f89));
g.setFont(13.0f);
g.drawText(
"DOWNTUNE > GATE > COMPRESSOR > DISTORTION > EQ",
28,
50,
getWidth() - 56,
22,
juce::Justification::centredLeft
);
const bool panelStates[]
{
downTuneButton.getToggleState(),
gateToggleButton.getToggleState(),
compressorToggleButton.getToggleState(),
distortionToggleButton.getToggleState(),
eqToggleButton.getToggleState()
};
const juce::Rectangle<int> panelBounds[]
{
{ 15, 85, 180, 300 },
{ 205, 85, 265, 300 },
{ 480, 85, 265, 300 },
{ 15, 395, 300, 310 },
{ 325, 395, 420, 310 }
};
for (int i = 0; i < 5; ++i)
{
const auto bounds = panelBounds[i].toFloat();
const bool active = panelStates[i];
g.setColour(active
? juce::Colour(0xff1b1f24)
: juce::Colour(0xff171a1e));
g.fillRoundedRectangle(bounds, 10.0f);
g.setColour(active
? juce::Colour(0xff4a5058)
: juce::Colour(0xff2a2e34));
g.drawRoundedRectangle(bounds, 10.0f, 2.0f);
}
g.setColour(downTuneButton.getToggleState()
? juce::Colour(0xfff0f1f2)
: juce::Colour(0xff686e76));
g.setFont(juce::Font(15.0f, juce::Font::bold));
g.drawText(
"DOWNTUNE",
15,
85,
180,
38,
juce::Justification::centred);
const bool tunerEnabled = tunerToggleButton.getToggleState();
if (tunerEnabled)
{
const float inputFrequency =
processor.getDetectedFrequencyHz();
const bool downTuneEnabled =
processor.apvts
.getRawParameterValue("downTuneOn")
->load() > 0.5f;
const float outputFrequency =
downTuneEnabled
? transposeFrequency(
inputFrequency,
-1
)
: inputFrequency;
const auto outputNote =
frequencyToNoteName(outputFrequency);
g.setColour(juce::Colour(0xff777f89));
g.setFont(juce::Font(11.0f, juce::Font::bold));
g.drawText(
"OUTPUT",
30,
300,
150,
16,
juce::Justification::centred
);
g.setColour(
downTuneEnabled
? juce::Colour(0xffffc247)
: juce::Colour(0xfff0f1f2)
);
g.setFont(juce::Font(27.0f, juce::Font::bold));
g.drawText(
outputNote,
30,
315,
150,
30,
juce::Justification::centred
);
g.setColour(juce::Colour(0xffaeb4bc));
g.setFont(10.0f);
const auto outputFrequencyText =
outputFrequency > 0.0f
? juce::String(outputFrequency, 1) + " Hz"
: "-- Hz";
g.drawText(
outputFrequencyText,
30,
342,
150,
16,
juce::Justification::centred
);
}
const auto latencyText =
"LATENCY: "
+ juce::String(
processor.getDownTuneLatencyMs(),
1
)
+ " ms";
g.setColour(juce::Colour(0xffaeb4bc));
g.setFont(
juce::Font(
10.0f,
juce::Font::bold
)
);
g.drawText(
latencyText,
25,
362,
160,
18,
juce::Justification::centred
);
}
void CrilleBassStripAudioProcessorEditor::resized()
{
gateToggleButton.setBounds(
245,
91,
185,
28);
compressorToggleButton.setBounds(
520,
91,
185,
28);
distortionToggleButton.setBounds(
70,
401,
190,
28);
eqToggleButton.setBounds(
435,
401,
200,
28);
downTuneButton.setBounds(
35,
120,
140,
50
);
tunerToggleButton.setBounds(
50,
175,
110,
30
);
tunerDisplay.setBounds(
30,
210,
150,
100
);
gateInputMeter.setBounds(
215,
125,
42,
225
);
compressorInputMeter.setBounds(
490,
125,
42,
225
);
compressorReductionMeter.setBounds(
693,
125,
42,
225
);
// Noise gate
knobs[0]->slider.setBounds(
265,
125,
92,
100
);
knobs[0]->label.setBounds(
265,
225,
92,
22
);
knobs[1]->slider.setBounds(
365,
125,
92,
100
);
knobs[1]->label.setBounds(
365,
225,
92,
22
);
knobs[2]->slider.setBounds(
315,
250,
92,
100
);
knobs[2]->label.setBounds(
315,
350,
92,
22
);
// Compressor
knobs[3]->slider.setBounds(
540,
125,
72,
100
);
knobs[3]->label.setBounds(
535,
225,
82,
22
);
knobs[4]->slider.setBounds(
615,
125,
72,
100
);
knobs[4]->label.setBounds(
610,
225,
82,
22
);
knobs[5]->slider.setBounds(
575,
250,
82,
100
);
knobs[5]->label.setBounds(
575,
350,
82,
22
);
// Distortion
knobs[6]->slider.setBounds(
35,
455,
82,
100
);
knobs[6]->label.setBounds(
35,
555,
82,
22
);
knobs[7]->slider.setBounds(
124,
455,
82,
100
);
knobs[7]->label.setBounds(
124,
555,
82,
22
);
knobs[8]->slider.setBounds(
213,
455,
82,
100
);
knobs[8]->label.setBounds(
213,
555,
82,
22
);
// Equalizer
for (int i = 0; i < 5; ++i)
{
const int knobIndex = 9 + i;
const int x = 335 + i * 80;
knobs[knobIndex]->slider.setBounds(
x,
455,
76,
100
);
knobs[knobIndex]->label.setBounds(
x,
555,
76,
22
);
}
}