From 2b1a2b59d447e00bc9bc3b121e7dadd1a5b460cb Mon Sep 17 00:00:00 2001 From: Thibaud Date: Mon, 2 Nov 2026 21:17:24 +0100 Subject: [PATCH] implement voice stealing --- main.c | 51 ++++++++++++++++++++++++++++++++++++++------------- synth.c | 13 +++++-------- synth.h | 5 ++--- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/main.c b/main.c index 0a626c2..7c9d94e 100644 --- a/main.c +++ b/main.c @@ -96,29 +96,54 @@ static int parse_port(int *src_client, int *src_port, const char *str) { static Note notes[MAX_NOTES] = {0}; -void note_on(uint8_t note, uint8_t velocity) { +static void start_note(Note *n, uint8_t id, float freq, uint8_t velocity) { + n->id = id; + n->gain = 0.0f; + n->attack_ms = 100.0f; + n->release_ms = 50.0f; + n->form = WAVE_SAW; + n->freq = freq; + n->is_playing = velocity != 0; +} + +static void note_on(uint8_t note, uint8_t velocity) { // TODO map velocity to note gain // find first free slot in notes array for (size_t i = 0; i < MAX_NOTES; i++) { - if (!notes[i].is_playing && notes[i].gain == 0) { - notes[i].attack_ms = 100.0f; - notes[i].release_ms = 50.0f; - notes[i].form = WAVE_SAW; - notes[i].freq = midi_note_to_freq(note); - notes[i].is_playing = velocity != 0; + if (!notes[i].is_playing && notes[i].gain <= 0) { + start_note(¬es[i], note, midi_note_to_freq(note), velocity); return; } } - // TODO: handle no free slots - assert(false && "No free slots in notes array"); + // No free slots. Note stealing + // first pass: steal voice with quietest note and not playing + float min_gain = INFINITY; + Note* note_ptr = NULL; + for (size_t i = 0; i < MAX_NOTES; i++) { + if (!notes[i].is_playing && notes[i].gain < min_gain) { + min_gain = notes[i].gain; + note_ptr = ¬es[i]; + } + } + // second pass: steal voice with quietest note and playing + min_gain = INFINITY; + if (note_ptr == NULL) { + for (size_t i = 0; i < MAX_NOTES; i++) { + if (notes[i].gain < min_gain) { + min_gain = notes[i].gain; + note_ptr = ¬es[i]; + } + } + } + assert(note_ptr != NULL && "No free slots"); + start_note(note_ptr, note, midi_note_to_freq(note), velocity); } -void note_off(uint8_t note) { - float freq = midi_note_to_freq(note); +static void note_off(uint8_t note) { for (size_t i = 0; i < MAX_NOTES; i++) { - if (notes[i].is_playing && notes[i].freq == freq) { + if (notes[i].is_playing && notes[i].id == note) { notes[i].is_playing = false; return; } @@ -216,7 +241,7 @@ int main(int argc, const char** argv) { memset(mixing_buffer, 0, sizeof(mixing_buffer)); for (size_t i = 0; i < MAX_NOTES; i++) { - play_note(¬es[i], mixing_buffer, PLAYBACK_BUFFER_SIZE); + play_note(¬es[i], mixing_buffer, PLAYBACK_BUFFER_SIZE, SAMPLE_RATE); } // mix voices into playing buffer diff --git a/synth.c b/synth.c index d37a42b..bba53c3 100644 --- a/synth.c +++ b/synth.c @@ -1,11 +1,10 @@ #include "synth.h" #include -#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); } } diff --git a/synth.h b/synth.h index 03502dd..10a7449 100644 --- a/synth.h +++ b/synth.h @@ -13,17 +13,16 @@ typedef enum { } Waveform; typedef struct { + uint8_t id; float phase; float freq; float gain; float attack_ms; float release_ms; - float filter_cutoff; - float lp; bool is_playing; Waveform form; } Note; -void play_note(Note *note, float *buffer, int buffer_size); +void play_note(Note *note, float *buffer, unsigned int buffer_size, float sample_rate); #endif // NOTE_H