From 20b1dbf6b0b99913c8fc6a5283ec043240378129 Mon Sep 17 00:00:00 2001 From: kazu Date: Mon, 15 Apr 2024 12:11:45 +0000 Subject: [PATCH] i think crash fixed (idk) git-svn-id: file:///raid/svn-main/kazu-ksynth/trunk@20 7b47e76f-e598-2f43-bc14-414d160cc389 --- src/ksynth.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/ksynth.c b/src/ksynth.c index 40a6338..6ef285d 100644 --- a/src/ksynth.c +++ b/src/ksynth.c @@ -57,9 +57,6 @@ struct KSynth* ksynth_new(const char* sample_file_path, unsigned int sample_rate ksynth_instance->rendering_time = 0; ksynth_instance->voices = malloc(sizeof(struct Voice*) * max_polyphony); - for(int i = 0; i < ksynth_instance->max_polyphony; i++) { - ksynth_instance->voices[i] = malloc(sizeof(struct Voice)); - } } else { fprintf(stderr, "[KSynth] Error: Failed to allocate memory for KSynth instance.\n"); } @@ -119,7 +116,7 @@ void ksynth_note_off(struct KSynth* ksynth_instance, unsigned char channel, unsi ksynth_instance->polyphony--; ksynth_instance->polyphony_per_channel[channel]--; - break; + break; } } } @@ -221,8 +218,7 @@ 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; - if(sample_position >= sample_length) break; - + char killed = 0; for(int j = 0; j < buffer_size; ++j) { float sample = ksynth_instance->samples[voice->noteNumber]->sample[sample_position]; @@ -231,24 +227,24 @@ float* ksynth_generate_buffer(struct KSynth* ksynth_instance, unsigned short buf } sample_position++; - } - voice->sample_position = sample_position; + // If sample_position exceeds sample_length, the voice has finished playing + if(sample_position >= sample_length && ksynth_instance->voices[i] != NULL) { + voice_free(voice); - // If sample_position exceeds sample_length, the voice has finished playing - if(sample_position >= sample_length && ksynth_instance->voices[i] != NULL) { - ksynth_instance->polyphony_per_channel[voice->channel]--; - voice_free(voice); + // Shift remaining voices to fill the gap + for(int k = i; k < ksynth_instance->polyphony - 1; ++k) { + ksynth_instance->voices[k] = ksynth_instance->voices[k + 1]; + } - // Shift remaining voices to fill the gap - for(int k = i; k < ksynth_instance->polyphony - 1; ++k) { - ksynth_instance->voices[k] = ksynth_instance->voices[k + 1]; + ksynth_instance->polyphony--; + ksynth_instance->polyphony_per_channel[voice->channel]--; + killed = 1; + break; } - - ksynth_instance->polyphony--; - - break; } + + if(!killed) voice->sample_position = sample_position; } clock_gettime(CLOCK_MONOTONIC, &rendering_time_end); -- 2.43.0