]> Nishi Git Mirror - ksynth.git/commitdiff
i think crash fixed (idk)
authorkazu <kazu@7b47e76f-e598-2f43-bc14-414d160cc389>
Mon, 15 Apr 2024 12:11:45 +0000 (12:11 +0000)
committerkazu <kazu@7b47e76f-e598-2f43-bc14-414d160cc389>
Mon, 15 Apr 2024 12:11:45 +0000 (12:11 +0000)
git-svn-id: file:///raid/svn-main/kazu-ksynth/trunk@20 7b47e76f-e598-2f43-bc14-414d160cc389

src/ksynth.c

index 40a63389799b3d2194765af7532ae856216f3a13..6ef285d3615659ffe42f8f3aa52e63b20e84e6db 100644 (file)
@@ -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);