#include "midi_device.h"
#include "midi.h"
#ifndef NULL
# define NULL 0
#endif
void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
void midi_process_byte(MidiDevice* device, uint8_t input);
void midi_device_init(MidiDevice* device) {
device->input_state = IDLE;
device->input_count = 0;
bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH);
device->input_cc_callback = NULL;
device->input_noteon_callback = NULL;
device->input_noteoff_callback = NULL;
device->input_aftertouch_callback = NULL;
device->input_pitchbend_callback = NULL;
device->input_songposition_callback = NULL;
device->input_progchange_callback = NULL;
device->input_chanpressure_callback = NULL;
device->input_songselect_callback = NULL;
device->input_tc_quarterframe_callback = NULL;
device->input_realtime_callback = NULL;
device->input_tunerequest_callback = NULL;
device->input_sysex_callback = NULL;
device->input_fallthrough_callback = NULL;
device->input_catchall_callback = NULL;
device->pre_input_process_callback = NULL;
}
void midi_device_input(MidiDevice* device, uint8_t cnt, uint8_t* input) {
uint8_t i;
for (i = 0; i < cnt; i++) bytequeue_enqueue(&device->input_queue, input[i]);
}
void midi_device_set_send_func(MidiDevice* device, midi_var_byte_func_t send_func) { device->send_func = send_func; }
void midi_device_set_pre_input_process_func(MidiDevice* device, midi_no_byte_func_t pre_process_func) { device->pre_input_process_callback = pre_process_func; }
void midi_device_process(MidiDevice* device) {
if (device->pre_input_process_callback) device->pre_input_process_callback(device);
byteQueueIndex_t len = bytequeue_length(&device->input_queue);
uint16_t i;
for (i = 0; i < len; i++) {
uint8_t val = bytequeue_get(&device->input_queue, 0);
midi_process_byte(device, val);
bytequeue_remove(&device->input_queue, 1);
}
}
void midi_process_byte(MidiDevice* device, uint8_t input) {
if (midi_is_realtime(input)) {
input_state_t state = device->input_state;
device->input_state = ONE_BYTE_MESSAGE;
midi_input_callbacks(device, 1, input, 0, 0);
device->input_state = state;
} else if (midi_is_statusbyte(input)) {
if (device->input_state != SYSEX_MESSAGE) {
device->input_buffer[0] = input;
device->input_count = 1;
}
switch (midi_packet_length(input)) {
case ONE:
device->input_state = ONE_BYTE_MESSAGE;
;
midi_input_callbacks(device, 1, input, 0, 0);
device->input_state = IDLE;
break;
case TWO:
device->input_state = TWO_BYTE_MESSAGE;
break;
case THREE:
device->input_state = THREE_BYTE_MESSAGE;
break;
case UNDEFINED:
switch (input) {
case SYSEX_BEGIN:
device->input_state = SYSEX_MESSAGE;
device->input_buffer[0] = input;
device->input_count = 1;
break;
case SYSEX_END:
device->input_buffer[device->input_count % 3] = input;
device->input_count += 1;
midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
device->input_state = IDLE;
break;
default:
device->input_state = IDLE;
device->input_count = 0;
}
break;
default:
device->input_state = IDLE;
device->input_count = 0;
break;
}
} else {
if (device->input_state != IDLE) {
device->input_buffer[device->input_count % 3] = input;
uint16_t prev = device->input_count;
device->input_count += 1;
switch (prev % 3) {
case 2:
midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
if (device->input_state != SYSEX_MESSAGE) {
device->input_count = 1;
}
break;
case 1:
if (device->input_state == TWO_BYTE_MESSAGE) {
midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], 0);
if (device->input_state != SYSEX_MESSAGE) {
device->input_count = 1;
}
}
break;
case 0:
default:
break;
}
}
}
}
void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
bool called = false;
if (device->input_state == SYSEX_MESSAGE) {
if (device->input_sysex_callback) {
const uint16_t start = ((cnt - 1) / 3) * 3;
const uint8_t length = (cnt - start);
uint8_t data[3];
data[0] = byte0;
data[1] = byte1;
data[2] = byte2;
device->input_sysex_callback(device, start, length, data);
called = true;
}
} else {
switch (cnt) {
case 3: {
midi_three_byte_func_t func = NULL;
switch (byte0 & 0xF0) {
case MIDI_CC:
func = device->input_cc_callback;
break;
case MIDI_NOTEON:
func = device->input_noteon_callback;
break;
case MIDI_NOTEOFF:
func = device->input_noteoff_callback;
break;
case MIDI_AFTERTOUCH:
func = device->input_aftertouch_callback;
break;
case MIDI_PITCHBEND:
func = device->input_pitchbend_callback;
break;
case 0xF0:
if (byte0 == MIDI_SONGPOSITION) func = device->input_songposition_callback;
break;
default:
break;
}
if (func) {
if (byte0 == MIDI_SONGPOSITION)
func(device, byte0, byte1, byte2);
else
func(device, byte0 & 0x0F, byte1, byte2);
called = true;
}
} break;
case 2: {
midi_two_byte_func_t func = NULL;
switch (byte0 & 0xF0) {
case MIDI_PROGCHANGE:
func = device->input_progchange_callback;
break;
case MIDI_CHANPRESSURE:
func = device->input_chanpressure_callback;
break;
case 0xF0:
if (byte0 == MIDI_SONGSELECT)
func = device->input_songselect_callback;
else if (byte0 == MIDI_TC_QUARTERFRAME)
func = device->input_tc_quarterframe_callback;
break;
default:
break;
}
if (func) {
if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME)
func(device, byte0, byte1);
else
func(device, byte0 & 0x0F, byte1);
called = true;
}
} break;
case 1: {
midi_one_byte_func_t func = NULL;
if (midi_is_realtime(byte0))
func = device->input_realtime_callback;
else if (byte0 == MIDI_TUNEREQUEST)
func = device->input_tunerequest_callback;
if (func) {
func(device, byte0);
called = true;
}
} break;
default:
if (cnt > 3) cnt = 0;
break;
}
}
if (!called && device->input_fallthrough_callback) device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2);
if (device->input_catchall_callback) device->input_catchall_callback(device, cnt, byte0, byte1, byte2);
}