From: kazu Date: Mon, 8 Apr 2024 02:14:36 +0000 (+0000) Subject: Update the types for better suitability X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=eaa67ccd2e28ed9542ff45cfbac77711a9dd5f8c;p=ksynth.git Update the types for better suitability git-svn-id: file:///raid/svn-main/kazu-ksynth/trunk@16 7b47e76f-e598-2f43-bc14-414d160cc389 --- diff --git a/src/ksynth.c b/src/ksynth.c index 96ea9c5..5cde7b0 100644 --- a/src/ksynth.c +++ b/src/ksynth.c @@ -12,7 +12,7 @@ #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); @@ -66,7 +66,7 @@ struct KSynth* ksynth_new(const char* sample_file_path, int sample_rate, int num 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; @@ -93,35 +93,35 @@ void ksynth_note_on(struct KSynth* ksynth_instance, int channel, int note, int v 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) { @@ -156,7 +156,7 @@ int ksynth_get_max_polyphony(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; @@ -187,7 +187,7 @@ bool ksynth_set_max_polyphony(struct KSynth* ksynth_instance, int max_polyphony) }*/ } -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; @@ -227,7 +227,7 @@ float* ksynth_generate_buffer(struct KSynth* ksynth_instance, int buffer_size) { 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++; @@ -271,7 +271,7 @@ float ksynth_get_rendering_time(struct KSynth* ksynth_instance) { } } -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; diff --git a/src/ksynth.h b/src/ksynth.h index 38a5db3..5ddbadc 100644 --- a/src/ksynth.h +++ b/src/ksynth.h @@ -10,11 +10,11 @@ extern "C" { 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; }; @@ -35,8 +35,7 @@ struct KSynth { * @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. @@ -46,7 +45,7 @@ struct KSynth* ksynth_new(const char* sample_file_path, int sample_rate, int num * @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. @@ -55,7 +54,7 @@ void ksynth_note_on(struct KSynth* ksynth_instance, int channel, int note, int v * @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. @@ -88,7 +87,7 @@ int ksynth_get_max_polyphony(struct KSynth* ksynth_instance); * @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. @@ -103,7 +102,7 @@ bool ksynth_set_max_polyphony(struct KSynth* ksynth_instance, int max_polyphony) * @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. @@ -124,7 +123,7 @@ float ksynth_get_rendering_time(struct KSynth* ksynth_instance); * @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. diff --git a/src/sample.h b/src/sample.h index 4839e25..0726769 100644 --- a/src/sample.h +++ b/src/sample.h @@ -10,7 +10,7 @@ extern "C" { struct Sample { float* sample; - int length; + unsigned int length; }; void sample_free(struct Sample* sample); diff --git a/src/voice.h b/src/voice.h index 73c8538..88ede9e 100644 --- a/src/voice.h +++ b/src/voice.h @@ -9,11 +9,11 @@ extern "C" { #include struct Voice { - int sample_rate; - int channel; - int noteNumber; - float velocity; - int sample_position; + unsigned int sample_rate; + unsigned char channel; + unsigned char noteNumber; + unsigned char velocity; + unsigned int sample_position; }; void voice_free(struct Voice* voice);