handle polyphony

This commit is contained in:
thib8956 committed 2026-10-30 13:32:57 +01:00
1 parent 91a1e2e312
commit 003fc0cb1b
4 files changed
+96 -26

No files matched your search

+90 -21
View File
@@ -1,4 +1,5 @@
#include <alsa/asoundlib.h>
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
@@ -17,6 +18,16 @@
#define SAMPLE_RATE 44100
#define PLAYBACK_LATENCY 50000 // 50 ms
#define PLAYBACK_BUFFER_SIZE 512
#define MAX_NOTES 8
static int init_audio(snd_pcm_t **handle) {
int err = 0;
err = snd_pcm_open(handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
ALSA_CHK(err, "Could not open alsa device: ");
err = snd_pcm_set_params(*handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, N_CHANNELS, SAMPLE_RATE, 1, PLAYBACK_LATENCY);
ALSA_CHK(err, "Could not set alsa params: ");
return 0;
}
static int write_audio(snd_pcm_t *handle, const int16_t *buffer) {
snd_pcm_uframes_t offset = 0;
@@ -82,6 +93,38 @@ static int parse_port(int *src_client, int *src_port, const char *str) {
return sscanf(str, "%d:%d", src_client, src_port) == 2;
}
static Note notes[MAX_NOTES] = {0};
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;
return;
}
}
// TODO: handle no free slots
assert(false && "No free slots in notes array");
}
void note_off(uint8_t note) {
float freq = midi_note_to_freq(note);
for (size_t i = 0; i < MAX_NOTES; i++) {
if (notes[i].is_playing && notes[i].freq == freq) {
notes[i].is_playing = false;
return;
}
}
//assert(false && "Note not found");
}
int main(int argc, const char** argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s 20:0\n", argv[0]);
@@ -94,14 +137,13 @@ int main(int argc, const char** argv) {
fprintf(stderr, "Unable to parse midi port %s\n", argv[1]);
return 1;
}
int err = 0;
snd_pcm_t *handle = 0;
// Init alsa audio
err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
ALSA_CHK(err, "Could not open alsa device: ");
err = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, N_CHANNELS, SAMPLE_RATE, 1, PLAYBACK_LATENCY);
ALSA_CHK(err, "Could not set alsa params: ");
if (init_audio(&handle)) {
printf("Error initializing audio\n");
return 1;
}
// Init alsa midi
snd_seq_t *seq_handle = 0;
@@ -112,12 +154,8 @@ int main(int argc, const char** argv) {
}
// Main loop
Note note = {
.form = WAVE_SAW,
.attack_ms = 100.0f,
.release_ms = 50.0f
};
int16_t buf[PLAYBACK_BUFFER_SIZE] = {0};
float mixing_buffer[PLAYBACK_BUFFER_SIZE] = {0};
int16_t playing_buffer[PLAYBACK_BUFFER_SIZE] = {0};
for (;;) {
// Drain queued MIDI events
for (;;) {
@@ -133,29 +171,60 @@ int main(int argc, const char** argv) {
switch (ev->type) {
case SND_SEQ_EVENT_NOTEON: {
note.is_playing = ev->data.note.velocity != 0;
note.freq = midi_note_to_freq(ev->data.note.note);
// TODO: map note velocity to volume
//note.is_playing = ev->data.note.velocity != 0;
//note.freq = midi_note_to_freq(ev->data.note.note);
note_on(ev->data.note.note, ev->data.note.velocity);
} break;
case SND_SEQ_EVENT_NOTEOFF: {
note.is_playing = false;
//note.is_playing = false;
note_off(ev->data.note.note);
} break;
case SND_SEQ_EVENT_CONTROLLER: {
note.filter_cutoff = (float) ev->data.control.value / 64.0f;
printf("filter cutoff: %f\n", note.filter_cutoff);
uint32_t param = ev->data.control.param;
float control_value = (float) ev->data.control.value / 127.0f; // [0, 1]
switch (param) {
case 1: {
//note.attack_ms = control_value * 1000.0f;
//printf("note attack: %f\n", note.attack_ms);
break;
}
case 2: {
//note.release_ms = control_value * 1000.0f;
//printf("note release: %f\n", note.release_ms);
break;
}
default: {
printf("control %d\n", param);
break;
}
}
//printf("control %d: %f\n", param, control_value)
//note.attack_ms = control_value * 1000.0f;
//printf("note attack: %f\n", note.attack_ms);
} break;
case SND_SEQ_EVENT_PGMCHANGE: {
note.form = cycle_waveform(note.form);
// note.form = cycle_waveform(note.form);
} break;
default:
printf("%d\n", ev->type);
printf("Midi event %d\n", ev->type);
break;
}
snd_seq_free_event(ev);
}
play_note(&note, buf, PLAYBACK_BUFFER_SIZE);
memset(mixing_buffer, 0, sizeof(mixing_buffer));
for (size_t i = 0; i < MAX_NOTES; i++) {
play_note(&notes[i], mixing_buffer, PLAYBACK_BUFFER_SIZE);
}
if (write_audio(handle, buf) < 0) {
// mix voices into playing buffer
for (size_t i = 0; i < PLAYBACK_BUFFER_SIZE; i++) {
// TODO: handle saturation (sum ouf of int16 range)
playing_buffer[i] = (int16_t) (mixing_buffer[i] * (float) INT16_MAX);
}
if (write_audio(handle, playing_buffer) < 0) {
return 1;
}
}