From 54c4e540ac687b5440c84214e036de5a0563a2bc Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Tue, 5 Sep 2023 23:45:16 +0400 Subject: [PATCH] feat: adsr gui --- inc/ADSR.h | 1 + inc/Renderer.h | 5 ++-- inc/Synth.h | 2 ++ inc/SynthGuiState.h | 8 ++++++ src/ADSR.cpp | 7 +++++ src/Renderer.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 88 insertions(+), 5 deletions(-) diff --git a/inc/ADSR.h b/inc/ADSR.h index 3741bb3..53b8c20 100644 --- a/inc/ADSR.h +++ b/inc/ADSR.h @@ -33,4 +33,5 @@ class ADSR : public Effect { // void RetriggerState() override; void Process(std::vector& samples) override; void Reset(); + void SetParameters(float attack, float decay, float sustain, float release); }; diff --git a/inc/Renderer.h b/inc/Renderer.h index 0b59ec7..ad6f92f 100644 --- a/inc/Renderer.h +++ b/inc/Renderer.h @@ -3,13 +3,14 @@ #include "SynthGuiState.h" #include "raylib.h" #include +#include "ADSR.h" class Renderer { private: void draw_main_panel(const Rectangle& panel_bounds); void draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui, Rectangle panel_bounds); - void draw_oscillators_panels( + float draw_oscillators_panels( const std::vector& oscillators, const std::vector& gui_oscillators, const Rectangle& panel_bounds); @@ -18,7 +19,7 @@ class Renderer { const std::vector& guiOscillators); void draw_ui(Synth& synth, SynthGuiState& synth_gui); 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); public: Renderer(/* args */); ~Renderer(); diff --git a/inc/Synth.h b/inc/Synth.h index e897460..1d2e600 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -6,6 +6,7 @@ #include "Oscillator.h" #include "Settings.h" #include +#include "ADSR.h" class Synth { private: @@ -32,4 +33,5 @@ class Synth { const std::vector& GetOutSignal() { return m_out_signal; } const std::vector& GetOscillators() { return m_oscillators; } const bool& GetIsNoteTriggered() { return is_note_triggered; } + ADSR* GetADSR() { return (ADSR*)m_effects[0]; } }; \ No newline at end of file diff --git a/inc/SynthGuiState.h b/inc/SynthGuiState.h index 5eb02c3..f07a21d 100644 --- a/inc/SynthGuiState.h +++ b/inc/SynthGuiState.h @@ -11,6 +11,14 @@ struct OscillatorGuiState { Rectangle shape_dropdown_rect; }; +struct ADSRGuiState { + float attack; + float decay; + float sustain; + float release; +}; + struct SynthGuiState { std::vector oscillators; + ADSRGuiState adsr; }; \ No newline at end of file diff --git a/src/ADSR.cpp b/src/ADSR.cpp index 6a898a7..879b106 100644 --- a/src/ADSR.cpp +++ b/src/ADSR.cpp @@ -86,4 +86,11 @@ void ADSR::Process(std::vector& samples) { recheck_state(); process_sample(&samples[i]); } +} + +void ADSR::SetParameters(float attack, float decay, float sustain, float release) { + m_parameters.attack_time = attack; + m_parameters.decay_time = decay; + m_parameters.sustain_level = sustain; + m_parameters.release_time = release; } \ No newline at end of file diff --git a/src/Renderer.cpp b/src/Renderer.cpp index c1cb22e..faf46a1 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -76,7 +76,7 @@ void Renderer::draw_oscillators_shape_inputs( } } -void Renderer::draw_oscillators_panels( +float Renderer::draw_oscillators_panels( const std::vector& oscillators, const std::vector& gui_oscillators, const Rectangle& panel_bounds) { @@ -138,6 +138,8 @@ void Renderer::draw_oscillators_panels( // synth->ui_oscillator_count -= 1; } } + + return panel_y_offset; } void Renderer::draw_main_panel(const Rectangle& panel_bounds) { @@ -177,6 +179,68 @@ void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) { std::vector oscillators = synth.GetOscillators(); std::vector gui_oscillators = synth_gui.oscillators; - draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds); + float panel_y_offset = draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds); draw_oscillators_shape_inputs(oscillators, gui_oscillators); -} \ No newline at end of file + + draw_adsr_panel(synth.GetADSR(), synth_gui.adsr, panel_bounds, panel_y_offset); +} + +void Renderer::draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr, const Rectangle& panel_bounds, float panel_y_offset) { + // Draw ADSR 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; + 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}, + "ADSR"); + + 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}; + + // Attack slider + float attack = gui_adsr.attack; + char attack_slider_label[32]; + snprintf(attack_slider_label, 7, "%.1f s", attack); + attack = + GuiSlider(el_rect, attack_slider_label, "", attack, 0.0f, 2.0f); + gui_adsr.attack = attack; + el_rect.y += el_rect.height + el_spacing; + + //Decay slider + float decay = gui_adsr.decay; + char decay_slider_label[32]; + snprintf(decay_slider_label, 7, "%.1f s", decay); + decay = + GuiSlider(el_rect, decay_slider_label, "", decay, 0.0f, 1.0f); + gui_adsr.decay = decay; + el_rect.y += el_rect.height + el_spacing; + + //Sustain slider + float sustain = gui_adsr.sustain; + char sustain_slider_label[32]; + snprintf(sustain_slider_label, 7, "%.1f u", sustain); + sustain = + GuiSlider(el_rect, sustain_slider_label, "", sustain, 0.0f, 1.0f); + gui_adsr.sustain = sustain; + el_rect.y += el_rect.height + el_spacing; + + + //Release slider + float release = gui_adsr.release; + char release_slider_label[32]; + snprintf(release_slider_label, 7, "%.1f s", release); + release = + GuiSlider(el_rect, release_slider_label, "", release, 0.0f, 5.0f); + gui_adsr.release = release; + el_rect.y += el_rect.height + el_spacing; + + //apply values to real one + adsr->SetParameters(gui_adsr.attack, gui_adsr.decay, gui_adsr.sustain, gui_adsr.release); +}