From de37b814bf2eb2e53c4c37bffb382288b04b5887 Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Sun, 10 Sep 2023 01:02:22 +0400 Subject: [PATCH] feat: filter type input --- inc/BandPassFilter.h | 1 + inc/Filter.h | 9 ++++++++- inc/FilterFactory.h | 25 +++++++++++++++++++++++++ inc/HighPassFilter.h | 1 + inc/LowPassFilter.h | 1 + inc/Renderer.h | 2 +- inc/Synth.h | 1 + inc/SynthGuiState.h | 6 ++++-- src/Renderer.cpp | 24 ++++++++++++++++++++++-- src/Synth.cpp | 9 +++++++++ 10 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 inc/FilterFactory.h diff --git a/inc/BandPassFilter.h b/inc/BandPassFilter.h index 7e9ddb7..43e287d 100644 --- a/inc/BandPassFilter.h +++ b/inc/BandPassFilter.h @@ -10,4 +10,5 @@ class BandPassFilter : public Filter { BandPassFilter(float freq, float res, float q); BandPassFilter(/* args */); ~BandPassFilter(); + bool IsSameFilterType(FilterType type) override { return type == BandPass; }; }; diff --git a/inc/Filter.h b/inc/Filter.h index 7127945..75a82bd 100644 --- a/inc/Filter.h +++ b/inc/Filter.h @@ -1,6 +1,12 @@ #pragma once #include "Effect.h" +enum FilterType { + LowPass, + BandPass, + HighPass +}; + class Filter : public Effect { protected: float m_freq; // cutoff frequency @@ -16,7 +22,7 @@ class Filter : public Effect { public: Filter(/* args */); - ~Filter(); + virtual ~Filter(); void Trigger() override; void Release() override; float Process(float in); @@ -25,4 +31,5 @@ class Filter : public Effect { float GetFreq() { return m_freq; } float GetRes() { return m_q; } float GetPeakGain() { return m_norm; } + virtual bool IsSameFilterType(FilterType type){ return false; }; }; diff --git a/inc/FilterFactory.h b/inc/FilterFactory.h new file mode 100644 index 0000000..a4e1fef --- /dev/null +++ b/inc/FilterFactory.h @@ -0,0 +1,25 @@ +#pragma once +#include "Filter.h" +#include "LowPassFilter.h" +#include "BandPassFilter.h" +#include "HighPassFilter.h" + +struct FilterFactory { + static Filter* CreateFilter(Filter* oldFilter, FilterType newType) { + Filter* newFilter; + switch (newType) { + case LowPass: + newFilter = new LowPassFilter(oldFilter); + break; + case BandPass: + newFilter = new BandPassFilter(oldFilter); + break; + case HighPass: + newFilter = new HighPassFilter(oldFilter); + break; + default: + break; + } + return newFilter; + } +}; \ No newline at end of file diff --git a/inc/HighPassFilter.h b/inc/HighPassFilter.h index efd6d12..3461322 100644 --- a/inc/HighPassFilter.h +++ b/inc/HighPassFilter.h @@ -10,4 +10,5 @@ class HighPassFilter : public Filter { HighPassFilter(Filter* filter); HighPassFilter(float freq, float res, float q); ~HighPassFilter(); + bool IsSameFilterType(FilterType type) override { return type == HighPass; }; }; diff --git a/inc/LowPassFilter.h b/inc/LowPassFilter.h index cbfacab..53e1084 100644 --- a/inc/LowPassFilter.h +++ b/inc/LowPassFilter.h @@ -11,4 +11,5 @@ class LowPassFilter : public Filter { LowPassFilter(Filter* filter); LowPassFilter(float freq, float res, float q); ~LowPassFilter(); + bool IsSameFilterType(FilterType type) override { return type == LowPass; }; }; diff --git a/inc/Renderer.h b/inc/Renderer.h index e2e65dd..ec4bc36 100644 --- a/inc/Renderer.h +++ b/inc/Renderer.h @@ -21,7 +21,7 @@ class Renderer { void draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr, const Rectangle& panel_bounds, float panel_y_offset); void draw_second_panel(Rectangle& bounds); - float draw_filter_panel(Filter* filter, FilterGuiState& gui_filter, + float draw_filter_panel(Synth& synth, FilterGuiState& gui_filter, const Rectangle& panel_bounds); public: diff --git a/inc/Synth.h b/inc/Synth.h index b1609ce..c9bc8d8 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -35,4 +35,5 @@ class Synth { const bool& GetIsNoteTriggered() { return is_note_triggered; } ADSR* GetADSR() { return (ADSR*)m_effects[0]; } Filter* GetFilter() { return (Filter*)m_effects[1]; } + void SetFilter(FilterType type); }; \ No newline at end of file diff --git a/inc/SynthGuiState.h b/inc/SynthGuiState.h index 778e1b7..6d404ed 100644 --- a/inc/SynthGuiState.h +++ b/inc/SynthGuiState.h @@ -2,6 +2,7 @@ #include "OscillatorType.h" #include "raygui.h" #include +#include "Filter.h" struct OscillatorGuiState { float volume; @@ -20,8 +21,9 @@ struct ADSRGuiState { struct FilterGuiState { float freq; - float res; - //todo: type + float res; //todo: res + FilterType type; + bool is_dropdown_open; }; struct SynthGuiState { diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 393d784..7ab1942 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -190,8 +190,10 @@ void Renderer::draw_second_panel(Rectangle& bounds) { GuiPanel(bounds, ""); } -float Renderer::draw_filter_panel(Filter* filter, FilterGuiState& gui_filter, +float Renderer::draw_filter_panel(Synth& synth, FilterGuiState& gui_filter, const Rectangle& panel_bounds) { +#define FILTER_TYPE_OPTIONS "LP;BP;HP" + Filter* filter = synth.GetFilter(); float panel_y_offset = 0; // Draw Filter Panel @@ -228,6 +230,24 @@ float Renderer::draw_filter_panel(Filter* filter, FilterGuiState& gui_filter, el_rect.y += el_rect.height + el_spacing; // todo: filter type + // Shape select + int shape_index = (int)(gui_filter.type); + bool is_dropdown_click = + GuiDropdownBox(el_rect, FILTER_TYPE_OPTIONS, + &shape_index, gui_filter.is_dropdown_open); + + if (is_dropdown_click) { + write_log("Dropdown clicked!\n"); + gui_filter.is_dropdown_open = !gui_filter.is_dropdown_open; + gui_filter.type = (FilterType)(shape_index); + // APPLY STATE TO REAL SYNTH + if (!filter->IsSameFilterType(gui_filter.type)) { + synth.SetFilter(gui_filter.type); + } + //filter.SetType(ui_osc->waveshape); + } + // if (gui_filter.is_dropdown_open) + // break; // apply values to real one // todo: thrid (order) parameter @@ -254,5 +274,5 @@ void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) { draw_oscillators_shape_inputs(oscillators, gui_oscillators); draw_second_panel(panel_bounds); - draw_filter_panel(synth.GetFilter(), synth_gui.filter, panel_bounds); + draw_filter_panel(synth, synth_gui.filter, panel_bounds); } diff --git a/src/Synth.cpp b/src/Synth.cpp index 0e49675..4253366 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -4,6 +4,7 @@ #include "OscillatorType.h" #include "Settings.h" #include "LowPassFilter.h" +#include "FilterFactory.h" Synth::Synth(/* args */) { m_lfo = new Oscillator(OscillatorType::Sine, 5.f, VOLUME); @@ -70,6 +71,7 @@ void Synth::Trigger(Note input) { trigger_note_on_effects(); } +// todo: fix this void Synth::apply_filter_lfo() { float dt = m_lfo->Process(); Filter* filter = (Filter*)m_effects[1]; @@ -93,3 +95,10 @@ void Synth::Release() { } void Synth::AddEffect(Effect* fx) { m_effects.push_back(fx); } + +void Synth::SetFilter(FilterType type) { + Filter* oldFilter = this->GetFilter(); + Filter* newFilter = FilterFactory::CreateFilter(oldFilter, type); + delete oldFilter; + m_effects[1] = newFilter; +}