From d7dd36ccd1471aad16ea965124e2f57c2f44e402 Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Sun, 10 Sep 2023 01:13:25 +0400 Subject: [PATCH] refactor: move around some code --- inc/FilterFactory.h | 4 ++++ src/Filter.cpp | 5 +---- src/Renderer.cpp | 7 +------ src/Synth.cpp | 11 ++++++----- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/inc/FilterFactory.h b/inc/FilterFactory.h index a4e1fef..830838b 100644 --- a/inc/FilterFactory.h +++ b/inc/FilterFactory.h @@ -22,4 +22,8 @@ struct FilterFactory { } return newFilter; } + + static Filter* GetDefaultFilter() { + return new LowPassFilter(); + } }; \ No newline at end of file diff --git a/src/Filter.cpp b/src/Filter.cpp index 77962e7..82ddbe4 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -15,6 +15,7 @@ void Filter::Trigger() {} void Filter::Release() {} float Filter::Process(float in) { + // may move to a compile-time dictionary calculation, if needed calculate_coefficients(); float out = in * m_a0 + m_z1; m_z1 = in * m_a1 + m_z2 - m_b1 * out; @@ -23,10 +24,6 @@ float Filter::Process(float in) { } void Filter::Process(std::vector& samples) { - // todo: that will not work for ADSR-controlled filter. So, let's calculate - // all the possible frequency values into array/dictionary then just lookup - // for each sample - for (std::size_t i = 0; i < samples.size(); i++) { samples[i] = Process(samples[i]); } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 7ab1942..f75abe5 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -241,13 +241,8 @@ float Renderer::draw_filter_panel(Synth& synth, FilterGuiState& gui_filter, 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); + synth.SetFilter(gui_filter.type); } - // if (gui_filter.is_dropdown_open) - // break; // apply values to real one // todo: thrid (order) parameter diff --git a/src/Synth.cpp b/src/Synth.cpp index 4253366..e4dfe41 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -3,7 +3,6 @@ #include "Logger.h" #include "OscillatorType.h" #include "Settings.h" -#include "LowPassFilter.h" #include "FilterFactory.h" Synth::Synth(/* args */) { @@ -11,7 +10,7 @@ Synth::Synth(/* args */) { add_oscillator(); add_oscillator(); AddEffect(new ADSR()); - AddEffect(new LowPassFilter()); + AddEffect(FilterFactory::GetDefaultFilter()); for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) { float sample = 0.0f; m_out_signal.push_back(sample); @@ -98,7 +97,9 @@ 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; + if (!oldFilter->IsSameFilterType(type)) { + Filter* newFilter = FilterFactory::CreateFilter(oldFilter, type); + delete oldFilter; + m_effects[1] = newFilter; + } }