implement voice stealing

This commit is contained in:
thib8956 committed 2026-11-02 21:18:28 +01:00
1 parent 5e5f08c4f9
commit 2b1a2b59d4
3 files changed
+45 -24

No files matched your search

+5 -8
View File
@@ -1,11 +1,10 @@
#include "synth.h"
#include <math.h>
#define SAMPLE_RATE 44100.0f
void play_note(Note *note, float *buffer, int buffer_size) {
for (int i = 0; i < buffer_size; i++) {
void play_note(Note *note, float *buffer, unsigned int buffer_size, float sample_rate) {
for (unsigned int i = 0; i < buffer_size; i++) {
// update phase counter
note->phase = note->phase + note->freq / SAMPLE_RATE;
note->phase = note->phase + note->freq / sample_rate;
if (note->phase >= 1.0f) { note->phase -= 1.0f; }
// oscillator
@@ -25,14 +24,12 @@ void play_note(Note *note, float *buffer, int buffer_size) {
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 / (attack_sec * SAMPLE_RATE);
float release_step = 0.2f / (release_sec * 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
buffer[i] += sample * note->gain;
// convert sample to 16-bit signed integer and apply gain
//buffer[i] = (int16_t) (note->gain * sample * (float) INT16_MAX);
}
}