fix some UB

This commit is contained in:
thib8956 committed 2026-11-02 20:48:57 +01:00
1 parent 003fc0cb1b
commit 5e5f08c4f9
4 files changed
+14 -6

No files matched your search

+6 -3
View File
@@ -5,7 +5,8 @@
void play_note(Note *note, float *buffer, int buffer_size) {
for (int i = 0; i < buffer_size; i++) {
// update phase counter
note->phase = fmodf(note->phase + note->freq / SAMPLE_RATE, 1.0f);
note->phase = note->phase + note->freq / SAMPLE_RATE;
if (note->phase >= 1.0f) { note->phase -= 1.0f; }
// oscillator
float sample = 0.0f;
@@ -21,9 +22,11 @@ void play_note(Note *note, float *buffer, int buffer_size) {
//sample = note->lp;
// basic attack / release
float attack_sec = fmaxf(note->attack_ms / 1000.0f, 1e-6f); // apply minimum value to avoid division by zero
float release_sec = fmaxf(note->release_ms / 1000.0f, 1e-6f);
// convert attack / release time to a number of steps to reach target gain
float attack_step = 0.2f / ((note->attack_ms / 1000.0f) * SAMPLE_RATE);
float release_step = 0.2f / ((note->release_ms / 1000.0f) * SAMPLE_RATE);
float attack_step = 0.2f / (attack_sec * SAMPLE_RATE);
float release_step = 0.2f / (release_sec * SAMPLE_RATE);
note->gain = note->is_playing
? fminf(note->gain + attack_step, 0.2f) // attack
: fmaxf(note->gain - release_step, 0); // release