From: kazu Date: Sat, 6 Apr 2024 05:46:54 +0000 (+0000) Subject: Fixed crash problem finally! X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=fc2fe2620f85e8bd095ee87dd2266cdeb99f33cf;p=ksynth.git Fixed crash problem finally! git-svn-id: file:///raid/svn-main/kazu-ksynth/trunk@6 7b47e76f-e598-2f43-bc14-414d160cc389 --- diff --git a/src/ksynth.c b/src/ksynth.c index 8f696ba..7019213 100644 --- a/src/ksynth.c +++ b/src/ksynth.c @@ -67,38 +67,33 @@ struct KSynth* ksynth_new(int sample_rate, int num_channel, int max_polyphony) { } 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; @@ -106,30 +101,27 @@ void ksynth_note_on(struct KSynth* ksynth_instance, int channel, int note, int v } 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) {