diff options
| author | spl3g <spleefer6@yandex.ru> | 2025-10-10 14:20:10 +0300 |
|---|---|---|
| committer | spl3g <spleefer6@yandex.ru> | 2025-10-10 14:20:10 +0300 |
| commit | 599118dc7be359842f06bfdc903d38cf66e8a432 (patch) | |
| tree | df720fe158b17911d8e4e09ab3c3fe98862127ce | |
| parent | 145229b9ec9b0f50cdef537f1bae5f09c3198ead (diff) | |
Fix cracking when key is pressed many times
| -rw-r--r-- | src/sounds.c | 26 | ||||
| -rw-r--r-- | src/sounds.h | 3 |
2 files changed, 16 insertions, 13 deletions
diff --git a/src/sounds.c b/src/sounds.c index b011f0c..5445df6 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -61,22 +61,23 @@ float envelope_next(envelope *env) { next_state = true; } - if (env->current_value == 0) { - env->current_value = 1.0 / (float)env->params.attack_time; + if (env->current_inc == 0) { + env->current_inc = (1.0 - env->release_value) / (float)env->params.attack_time; } - value = env->current_value * (float)env->counter; + value = env->release_value + env->current_inc * (float)env->counter; break; } case ENV_DECAY: { if (env->counter >= env->params.decay_time) { next_state = true; } - if (env->current_value == 0) { - env->current_value = (1.0 - env->params.sustain_level) / (float)env->params.decay_time; + + if (env->current_inc == 0) { + env->current_inc = (1.0 - env->params.sustain_level) / (float)env->params.decay_time; } - value = 1.0 - env->current_value * (float)env->counter; + value = 1.0 - env->current_inc * (float)env->counter; break; } case ENV_SUSTAIN: { @@ -86,21 +87,22 @@ float envelope_next(envelope *env) { case ENV_RELEASE: { if (env->counter >= env->params.release_time) { env->state = ENV_OFF; - env->counter = env->current_value = 0; + env->counter = env->current_inc = 0; return 0; } - if (env->current_value == 0) { - env->current_value = env->params.sustain_level / (float)env->params.release_time; + if (env->current_inc == 0) { + env->current_inc = env->params.sustain_level / (float)env->params.release_time; } - value = env->params.sustain_level - env->current_value * (float)env->counter; + value = env->params.sustain_level - env->current_inc * (float)env->counter; + env->release_value = value; break; } } if (next_state) { - env->counter = env->current_value = 0; + env->counter = env->current_inc = 0; env->state++; } return value; @@ -277,7 +279,7 @@ void sound_loop_start(snd_pcm_t *pcm, message_queue *queue, generate_voices(voices, params, scratch_buffer, PERIOD_SIZE); - /* post_process(params, scratch_buffer, PERIOD_SIZE); */ + post_process(params, scratch_buffer, PERIOD_SIZE); prepare_output(scratch_buffer, output_buffer, PERIOD_SIZE); int period_size = PERIOD_SIZE; diff --git a/src/sounds.h b/src/sounds.h index 49386ce..61346c6 100644 --- a/src/sounds.h +++ b/src/sounds.h @@ -93,7 +93,8 @@ typedef struct { typedef struct { envelope_state state; int counter; - float current_value; + float current_inc; + float release_value; envelope_params params; } envelope; |
