From c39f5d70167a8283c129846390c9656045366634 Mon Sep 17 00:00:00 2001 From: kazu Date: Tue, 16 Apr 2024 03:50:31 +0000 Subject: [PATCH] Fix velocity calculation and swapped if check git-svn-id: file:///raid/svn-main/kazu-ksynth/trunk@21 7b47e76f-e598-2f43-bc14-414d160cc389 --- src/ksynth.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ksynth.c b/src/ksynth.c index 6ef285d..4faa444 100644 --- a/src/ksynth.c +++ b/src/ksynth.c @@ -217,19 +217,21 @@ float* ksynth_generate_buffer(struct KSynth* ksynth_instance, unsigned short buf int sample_position = voice->sample_position; int sample_length = ksynth_instance->samples[voice->noteNumber]->length; + float logVel = (float)voice->velocity / 127.0f; + float velocity = fmin(pow(logVel, 2.5) + 0.03, 1.0); char killed = 0; for(int j = 0; j < buffer_size; ++j) { float sample = ksynth_instance->samples[voice->noteNumber]->sample[sample_position]; for(int c = 0; c < num_channels; ++c) { - buffer[j * num_channels + c] += sample * (float)voice->velocity / 127.0f; + buffer[j * num_channels + c] += sample * velocity; } sample_position++; // If sample_position exceeds sample_length, the voice has finished playing - if(sample_position >= sample_length && ksynth_instance->voices[i] != NULL) { + if(ksynth_instance->voices[i] != NULL && sample_position >= sample_length) { voice_free(voice); // Shift remaining voices to fill the gap -- 2.43.0