midi_device.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // midi for embedded chips,
  2. // Copyright 2010 Alex Norman
  3. //
  4. // This file is part of avr-midi.
  5. //
  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.
  10. //
  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.
  15. //
  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/>.
  18. #include "midi_device.h"
  19. #include "midi.h"
  20. #ifndef NULL
  21. # define NULL 0
  22. #endif
  23. // forward declarations, internally used to call the callbacks
  24. void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2);
  25. void midi_process_byte(MidiDevice* device, uint8_t input);
  26. void midi_device_init(MidiDevice* device) {
  27. device->input_state = IDLE;
  28. device->input_count = 0;
  29. bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH);
  30. // three byte funcs
  31. device->input_cc_callback = NULL;
  32. device->input_noteon_callback = NULL;
  33. device->input_noteoff_callback = NULL;
  34. device->input_aftertouch_callback = NULL;
  35. device->input_pitchbend_callback = NULL;
  36. device->input_songposition_callback = NULL;
  37. // two byte funcs
  38. device->input_progchange_callback = NULL;
  39. device->input_chanpressure_callback = NULL;
  40. device->input_songselect_callback = NULL;
  41. device->input_tc_quarterframe_callback = NULL;
  42. // one byte funcs
  43. device->input_realtime_callback = NULL;
  44. device->input_tunerequest_callback = NULL;
  45. // var byte functions
  46. device->input_sysex_callback = NULL;
  47. device->input_fallthrough_callback = NULL;
  48. device->input_catchall_callback = NULL;
  49. device->pre_input_process_callback = NULL;
  50. }
  51. void midi_device_input(MidiDevice* device, uint8_t cnt, uint8_t* input) {
  52. uint8_t i;
  53. for (i = 0; i < cnt; i++) bytequeue_enqueue(&device->input_queue, input[i]);
  54. }
  55. void midi_device_set_send_func(MidiDevice* device, midi_var_byte_func_t send_func) { device->send_func = send_func; }
  56. 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; }
  57. void midi_device_process(MidiDevice* device) {
  58. // call the pre_input_process_callback if there is one
  59. if (device->pre_input_process_callback) device->pre_input_process_callback(device);
  60. // pull stuff off the queue and process
  61. byteQueueIndex_t len = bytequeue_length(&device->input_queue);
  62. uint16_t i;
  63. // TODO limit number of bytes processed?
  64. for (i = 0; i < len; i++) {
  65. uint8_t val = bytequeue_get(&device->input_queue, 0);
  66. midi_process_byte(device, val);
  67. bytequeue_remove(&device->input_queue, 1);
  68. }
  69. }
  70. void midi_process_byte(MidiDevice* device, uint8_t input) {
  71. if (midi_is_realtime(input)) {
  72. // call callback, store and restore state
  73. input_state_t state = device->input_state;
  74. device->input_state = ONE_BYTE_MESSAGE;
  75. midi_input_callbacks(device, 1, input, 0, 0);
  76. device->input_state = state;
  77. } else if (midi_is_statusbyte(input)) {
  78. // store the byte
  79. if (device->input_state != SYSEX_MESSAGE) {
  80. device->input_buffer[0] = input;
  81. device->input_count = 1;
  82. }
  83. switch (midi_packet_length(input)) {
  84. case ONE:
  85. device->input_state = ONE_BYTE_MESSAGE;
  86. ;
  87. midi_input_callbacks(device, 1, input, 0, 0);
  88. device->input_state = IDLE;
  89. break;
  90. case TWO:
  91. device->input_state = TWO_BYTE_MESSAGE;
  92. break;
  93. case THREE:
  94. device->input_state = THREE_BYTE_MESSAGE;
  95. break;
  96. case UNDEFINED:
  97. switch (input) {
  98. case SYSEX_BEGIN:
  99. device->input_state = SYSEX_MESSAGE;
  100. device->input_buffer[0] = input;
  101. device->input_count = 1;
  102. break;
  103. case SYSEX_END:
  104. // send what is left in the input buffer, set idle
  105. device->input_buffer[device->input_count % 3] = input;
  106. device->input_count += 1;
  107. // call the callback
  108. midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
  109. device->input_state = IDLE;
  110. break;
  111. default:
  112. device->input_state = IDLE;
  113. device->input_count = 0;
  114. }
  115. break;
  116. default:
  117. device->input_state = IDLE;
  118. device->input_count = 0;
  119. break;
  120. }
  121. } else {
  122. if (device->input_state != IDLE) {
  123. // store the byte
  124. device->input_buffer[device->input_count % 3] = input;
  125. // increment count
  126. uint16_t prev = device->input_count;
  127. device->input_count += 1;
  128. switch (prev % 3) {
  129. case 2:
  130. // call callback
  131. midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]);
  132. if (device->input_state != SYSEX_MESSAGE) {
  133. // set to 1, keeping status byte, allowing for running status
  134. device->input_count = 1;
  135. }
  136. break;
  137. case 1:
  138. if (device->input_state == TWO_BYTE_MESSAGE) {
  139. // call callback
  140. midi_input_callbacks(device, device->input_count, device->input_buffer[0], device->input_buffer[1], 0);
  141. if (device->input_state != SYSEX_MESSAGE) {
  142. // set to 1, keeping status byte, allowing for running status
  143. device->input_count = 1;
  144. }
  145. }
  146. break;
  147. case 0:
  148. default:
  149. // one byte messages are dealt with directly
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. void midi_input_callbacks(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
  156. // did we end up calling a callback?
  157. bool called = false;
  158. if (device->input_state == SYSEX_MESSAGE) {
  159. if (device->input_sysex_callback) {
  160. const uint16_t start = ((cnt - 1) / 3) * 3;
  161. const uint8_t length = (cnt - start);
  162. uint8_t data[3];
  163. data[0] = byte0;
  164. data[1] = byte1;
  165. data[2] = byte2;
  166. device->input_sysex_callback(device, start, length, data);
  167. called = true;
  168. }
  169. } else {
  170. switch (cnt) {
  171. case 3: {
  172. midi_three_byte_func_t func = NULL;
  173. switch (byte0 & 0xF0) {
  174. case MIDI_CC:
  175. func = device->input_cc_callback;
  176. break;
  177. case MIDI_NOTEON:
  178. func = device->input_noteon_callback;
  179. break;
  180. case MIDI_NOTEOFF:
  181. func = device->input_noteoff_callback;
  182. break;
  183. case MIDI_AFTERTOUCH:
  184. func = device->input_aftertouch_callback;
  185. break;
  186. case MIDI_PITCHBEND:
  187. func = device->input_pitchbend_callback;
  188. break;
  189. case 0xF0:
  190. if (byte0 == MIDI_SONGPOSITION) func = device->input_songposition_callback;
  191. break;
  192. default:
  193. break;
  194. }
  195. if (func) {
  196. // mask off the channel for non song position functions
  197. if (byte0 == MIDI_SONGPOSITION)
  198. func(device, byte0, byte1, byte2);
  199. else
  200. func(device, byte0 & 0x0F, byte1, byte2);
  201. called = true;
  202. }
  203. } break;
  204. case 2: {
  205. midi_two_byte_func_t func = NULL;
  206. switch (byte0 & 0xF0) {
  207. case MIDI_PROGCHANGE:
  208. func = device->input_progchange_callback;
  209. break;
  210. case MIDI_CHANPRESSURE:
  211. func = device->input_chanpressure_callback;
  212. break;
  213. case 0xF0:
  214. if (byte0 == MIDI_SONGSELECT)
  215. func = device->input_songselect_callback;
  216. else if (byte0 == MIDI_TC_QUARTERFRAME)
  217. func = device->input_tc_quarterframe_callback;
  218. break;
  219. default:
  220. break;
  221. }
  222. if (func) {
  223. // mask off the channel
  224. if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME)
  225. func(device, byte0, byte1);
  226. else
  227. func(device, byte0 & 0x0F, byte1);
  228. called = true;
  229. }
  230. } break;
  231. case 1: {
  232. midi_one_byte_func_t func = NULL;
  233. if (midi_is_realtime(byte0))
  234. func = device->input_realtime_callback;
  235. else if (byte0 == MIDI_TUNEREQUEST)
  236. func = device->input_tunerequest_callback;
  237. if (func) {
  238. func(device, byte0);
  239. called = true;
  240. }
  241. } break;
  242. default:
  243. // just in case
  244. if (cnt > 3) cnt = 0;
  245. break;
  246. }
  247. }
  248. // if there is fallthrough default callback and we haven't called a more specific one,
  249. // call the fallthrough
  250. if (!called && device->input_fallthrough_callback) device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2);
  251. // always call the catch all if it exists
  252. if (device->input_catchall_callback) device->input_catchall_callback(device, cnt, byte0, byte1, byte2);
  253. }