Commit 6ac18f85 authored by Lyberta's avatar Lyberta

Improved simple plugins.

parent f4179532
/// \file
/// \brief Header file that describes the PluginTemplate class template.
/// \brief Header file that describes the PluginBaseTemplate class template.
/// \author Lyberta
/// \copyright GNU GPLv3 or any later version.
#pragma once
#include <ftz/Audio/SynthSettings.h>
#include <ftz/Audio/EnvelopeSettings.h>
#include <ftz/Audio/PolyphonicSynth.h>
#include <ftz/Audio/Pan.h>
#include <ftz/Audio/Amplifier.h>
#include <ftz/Audio/Clamper.h>
#include <ftz/Audio/EffectChain.h>
#include "../DPF/Config.h"
#include "DistrhoPlugin.hpp"
namespace DISTRHO
{
/// \brief A synth plugin template.
/// \details This is a DPF wrapper template of the synth.
/// \tparam Generator Generator of the synth.
enum class BaseParameterIndex
{
MasterVolume,
Pan,
PitchBendRange,
AttackTime,
DecayTime,
SustainLevel,
ReleaseTime
};
/// \brief A minimal base template for plugins.
/// \details This class template contains code that is shared by all plugins.
/// \tparam PluginSettings Settings of the plugin. See Config.h.
template <typename Generator, typename PluginSettings>
class PluginTemplate : public Plugin
template <typename PluginSettings>
class PluginBaseTemplate : public Plugin
{
public:
/// \brief Constructor.
/// \param[in] settings Settings of the generator.
PluginTemplate(typename Generator::Settings generator_settings);
protected:
PluginBaseTemplate(std::uint32_t num_parameters);
/// \brief Returns plugin author.
/// \return Plugin author.
const char* getMaker() const override;
......@@ -48,23 +54,31 @@ protected:
std::uint32_t getVersion() const override;
/// \brief Initializes plugin parameter.
/// \tparam Synth Type of the synth.
/// \param[in] index Index of the parameter.
/// \param[out] parameter Parameter info.
void initParameter(std::uint32_t index, Parameter& parameter) override;
template <typename Synth>
void InitParameter(BaseParameterIndex index, Parameter& parameter) const;
/// \brief Returns the value of the plugin parameter.
/// \tparam Synth Type of the synth.
/// \param[in] synth Synth to read from.
/// \param[in] index Index of the parameter.
/// \return Parameter value.
float getParameterValue(std::uint32_t index) const override;
template <typename Synth>
float GetParameterValue(const Synth& synth, BaseParameterIndex index) const;
/// \brief Sets the value of the plugin parameter.
/// \tparam Synth Type of the synth.
/// \param[in] index Index of the parameter.
/// \param[in] value Value to set.
void setParameterValue(std::uint32_t index, float value) override;
template <typename Synth>
void SetParameterValue(Synth& synth, BaseParameterIndex index,
float value);
/// \brief Activates the plugin.
void activate() override;
/// \brief Processes MIDI input and renders audio.
/// \param[out] outputs Array of output channels.
/// \param[in] frames Number of frames to process.
......@@ -73,18 +87,10 @@ protected:
void run(const float**, float** outputs, std::uint32_t frames,
const MidiEvent* midi_events, std::uint32_t midi_event_count)
override;
/// \brief Called when sample rate has changed.
/// \param[in] new_sample_rate Sample rate to set.
void sampleRateChanged(double new_sample_rate) override;
private:
/// \brief Type of the synth.
using Synth = ftz::Audio::PolyphonicSynth<Generator,
typename PluginSettings::SampleFormat>;
ftz::MIDI::InputChannel channel; ///< Holds the MIDI channel.
/// \brief Holds synth settings.
ftz::Audio::SynthSettings<typename PluginSettings::CalcType> settings;
Synth synth; ///< Holds the synth.
/// \brief Holds the pan.
ftz::Audio::Pan<typename PluginSettings::SampleFormat> pan;
/// \brief Holds the amplifier.
......@@ -97,4 +103,4 @@ private:
}
#include "PluginTemplate.hpp"
#include "PluginBaseTemplate.hpp"
/// \file
/// \brief Internal header file that contains implementation of the
/// PluginTemplate class template.
/// PluginBaseTemplate class template.
/// \author Lyberta
/// \copyright GNU GPLv3 or any later version.
#pragma once
#include "../DPF/Utils.h"
#include "Config.h"
#include "Utils.h"
namespace DISTRHO
{
const std::uint32_t NumParameters = 7;
enum class ParameterIndex
{
MasterVolume,
Pan,
PitchBendRange,
AttackTime,
DecayTime,
SustainLevel,
ReleaseTime
};
template <typename Generator, typename PluginSettings>
PluginTemplate<Generator, PluginSettings>::PluginTemplate(
typename Generator::Settings generator_settings)
: Plugin{NumParameters, 0, 0},
synth{settings, channel, generator_settings, {}},
template <typename PluginSettings>
PluginBaseTemplate<PluginSettings>::PluginBaseTemplate(
std::uint32_t num_parameters)
: Plugin{num_parameters, 0, 0},
amp{PluginSettings::DefaultGain}
{
effects.PushBack(synth);
effects.PushBack(pan);
effects.PushBack(amp);
effects.PushBack(clamp);
effects.push_back(pan);
effects.push_back(amp);
effects.push_back(clamp);
}
template <typename Generator, typename PluginSettings>
const char* PluginTemplate<Generator, PluginSettings>::getMaker() const
template <typename PluginSettings>
const char* PluginBaseTemplate<PluginSettings>::getMaker() const
{
return "Lyberta";
}
template <typename Generator, typename PluginSettings>
const char* PluginTemplate<Generator, PluginSettings>::getHomePage() const
template <typename PluginSettings>
const char* PluginBaseTemplate<PluginSettings>::getHomePage() const
{
return "https://ftz.lyberta.net/projects/chiptune";
}
template <typename Generator, typename PluginSettings>
const char* PluginTemplate<Generator, PluginSettings>::getLicense() const
template <typename PluginSettings>
const char* PluginBaseTemplate<PluginSettings>::getLicense() const
{
return "GNU GPLv3+";
}
template <typename Generator, typename PluginSettings>
std::uint32_t PluginTemplate<Generator, PluginSettings>::getVersion() const
template <typename PluginSettings>
std::uint32_t PluginBaseTemplate<PluginSettings>::getVersion() const
{
return PluginVersion;
}
template <typename Generator, typename PluginSettings>
void PluginTemplate<Generator, PluginSettings>::initParameter(
std::uint32_t index, Parameter& parameter)
template <typename PluginSettings> template <typename Synth>
void PluginBaseTemplate<PluginSettings>::InitParameter(BaseParameterIndex index,
Parameter& parameter) const
{
switch (static_cast<ParameterIndex>(index))
switch (index)
{
case ParameterIndex::MasterVolume:
case BaseParameterIndex::MasterVolume:
{
parameter.hints = kParameterIsAutomable;
parameter.name = "Master volume";
......@@ -77,7 +63,7 @@ void PluginTemplate<Generator, PluginSettings>::initParameter(
parameter.ranges.def = PluginSettings::DefaultGain.GetValue();
return;
}
case ParameterIndex::Pan:
case BaseParameterIndex::Pan:
{
parameter.hints = kParameterIsAutomable;
parameter.name = "Pan";
......@@ -87,7 +73,7 @@ void PluginTemplate<Generator, PluginSettings>::initParameter(
parameter.ranges.def = 0.0f;
return;
}
case ParameterIndex::PitchBendRange:
case BaseParameterIndex::PitchBendRange:
{
parameter.hints = kParameterIsAutomable;
parameter.name = "Pitch bend range";
......@@ -97,7 +83,7 @@ void PluginTemplate<Generator, PluginSettings>::initParameter(
parameter.ranges.def = Synth::DefaultPitchBendRange;
return;
}
case ParameterIndex::AttackTime:
case BaseParameterIndex::AttackTime:
{
parameter.hints = kParameterIsAutomable;
parameter.name = "Attack time";
......@@ -108,7 +94,7 @@ void PluginTemplate<Generator, PluginSettings>::initParameter(
parameter.ranges.def = 0.0f;
return;
}
case ParameterIndex::DecayTime:
case BaseParameterIndex::DecayTime:
{
parameter.hints = kParameterIsAutomable;
parameter.name = "Decay time";
......@@ -119,7 +105,7 @@ void PluginTemplate<Generator, PluginSettings>::initParameter(
parameter.ranges.def = 0.0f;
return;
}
case ParameterIndex::SustainLevel:
case BaseParameterIndex::SustainLevel:
{
parameter.hints = kParameterIsAutomable;
parameter.name = "Sustain level";
......@@ -129,7 +115,7 @@ void PluginTemplate<Generator, PluginSettings>::initParameter(
parameter.ranges.def = 1.0f;
return;
}
case ParameterIndex::ReleaseTime:
case BaseParameterIndex::ReleaseTime:
{
parameter.hints = kParameterIsAutomable;
parameter.name = "Release time";
......@@ -143,37 +129,37 @@ void PluginTemplate<Generator, PluginSettings>::initParameter(
}
}
template <typename Generator, typename PluginSettings>
float PluginTemplate<Generator, PluginSettings>::getParameterValue(
std::uint32_t index) const
template <typename PluginSettings> template <typename Synth>
float PluginBaseTemplate<PluginSettings>::GetParameterValue(const Synth& synth,
BaseParameterIndex index) const
{
switch (static_cast<ParameterIndex>(index))
switch (index)
{
case ParameterIndex::MasterVolume:
case BaseParameterIndex::MasterVolume:
{
return amp.GetGain().GetValue();
}
case ParameterIndex::Pan:
case BaseParameterIndex::Pan:
{
return pan.GetPosition();
}
case ParameterIndex::PitchBendRange:
case BaseParameterIndex::PitchBendRange:
{
return synth.GetPitchBendRange();
}
case ParameterIndex::AttackTime:
case BaseParameterIndex::AttackTime:
{
return synth.GetEnvelopeSettings().GetAttackTime().count();
}
case ParameterIndex::DecayTime:
case BaseParameterIndex::DecayTime:
{
return synth.GetEnvelopeSettings().GetDecayTime().count();
}
case ParameterIndex::SustainLevel:
case BaseParameterIndex::SustainLevel:
{
return synth.GetEnvelopeSettings().GetSustainLevel();
}
case ParameterIndex::ReleaseTime:
case BaseParameterIndex::ReleaseTime:
{
return synth.GetEnvelopeSettings().GetDecayTime().count();
}
......@@ -181,29 +167,29 @@ float PluginTemplate<Generator, PluginSettings>::getParameterValue(
return 0.0f;
}
template <typename Generator, typename PluginSettings>
void PluginTemplate<Generator, PluginSettings>::setParameterValue(
std::uint32_t index, float value)
template <typename PluginSettings> template <typename Synth>
void PluginBaseTemplate<PluginSettings>::SetParameterValue(Synth& synth,
BaseParameterIndex index, float value)
{
switch (static_cast<ParameterIndex>(index))
switch (index)
{
case ParameterIndex::MasterVolume:
case BaseParameterIndex::MasterVolume:
{
amp.SetGain(ftz::Audio::Gain<typename PluginSettings::CalcType>{
value});
return;
}
case ParameterIndex::Pan:
case BaseParameterIndex::Pan:
{
pan.SetPosition(value);
return;
}
case ParameterIndex::PitchBendRange:
case BaseParameterIndex::PitchBendRange:
{
synth.SetPitchBendRange(value);
return;
}
case ParameterIndex::AttackTime:
case BaseParameterIndex::AttackTime:
{
auto envelope = synth.GetEnvelopeSettings();
envelope.SetAttackTime(ftz::Audio::TimeDuration<
......@@ -211,7 +197,7 @@ void PluginTemplate<Generator, PluginSettings>::setParameterValue(
synth.SetEnvelopeSettings(std::move(envelope));
return;
}
case ParameterIndex::DecayTime:
case BaseParameterIndex::DecayTime:
{
auto envelope = synth.GetEnvelopeSettings();
envelope.SetDecayTime(ftz::Audio::TimeDuration<
......@@ -219,14 +205,14 @@ void PluginTemplate<Generator, PluginSettings>::setParameterValue(
synth.SetEnvelopeSettings(std::move(envelope));
return;
}
case ParameterIndex::SustainLevel:
case BaseParameterIndex::SustainLevel:
{
auto envelope = synth.GetEnvelopeSettings();
envelope.SetSustainLevel(value);
synth.SetEnvelopeSettings(std::move(envelope));
return;
}
case ParameterIndex::ReleaseTime:
case BaseParameterIndex::ReleaseTime:
{
auto envelope = synth.GetEnvelopeSettings();
envelope.SetReleaseTime(ftz::Audio::TimeDuration<
......@@ -237,16 +223,16 @@ void PluginTemplate<Generator, PluginSettings>::setParameterValue(
}
}
template <typename Generator, typename PluginSettings>
void PluginTemplate<Generator, PluginSettings>::activate()
template <typename PluginSettings>
void PluginBaseTemplate<PluginSettings>::activate()
{
auto sample_rate = this->getSampleRate();
this->sampleRateChanged(sample_rate);
}
template <typename Generator, typename PluginSettings>
void PluginTemplate<Generator, PluginSettings>::run(const float**,
float** outputs, std::uint32_t frames, const MidiEvent* midi_events,
template <typename PluginSettings>
void PluginBaseTemplate<PluginSettings>::run(const float**, float** outputs,
std::uint32_t frames, const MidiEvent* midi_events,
std::uint32_t midi_event_count)
{
ftz::Audio::MultichannelAudioBufferReference<
......@@ -264,17 +250,4 @@ void PluginTemplate<Generator, PluginSettings>::run(const float**,
effects.Render(first, std::end(buffer));
}
template <typename Generator, typename PluginSettings>
void PluginTemplate<Generator, PluginSettings>::sampleRateChanged(
double new_sample_rate)
{
settings.SetSampleRate(new_sample_rate);
auto generator_settings = synth.GetGeneratorSettings();
generator_settings.SetSampleRate(new_sample_rate);
synth.SetGeneratorSettings(std::move(generator_settings));
auto envelope = synth.GetEnvelopeSettings();
envelope.SetSampleRate(new_sample_rate);
synth.SetEnvelopeSettings(std::move(envelope));
}
}
/// \file
/// \brief Header file that describes the SimplePluginTemplate class template.
/// \author Lyberta
/// \copyright GNU GPLv3 or any later version.
#pragma once
#include <ftz/Audio/PolyphonicSynth.h>
#include "PluginBaseTemplate.h"
namespace DISTRHO
{
/// \brief A plugin template for a synth without special settings.
/// \details This template is used by all CPU synths except pulse wave.
/// \tparam Generator Generator of the synth.
/// \tparam PluginSettings Settings of the plugin. See Config.h.
template <typename Generator, typename PluginSettings>
class SimplePluginTemplate : public PluginBaseTemplate<PluginSettings>
{
public:
/// \brief Constructor.
/// \param[in] settings Settings of the generator.
SimplePluginTemplate(typename Generator::Settings generator_settings);
protected:
/// \brief Initializes plugin parameter.
/// \param[in] index Index of the parameter.
/// \param[out] parameter Parameter info.
void initParameter(std::uint32_t index, Parameter& parameter) override;
/// \brief Returns the value of the plugin parameter.
/// \param[in] index Index of the parameter.
/// \return Parameter value.
float getParameterValue(std::uint32_t index) const override;
/// \brief Sets the value of the plugin parameter.
/// \param[in] index Index of the parameter.
/// \param[in] value Value to set.
void setParameterValue(std::uint32_t index, float value) override;
/// \brief Called when sample rate has changed.
/// \param[in] new_sample_rate Sample rate to set.
void sampleRateChanged(double new_sample_rate) override;
private:
/// \brief Type of the synth.
using Synth = ftz::Audio::PolyphonicSynth<Generator,
typename PluginSettings::SampleFormat>;
Synth synth; ///< Holds the synth.
};
}
#include "SimplePluginTemplate.hpp"
/// \file
/// \brief Internal header file that contains implementation of the
/// SimplePluginTemplate class template.
/// \author Lyberta
/// \copyright GNU GPLv3 or any later version.
#pragma once
namespace DISTRHO
{
const std::uint32_t NumParameters = 7;
template <typename Generator, typename PluginSettings>
SimplePluginTemplate<Generator, PluginSettings>::SimplePluginTemplate(
typename Generator::Settings generator_settings)
: PluginBaseTemplate<PluginSettings>{NumParameters},
synth{this->settings, this->channel, generator_settings, {}}
{
this->effects.insert(std::begin(this->effects), synth);
}
template <typename Generator, typename PluginSettings>
void SimplePluginTemplate<Generator, PluginSettings>::initParameter(
std::uint32_t index, Parameter& parameter)
{
this->template InitParameter<Synth>(static_cast<BaseParameterIndex>(index),
parameter);
}
template <typename Generator, typename PluginSettings>
float SimplePluginTemplate<Generator, PluginSettings>::getParameterValue(
std::uint32_t index) const
{
return this->GetParameterValue(synth,
static_cast<BaseParameterIndex>(index));
}
template <typename Generator, typename PluginSettings>
void SimplePluginTemplate<Generator, PluginSettings>::setParameterValue(
std::uint32_t index, float value)
{
this->SetParameterValue(synth, static_cast<BaseParameterIndex>(index),
value);
}
template <typename Generator, typename PluginSettings>
void SimplePluginTemplate<Generator, PluginSettings>::sampleRateChanged(
double new_sample_rate)
{
this->settings.SetSampleRate(new_sample_rate);
auto generator_settings = synth.GetGeneratorSettings();
generator_settings.SetSampleRate(new_sample_rate);
synth.SetGeneratorSettings(std::move(generator_settings));
auto envelope = synth.GetEnvelopeSettings();
envelope.SetSampleRate(new_sample_rate);
synth.SetEnvelopeSettings(std::move(envelope));
}
}
......@@ -10,8 +10,9 @@ namespace DISTRHO
{
AliasedSawtoothPlugin::AliasedSawtoothPlugin()
: PluginTemplate<ftz::Audio::OscillatorGenerator<ftz::Audio::SawtoothWave<
AliasedSettings::CalcType>>, AliasedSettings>{{{}, {}}}
: SimplePluginTemplate<ftz::Audio::OscillatorGenerator<
ftz::Audio::SawtoothWave<AliasedSettings::CalcType>>, AliasedSettings>{
{{}, {}}}
{
}
......
......@@ -8,7 +8,7 @@
#include <ftz/Audio/SawtoothWave.h>
#include <ftz/Audio/OscillatorGenerator.h>
#include "../DPF/PluginTemplate.h"
#include "../DPF/SimplePluginTemplate.h"
namespace DISTRHO
{
......@@ -16,7 +16,7 @@ namespace DISTRHO
/// \brief An aliased sawtooth synth plugin.
/// \details This is a DPF wrapper of the aliased sawtooth synth.
class AliasedSawtoothPlugin final : public PluginTemplate<
class AliasedSawtoothPlugin final : public SimplePluginTemplate<
ftz::Audio::OscillatorGenerator<ftz::Audio::SawtoothWave<
AliasedSettings::CalcType>>, AliasedSettings>
{
......
......@@ -10,7 +10,7 @@ namespace DISTRHO
{
BandlimitedSawtoothPlugin::BandlimitedSawtoothPlugin()
: PluginTemplate<ftz::Audio::BandlimitedSawtoothWave<
: SimplePluginTemplate<ftz::Audio::BandlimitedSawtoothWave<
BandlimitedSettings::CalcType>, BandlimitedSettings>{{{}}}
{
}
......
......@@ -7,7 +7,7 @@
#include <ftz/Audio/SawtoothWave.h>
#include "../DPF/PluginTemplate.h"
#include "../DPF/SimplePluginTemplate.h"
namespace DISTRHO
{
......@@ -15,7 +15,7 @@ namespace DISTRHO
/// \brief A bandlimited sawtooth synth plugin.
/// \details This is a DPF wrapper of the bandlimited sawtooth synth.
class BandlimitedSawtoothPlugin final : public PluginTemplate<
class BandlimitedSawtoothPlugin final : public SimplePluginTemplate<
ftz::Audio::BandlimitedSawtoothWave<BandlimitedSettings::CalcType>,
BandlimitedSettings>
{
......
......@@ -9,7 +9,7 @@ namespace DISTRHO
{
SinePlugin::SinePlugin()
: PluginTemplate<ftz::Audio::OscillatorGenerator<
: SimplePluginTemplate<ftz::Audio::OscillatorGenerator<
ftz::Audio::SineWave<AliasedSettings::CalcType>>, AliasedSettings>{{{}, {}}}
{
}
......
......@@ -8,7 +8,7 @@
#include <ftz/Audio/SineWave.h>
#include <ftz/Audio/OscillatorGenerator.h>
#include "../DPF/PluginTemplate.h"
#include "../DPF/SimplePluginTemplate.h"
namespace DISTRHO
{
......@@ -16,8 +16,9 @@ namespace DISTRHO
/// \brief A sine synth plugin.
/// \details This is a DPF wrapper of the sine synth.
class SinePlugin final : public PluginTemplate<ftz::Audio::OscillatorGenerator<
ftz::Audio::SineWave<AliasedSettings::CalcType>>, AliasedSettings>
class SinePlugin final : public SimplePluginTemplate<
ftz::Audio::OscillatorGenerator<ftz::Audio::SineWave<
AliasedSettings::CalcType>>, AliasedSettings>
{
public:
/// \brief Constructor.
......
......@@ -10,7 +10,7 @@ namespace DISTRHO
{
AliasedTrianglePlugin::AliasedTrianglePlugin()
: PluginTemplate<ftz::Audio::OscillatorGenerator<
: SimplePluginTemplate<ftz::Audio::OscillatorGenerator<
ftz::Audio::TriangleWave<AliasedSettings::CalcType>>, AliasedSettings>{{{},
{}}}
{
......
......@@ -8,7 +8,7 @@
#include <ftz/Audio/TriangleWave.h>
#include <ftz/Audio/OscillatorGenerator.h>
#include "../DPF/PluginTemplate.h"
#include "../DPF/SimplePluginTemplate.h"
namespace DISTRHO
{
......@@ -16,7 +16,7 @@ namespace DISTRHO
/// \brief An aliased triangle synth plugin.
/// \details This is a DPF wrapper of the aliased triangle synth.
class AliasedTrianglePlugin final : public PluginTemplate<
class AliasedTrianglePlugin final : public SimplePluginTemplate<
ftz::Audio::OscillatorGenerator<ftz::Audio::TriangleWave<
AliasedSettings::CalcType>>, AliasedSettings>
{
......
......@@ -10,7 +10,7 @@ namespace DISTRHO
{
BandlimitedTrianglePlugin::BandlimitedTrianglePlugin()
: PluginTemplate<ftz::Audio::BandlimitedTriangleWave<
: SimplePluginTemplate<ftz::Audio::BandlimitedTriangleWave<
BandlimitedSettings::CalcType>, BandlimitedSettings>{{{}}}
{
}
......
......@@ -7,7 +7,7 @@
#include <ftz/Audio/TriangleWave.h>
#include "../DPF/PluginTemplate.h"
#include "../DPF/SimplePluginTemplate.h"
namespace DISTRHO
{
......@@ -15,7 +15,7 @@ namespace DISTRHO
/// \brief A bandlimited triangle synth plugin.
/// \details This is a DPF wrapper of the bandlimited triangle synth.
class BandlimitedTrianglePlugin final : public PluginTemplate<
class BandlimitedTrianglePlugin final : public SimplePluginTemplate<
ftz::Audio::BandlimitedTriangleWave<BandlimitedSettings::CalcType>,
BandlimitedSettings>
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment