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");
}
ksynth_instance->polyphony--;
ksynth_instance->polyphony_per_channel[channel]--;
- break;
+ break;
}
}
}
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];
}
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);