diff --git a/inc/Renderer.h b/inc/Renderer.h index fccd7a6..c3906ad 100644 --- a/inc/Renderer.h +++ b/inc/Renderer.h @@ -1,4 +1,5 @@ #pragma once +#include "Filter.h" #include "ADSR.h" #include "Synth.h" #include "SynthGuiState.h" @@ -19,7 +20,8 @@ class Renderer { void draw_signal(Synth& synth, SynthGuiState& synth_gui); 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, const Rectangle& panel_bounds); public: Renderer(/* args */); ~Renderer(); diff --git a/inc/Synth.h b/inc/Synth.h index eb12ea0..4c1ffbd 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -1,6 +1,7 @@ #pragma once #include "ADSR.h" +#include "Filter.h" #include "Adder.h" #include "Effect.h" #include "Note.h" @@ -32,4 +33,5 @@ class Synth { const std::vector& GetOscillators() { return m_oscillators; } const bool& GetIsNoteTriggered() { return is_note_triggered; } ADSR* GetADSR() { return (ADSR*)m_effects[0]; } + Filter* GetFilter() { return (Filter*)m_effects[1]; } }; \ No newline at end of file diff --git a/inc/SynthGuiState.h b/inc/SynthGuiState.h index f07a21d..778e1b7 100644 --- a/inc/SynthGuiState.h +++ b/inc/SynthGuiState.h @@ -18,7 +18,14 @@ struct ADSRGuiState { float release; }; +struct FilterGuiState { + float freq; + float res; + //todo: type +}; + struct SynthGuiState { std::vector oscillators; ADSRGuiState adsr; + FilterGuiState filter; }; \ No newline at end of file diff --git a/src/Filter.cpp b/src/Filter.cpp index 7fcde3d..a841796 100644 --- a/src/Filter.cpp +++ b/src/Filter.cpp @@ -1,4 +1,5 @@ #include "Filter.h" +#include "Settings.h" Filter::Filter(/* args */) { } @@ -37,7 +38,7 @@ void Filter::Process(std::vector& samples) { } void Filter::SetParameters(float freq, float res, float q) { - m_freq = freq; + m_freq = freq / SAMPLE_RATE; m_q = res; m_order = q; } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index e3db365..1360e62 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -185,6 +185,57 @@ void Renderer::draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr, gui_adsr.release); } +void Renderer::draw_second_panel(Rectangle& bounds) { + bounds.x += bounds.width; + GuiPanel(bounds, ""); +} + +float Renderer::draw_filter_panel(Filter* filter, FilterGuiState& gui_filter, const Rectangle& panel_bounds) { + float panel_y_offset = 0; + + // Draw Filter Panel + const int osc_panel_width = panel_bounds.width - 20; + const int osc_panel_height = 120; + const int osc_panel_x = panel_bounds.x + 10; + const int osc_panel_y = panel_bounds.y + 50; + panel_y_offset += osc_panel_height + 5; + GuiPanel((Rectangle){(float)osc_panel_x, (float)osc_panel_y, + (float)osc_panel_width, (float)osc_panel_height}, + "Filter"); + + const float slider_padding = 50.f; + const float el_spacing = 5.f; + Rectangle el_rect = {.x = (float)osc_panel_x + slider_padding + 30, + .y = (float)osc_panel_y + 10, + .width = (float)osc_panel_width - (slider_padding * 2), + .height = 25.f}; + + // Frequency slider + float freq = gui_filter.freq; + char freq_slider_label[32]; + snprintf(freq_slider_label, 10, "%.1f hz", freq); + freq = GuiSlider(el_rect, freq_slider_label, "", freq, 0.0f, 16000.f); + gui_filter.freq = freq; + el_rect.y += el_rect.height + el_spacing; + + // Resonance slider + float res = gui_filter.res; + char res_slider_label[32]; + snprintf(res_slider_label, 7, "%.1f u", res); + res = GuiSlider(el_rect, res_slider_label, "", res, 0.0f, 1.0f); + gui_filter.res = res; + el_rect.y += el_rect.height + el_spacing; + + // todo: filter type + + // apply values to real one + // todo: thrid (order) parameter + // todo: why resonance changing does not work? + filter->SetParameters(gui_filter.freq, 0.707, 0); + + return panel_y_offset; +} + void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) { Rectangle panel_bounds = {.x = 0, .y = 0, @@ -200,4 +251,7 @@ void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) { draw_adsr_panel(synth.GetADSR(), synth_gui.adsr, panel_bounds, panel_y_offset); draw_oscillators_shape_inputs(oscillators, gui_oscillators); + + draw_second_panel(panel_bounds); + draw_filter_panel(synth.GetFilter(), synth_gui.filter, panel_bounds); } diff --git a/src/Synth.cpp b/src/Synth.cpp index b924043..1ea0484 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -1,5 +1,4 @@ #include "Synth.h" -#include "ADSR.h" #include "KeyBoard.h" #include "Logger.h" #include "OscillatorType.h"