upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

midi_device.c 9.3KB

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