diff options
Diffstat (limited to 'src/midi.c')
-rw-r--r-- | src/midi.c | 20 |
1 files changed, 17 insertions, 3 deletions
@@ -1,8 +1,9 @@ #include "midi.h" +#include "notes.h" #include <string.h> -void midi_decode(uint32_t msg) { +void midi_decode(uint32_t msg, synth_t * synth) { // printf("MIDI message: 0x%X\n", msg); uint8_t status = msg; uint8_t data1 = (msg >> 8) & 0xFF; @@ -13,9 +14,21 @@ void midi_decode(uint32_t msg) { switch (message) { case 0x08: printf("Note Off: channel=%d, note=%d, velocity=%d\n", channel, data1, data2); + if (synth->n.key == data1) { + synth->n.noteOff = Pa_GetStreamTime(synth->stream); + } break; case 0x09: printf("Note On: channel=%d, note=%d, velocity=%d\n", channel, data1, data2); + synth->n.key = data1; + synth->n.freq = notes[data1 % 12][(data1 / 12) % 8]; + synth->n.noteOn = Pa_GetStreamTime(synth->stream); + synth->n.noteOff = 0; + synth->n.elapsed = 0; + synth->adsr.elapsed = 0; + synth->active = 1; + synth->gain = data2 / 127.0; + break; case 0x0A: printf("Aftertouch: channel=%d, note=%d, pressure=%d\n", channel, data1, data2); @@ -51,7 +64,7 @@ midiCallback(PtTimestamp timestamp, void *userData) { Pm_Read(m->stream, &buf, 1); //printf("%d\n", buf.message); - midi_decode(buf.message); + midi_decode(buf.message, m->synth); } @@ -59,7 +72,8 @@ void init_midi(midi_t *m, synth_t *synth) { m->stream = NULL; - + m->synth = synth; + Pm_Initialize(); printf("midi devs: %d\n", Pm_CountDevices()); |