diff --git a/inc/ADSR.h b/inc/ADSR.h index 1d52a77..a33aa8d 100644 --- a/inc/ADSR.h +++ b/inc/ADSR.h @@ -30,8 +30,6 @@ class ADSR : public Effect { ~ADSR(); void Trigger() override; void Release() override; - // 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/Adder.h b/inc/Adder.h index d3bd044..3052b15 100644 --- a/inc/Adder.h +++ b/inc/Adder.h @@ -8,11 +8,7 @@ struct Adder { static void SumOscillators(const std::vector& oscillators, std::vector& signal) { - size_t sample_count = - STREAM_BUFFER_SIZE; //(size_t)(1.f/FPS * SAMPLE_RATE); - - // std::vector* output = new std::vector(); - // output->reserve(sample_count); + size_t sample_count = STREAM_BUFFER_SIZE; for (size_t i = 0; i < sample_count; i++) { float sample = 0.0f; diff --git a/inc/Effect.h b/inc/Effect.h index 5b731ef..6e9f918 100644 --- a/inc/Effect.h +++ b/inc/Effect.h @@ -8,6 +8,5 @@ class Effect { ~Effect(){}; virtual void Trigger(){}; virtual void Release(){}; - // virtual void RetriggerState(){}; virtual void Process(std::vector& samples){}; }; diff --git a/inc/KeyBoard.h b/inc/KeyBoard.h index c2e7305..85fdc97 100644 --- a/inc/KeyBoard.h +++ b/inc/KeyBoard.h @@ -7,7 +7,6 @@ class KeyBoard { private: - /* data */ static int get_semitone_shift_internal(const char* root_note, char* target_note) { const char* pitch_classes[12] = {"C", "C#", "D", "D#", "E", "F", diff --git a/inc/Logger.h b/inc/Logger.h index cdc57d0..ba01672 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -1,6 +1,7 @@ #pragma once #include "cstdio" -#define write_log(format,args...) do { \ - printf(format, ## args); \ - } while(0) +#define write_log(format, args...) \ + do { \ + printf(format, ##args); \ + } while (0) diff --git a/inc/Renderer.h b/inc/Renderer.h index 169a788..fccd7a6 100644 --- a/inc/Renderer.h +++ b/inc/Renderer.h @@ -8,8 +8,6 @@ class Renderer { private: void draw_main_panel(const Rectangle& panel_bounds); - void draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui, - Rectangle panel_bounds); float draw_oscillators_panels( const std::vector& oscillators, const std::vector& gui_oscillators, diff --git a/inc/Synth.h b/inc/Synth.h index f96b0a3..eb12ea0 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -13,14 +13,13 @@ class Synth { bool is_note_triggered; std::vector m_oscillators; std::vector m_effects; - // OscillatorUI* ui_oscillators; - // Note m_current_note; std::vector m_out_signal; void zero_signal(); void get_note(); void trigger_note_on_effects(); void untrigger_note_on_effects(); void apply_effects(); + void add_oscillator(); public: Synth(/* args */); @@ -28,7 +27,6 @@ class Synth { void Trigger(Note input); void Process(); void Release(); - void AddOscillator(); void AddEffect(Effect* fx); const std::vector& GetOutSignal() { return m_out_signal; } const std::vector& GetOscillators() { return m_oscillators; } diff --git a/src/Application.cpp b/src/Application.cpp index 31a024b..9866190 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -32,11 +32,10 @@ void Application::init_audio() { } void Application::init_synth() { - // todo: move that variables to Synth declaration std::string* nameString = new std::string(std::string(new char[3])); m_current_note = new Note{.length = 1, .name = (*nameString)}; m_current_note->name.assign("G4"); - // todo: move somewhere in initialization + std::vector oscillators = m_synth.GetOscillators(); m_synth_gui_state.oscillators.reserve(oscillators.size()); for (size_t i = 0; i < oscillators.size(); i++) { @@ -86,20 +85,15 @@ bool Application::detect_note_pressed(Note* note) { } bool is_note_up() { - return IsKeyUp(KEY_A) || IsKeyUp(KEY_B) || - IsKeyUp(KEY_C) || IsKeyUp(KEY_D) || - IsKeyUp(KEY_E) || IsKeyUp(KEY_F) || IsKeyUp(KEY_G); + return IsKeyUp(KEY_A) || IsKeyUp(KEY_B) || IsKeyUp(KEY_C) || + IsKeyUp(KEY_D) || IsKeyUp(KEY_E) || IsKeyUp(KEY_F) || IsKeyUp(KEY_G); } -// Update On Input void Application::update_on_note_input() { if (detect_note_pressed(m_current_note)) { - if (!m_synth.GetIsNoteTriggered()) { m_synth.Trigger((*m_current_note)); } - - // m_sound_played_count = 0; write_log("Note played: %s\n", m_current_note->name.c_str()); } else if (is_note_up() && m_synth.GetIsNoteTriggered()) { m_synth.Release(); @@ -111,14 +105,9 @@ void Application::update_on_note_input() { // Play ring-buffered audio void Application::play_buffered_audio() { if (IsAudioStreamProcessed(m_synth_stream)) { - // const float audio_frame_start_time = GetTime(); update_on_note_input(); UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(), STREAM_BUFFER_SIZE); - // const float audio_freme_duration = GetTime() - - // audio_frame_start_time; write_log("Frame time: %.3f%% \n", 100.0f / - // ((1.0f / audio_freme_duration) / - // ((float)SAMPLE_RATE/STREAM_BUFFER_SIZE))); } } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 7fa528f..e3db365 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -19,13 +19,10 @@ Renderer::~Renderer() {} void Renderer::Draw(Synth& synth, SynthGuiState& synth_gui) { BeginDrawing(); - ClearBackground(RAYWHITE); - // todo: implement renderer + draw_ui(synth, synth_gui); draw_signal(synth, synth_gui); - // DrawText("Congrats! You created your first window!", 190, 200, 20, - // LIGHTGRAY); DrawFPS(0,0); EndDrawing(); } @@ -122,21 +119,6 @@ float Renderer::draw_oscillators_panels( // Defer shape drop-down box. ui_osc->shape_dropdown_rect = el_rect; el_rect.y += el_rect.height + el_spacing; - - Rectangle delete_button_rect = el_rect; - delete_button_rect.x = osc_panel_x + 5; - delete_button_rect.y -= el_rect.height + el_spacing; - delete_button_rect.width = 30; - bool is_delete_button_pressed = GuiButton(delete_button_rect, "X"); - if (is_delete_button_pressed) { - // memmove( - // synth->ui_oscillator + ui_osc_i, - // synth->ui_oscillator + ui_osc_i + 1, - // (synth->ui_oscillator_count - ui_osc_i) * - // sizeof(UiOscillator) - // ); - // synth->ui_oscillator_count -= 1; - } } return panel_y_offset; @@ -146,47 +128,6 @@ void Renderer::draw_main_panel(const Rectangle& panel_bounds) { GuiPanel(panel_bounds, ""); } -void Renderer::draw_add_oscillator_button(Synth& synth, - SynthGuiState& synth_gui, - Rectangle panel_bounds) { - //clang-format off - bool click_add_oscillator = - GuiButton((Rectangle){panel_bounds.x + 10, panel_bounds.y + 10, - panel_bounds.width - 20, 25.f}, - "Add Oscillator"); - //clang-format on - - if (click_add_oscillator) { - synth.AddOscillator(); - Oscillator* osc = synth.GetOscillators().back(); - - OscillatorGuiState* ui = - new OscillatorGuiState{.freq = osc->GetFreq(), - .waveshape = osc->GetType(), - .volume = osc->GetVolume()}; - synth_gui.oscillators.push_back(ui); - } -} - -void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) { - Rectangle panel_bounds = {.x = 0, - .y = 0, - .width = OSCILLATOR_PANEL_WIDTH, - .height = WINDOW_HEIGHT}; - draw_main_panel(panel_bounds); - draw_add_oscillator_button(synth, synth_gui, panel_bounds); - // Draw Oscillators - std::vector oscillators = synth.GetOscillators(); - std::vector gui_oscillators = synth_gui.oscillators; - - float panel_y_offset = - draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds); - draw_oscillators_shape_inputs(oscillators, gui_oscillators); - - 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) { @@ -243,3 +184,20 @@ void Renderer::draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr, adsr->SetParameters(gui_adsr.attack, gui_adsr.decay, gui_adsr.sustain, gui_adsr.release); } + +void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) { + Rectangle panel_bounds = {.x = 0, + .y = 0, + .width = OSCILLATOR_PANEL_WIDTH, + .height = WINDOW_HEIGHT}; + draw_main_panel(panel_bounds); + + std::vector oscillators = synth.GetOscillators(); + std::vector gui_oscillators = synth_gui.oscillators; + + float panel_y_offset = + draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds); + draw_adsr_panel(synth.GetADSR(), synth_gui.adsr, panel_bounds, + panel_y_offset); + draw_oscillators_shape_inputs(oscillators, gui_oscillators); +} diff --git a/src/Synth.cpp b/src/Synth.cpp index abe0f61..fc99405 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -6,7 +6,8 @@ #include "Settings.h" Synth::Synth(/* args */) { - AddOscillator(); + add_oscillator(); + add_oscillator(); AddEffect(new ADSR()); for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) { float sample = 0.0f; @@ -51,11 +52,15 @@ void Synth::untrigger_note_on_effects() { } } +void Synth::add_oscillator() { + m_oscillators.push_back( + new Oscillator(OscillatorType::Sine, 440.f, VOLUME)); +} + void Synth::Trigger(Note input) { int semitone_shift = KeyBoard::GetSemitoneShift(input.name); float hz = KeyBoard::GetHzBySemitone(semitone_shift); - // will change after oscillator starts to be more autonomous for (Oscillator* osc : m_oscillators) { osc->SetFreq(hz); } @@ -68,16 +73,10 @@ void Synth::Process() { apply_effects(); } -// todo: rename to something like untrigger note void Synth::Release() { zero_signal(); is_note_triggered = false; untrigger_note_on_effects(); } -void Synth::AddOscillator() { - m_oscillators.push_back( - new Oscillator(OscillatorType::Sine, 440.f, VOLUME)); -} - void Synth::AddEffect(Effect* fx) { m_effects.push_back(fx); }