From ec01773ab1c2f9aa2fd9de38188712b428c03e3c Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Tue, 8 Aug 2023 23:02:25 +0400 Subject: [PATCH] feat: add oscillator button --- inc/Renderer.h | 7 ++++--- inc/Synth.h | 1 + src/Renderer.cpp | 33 +++++++++++++++++++-------------- src/Synth.cpp | 10 ++++++++-- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/inc/Renderer.h b/inc/Renderer.h index 750f6c8..cbdb1f3 100644 --- a/inc/Renderer.h +++ b/inc/Renderer.h @@ -8,17 +8,18 @@ class Renderer { private: void DrawMainPanel(const Rectangle& panel_bounds); + void DrawAddOscillatorButton(Synth & synth, SynthGuiState & synthGui, Rectangle panel_bounds); void DrawOscillatorsPanels(const std::vector& oscillators, const std::vector& guiOscillators, const Rectangle& panel_bounds); void DrawOscillatorsShapeInputs(const std::vector& oscillators, const std::vector& guiOscillators); - void DrawUi(Synth & synth, const SynthGuiState & synthGui); - void DrawSignal(Synth & synth, const SynthGuiState & synthGui); + void DrawUi(Synth & synth, SynthGuiState & synthGui); + void DrawSignal(Synth & synth, SynthGuiState & synthGui); public: Renderer(/* args */); ~Renderer(); - void Draw(Synth& synth, const SynthGuiState & synthGui); + void Draw(Synth& synth, SynthGuiState & synthGui); }; diff --git a/inc/Synth.h b/inc/Synth.h index c50d46e..a0612d1 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -20,6 +20,7 @@ public: Synth(/* args */); ~Synth(); void ProduceNoteSound(Note input); + void AddOscillator(); const std::vector & GetOutSignal() { return m_out_signal; } const std::vector& GetOscillators() { return m_oscillators; } }; \ No newline at end of file diff --git a/src/Renderer.cpp b/src/Renderer.cpp index ef77924..86c4e42 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -14,7 +14,7 @@ Renderer::~Renderer() { } -void Renderer::Draw(Synth& synth, const SynthGuiState& synthGui) +void Renderer::Draw(Synth& synth, SynthGuiState& synthGui) { BeginDrawing(); @@ -28,7 +28,7 @@ void Renderer::Draw(Synth& synth, const SynthGuiState& synthGui) EndDrawing(); } -void Renderer::DrawSignal(Synth & synth, const SynthGuiState & synthGui) +void Renderer::DrawSignal(Synth & synth, SynthGuiState & synthGui) { GuiGrid((Rectangle){0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}, "", WINDOW_HEIGHT / 8, 2); auto signal = synth.GetOutSignal(); @@ -158,8 +158,10 @@ void Renderer::DrawMainPanel(const Rectangle& panel_bounds) bool is_shape_dropdown_open = false; int shape_index = 0; GuiPanel(panel_bounds, ""); +} - /**/ +void Renderer::DrawAddOscillatorButton(Synth & synth, SynthGuiState & synthGui, Rectangle panel_bounds) +{ bool click_add_oscillator = GuiButton((Rectangle){ panel_bounds.x + 10, panel_bounds.y + 10, @@ -168,24 +170,27 @@ void Renderer::DrawMainPanel(const Rectangle& panel_bounds) }, "Add Oscillator"); if (click_add_oscillator) { - // synth->ui_oscillator_count += 1; - // // Set defaults: - // UiOscillator *ui_osc = synth->ui_oscillator + (synth->ui_oscillator_count - 1); - // ui_osc->shape = WaveShape_SINE; - // ui_osc->freq = BASE_NOTE_FREQ; - // ui_osc->amplitude_ratio = 0.1f; - // ui_osc->shape_parameter_0 = 0.5f; - } -} + synth.AddOscillator(); + Oscillator* osc = synth.GetOscillators().back(); -void Renderer::DrawUi(Synth & synth, const SynthGuiState & synthGui) + OscillatorGuiState* ui = new OscillatorGuiState { + .freq = osc->GetFreq(), + .waveshape = osc->GetType(), + .volume = osc->GetVolume() + }; + synthGui.oscillators.push_back(ui); + } +} + +void Renderer::DrawUi(Synth & synth, SynthGuiState & synthGui) { Rectangle panel_bounds = {.x = 0, .y = 0, .width = OSCILLATOR_PANEL_WIDTH, .height = WINDOW_HEIGHT }; DrawMainPanel(panel_bounds); - + DrawAddOscillatorButton(synth, synthGui, panel_bounds); // Draw Oscillators std::vector oscillators = synth.GetOscillators(); std::vector guiOscillators = synthGui.oscillators; + DrawOscillatorsPanels(oscillators, guiOscillators, panel_bounds); DrawOscillatorsShapeInputs(oscillators, guiOscillators); } \ No newline at end of file diff --git a/src/Synth.cpp b/src/Synth.cpp index 8b74eaa..f388cd9 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -5,7 +5,7 @@ Synth::Synth(/* args */) { - m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME)); + AddOscillator(); } Synth::~Synth() @@ -26,8 +26,14 @@ std::vector & Synth::get_note(int semitone, float beats) return m_adder.SumOscillators(m_oscillators, duration); //todo: add other pipeline steps (e.g ADSR, Filters, FX); } -void Synth::ProduceNoteSound(Note input) { +void Synth::ProduceNoteSound(Note input) +{ float length = 1.f / input.length; int semitone_shift = KeyBoard::GetSemitoneShift(input.name); m_out_signal = get_note(semitone_shift, length); } + +void Synth::AddOscillator() +{ + m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME)); +}