midi_device.c 11 KB

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