diff --git a/Makefile b/Makefile index f93feac..6520d49 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CC = clang +CC = gcc CFLAGS = -g3 -Wall -Wextra -pedantic -Wconversion -Wdouble-promotion \ -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion \ -fsanitize=undefined -fsanitize-trap -fanalyzer @@ -9,7 +9,7 @@ main: main.c synth.c .PHONY: clean run run: main - ./main 24:0 + ./main 20:0 clean: rm -f main diff --git a/main.c b/main.c index f66da66..78c985d 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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(¬e, buf, PLAYBACK_BUFFER_SIZE); + 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); + } - 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; } } diff --git a/synth.c b/synth.c index 0cce8b6..d01ec0d 100644 --- a/synth.c +++ b/synth.c @@ -2,7 +2,7 @@ #include #define SAMPLE_RATE 44100.0f -void play_note(Note *note, int16_t *buffer, int buffer_size) { +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); @@ -28,7 +28,8 @@ void play_note(Note *note, int16_t *buffer, int buffer_size) { ? 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); + //buffer[i] = (int16_t) (note->gain * sample * (float) INT16_MAX); } } diff --git a/synth.h b/synth.h index 3f10fdd..03502dd 100644 --- a/synth.h +++ b/synth.h @@ -24,6 +24,6 @@ typedef struct { Waveform form; } Note; -void play_note(Note *note, int16_t *buffer, int buffer_size); +void play_note(Note *note, float *buffer, int buffer_size); #endif // NOTE_H