}
void ksynth_note_on(struct KSynth* ksynth_instance, int channel, int note, int velocity) {
- if(ksynth_instance == NULL || channel < 0 || channel > 16 || note < 0 || note > 127 || velocity < 0 || velocity > 127) {
+ if(ksynth_instance == NULL || channel < 0 || channel > 15 || note < 0 || note > 127 || velocity < 0 || velocity > 127) {
fprintf(stderr, "[KSynth] Error: Invalid parameters for note on.\n");
return;
}
- // If polyphony limit reached, remove oldest voice
+ if(channel == 10) return;
+
if(ksynth_instance->polyphony >= ksynth_instance->max_polyphony) {
voice_free(ksynth_instance->voices[0]);
- // Shift remaining voices to fill the gap
+
for(int i = 0; i < ksynth_instance->polyphony - 1; ++i) {
ksynth_instance->voices[i] = ksynth_instance->voices[i + 1];
}
+
ksynth_instance->polyphony--;
}
- //if(channel == 10) return;
-
- // Create a new voice
struct Voice* newVoice = malloc(sizeof(struct Voice));
if(newVoice == NULL) {
fprintf(stderr, "[KSynth] Error: Failed to allocate memory for new voice.\n");
return;
}
- // Calculate velocity
- float logVel = velocity / 127.0f;
- float calculatedVelocity = fminf(powf(logVel, 2.5f) + 0.03f, 1.0f);
-
newVoice->sample_rate = ksynth_instance->sample_rate;
newVoice->channel = channel;
newVoice->noteNumber = note;
- newVoice->velocity = calculatedVelocity;
+ newVoice->velocity = (float)velocity / 127.0f;
newVoice->sample_position = 0;
ksynth_instance->voices[ksynth_instance->polyphony++] = newVoice;
}
void ksynth_note_off(struct KSynth* ksynth_instance, int channel, int note) {
- if(ksynth_instance == NULL || channel < 0 || channel > 16 || note < 0 || note > 127) {
- fprintf(stderr, "[KSynth] Error: Invalid parameters for note off.\n");
- return;
- }
+ if (ksynth_instance == NULL || channel < 0 || channel > 15 || note < 0 || note > 127) {
+ fprintf(stderr, "[KSynth] Error: Invalid parameters for note off.\n");
+ return;
+ }
- if(channel == 10) return;
+ if (channel == 10) return;
- for(int i = 0; i < ksynth_instance->polyphony; ++i) {
- if(ksynth_instance->voices[i]->channel == channel && note == ksynth_instance->voices[i]->noteNumber) {
- // Free memory allocated for the voice
- voice_free(ksynth_instance->voices[i]);
+ for (int i = 0; i < ksynth_instance->polyphony; ++i) {
+ if (ksynth_instance->voices[i]->channel == channel && note == ksynth_instance->voices[i]->noteNumber) {
+ voice_free(ksynth_instance->voices[i]);
- // Shift the remaining voices to fill the gap
- for(int j = i; j < ksynth_instance->polyphony - 1; ++j) {
- ksynth_instance->voices[j] = ksynth_instance->voices[j + 1];
- }
+ for (int j = i; j < ksynth_instance->polyphony - 1; ++j) {
+ ksynth_instance->voices[j] = ksynth_instance->voices[j + 1];
+ }
- ksynth_instance->polyphony--;
- ksynth_instance->polyphony_per_channel[channel]--;
+ ksynth_instance->polyphony--;
+ ksynth_instance->polyphony_per_channel[channel]--;
- // Adjust the index after shifting the voices
- i--;
- }
- }
+ i--;
+ }
+ }
}
void ksynth_note_off_all(struct KSynth* ksynth_instance) {