midi.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, MIDI_CC | (chan & MIDI_CHANMASK), num & 0x7F, val & 0x7F);
  71. }
  72. void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel) {
  73. // Note Data: Note Num, Note Velocity
  74. device->send_func(device, 3, MIDI_NOTEON | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F);
  75. }
  76. void midi_send_noteoff(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel) {
  77. // Note Data: Note Num, Note Velocity
  78. device->send_func(device, 3, MIDI_NOTEOFF | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F);
  79. }
  80. void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt) {
  81. device->send_func(device, 3, MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK), note_num & 0x7F, amt & 0x7F);
  82. }
  83. // XXX does this work right?
  84. // amt in range -0x2000, 0x1fff
  85. // uAmt should be in range..
  86. // 0x0000 to 0x3FFF
  87. void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt) {
  88. uint16_t uAmt;
  89. // check range
  90. if (amt > 0x1fff) {
  91. uAmt = 0x3FFF;
  92. } else if (amt < -0x2000) {
  93. uAmt = 0;
  94. } else {
  95. uAmt = amt + 0x2000;
  96. }
  97. device->send_func(device, 3, MIDI_PITCHBEND | (chan & MIDI_CHANMASK), uAmt & 0x7F, (uAmt >> 7) & 0x7F);
  98. }
  99. void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num) {
  100. device->send_func(device, 2, MIDI_PROGCHANGE | (chan & MIDI_CHANMASK), num & 0x7F, 0);
  101. }
  102. void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt) {
  103. device->send_func(device, 2, MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK), amt & 0x7F, 0);
  104. }
  105. void midi_send_clock(MidiDevice* device) {
  106. device->send_func(device, 1, MIDI_CLOCK, 0, 0);
  107. }
  108. void midi_send_tick(MidiDevice* device) {
  109. device->send_func(device, 1, MIDI_TICK, 0, 0);
  110. }
  111. void midi_send_start(MidiDevice* device) {
  112. device->send_func(device, 1, MIDI_START, 0, 0);
  113. }
  114. void midi_send_continue(MidiDevice* device) {
  115. device->send_func(device, 1, MIDI_CONTINUE, 0, 0);
  116. }
  117. void midi_send_stop(MidiDevice* device) {
  118. device->send_func(device, 1, MIDI_STOP, 0, 0);
  119. }
  120. void midi_send_activesense(MidiDevice* device) {
  121. device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0);
  122. }
  123. void midi_send_reset(MidiDevice* device) {
  124. device->send_func(device, 1, MIDI_RESET, 0, 0);
  125. }
  126. void midi_send_tcquarterframe(MidiDevice* device, uint8_t time) {
  127. device->send_func(device, 2, MIDI_TC_QUARTERFRAME, time & 0x7F, 0);
  128. }
  129. // XXX is this right?
  130. void midi_send_songposition(MidiDevice* device, uint16_t pos) {
  131. device->send_func(device, 3, MIDI_SONGPOSITION, pos & 0x7F, (pos >> 7) & 0x7F);
  132. }
  133. void midi_send_songselect(MidiDevice* device, uint8_t song) {
  134. device->send_func(device, 2, MIDI_SONGSELECT, song & 0x7F, 0);
  135. }
  136. void midi_send_tunerequest(MidiDevice* device) {
  137. device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0);
  138. }
  139. void midi_send_byte(MidiDevice* device, uint8_t b) {
  140. device->send_func(device, 1, b, 0, 0);
  141. }
  142. void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
  143. // ensure that the count passed along is always 3 or lower
  144. if (count > 3) {
  145. // TODO how to do this correctly?
  146. }
  147. device->send_func(device, count, byte0, byte1, byte2);
  148. }
  149. void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array) {
  150. uint16_t i;
  151. for (i = 0; i < count; i += 3) {
  152. uint8_t b[3] = {0, 0, 0};
  153. uint16_t to_send = count - i;
  154. to_send = (to_send > 3) ? 3 : to_send;
  155. memcpy(b, array + i, to_send);
  156. midi_send_data(device, to_send, b[0], b[1], b[2]);
  157. }
  158. }
  159. void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func) {
  160. device->input_cc_callback = func;
  161. }
  162. void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func) {
  163. device->input_noteon_callback = func;
  164. }
  165. void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func) {
  166. device->input_noteoff_callback = func;
  167. }
  168. void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func) {
  169. device->input_aftertouch_callback = func;
  170. }
  171. void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func) {
  172. device->input_pitchbend_callback = func;
  173. }
  174. void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func) {
  175. device->input_songposition_callback = func;
  176. }
  177. void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func) {
  178. device->input_progchange_callback = func;
  179. }
  180. void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func) {
  181. device->input_chanpressure_callback = func;
  182. }
  183. void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func) {
  184. device->input_songselect_callback = func;
  185. }
  186. void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func) {
  187. device->input_tc_quarterframe_callback = func;
  188. }
  189. void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func) {
  190. device->input_realtime_callback = func;
  191. }
  192. void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func) {
  193. device->input_tunerequest_callback = func;
  194. }
  195. void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func) {
  196. device->input_sysex_callback = func;
  197. }
  198. void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func) {
  199. device->input_fallthrough_callback = func;
  200. }
  201. void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func) {
  202. device->input_catchall_callback = func;
  203. }