#define MAX_POLYPHONY 100000
-struct KSynth* ksynth_new(const char* sample_file_path, int sample_rate, int num_channel, int max_polyphony) {
+struct KSynth* ksynth_new(const char* sample_file_path, unsigned int sample_rate, unsigned char num_channel, unsigned long max_polyphony) {
struct KSynth* ksynth_instance = malloc(sizeof(struct KSynth));
if(ksynth_instance != NULL) {
ksynth_instance->samples = malloc(sizeof(struct Sample*) * 128);
return ksynth_instance;
}
-void ksynth_note_on(struct KSynth* ksynth_instance, int channel, int note, int velocity) {
+void ksynth_note_on(struct KSynth* ksynth_instance, unsigned char channel, unsigned char note, unsigned char velocity) {
if(ksynth_instance == NULL || channel < 0 || channel > 16 || note < 0 || note > 127 || velocity < 0 || velocity > 127) {
fprintf(stderr, "[KSynth] Error: Invalid parameters for note on.\n");
return;
newVoice->sample_rate = ksynth_instance->sample_rate;
newVoice->channel = channel;
newVoice->noteNumber = note;
- newVoice->velocity = (float)velocity / 127.0f;
+ newVoice->velocity = velocity;
newVoice->sample_position = 0;
ksynth_instance->voices[ksynth_instance->polyphony++] = newVoice;
ksynth_instance->polyphony_per_channel[channel]++;
}
-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;
- }
+void ksynth_note_off(struct KSynth* ksynth_instance, unsigned char channel, unsigned char 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 (channel == 9) return;
+ if(channel == 9) return;
- 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]);
+ 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]);
- 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]--;
- i--;
- }
- }
+ i--;
+ }
+ }
}
void ksynth_note_off_all(struct KSynth* ksynth_instance) {
}
};
-bool ksynth_set_max_polyphony(struct KSynth* ksynth_instance, int max_polyphony) {
+bool ksynth_set_max_polyphony(struct KSynth* ksynth_instance, unsigned long max_polyphony) {
fprintf(stderr, "[KSynth] Error: Setting max polyphony is currently not supported.\n");
return false;
}*/
}
-float* ksynth_generate_buffer(struct KSynth* ksynth_instance, int buffer_size) {
+float* ksynth_generate_buffer(struct KSynth* ksynth_instance, unsigned short buffer_size) {
if(ksynth_instance == NULL || ksynth_instance->samples == NULL) {
fprintf(stderr, "[KSynth] Error: Invalid KSynth instance or no samples loaded.\n");
return NULL;
float sample = ksynth_instance->samples[voice->noteNumber]->sample[sample_position];
for(int c = 0; c < num_channels; ++c) {
- buffer[j * num_channels + c] += sample * voice->velocity;
+ buffer[j * num_channels + c] += sample * (float)voice->velocity / 127.0f;
}
sample_position++;
}
}
-int ksynth_get_polyphony_for_channel(struct KSynth* ksynth_instance, int channel) {
+int ksynth_get_polyphony_for_channel(struct KSynth* ksynth_instance, unsigned char channel) {
if(ksynth_instance == NULL) {
fprintf(stderr, "[KSynth] Error: Invalid KSynth instance.\n");
return -1;
struct KSynth {
struct Sample** samples;
- int sample_rate;
- int num_channel;
- int polyphony;
- int polyphony_per_channel[16];
- int max_polyphony;
+ unsigned int sample_rate;
+ unsigned char num_channel;
+ unsigned long polyphony;
+ unsigned char polyphony_per_channel[16];
+ unsigned long max_polyphony;
float rendering_time;
struct Voice** voices;
};
* @param max_polyphony Maximum polyphony (1-100,000)
* @return A pointer to the newly created KSynth instance on success, NULL on failure.
*/
-struct KSynth* ksynth_new(const char* sample_file_path, int sample_rate, int num_channel, int max_polyphony);
-
+struct KSynth* ksynth_new(const char* sample_file_path, unsigned int sample_rate, unsigned char num_channel, unsigned long max_polyphony);
/**
* @brief Turns a note on for the specified MIDI channel and note.
* @param note Note number
* @param velocity Velocity
*/
-void ksynth_note_on(struct KSynth* ksynth_instance, int channel, int note, int velocity);
+void ksynth_note_on(struct KSynth* ksynth_instance, unsigned char channel, unsigned char note, unsigned char velocity);
/**
* @brief Turns a note off for the specified MIDI channel and note.
* @param channel MIDI channel
* @param note Note number
*/
-void ksynth_note_off(struct KSynth* ksynth_instance, int channel, int note);
+void ksynth_note_off(struct KSynth* ksynth_instance, unsigned char channel, unsigned char note);
/**
* @brief Turns off all notes.
* @param max_polyphony Maximum polyphony (Note: Values exceeding 100,000 will be clamped)
* @return Returns true on success, false on failure.
*/
-bool ksynth_set_max_polyphony(struct KSynth* ksynth_instance, int max_polyphony);
+bool ksynth_set_max_polyphony(struct KSynth* ksynth_instance, unsigned long max_polyphony);
/**
* @brief Generates a buffer with the specified size.
* @param buffer_size Size of the buffer (Note: Values are typically between 32 and 16384)
* @return A pointer to the generated buffer on success, NULL on failure.
*/
-float* ksynth_generate_buffer(struct KSynth* ksynth_instance, int buffer_size);
+float* ksynth_generate_buffer(struct KSynth* ksynth_instance, unsigned short buffer_size);
/**
* @brief Gets the rendering time.
* @param channel MIDI channel
* @return Number of active polyphony for the specified MIDI channel
*/
-int ksynth_get_polyphony_for_channel(struct KSynth* ksynth_instance, int channel);
+int ksynth_get_polyphony_for_channel(struct KSynth* ksynth_instance, unsigned char channel);
/**
* @brief Frees the buffer.