256 lines
10 KiB
C++
256 lines
10 KiB
C++
|
|
#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();
|
||
|
|
}
|