Commit 166b7503 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add program change support to midi parser

parent adcf2ec4
......@@ -89,11 +89,38 @@ test_note_off(void)
assert_status(MIDI_NOTE_OFF, 1, 0x1200);
}
void
test_program_change(void)
{
desc("test program change messages");
status_init();
assert_status(MIDI_NUM_STATUSES, 0, 0);
midi_parse(0xc0);
midi_parse(0x12);
assert_status(MIDI_PROGRAM_CHANGE, 1, 0x1200);
// a little silly, running status for program change messages
midi_parse(0);
assert_status(MIDI_PROGRAM_CHANGE, 1, 0);
// throw in a different message
midi_parse(0x80); // note off
midi_parse(0x12);
midi_parse(0);
assert_status(MIDI_NOTE_OFF, 1, 0x1200);
midi_parse(0xc0);
midi_parse(0x34);
assert_status(MIDI_PROGRAM_CHANGE, 1, 0x3400);
}
int
main(int argc, char** argv)
{
test_init();
test_note_on();
test_note_off();
test_program_change();
}
......@@ -24,12 +24,16 @@ uint8_t parsed_len;
const uint8_t statuses[MIDI_NUM_STATUSES] = {
0b1001, // note on
0b1000, // note off
0b1100, // program change
};
uint8_t
data_len(uint8_t st)
{
// Some channel messages have only 1 data byte, but we don't support any of those yet.
if (st == MIDI_PROGRAM_CHANGE) {
return 1;
}
return 2;
}
......@@ -89,7 +93,7 @@ midi_parse(uint8_t midi_byte)
return;
}
if (parsed_len >= 2) {
if (parsed_len >= data_len(status)) {
clear_data();
}
......
......@@ -6,6 +6,7 @@
enum _statuses {
MIDI_NOTE_ON,
MIDI_NOTE_OFF,
MIDI_PROGRAM_CHANGE,
MIDI_NUM_STATUSES,
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment