From c6c2956ac08f38750dc4c7b294e866e2cdbbff96 Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Sun, 13 Aug 2023 01:13:54 +0400 Subject: [PATCH] fix: retriggering phase problem --- inc/Synth.h | 9 ++++++--- src/Application.cpp | 7 +++++-- src/Synth.cpp | 26 +++++++++++++++----------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/inc/Synth.h b/inc/Synth.h index dc84225..679817d 100644 --- a/inc/Synth.h +++ b/inc/Synth.h @@ -9,22 +9,25 @@ class Synth { private: + bool is_note_triggered; std::vector m_oscillators; Adder m_adder; std::vector m_effects; // OscillatorUI* ui_oscillators; // Note m_current_note; std::vector m_out_signal; - void get_note(int semitone, float beats); + void get_note(); void apply_effects(); public: Synth(/* args */); ~Synth(); - void ProduceNoteSound(Note input); - void StopNoteSound(); + void TriggerNote(Note input); + void ProduceSound(); + void StopSound(); void AddOscillator(); void AddEffect(Effect* fx); const std::vector& GetOutSignal() { return m_out_signal; } const std::vector& GetOscillators() { return m_oscillators; } + const bool& GetIsNoteTriggered() { return is_note_triggered; } }; \ No newline at end of file diff --git a/src/Application.cpp b/src/Application.cpp index 633b869..db24330 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -93,13 +93,16 @@ bool Application::detect_note_pressed(Note* note) { void Application::update_on_note_input() { if (detect_note_pressed(m_current_note)) { // if (detect_note_pressed(m_current_note)) { - m_synth.ProduceNoteSound((*m_current_note)); + if (!m_synth.GetIsNoteTriggered()){ + m_synth.TriggerNote((*m_current_note)); + } + m_synth.ProduceSound(); //m_sound_played_count = 0; write_log("Note played: %s\n", m_current_note->name.c_str()); //} } else { - m_synth.StopNoteSound(); + m_synth.StopSound(); } } diff --git a/src/Synth.cpp b/src/Synth.cpp index 67fa36c..16f3780 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -19,19 +19,12 @@ Synth::~Synth() { m_out_signal.clear(); } -void Synth::get_note(int semitone, float beats) { +void Synth::get_note() { for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) { float sample = 0.0f; m_out_signal[i] = sample; } - float hz = KeyBoard::GetHzBySemitone(semitone); - - // will change after oscillator starts to be more autonomous - for (Oscillator* osc : m_oscillators) { - osc->SetFreq(hz); - } - // todo: add other pipeline steps (e.g ADSR, Filters, FX); Adder::SumOscillators(m_oscillators, m_out_signal); } @@ -44,18 +37,29 @@ void Synth::apply_effects() { } } -void Synth::ProduceNoteSound(Note input) { +void Synth::TriggerNote(Note input) { float length = 1.f / input.length; int semitone_shift = KeyBoard::GetSemitoneShift(input.name); - get_note(semitone_shift, length); + float hz = KeyBoard::GetHzBySemitone(semitone_shift); + + // will change after oscillator starts to be more autonomous + for (Oscillator* osc : m_oscillators) { + osc->SetFreq(hz); + } + is_note_triggered = true; +} + +void Synth::ProduceSound() { + get_note(); apply_effects(); } -void Synth::StopNoteSound() { +void Synth::StopSound() { for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) { float sample = 0.0f; m_out_signal[i] = sample; } + is_note_triggered = false; } void Synth::AddOscillator() {