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.

247 lines
7.8 KiB

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