handle polyphony
This commit is contained in:
1 parent
91a1e2e312
commit
003fc0cb1b
4 files changed
+96
-26
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
CC = clang
|
CC = gcc
|
||||||
CFLAGS = -g3 -Wall -Wextra -pedantic -Wconversion -Wdouble-promotion \
|
CFLAGS = -g3 -Wall -Wextra -pedantic -Wconversion -Wdouble-promotion \
|
||||||
-Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion \
|
-Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion \
|
||||||
-fsanitize=undefined -fsanitize-trap -fanalyzer
|
-fsanitize=undefined -fsanitize-trap -fanalyzer
|
||||||
@@ -9,7 +9,7 @@ main: main.c synth.c
|
|||||||
.PHONY: clean run
|
.PHONY: clean run
|
||||||
|
|
||||||
run: main
|
run: main
|
||||||
./main 24:0
|
./main 20:0
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f main
|
rm -f main
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <alsa/asoundlib.h>
|
#include <alsa/asoundlib.h>
|
||||||
|
#include <assert.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -17,6 +18,16 @@
|
|||||||
#define SAMPLE_RATE 44100
|
#define SAMPLE_RATE 44100
|
||||||
#define PLAYBACK_LATENCY 50000 // 50 ms
|
#define PLAYBACK_LATENCY 50000 // 50 ms
|
||||||
#define PLAYBACK_BUFFER_SIZE 512
|
#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) {
|
static int write_audio(snd_pcm_t *handle, const int16_t *buffer) {
|
||||||
snd_pcm_uframes_t offset = 0;
|
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;
|
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) {
|
int main(int argc, const char** argv) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stderr, "Usage: %s 20:0\n", argv[0]);
|
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]);
|
fprintf(stderr, "Unable to parse midi port %s\n", argv[1]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int err = 0;
|
int err = 0;
|
||||||
snd_pcm_t *handle = 0;
|
snd_pcm_t *handle = 0;
|
||||||
|
if (init_audio(&handle)) {
|
||||||
// Init alsa audio
|
printf("Error initializing audio\n");
|
||||||
err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
|
return 1;
|
||||||
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: ");
|
|
||||||
|
|
||||||
// Init alsa midi
|
// Init alsa midi
|
||||||
snd_seq_t *seq_handle = 0;
|
snd_seq_t *seq_handle = 0;
|
||||||
@@ -112,12 +154,8 @@ int main(int argc, const char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
Note note = {
|
float mixing_buffer[PLAYBACK_BUFFER_SIZE] = {0};
|
||||||
.form = WAVE_SAW,
|
int16_t playing_buffer[PLAYBACK_BUFFER_SIZE] = {0};
|
||||||
.attack_ms = 100.0f,
|
|
||||||
.release_ms = 50.0f
|
|
||||||
};
|
|
||||||
int16_t buf[PLAYBACK_BUFFER_SIZE] = {0};
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Drain queued MIDI events
|
// Drain queued MIDI events
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -133,29 +171,60 @@ int main(int argc, const char** argv) {
|
|||||||
|
|
||||||
switch (ev->type) {
|
switch (ev->type) {
|
||||||
case SND_SEQ_EVENT_NOTEON: {
|
case SND_SEQ_EVENT_NOTEON: {
|
||||||
note.is_playing = ev->data.note.velocity != 0;
|
// TODO: map note velocity to volume
|
||||||
note.freq = midi_note_to_freq(ev->data.note.note);
|
//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;
|
} break;
|
||||||
case SND_SEQ_EVENT_NOTEOFF: {
|
case SND_SEQ_EVENT_NOTEOFF: {
|
||||||
note.is_playing = false;
|
//note.is_playing = false;
|
||||||
|
note_off(ev->data.note.note);
|
||||||
} break;
|
} break;
|
||||||
case SND_SEQ_EVENT_CONTROLLER: {
|
case SND_SEQ_EVENT_CONTROLLER: {
|
||||||
note.filter_cutoff = (float) ev->data.control.value / 64.0f;
|
uint32_t param = ev->data.control.param;
|
||||||
printf("filter cutoff: %f\n", note.filter_cutoff);
|
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;
|
} break;
|
||||||
case SND_SEQ_EVENT_PGMCHANGE: {
|
case SND_SEQ_EVENT_PGMCHANGE: {
|
||||||
note.form = cycle_waveform(note.form);
|
// note.form = cycle_waveform(note.form);
|
||||||
} break;
|
} break;
|
||||||
default:
|
default:
|
||||||
printf("%d\n", ev->type);
|
printf("Midi event %d\n", ev->type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
snd_seq_free_event(ev);
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#define SAMPLE_RATE 44100.0f
|
#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++) {
|
for (int i = 0; i < buffer_size; i++) {
|
||||||
// update phase counter
|
// update phase counter
|
||||||
note->phase = fmodf(note->phase + note->freq / SAMPLE_RATE, 1.0f);
|
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
|
? fminf(note->gain + attack_step, 0.2f) // attack
|
||||||
: fmaxf(note->gain - release_step, 0); // release
|
: fmaxf(note->gain - release_step, 0); // release
|
||||||
|
|
||||||
|
buffer[i] += sample * note->gain;
|
||||||
// convert sample to 16-bit signed integer and apply 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user