diff --git a/inc/ADSR.h b/inc/ADSR.h index 71c3acf..624c922 100644 --- a/inc/ADSR.h +++ b/inc/ADSR.h @@ -1,9 +1,9 @@ #pragma once -#include "Effect.h" +#include "IEffect.h" #include "Ramp.h" #include -class ADSR : public Effect { +class ADSR : public IEffect { enum ADSRState { sOff, sAttack, sDecay, sSustain, sRelease }; private: @@ -14,11 +14,11 @@ class ADSR : public Effect { ADSRState m_state; Ramp* m_ramp; - void process_sample(float* sample); - bool is_attack_elapsed(); - bool is_decay_elapsed(); - bool is_release_elapsed(); - void recheck_state(); + void ProcessSample(float* sample); + bool IsAttackElapsed(); + bool IsDecayElapsed(); + bool IsReleaseElapsed(); + void RecheckState(); public: ADSR(/* args */); diff --git a/inc/Effect.h b/inc/Effect.h deleted file mode 100644 index 6e9f918..0000000 --- a/inc/Effect.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include -class Effect { - private: - /* data */ - public: - Effect(/* args */){}; - ~Effect(){}; - virtual void Trigger(){}; - virtual void Release(){}; - virtual void Process(std::vector& samples){}; -}; diff --git a/inc/Filter.h b/inc/Filter.h index 7b5f567..c4d8ba4 100644 --- a/inc/Filter.h +++ b/inc/Filter.h @@ -1,5 +1,5 @@ #pragma once -#include "Effect.h" +#include "IEffect.h" enum FilterType { LowPass, @@ -7,7 +7,7 @@ enum FilterType { HighPass }; -class Filter : public Effect { +class Filter : public IEffect { protected: float m_freq; // cutoff frequency float m_q; // filter quantity (resonance) @@ -23,10 +23,10 @@ class Filter : public Effect { public: Filter(/* args */); virtual ~Filter(); - void Trigger() override; - void Release() override; + void Trigger() override final; + void Release() override final; float Process(float in); - void Process(std::vector& samples) override; + void Process(std::vector& samples) override final; void SetParameters(float freq, float res, float q); float GetFreq() { return m_freq; } float GetRes() { return m_q; } diff --git a/inc/IEffect.h b/inc/IEffect.h new file mode 100644 index 0000000..9a8bc68 --- /dev/null +++ b/inc/IEffect.h @@ -0,0 +1,10 @@ +#pragma once +#include +class IEffect { + private: + /* data */ + public: + virtual void Trigger() = 0; + virtual void Release() = 0; + virtual void Process(std::vector& samples) = 0; +}; diff --git a/inc/Synth.h b/inc/Synth.h index c9bc8d8..d6acede 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -3,7 +3,7 @@ #include "ADSR.h" #include "Filter.h" #include "Adder.h" -#include "Effect.h" +#include "IEffect.h" #include "Note.h" #include "Oscillator.h" #include "Settings.h" @@ -13,23 +13,23 @@ class Synth { private: bool is_note_triggered; std::vector m_oscillators; - std::vector m_effects; + std::vector m_effects; std::vector m_out_signal; Oscillator* m_lfo; - void zero_signal(); - void get_note(); - void trigger_note_on_effects(); - void untrigger_note_on_effects(); - void apply_effects(); - void add_oscillator(); - void apply_filter_lfo(); + void ZeroSignal(); + void GetNote(); + void TriggerNoteOnEffects(); + void UntriggerNoteOnEffects(); + void ApplyEffects(); + void AddOscillator(); + void ApplyFilterLfo(); public: Synth(/* args */); ~Synth(); void Trigger(Note input); void Process(); void Release(); - void AddEffect(Effect* fx); + void AddEffect(IEffect* fx); const std::vector& GetOutSignal() { return m_out_signal; } const std::vector& GetOscillators() { return m_oscillators; } const bool& GetIsNoteTriggered() { return is_note_triggered; } diff --git a/src/ADSR.cpp b/src/ADSR.cpp index 9f043a9..7a4c435 100644 --- a/src/ADSR.cpp +++ b/src/ADSR.cpp @@ -12,33 +12,33 @@ ADSR::ADSR(/* args */) { ADSR::~ADSR() { delete m_ramp; } -bool ADSR::is_attack_elapsed() { +bool ADSR::IsAttackElapsed() { return m_state == sAttack && m_ramp->IsCompleted(); } -bool ADSR::is_decay_elapsed() { +bool ADSR::IsDecayElapsed() { return m_state == sDecay && m_ramp->IsCompleted(); } -bool ADSR::is_release_elapsed() { +bool ADSR::IsReleaseElapsed() { return m_state == sRelease && m_ramp->IsCompleted(); } -void ADSR::recheck_state() { +void ADSR::RecheckState() { switch (m_state) { case sAttack: - if (is_attack_elapsed()) { + if (IsAttackElapsed()) { m_state = sDecay; m_ramp->RampTo(m_sustain_level, m_decay_time); } break; case sDecay: - if (is_decay_elapsed()) { + if (IsDecayElapsed()) { m_state = sSustain; } break; case sRelease: - if (is_release_elapsed()) { + if (IsReleaseElapsed()) { m_state = sOff; } break; @@ -47,7 +47,7 @@ void ADSR::recheck_state() { } } -void ADSR::process_sample(float* sample) { +void ADSR::ProcessSample(float* sample) { if (m_state == sOff) { (*sample) = 0; } else if (m_state == sAttack) { @@ -80,8 +80,8 @@ void ADSR::Release() { void ADSR::Process(std::vector& samples) { for (std::size_t i = 0; i < samples.size(); i++) { - recheck_state(); - process_sample(&samples[i]); + RecheckState(); + ProcessSample(&samples[i]); } } diff --git a/src/Synth.cpp b/src/Synth.cpp index 59b1efa..3cc0a36 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -7,15 +7,15 @@ Synth::Synth(/* args */) { m_lfo = new Oscillator(OscillatorType::Sine, 5.f, VOLUME); - add_oscillator(); - add_oscillator(); + AddOscillator(); + AddOscillator(); AddEffect(new ADSR()); AddEffect(FilterFactory::GetDefaultFilter()); for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) { float sample = 0.0f; m_out_signal.push_back(sample); } - zero_signal(); + ZeroSignal(); } Synth::~Synth() { @@ -24,37 +24,37 @@ Synth::~Synth() { m_out_signal.clear(); } -void Synth::zero_signal() { +void Synth::ZeroSignal() { float sample = 0.0f; for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) { m_out_signal[i] = sample; } } -void Synth::get_note() { - zero_signal(); +void Synth::GetNote() { + ZeroSignal(); Adder::SumOscillators(m_oscillators, m_out_signal); } -void Synth::apply_effects() { - for (Effect* effect : m_effects) { +void Synth::ApplyEffects() { + for (IEffect* effect : m_effects) { effect->Process(m_out_signal); } } -void Synth::trigger_note_on_effects() { - for (Effect* effect : m_effects) { +void Synth::TriggerNoteOnEffects() { + for (IEffect* effect : m_effects) { effect->Trigger(); } } -void Synth::untrigger_note_on_effects() { - for (Effect* effect : m_effects) { +void Synth::UntriggerNoteOnEffects() { + for (IEffect* effect : m_effects) { effect->Release(); } } -void Synth::add_oscillator() { +void Synth::AddOscillator() { m_oscillators.push_back( new Oscillator(OscillatorType::Sine, 440.f, VOLUME)); } @@ -67,11 +67,11 @@ void Synth::Trigger(Note input) { osc->SetFreq(hz); } is_note_triggered = true; - trigger_note_on_effects(); + TriggerNoteOnEffects(); } // todo: fix this -void Synth::apply_filter_lfo() { +void Synth::ApplyFilterLfo() { float dt = m_lfo->Process(); Filter* filter = (Filter*)m_effects[1]; float freq = filter->GetFreq(); @@ -82,18 +82,18 @@ void Synth::apply_filter_lfo() { void Synth::Process() { //todo: on each sample. //in order to do that, we need to move to per-sample processing - apply_filter_lfo(); - get_note(); - apply_effects(); + ApplyFilterLfo(); + GetNote(); + ApplyEffects(); } void Synth::Release() { - zero_signal(); + ZeroSignal(); is_note_triggered = false; - untrigger_note_on_effects(); + UntriggerNoteOnEffects(); } -void Synth::AddEffect(Effect* fx) { m_effects.push_back(fx); } +void Synth::AddEffect(IEffect* fx) { m_effects.push_back(fx); } void Synth::SetFilter(FilterType type) { Filter* old_filter = this->GetFilter();