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.c 7.8KB

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.h"
  19. #include <string.h> //for memcpy
  20. #define MIN(x,y) (((x) < (y)) ? (x) : (y))
  21. #ifndef NULL
  22. #define NULL 0
  23. #endif
  24. bool midi_is_statusbyte(uint8_t theByte){
  25. return (bool)(theByte & MIDI_STATUSMASK);
  26. }
  27. bool midi_is_realtime(uint8_t theByte){
  28. return (theByte >= MIDI_CLOCK);
  29. }
  30. midi_packet_length_t midi_packet_length(uint8_t status){
  31. switch(status & 0xF0){
  32. case MIDI_CC:
  33. case MIDI_NOTEON:
  34. case MIDI_NOTEOFF:
  35. case MIDI_AFTERTOUCH:
  36. case MIDI_PITCHBEND:
  37. return THREE;
  38. case MIDI_PROGCHANGE:
  39. case MIDI_CHANPRESSURE:
  40. case MIDI_SONGSELECT:
  41. return TWO;
  42. case 0xF0:
  43. switch(status) {
  44. case MIDI_CLOCK:
  45. case MIDI_TICK:
  46. case MIDI_START:
  47. case MIDI_CONTINUE:
  48. case MIDI_STOP:
  49. case MIDI_ACTIVESENSE:
  50. case MIDI_RESET:
  51. case MIDI_TUNEREQUEST:
  52. return ONE;
  53. case MIDI_SONGPOSITION:
  54. return THREE;
  55. case MIDI_TC_QUARTERFRAME:
  56. case MIDI_SONGSELECT:
  57. return TWO;
  58. case SYSEX_END:
  59. case SYSEX_BEGIN:
  60. default:
  61. return UNDEFINED;
  62. }
  63. default:
  64. return UNDEFINED;
  65. }
  66. }
  67. void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val){
  68. //CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel.
  69. //CC Data: Controller Num, Controller Val
  70. device->send_func(device, 3,
  71. MIDI_CC | (chan & MIDI_CHANMASK),
  72. num & 0x7F,
  73. val & 0x7F);
  74. }
  75. void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){
  76. //Note Data: Note Num, Note Velocity
  77. device->send_func(device, 3,
  78. MIDI_NOTEON | (chan & MIDI_CHANMASK),
  79. num & 0x7F,
  80. vel & 0x7F);
  81. }
  82. void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){
  83. //Note Data: Note Num, Note Velocity
  84. device->send_func(device, 3,
  85. MIDI_NOTEOFF | (chan & MIDI_CHANMASK),
  86. num & 0x7F,
  87. vel & 0x7F);
  88. }
  89. void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, uint8_t amt){
  90. device->send_func(device, 3,
  91. MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK),
  92. note_num & 0x7F,
  93. amt & 0x7F);
  94. }
  95. //XXX does this work right?
  96. //amt in range -0x2000, 0x1fff
  97. //uAmt should be in range..
  98. //0x0000 to 0x3FFF
  99. void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt){
  100. uint16_t uAmt;
  101. //check range
  102. if(amt > 0x1fff){
  103. uAmt = 0x3FFF;
  104. } else if(amt < -0x2000){
  105. uAmt = 0;
  106. } else {
  107. uAmt = amt + 0x2000;
  108. }
  109. device->send_func(device, 3,
  110. MIDI_PITCHBEND | (chan & MIDI_CHANMASK),
  111. uAmt & 0x7F,
  112. (uAmt >> 7) & 0x7F);
  113. }
  114. void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num){
  115. device->send_func(device, 2,
  116. MIDI_PROGCHANGE | (chan & MIDI_CHANMASK),
  117. num & 0x7F,
  118. 0);
  119. }
  120. void midi_send_channelpressure(MidiDevice * device, uint8_t chan, uint8_t amt){
  121. device->send_func(device, 2,
  122. MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK),
  123. amt & 0x7F,
  124. 0);
  125. }
  126. void midi_send_clock(MidiDevice * device){
  127. device->send_func(device, 1, MIDI_CLOCK, 0, 0);
  128. }
  129. void midi_send_tick(MidiDevice * device){
  130. device->send_func(device, 1, MIDI_TICK, 0, 0);
  131. }
  132. void midi_send_start(MidiDevice * device){
  133. device->send_func(device, 1, MIDI_START, 0, 0);
  134. }
  135. void midi_send_continue(MidiDevice * device){
  136. device->send_func(device, 1, MIDI_CONTINUE, 0, 0);
  137. }
  138. void midi_send_stop(MidiDevice * device){
  139. device->send_func(device, 1, MIDI_STOP, 0, 0);
  140. }
  141. void midi_send_activesense(MidiDevice * device){
  142. device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0);
  143. }
  144. void midi_send_reset(MidiDevice * device){
  145. device->send_func(device, 1, MIDI_RESET, 0, 0);
  146. }
  147. void midi_send_tcquarterframe(MidiDevice * device, uint8_t time){
  148. device->send_func(device, 2,
  149. MIDI_TC_QUARTERFRAME,
  150. time & 0x7F,
  151. 0);
  152. }
  153. //XXX is this right?
  154. void midi_send_songposition(MidiDevice * device, uint16_t pos){
  155. device->send_func(device, 3,
  156. MIDI_SONGPOSITION,
  157. pos & 0x7F,
  158. (pos >> 7) & 0x7F);
  159. }
  160. void midi_send_songselect(MidiDevice * device, uint8_t song){
  161. device->send_func(device, 2,
  162. MIDI_SONGSELECT,
  163. song & 0x7F,
  164. 0);
  165. }
  166. void midi_send_tunerequest(MidiDevice * device){
  167. device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0);
  168. }
  169. void midi_send_byte(MidiDevice * device, uint8_t b){
  170. device->send_func(device, 1, b, 0, 0);
  171. }
  172. void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2){
  173. //ensure that the count passed along is always 3 or lower
  174. if (count > 3) {
  175. //TODO how to do this correctly?
  176. }
  177. device->send_func(device, count, byte0, byte1, byte2);
  178. }
  179. void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array) {
  180. uint16_t i;
  181. for (i = 0; i < count; i += 3) {
  182. uint8_t b[3] = { 0, 0, 0 };
  183. uint16_t to_send = count - i;
  184. to_send = (to_send > 3) ? 3 : to_send;
  185. memcpy(b, array + i, to_send);
  186. midi_send_data(device, to_send, b[0], b[1], b[2]);
  187. }
  188. }
  189. void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func){
  190. device->input_cc_callback = func;
  191. }
  192. void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t func){
  193. device->input_noteon_callback = func;
  194. }
  195. void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t func){
  196. device->input_noteoff_callback = func;
  197. }
  198. void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func_t func){
  199. device->input_aftertouch_callback = func;
  200. }
  201. void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_t func){
  202. device->input_pitchbend_callback = func;
  203. }
  204. void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_func_t func){
  205. device->input_songposition_callback = func;
  206. }
  207. void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t func) {
  208. device->input_progchange_callback = func;
  209. }
  210. void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func_t func) {
  211. device->input_chanpressure_callback = func;
  212. }
  213. void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t func) {
  214. device->input_songselect_callback = func;
  215. }
  216. void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_func_t func) {
  217. device->input_tc_quarterframe_callback = func;
  218. }
  219. void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t func){
  220. device->input_realtime_callback = func;
  221. }
  222. void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_t func){
  223. device->input_tunerequest_callback = func;
  224. }
  225. void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func) {
  226. device->input_sysex_callback = func;
  227. }
  228. void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_t func){
  229. device->input_fallthrough_callback = func;
  230. }
  231. void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t func){
  232. device->input_catchall_callback = func;
  233. }