адаптация s13bby/osc под vst и gui. (вайбкод)

вайбкод потому что мне в моменте стало понятно. что проще было сразу писать на фреймворке JUCE, чем адаптировать консольное приложение на MiniAudio.
обновляться данный репозиторий не будет. СmakeLists адаптирован онли под linux.
This commit is contained in:
2026-11-20 14:52:33 +06:00
parent 8384123f87
commit b0a75e98db
10387 changed files with 4025195 additions and 0 deletions
+377
View File
@@ -0,0 +1,377 @@
#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));
}
}
+91
View File
@@ -0,0 +1,91 @@
#pragma once
#include <juce_audio_processors/juce_audio_processors.h>
#include "PluginProcessor.h"
class DarkLookAndFeel : public juce::LookAndFeel_V4
{
public:
DarkLookAndFeel();
void drawRotarySlider (juce::Graphics&, int x, int y, int width, int height,
float sliderPosProportional, float rotaryStartAngle,
float rotaryEndAngle, juce::Slider&) override;
};
class WaveformView : public juce::Component, private juce::Timer
{
public:
WaveformView(OscAudioProcessor& p, int oscIndex);
void paint(juce::Graphics& g) override;
private:
void timerCallback() override;
OscAudioProcessor& processor;
int osc;
};
class EnvelopeView : public juce::Component, private juce::Timer
{
public:
EnvelopeView(OscAudioProcessor& p);
void paint(juce::Graphics& g) override;
private:
void timerCallback() override;
OscAudioProcessor& processor;
};
class FilterView : public juce::Component, private juce::Timer
{
public:
FilterView(OscAudioProcessor& p);
void paint(juce::Graphics& g) override;
private:
void timerCallback() override;
OscAudioProcessor& processor;
};
class OscAudioProcessorEditor : public juce::AudioProcessorEditor
{
public:
explicit OscAudioProcessorEditor (OscAudioProcessor&);
~OscAudioProcessorEditor() override;
void paint (juce::Graphics&) override;
void resized() override;
private:
void addSlider(juce::Slider& s, juce::Label& l, const juce::String& name, bool rotary = true);
void addCombo(juce::ComboBox& c, juce::Label& l, const juce::String& name);
OscAudioProcessor& processorRef;
DarkLookAndFeel lookAndFeel;
juce::ComboBox waveformBox, waveformBox2, filterTypeBox;
juce::ToggleButton osc1Enable { "OSC 1" }, osc2Enable { "OSC 2" };
juce::Slider osc1Level, osc2Level;
juce::Slider unisonVoices, unisonGain, stereoSpread, detuneAmount;
juce::Slider cutoff, resonance, attack, decay, sustain, release, lfoFreq, pitchBendRange;
juce::Label waveformLabel, waveformLabel2, filterTypeLabel;
juce::Label osc1LevelLabel, osc2LevelLabel;
juce::Label unisonVoicesLabel, unisonGainLabel, stereoSpreadLabel, detuneAmountLabel;
juce::Label cutoffLabel, resonanceLabel, attackLabel, decayLabel, sustainLabel, releaseLabel, lfoFreqLabel, pitchBendRangeLabel;
juce::Label osc1Title, osc2Title, unisonTitle, filterTitle, envTitle, lfoTitle, masterTitle;
WaveformView waveView1, waveView2;
EnvelopeView envView;
FilterView filterView;
using SliderAtt = juce::AudioProcessorValueTreeState::SliderAttachment;
using ComboAtt = juce::AudioProcessorValueTreeState::ComboBoxAttachment;
using ButtonAtt = juce::AudioProcessorValueTreeState::ButtonAttachment;
std::unique_ptr<ComboAtt> waveformAtt, waveformAtt2, filterTypeAtt;
std::unique_ptr<ButtonAtt> osc1EnableAtt, osc2EnableAtt;
std::unique_ptr<SliderAtt> osc1LevelAtt, osc2LevelAtt;
std::unique_ptr<SliderAtt> unisonVoicesAtt, unisonGainAtt, stereoSpreadAtt, detuneAmountAtt;
std::unique_ptr<SliderAtt> cutoffAtt, resonanceAtt, attackAtt, decayAtt, sustainAtt, releaseAtt, lfoFreqAtt, pitchBendRangeAtt;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OscAudioProcessorEditor)
};
+255
View File
@@ -0,0 +1,255 @@
#include "PluginProcessor.h"
#include "PluginEditor.h"
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";
}
OscAudioProcessor::OscAudioProcessor()
: AudioProcessor (BusesProperties()
.withOutput ("Output", juce::AudioChannelSet::stereo(), true)),
apvts (*this, nullptr, "Parameters", createParameterLayout())
{
}
juce::AudioProcessorValueTreeState::ParameterLayout OscAudioProcessor::createParameterLayout()
{
std::vector<std::unique_ptr<juce::RangedAudioParameter>> params;
params.push_back (std::make_unique<juce::AudioParameterChoice>(
Params::waveform, "Osc 1 Waveform",
juce::StringArray { "Sine", "Saw", "Square", "Triangle" }, 1));
params.push_back (std::make_unique<juce::AudioParameterChoice>(
Params::waveform2, "Osc 2 Waveform",
juce::StringArray { "Sine", "Saw", "Square", "Triangle" }, 2));
params.push_back (std::make_unique<juce::AudioParameterBool>(
Params::osc1Enabled, "Osc 1 Enabled", true));
params.push_back (std::make_unique<juce::AudioParameterBool>(
Params::osc2Enabled, "Osc 2 Enabled", false));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::osc1Level, "Osc 1 Level", juce::NormalisableRange<float>(0.0f, 1.0f), 1.0f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::osc2Level, "Osc 2 Level", juce::NormalisableRange<float>(0.0f, 1.0f), 0.5f));
params.push_back (std::make_unique<juce::AudioParameterInt>(
Params::unisonVoices, "Unison Voices", 1, 7, 7));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::unisonGain, "Unison Gain",
juce::NormalisableRange<float>(0.0f, 1.0f), 1.0f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::stereoSpread, "Stereo Spread",
juce::NormalisableRange<float>(0.0f, 1.0f), 1.0f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::detuneAmount, "Detune Amount",
juce::NormalisableRange<float>(0.0f, 100.0f), 30.0f));
params.push_back (std::make_unique<juce::AudioParameterChoice>(
Params::filterType, "Filter Type",
juce::StringArray { "Off", "Low-pass", "Band-pass", "High-pass" }, 0));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::filterCutoff, "Filter Cutoff",
juce::NormalisableRange<float>(20.0f, 18000.0f, 0.0f, 0.3f), 1000.0f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::filterRes, "Filter Resonance",
juce::NormalisableRange<float>(0.0f, 1.0f), 0.707f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::attack, "Attack",
juce::NormalisableRange<float>(0.001f, 10.0f, 0.0f, 0.3f), 0.8f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::decay, "Decay",
juce::NormalisableRange<float>(0.001f, 10.0f, 0.0f, 0.3f), 1.0f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::sustain, "Sustain",
juce::NormalisableRange<float>(0.0f, 1.0f), 0.7f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::release, "Release",
juce::NormalisableRange<float>(0.001f, 10.0f, 0.0f, 0.3f), 0.2f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::lfoFreq, "LFO Frequency",
juce::NormalisableRange<float>(0.1f, 20.0f, 0.0f, 0.3f), 5.0f));
params.push_back (std::make_unique<juce::AudioParameterFloat>(
Params::pitchBendRange, "Pitch Bend Range",
juce::NormalisableRange<float>(1.0f, 24.0f, 1.0f), 2.0f));
return { params.begin(), params.end() };
}
const juce::String OscAudioProcessor::getName() const { return "osc"; }
bool OscAudioProcessor::acceptsMidi() const { return true; }
bool OscAudioProcessor::producesMidi() const { return false; }
bool OscAudioProcessor::isMidiEffect() const { return false; }
double OscAudioProcessor::getTailLengthSeconds() const { return 0.0; }
int OscAudioProcessor::getNumPrograms() { return 1; }
int OscAudioProcessor::getCurrentProgram() { return 0; }
void OscAudioProcessor::setCurrentProgram (int) {}
const juce::String OscAudioProcessor::getProgramName (int) { return {}; }
void OscAudioProcessor::changeProgramName (int, const juce::String&) {}
void OscAudioProcessor::prepareToPlay (double sampleRate, int)
{
currentSampleRate = sampleRate;
synth.prepare(sampleRate);
updateSynthParameters();
}
void OscAudioProcessor::releaseResources()
{
synth.all_notes_off();
}
bool OscAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
{
if (layouts.getMainOutputChannelSet() != juce::AudioChannelSet::mono()
&& layouts.getMainOutputChannelSet() != juce::AudioChannelSet::stereo())
return false;
return true;
}
void OscAudioProcessor::updateSynthParameters()
{
synth.apply_waveform ((int) *apvts.getRawParameterValue (Params::waveform));
synth.apply_waveform2 ((int) *apvts.getRawParameterValue (Params::waveform2));
synth.apply_osc1_enabled (*apvts.getRawParameterValue (Params::osc1Enabled) > 0.5f);
synth.apply_osc2_enabled (*apvts.getRawParameterValue (Params::osc2Enabled) > 0.5f);
synth.apply_osc1_level (*apvts.getRawParameterValue (Params::osc1Level));
synth.apply_osc2_level (*apvts.getRawParameterValue (Params::osc2Level));
synth.apply_unison_voices ((int) *apvts.getRawParameterValue (Params::unisonVoices));
synth.apply_unison_gain (*apvts.getRawParameterValue (Params::unisonGain));
synth.apply_stereo_spread (*apvts.getRawParameterValue (Params::stereoSpread));
synth.apply_detune_amount (*apvts.getRawParameterValue (Params::detuneAmount));
synth.apply_filter_type ((int) *apvts.getRawParameterValue (Params::filterType));
{
double cutoff = (double) *apvts.getRawParameterValue (Params::filterCutoff);
double maxCutoff = currentSampleRate / 6.2;
if (cutoff > maxCutoff) cutoff = maxCutoff;
synth.apply_filter_cutoff (cutoff);
}
synth.apply_filter_resonance (*apvts.getRawParameterValue (Params::filterRes));
synth.set_attack (*apvts.getRawParameterValue (Params::attack));
synth.set_decay (*apvts.getRawParameterValue (Params::decay));
synth.set_sustain (*apvts.getRawParameterValue (Params::sustain));
synth.set_release (*apvts.getRawParameterValue (Params::release));
synth.set_lfo_frequency (*apvts.getRawParameterValue (Params::lfoFreq));
synth.set_pitch_bend_range (*apvts.getRawParameterValue (Params::pitchBendRange));
}
void OscAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
juce::ScopedNoDenormals noDenormals;
buffer.clear();
updateSynthParameters();
auto numSamples = buffer.getNumSamples();
auto* left = buffer.getWritePointer (0);
auto* right = buffer.getNumChannels() > 1 ? buffer.getWritePointer (1) : nullptr;
int currentSample = 0;
for (const auto metadata : midiMessages)
{
const auto msg = metadata.getMessage();
const int eventSample = std::clamp (metadata.samplePosition, 0, numSamples);
if (eventSample > currentSample)
{
for (int i = currentSample; i < eventSample; ++i)
{
auto s = synth.generate();
left[i] = (float) s.left;
if (right) right[i] = (float) s.right;
}
currentSample = eventSample;
}
if (msg.isNoteOn())
synth.note_on_direct ((uint8_t) msg.getNoteNumber(), (uint8_t) msg.getVelocity());
else if (msg.isNoteOff())
synth.note_off_direct ((uint8_t) msg.getNoteNumber());
else if (msg.isPitchWheel())
synth.apply_pitch_bend ((uint16_t) msg.getPitchWheelValue());
else if (msg.isController())
{
const auto cc = msg.getControllerNumber();
if (cc == 1)
synth.apply_mod_wheel ((uint8_t) msg.getControllerValue());
else if (cc == 120 || cc == 123) // all sound off / all notes off
synth.all_notes_off();
}
else if (msg.isAllNotesOff() || msg.isAllSoundOff())
{
synth.all_notes_off();
}
}
for (int i = currentSample; i < numSamples; ++i)
{
auto s = synth.generate();
left[i] = (float) s.left;
if (right) right[i] = (float) s.right;
}
if (right == nullptr && buffer.getNumChannels() == 1)
{
// mono already written
}
}
bool OscAudioProcessor::hasEditor() const { return true; }
juce::AudioProcessorEditor* OscAudioProcessor::createEditor()
{
return new OscAudioProcessorEditor (*this);
}
void OscAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
auto state = apvts.copyState();
std::unique_ptr<juce::XmlElement> xml (state.createXml());
copyXmlToBinary (*xml, destData);
}
void OscAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
std::unique_ptr<juce::XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
if (xmlState != nullptr && xmlState->hasTagName (apvts.state.getType()))
apvts.replaceState (juce::ValueTree::fromXml (*xmlState));
}
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
return new OscAudioProcessor();
}
+51
View File
@@ -0,0 +1,51 @@
#pragma once
#include <juce_audio_processors/juce_audio_processors.h>
#include "../include/synth.hpp"
class OscAudioProcessor : public juce::AudioProcessor
{
public:
OscAudioProcessor();
~OscAudioProcessor() override = default;
void prepareToPlay (double sampleRate, int samplesPerBlock) override;
void releaseResources() override;
bool isBusesLayoutSupported (const BusesLayout& layouts) const override;
void processBlock (juce::AudioBuffer<float>&, juce::MidiBuffer&) override;
using AudioProcessor::processBlock;
juce::AudioProcessorEditor* createEditor() override;
bool hasEditor() const override;
const juce::String getName() const override;
bool acceptsMidi() const override;
bool producesMidi() const override;
bool isMidiEffect() const override;
double getTailLengthSeconds() const override;
int getNumPrograms() override;
int getCurrentProgram() override;
void setCurrentProgram (int index) override;
const juce::String getProgramName (int index) override;
void changeProgramName (int index, const juce::String& newName) override;
void getStateInformation (juce::MemoryBlock& destData) override;
void setStateInformation (const void* data, int sizeInBytes) override;
juce::AudioProcessorValueTreeState apvts;
Synth& getSynth() { return synth; }
double getCurrentSampleRate() const { return currentSampleRate; }
private:
juce::AudioProcessorValueTreeState::ParameterLayout createParameterLayout();
void updateSynthParameters();
Synth synth;
double currentSampleRate = 48000.0;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OscAudioProcessor)
};