Files
osc-vst-gui/plugin/PluginEditor.cpp
T

378 lines
16 KiB
C++
Raw Normal View History

#include "PluginEditor.h"
#include <algorithm>
#include <cmath>
namespace Params
{
static constexpr const char* waveform = "waveform";
static constexpr const char* waveform2 = "waveform2";
static constexpr const char* osc1Enabled = "osc1Enabled";
static constexpr const char* osc2Enabled = "osc2Enabled";
static constexpr const char* osc1Level = "osc1Level";
static constexpr const char* osc2Level = "osc2Level";
static constexpr const char* unisonVoices = "unisonVoices";
static constexpr const char* unisonGain = "unisonGain";
static constexpr const char* stereoSpread = "stereoSpread";
static constexpr const char* detuneAmount = "detuneAmount";
static constexpr const char* filterType = "filterType";
static constexpr const char* filterCutoff = "filterCutoff";
static constexpr const char* filterRes = "filterResonance";
static constexpr const char* attack = "attack";
static constexpr const char* decay = "decay";
static constexpr const char* sustain = "sustain";
static constexpr const char* release = "release";
static constexpr const char* lfoFreq = "lfoFrequency";
static constexpr const char* pitchBendRange= "pitchBendRange";
}
DarkLookAndFeel::DarkLookAndFeel()
{
setColour (juce::ResizableWindow::backgroundColourId, juce::Colour (0xff1e1e24));
setColour (juce::Slider::thumbColourId, juce::Colour (0xff7cc7ff));
setColour (juce::Slider::rotarySliderFillColourId, juce::Colour (0xff4aa3df));
setColour (juce::Slider::rotarySliderOutlineColourId, juce::Colour (0xff3a3a44));
setColour (juce::Label::textColourId, juce::Colours::whitesmoke);
setColour (juce::ComboBox::backgroundColourId, juce::Colour (0xff2b2b33));
setColour (juce::ComboBox::textColourId, juce::Colours::whitesmoke);
setColour (juce::ComboBox::outlineColourId, juce::Colour (0xff555566));
setColour (juce::ToggleButton::textColourId, juce::Colours::whitesmoke);
setColour (juce::ToggleButton::tickColourId, juce::Colour (0xff7cc7ff));
}
void DarkLookAndFeel::drawRotarySlider (juce::Graphics& g, int x, int y, int width, int height,
float sliderPos, float rotaryStartAngle,
float rotaryEndAngle, juce::Slider& slider)
{
auto radius = (float) juce::jmin (width / 2, height / 2) - 6.0f;
auto centreX = (float) x + (float) width * 0.5f;
auto centreY = (float) y + (float) height * 0.5f;
auto rx = centreX - radius;
auto ry = centreY - radius;
auto rw = radius * 2.0f;
auto angle = rotaryStartAngle + sliderPos * (rotaryEndAngle - rotaryStartAngle);
g.setColour (juce::Colour (0xff2f2f38));
g.fillEllipse (rx, ry, rw, rw);
juce::Path outline;
outline.addCentredArc (centreX, centreY, radius, radius, 0.0f, rotaryStartAngle, rotaryEndAngle, true);
g.setColour (findColour (juce::Slider::rotarySliderOutlineColourId));
g.strokePath (outline, juce::PathStrokeType (4.0f));
juce::Path valueArc;
valueArc.addCentredArc (centreX, centreY, radius, radius, 0.0f, rotaryStartAngle, angle, true);
g.setColour (findColour (juce::Slider::rotarySliderFillColourId));
g.strokePath (valueArc, juce::PathStrokeType (4.0f));
juce::Path pointer;
auto pointerLength = radius * 0.7f;
auto pointerThickness = 3.0f;
pointer.addRectangle (-pointerThickness * 0.5f, -radius, pointerThickness, pointerLength);
pointer.applyTransform (juce::AffineTransform::rotation (angle).translated (centreX, centreY));
g.setColour (findColour (juce::Slider::thumbColourId));
g.fillPath (pointer);
juce::ignoreUnused (slider);
}
WaveformView::WaveformView(OscAudioProcessor& p, int oscIndex) : processor(p), osc(oscIndex)
{
startTimerHz(15);
}
void WaveformView::timerCallback() { repaint(); }
void WaveformView::paint(juce::Graphics& g)
{
g.fillAll(juce::Colour(0xff15151a));
g.setColour(juce::Colour(0xff333344));
g.drawRect(getLocalBounds());
int idx = (int) *processor.apvts.getRawParameterValue(osc == 1 ? Params::waveform : Params::waveform2);
bool enabled = *processor.apvts.getRawParameterValue(osc == 1 ? Params::osc1Enabled : Params::osc2Enabled) > 0.5f;
auto* wt = osc == 1 ? processor.getSynth().get_wavetable(idx) : processor.getSynth().get_wavetable2(idx);
if (!wt) return;
auto b = getLocalBounds().toFloat().reduced(4.0f);
juce::Path pth;
const auto& table = wt->wavetable;
for (size_t i = 0; i < table.size(); ++i)
{
float x = b.getX() + (float)i / (float)(table.size() - 1) * b.getWidth();
float y = b.getCentreY() - (float)table[i] * (b.getHeight() * 0.45f);
if (i == 0) pth.startNewSubPath(x, y); else pth.lineTo(x, y);
}
g.setColour(enabled ? juce::Colour(0xff66d9ff) : juce::Colour(0xff555566));
g.strokePath(pth, juce::PathStrokeType(enabled ? 2.0f : 1.0f));
}
EnvelopeView::EnvelopeView(OscAudioProcessor& p) : processor(p) { startTimerHz(15); }
void EnvelopeView::timerCallback() { repaint(); }
void EnvelopeView::paint(juce::Graphics& g)
{
g.fillAll(juce::Colour(0xff15151a));
g.setColour(juce::Colour(0xff333344));
g.drawRect(getLocalBounds());
float a = *processor.apvts.getRawParameterValue(Params::attack);
float d = *processor.apvts.getRawParameterValue(Params::decay);
float s = *processor.apvts.getRawParameterValue(Params::sustain);
float r = *processor.apvts.getRawParameterValue(Params::release);
float sustainHold = juce::jmax(0.2f, (a + d + r) * 0.3f);
float total = a + d + sustainHold + r;
if (total <= 0.0f) total = 1.0f;
auto b = getLocalBounds().toFloat().reduced(6.0f);
auto x = [&](float t){ return b.getX() + (t / total) * b.getWidth(); };
auto y = [&](float v){ return b.getBottom() - v * b.getHeight(); };
juce::Path p;
p.startNewSubPath(x(0), y(0));
p.lineTo(x(a), y(1.0f));
p.lineTo(x(a + d), y(s));
p.lineTo(x(a + d + sustainHold), y(s));
p.lineTo(x(total), y(0));
g.setColour(juce::Colour(0xff7cff9a));
g.strokePath(p, juce::PathStrokeType(2.0f));
}
FilterView::FilterView(OscAudioProcessor& p) : processor(p) { startTimerHz(15); }
void FilterView::timerCallback() { repaint(); }
void FilterView::paint(juce::Graphics& g)
{
g.fillAll(juce::Colour(0xff15151a));
g.setColour(juce::Colour(0xff333344));
g.drawRect(getLocalBounds());
int type = (int) *processor.apvts.getRawParameterValue(Params::filterType);
float cutoff = *processor.apvts.getRawParameterValue(Params::filterCutoff);
float res = *processor.apvts.getRawParameterValue(Params::filterRes);
auto b = getLocalBounds().toFloat().reduced(6.0f);
juce::Path p;
const int steps = 120;
for (int i = 0; i <= steps; ++i)
{
float t = (float)i / (float)steps;
float freq = 20.0f * std::pow(1000.0f, t); // 20..20k approx
float x = b.getX() + t * b.getWidth();
float mag = 1.0f;
if (type == 1) { // LP
float r = freq / juce::jmax(20.0f, cutoff);
mag = 1.0f / std::sqrt(1.0f + std::pow(r, 4.0f));
if (std::abs(freq - cutoff) < cutoff * 0.15f) mag *= (1.0f + res * 1.5f);
} else if (type == 2) { // BP approx
float d = (freq - cutoff) / (cutoff * (0.4f + (1.0f - res) * 0.8f));
mag = std::exp(-d * d * 2.0f);
} else if (type == 3) { // HP
float r = juce::jmax(20.0f, cutoff) / juce::jmax(20.0f, freq);
mag = 1.0f / std::sqrt(1.0f + std::pow(r, 4.0f));
if (std::abs(freq - cutoff) < cutoff * 0.15f) mag *= (1.0f + res * 1.5f);
} else { mag = 0.9f; }
mag = juce::jlimit(0.0f, 1.2f, mag);
float y = b.getBottom() - mag * b.getHeight() * 0.8f - b.getHeight() * 0.05f;
if (i == 0) p.startNewSubPath(x, y); else p.lineTo(x, y);
}
g.setColour(juce::Colour(0xffffcc66));
g.strokePath(p, juce::PathStrokeType(2.0f));
}
OscAudioProcessorEditor::OscAudioProcessorEditor (OscAudioProcessor& p)
: AudioProcessorEditor (&p), processorRef (p),
waveView1(p, 1), waveView2(p, 2), envView(p), filterView(p)
{
setLookAndFeel (&lookAndFeel);
setSize (980, 640);
auto setupTitle = [this](juce::Label& l, const juce::String& t) {
l.setText (t, juce::dontSendNotification);
l.setJustificationType (juce::Justification::centred);
l.setFont (juce::FontOptions (15.0f, juce::Font::bold));
addAndMakeVisible (l);
};
setupTitle (osc1Title, "Oscillator 1");
setupTitle (osc2Title, "Oscillator 2");
setupTitle (unisonTitle, "Unison");
setupTitle (filterTitle, "Filter");
setupTitle (envTitle, "Envelope (ADSR)");
setupTitle (lfoTitle, "LFO");
setupTitle (masterTitle, "Master");
addCombo (waveformBox, waveformLabel, "Waveform");
waveformBox.addItemList (juce::StringArray { "Sine", "Saw", "Square", "Triangle" }, 1);
waveformAtt = std::make_unique<ComboAtt> (processorRef.apvts, Params::waveform, waveformBox);
addCombo (waveformBox2, waveformLabel2, "Waveform");
waveformBox2.addItemList (juce::StringArray { "Sine", "Saw", "Square", "Triangle" }, 1);
waveformAtt2 = std::make_unique<ComboAtt> (processorRef.apvts, Params::waveform2, waveformBox2);
addAndMakeVisible(osc1Enable); addAndMakeVisible(osc2Enable);
osc1EnableAtt = std::make_unique<ButtonAtt>(processorRef.apvts, Params::osc1Enabled, osc1Enable);
osc2EnableAtt = std::make_unique<ButtonAtt>(processorRef.apvts, Params::osc2Enabled, osc2Enable);
addCombo (filterTypeBox, filterTypeLabel, "Type");
filterTypeBox.addItemList (juce::StringArray { "Off", "Low-pass", "Band-pass", "High-pass" }, 1);
filterTypeAtt = std::make_unique<ComboAtt> (processorRef.apvts, Params::filterType, filterTypeBox);
auto addRotary = [this](juce::Slider& s, juce::Label& l, const juce::String& name, const char* paramId, auto& att) {
addSlider (s, l, name, true);
att = std::make_unique<SliderAtt> (processorRef.apvts, paramId, s);
};
addRotary (osc1Level, osc1LevelLabel, "Level", Params::osc1Level, osc1LevelAtt);
addRotary (osc2Level, osc2LevelLabel, "Level", Params::osc2Level, osc2LevelAtt);
addSlider (unisonVoices, unisonVoicesLabel, "Voices", false);
unisonVoices.setSliderStyle (juce::Slider::LinearHorizontal);
unisonVoices.setTextBoxStyle (juce::Slider::TextBoxRight, false, 50, 20);
unisonVoicesAtt = std::make_unique<SliderAtt> (processorRef.apvts, Params::unisonVoices, unisonVoices);
addRotary (unisonGain, unisonGainLabel, "Gain", Params::unisonGain, unisonGainAtt);
addRotary (stereoSpread, stereoSpreadLabel, "Spread", Params::stereoSpread, stereoSpreadAtt);
addRotary (detuneAmount, detuneAmountLabel, "Detune", Params::detuneAmount, detuneAmountAtt);
addRotary (cutoff, cutoffLabel, "Cutoff", Params::filterCutoff, cutoffAtt);
addRotary (resonance, resonanceLabel, "Res", Params::filterRes, resonanceAtt);
addRotary (attack, attackLabel, "A", Params::attack, attackAtt);
addRotary (decay, decayLabel, "D", Params::decay, decayAtt);
addRotary (sustain, sustainLabel, "S", Params::sustain, sustainAtt);
addRotary (release, releaseLabel, "R", Params::release, releaseAtt);
addRotary (lfoFreq, lfoFreqLabel, "Freq", Params::lfoFreq, lfoFreqAtt);
addRotary (pitchBendRange, pitchBendRangeLabel, "PB Range", Params::pitchBendRange, pitchBendRangeAtt);
addAndMakeVisible(waveView1); addAndMakeVisible(waveView2);
addAndMakeVisible(envView); addAndMakeVisible(filterView);
}
OscAudioProcessorEditor::~OscAudioProcessorEditor()
{
setLookAndFeel (nullptr);
}
void OscAudioProcessorEditor::addSlider(juce::Slider& s, juce::Label& l, const juce::String& name, bool rotary)
{
addAndMakeVisible (s);
if (rotary)
{
s.setSliderStyle (juce::Slider::RotaryHorizontalVerticalDrag);
s.setTextBoxStyle (juce::Slider::TextBoxBelow, false, 64, 16);
}
l.setText (name, juce::dontSendNotification);
l.setJustificationType (juce::Justification::centred);
l.attachToComponent (&s, false);
addAndMakeVisible (l);
}
void OscAudioProcessorEditor::addCombo(juce::ComboBox& c, juce::Label& l, const juce::String& name)
{
addAndMakeVisible (c);
l.setText (name, juce::dontSendNotification);
l.setJustificationType (juce::Justification::centred);
l.attachToComponent (&c, false);
addAndMakeVisible (l);
}
void OscAudioProcessorEditor::paint (juce::Graphics& g)
{
g.fillAll (juce::Colour (0xff1e1e24));
auto drawSection = [&g](juce::Rectangle<int> r) {
g.setColour (juce::Colour (0xff2a2a33));
g.fillRoundedRectangle (r.toFloat(), 8.0f);
g.setColour (juce::Colour (0xff444455));
g.drawRoundedRectangle (r.toFloat(), 8.0f, 1.0f);
};
auto area = getLocalBounds().reduced (8);
auto top = area.removeFromTop (320);
auto bottom = area;
auto osc1Area = top.removeFromLeft (240).reduced (4);
auto osc2Area = top.removeFromLeft (240).reduced (4);
auto unisonArea = top.removeFromLeft (220).reduced (4);
auto filterArea = top.reduced (4);
auto envArea = bottom.removeFromLeft (360).reduced (4);
auto lfoArea = bottom.removeFromLeft (200).reduced (4);
auto masterArea = bottom.reduced (4);
drawSection(osc1Area); drawSection(osc2Area); drawSection(unisonArea);
drawSection(filterArea); drawSection(envArea); drawSection(lfoArea); drawSection(masterArea);
}
void OscAudioProcessorEditor::resized()
{
auto area = getLocalBounds().reduced (8);
auto top = area.removeFromTop (320);
auto bottom = area;
auto osc1Area = top.removeFromLeft (240).reduced (4);
auto osc2Area = top.removeFromLeft (240).reduced (4);
auto unisonArea = top.removeFromLeft (220).reduced (4);
auto filterArea = top.reduced (4);
auto envArea = bottom.removeFromLeft (360).reduced (4);
auto lfoArea = bottom.removeFromLeft (200).reduced (4);
auto masterArea = bottom.reduced (4);
auto placeTitle = [](juce::Label& t, juce::Rectangle<int>& r) { t.setBounds (r.removeFromTop (22)); r.removeFromTop (4); };
{
auto r = osc1Area.reduced (8);
placeTitle (osc1Title, r);
osc1Enable.setBounds(r.removeFromTop(22)); r.removeFromTop(18);
waveformBox.setBounds (r.removeFromTop (26)); r.removeFromTop(16);
waveView1.setBounds(r.removeFromTop(90)); r.removeFromTop(14);
osc1Level.setBounds(r.removeFromTop(110).reduced(30,0));
}
{
auto r = osc2Area.reduced (8);
placeTitle (osc2Title, r);
osc2Enable.setBounds(r.removeFromTop(22)); r.removeFromTop(18);
waveformBox2.setBounds (r.removeFromTop (26)); r.removeFromTop(16);
waveView2.setBounds(r.removeFromTop(90)); r.removeFromTop(14);
osc2Level.setBounds(r.removeFromTop(110).reduced(30,0));
}
{
auto r = unisonArea.reduced (8);
placeTitle (unisonTitle, r);
r.removeFromTop(18);
unisonVoices.setBounds (r.removeFromTop (28)); r.removeFromTop(18);
auto knobs = r;
auto w = knobs.getWidth() / 3;
unisonGain.setBounds (knobs.removeFromLeft (w).reduced (2));
stereoSpread.setBounds (knobs.removeFromLeft (w).reduced (2));
detuneAmount.setBounds (knobs.reduced (2));
}
{
auto r = filterArea.reduced (8);
placeTitle (filterTitle, r);
r.removeFromTop(18);
filterTypeBox.setBounds (r.removeFromTop (26).removeFromLeft (180)); r.removeFromTop(8);
filterView.setBounds(r.removeFromTop(110)); r.removeFromTop(8);
auto knobs = r;
auto w = knobs.getWidth() / 2;
cutoff.setBounds (knobs.removeFromLeft (w).reduced (10));
resonance.setBounds (knobs.reduced (10));
}
{
auto r = envArea.reduced (8);
placeTitle (envTitle, r);
envView.setBounds(r.removeFromTop(120)); r.removeFromTop(6);
auto knobs = r;
auto w = knobs.getWidth() / 4;
attack.setBounds (knobs.removeFromLeft (w).reduced (4));
decay.setBounds (knobs.removeFromLeft (w).reduced (4));
sustain.setBounds (knobs.removeFromLeft (w).reduced (4));
release.setBounds (knobs.reduced (4));
}
{
auto r = lfoArea.reduced (8);
placeTitle (lfoTitle, r);
lfoFreq.setBounds (r.reduced (20));
}
{
auto r = masterArea.reduced (8);
placeTitle (masterTitle, r);
pitchBendRange.setBounds (r.reduced (20));
}
}