fix: ADSR logic

This commit is contained in:
2023-09-04 23:25:54 +04:00
parent d883bbbf12
commit ef40eaf7ef
4 changed files with 14 additions and 14 deletions

View File

@@ -4,7 +4,7 @@
ADSR::ADSR(/* args */) { ADSR::ADSR(/* args */) {
m_parameters.attack_time = 1.f; m_parameters.attack_time = 1.f;
m_parameters.decay_time = 0.3f; m_parameters.decay_time = 0.4f;
m_parameters.sustain_level = 0.6f; m_parameters.sustain_level = 0.6f;
m_parameters.release_time = 0.8f; m_parameters.release_time = 0.8f;
m_ramp = new Ramp(0, SAMPLE_RATE); m_ramp = new Ramp(0, SAMPLE_RATE);
@@ -33,9 +33,6 @@ bool ADSR::is_release_elapsed() {
void ADSR::recheck_state() { void ADSR::recheck_state() {
switch (m_state) switch (m_state)
{ {
case Off:
m_state = Attack;
break;
case Attack: case Attack:
if (is_attack_elapsed()) { if (is_attack_elapsed()) {
m_state = Decay; m_state = Decay;
@@ -76,6 +73,7 @@ void ADSR::process_sample(float* sample) {
} }
void ADSR::OnSetNote() { void ADSR::OnSetNote() {
write_log("Set ADSR\n");
if (m_state == Off) { if (m_state == Off) {
m_state = Attack; m_state = Attack;
} }
@@ -93,7 +91,6 @@ void ADSR::OnUnsetNote() {
} }
void ADSR::Process(std::vector<float>& samples) { void ADSR::Process(std::vector<float>& samples) {
write_log("ADSR State: %d\n", m_state);
for (std::size_t i = 0; i < samples.size(); i++) { for (std::size_t i = 0; i < samples.size(); i++) {
recheck_state(); recheck_state();
process_sample(&samples[i]); process_sample(&samples[i]);

View File

@@ -102,7 +102,7 @@ void Application::update_on_note_input() {
//m_sound_played_count = 0; //m_sound_played_count = 0;
write_log("Note played: %s\n", m_current_note->name.c_str()); write_log("Note played: %s\n", m_current_note->name.c_str());
} }
else if (is_note_up()) { else if (is_note_up() && m_synth.GetIsNoteTriggered()) {
m_synth.StopSound(); m_synth.StopSound();
} }
// will produce 0 signal if ADSR is in off state // will produce 0 signal if ADSR is in off state
@@ -112,18 +112,17 @@ void Application::update_on_note_input() {
// Play ring-buffered audio // Play ring-buffered audio
void Application::play_buffered_audio() { void Application::play_buffered_audio() {
if (IsAudioStreamProcessed(m_synth_stream)) { if (IsAudioStreamProcessed(m_synth_stream)) {
const float audio_frame_start_time = GetTime(); //const float audio_frame_start_time = GetTime();
update_on_note_input(); update_on_note_input();
UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(), STREAM_BUFFER_SIZE); UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(), STREAM_BUFFER_SIZE);
const float audio_freme_duration = GetTime() - audio_frame_start_time; //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))); //write_log("Frame time: %.3f%% \n", 100.0f / ((1.0f / audio_freme_duration) / ((float)SAMPLE_RATE/STREAM_BUFFER_SIZE)));
} }
} }
void Application::Run() { void Application::Run() {
// Main game loop // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) {
{
play_buffered_audio(); play_buffered_audio();
m_renderer.Draw(m_synth, m_synth_gui_state); m_renderer.Draw(m_synth, m_synth_gui_state);
} }

View File

@@ -9,10 +9,10 @@ Ramp::Ramp(float starting_level, float sample_rate) {
Ramp::~Ramp() { Ramp::~Ramp() {
} }
void Ramp::RampTo(float value, float time) { void Ramp::RampTo(float val, float time) {
m_increment = (value - m_level) / (m_sample_rate * time); m_increment = (val - m_level) / (m_sample_rate * time);
m_counter = (int)(m_sample_rate * time); m_counter = (int)(m_sample_rate * time);
write_log("Ramping from: %.1f to: %.1f by: %.1f for: %d\n", m_level, value, m_increment, m_counter); write_log("Ramping from: %.1f to: %.1f by: %lf for: %d\n", m_level, val, m_increment, m_counter);
} }
float Ramp::Process() { float Ramp::Process() {

View File

@@ -3,11 +3,15 @@
#include "KeyBoard.h" #include "KeyBoard.h"
#include "OscillatorType.h" #include "OscillatorType.h"
#include "Settings.h" #include "Settings.h"
#include "Logger.h"
Synth::Synth(/* args */) { Synth::Synth(/* args */) {
AddOscillator(); AddOscillator();
AddEffect(new ADSR()); AddEffect(new ADSR());
m_out_signal.reserve(STREAM_BUFFER_SIZE); for (size_t i = 0; i < STREAM_BUFFER_SIZE; i++) {
float sample = 0.0f;
m_out_signal.push_back(sample);
}
zero_signal(); zero_signal();
} }