1 //midi for embedded chips,
2 //Copyright 2010 Alex Norman
4 //This file is part of avr-midi.
6 //avr-midi is free software: you can redistribute it and/or modify
7 //it under the terms of the GNU General Public License as published by
8 //the Free Software Foundation, either version 3 of the License, or
9 //(at your option) any later version.
11 //avr-midi is distributed in the hope that it will be useful,
12 //but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 //GNU General Public License for more details.
16 //You should have received a copy of the GNU General Public License
17 //along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
19 #include "midi_device.h"
26 //forward declarations, internally used to call the callbacks
27 void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
28 void midi_process_byte(MidiDevice * device, uint8_t input);
30 void midi_device_init(MidiDevice * device){
31 device->input_state = IDLE;
32 device->input_count = 0;
33 bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH);
36 device->input_cc_callback = NULL;
37 device->input_noteon_callback = NULL;
38 device->input_noteoff_callback = NULL;
39 device->input_aftertouch_callback = NULL;
40 device->input_pitchbend_callback = NULL;
41 device->input_songposition_callback = NULL;
44 device->input_progchange_callback = NULL;
45 device->input_chanpressure_callback = NULL;
46 device->input_songselect_callback = NULL;
47 device->input_tc_quarterframe_callback = NULL;
50 device->input_realtime_callback = NULL;
51 device->input_tunerequest_callback = NULL;
54 device->input_sysex_callback = NULL;
55 device->input_fallthrough_callback = NULL;
56 device->input_catchall_callback = NULL;
58 device->pre_input_process_callback = NULL;
61 void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input) {
63 for (i = 0; i < cnt; i++)
64 bytequeue_enqueue(&device->input_queue, input[i]);
67 void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func){
68 device->send_func = send_func;
71 void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func){
72 device->pre_input_process_callback = pre_process_func;
75 void midi_device_process(MidiDevice * device) {
76 //call the pre_input_process_callback if there is one
77 if(device->pre_input_process_callback)
78 device->pre_input_process_callback(device);
80 //pull stuff off the queue and process
81 byteQueueIndex_t len = bytequeue_length(&device->input_queue);
83 //TODO limit number of bytes processed?
84 for(i = 0; i < len; i++) {
85 uint8_t val = bytequeue_get(&device->input_queue, 0);
86 midi_process_byte(device, val);
87 bytequeue_remove(&device->input_queue, 1);
91 void midi_process_byte(MidiDevice * device, uint8_t input) {
92 if (midi_is_realtime(input)) {
93 //call callback, store and restore state
94 input_state_t state = device->input_state;
95 device->input_state = ONE_BYTE_MESSAGE;
96 midi_input_callbacks(device, 1, input, 0, 0);
97 device->input_state = state;
98 } else if (midi_is_statusbyte(input)) {
100 if (device->input_state != SYSEX_MESSAGE) {
101 device->input_buffer[0] = input;
102 device->input_count = 1;
104 switch (midi_packet_length(input)) {
106 device->input_state = ONE_BYTE_MESSAGE;;
107 midi_input_callbacks(device, 1, input, 0, 0);
108 device->input_state = IDLE;
111 device->input_state = TWO_BYTE_MESSAGE;
114 device->input_state = THREE_BYTE_MESSAGE;
119 device->input_state = SYSEX_MESSAGE;
120 device->input_buffer[0] = input;
121 device->input_count = 1;
124 //send what is left in the input buffer, set idle
125 device->input_buffer[device->input_count % 3] = input;
126 device->input_count += 1;
128 midi_input_callbacks(device, device->input_count,
129 device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
130 device->input_state = IDLE;
133 device->input_state = IDLE;
134 device->input_count = 0;
139 device->input_state = IDLE;
140 device->input_count = 0;
144 if (device->input_state != IDLE) {
146 device->input_buffer[device->input_count % 3] = input;
148 uint16_t prev = device->input_count;
149 device->input_count += 1;
154 midi_input_callbacks(device, device->input_count,
155 device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
156 if (device->input_state != SYSEX_MESSAGE) {
157 //set to 1, keeping status byte, allowing for running status
158 device->input_count = 1;
162 if (device->input_state == TWO_BYTE_MESSAGE) {
164 midi_input_callbacks(device, device->input_count,
165 device->input_buffer[0], device->input_buffer[1], 0);
166 if (device->input_state != SYSEX_MESSAGE) {
167 //set to 1, keeping status byte, allowing for running status
168 device->input_count = 1;
174 //one byte messages are dealt with directly
181 void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
182 //did we end up calling a callback?
184 if (device->input_state == SYSEX_MESSAGE) {
185 if (device->input_sysex_callback) {
186 const uint16_t start = ((cnt - 1) / 3) * 3;
187 const uint8_t length = (cnt - start);
192 device->input_sysex_callback(device, start, length, data);
199 midi_three_byte_func_t func = NULL;
200 switch (byte0 & 0xF0) {
202 func = device->input_cc_callback;
205 func = device->input_noteon_callback;
208 func = device->input_noteoff_callback;
210 case MIDI_AFTERTOUCH:
211 func = device->input_aftertouch_callback;
214 func = device->input_pitchbend_callback;
217 if (byte0 == MIDI_SONGPOSITION)
218 func = device->input_songposition_callback;
224 //mask off the channel for non song position functions
225 if (byte0 == MIDI_SONGPOSITION)
226 func(device, byte0, byte1, byte2);
228 func(device, byte0 & 0x0F, byte1, byte2);
235 midi_two_byte_func_t func = NULL;
236 switch (byte0 & 0xF0) {
237 case MIDI_PROGCHANGE:
238 func = device->input_progchange_callback;
240 case MIDI_CHANPRESSURE:
241 func = device->input_chanpressure_callback;
244 if (byte0 == MIDI_SONGSELECT)
245 func = device->input_songselect_callback;
246 else if (byte0 == MIDI_TC_QUARTERFRAME)
247 func = device->input_tc_quarterframe_callback;
253 //mask off the channel
254 if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME)
255 func(device, byte0, byte1);
257 func(device, byte0 & 0x0F, byte1);
264 midi_one_byte_func_t func = NULL;
265 if (midi_is_realtime(byte0))
266 func = device->input_realtime_callback;
267 else if (byte0 == MIDI_TUNEREQUEST)
268 func = device->input_tunerequest_callback;
283 //if there is fallthrough default callback and we haven't called a more specific one,
284 //call the fallthrough
285 if (!called && device->input_fallthrough_callback)
286 device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2);
287 //always call the catch all if it exists
288 if (device->input_catchall_callback)
289 device->input_catchall_callback(device, cnt, byte0, byte1, byte2);