midi.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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) { return (bool)(theByte & MIDI_STATUSMASK); }
  25. bool midi_is_realtime(uint8_t theByte) { return (theByte >= MIDI_CLOCK); }
  26. midi_packet_length_t midi_packet_length(uint8_t status) {
  27. switch (status & 0xF0) {
  28. case MIDI_CC:
  29. case MIDI_NOTEON:
  30. case MIDI_NOTEOFF:
  31. case MIDI_AFTERTOUCH:
  32. case MIDI_PITCHBEND:
  33. return THREE;
  34. case MIDI_PROGCHANGE:
  35. case MIDI_CHANPRESSURE:
  36. case MIDI_SONGSELECT:
  37. return TWO;
  38. case 0xF0:
  39. switch (status) {
  40. case MIDI_CLOCK:
  41. case MIDI_TICK:
  42. case MIDI_START:
  43. case MIDI_CONTINUE:
  44. case MIDI_STOP:
  45. case MIDI_ACTIVESENSE:
  46. case MIDI_RESET:
  47. case MIDI_TUNEREQUEST:
  48. return ONE;
  49. case MIDI_SONGPOSITION:
  50. return THREE;
  51. case MIDI_TC_QUARTERFRAME:
  52. case MIDI_SONGSELECT:
  53. return TWO;
  54. case SYSEX_END:
  55. case SYSEX_BEGIN:
  56. default:
  57. return UNDEFINED;
  58. }
  59. default:
  60. return UNDEFINED;
  61. }
  62. }
  63. void midi_send_cc(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val) {
  64. // CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel.
  65. // CC Data: Controller Num, Controller Val
  66. device->send_func(device, 3, MIDI_CC | (chan & MIDI_CHANMASK), num & 0x7F, val & 0x7F);
  67. }
  68. void midi_send_noteon(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t vel) {
  69. // Note Data: Note Num, Note Velocity
  70. device->send_func(device, 3, MIDI_NOTEON | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F);
  71. }
  72. void midi_send_noteoff(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_NOTEOFF | (chan & MIDI_CHANMASK), num & 0x7F, vel & 0x7F);
  75. }
  76. void midi_send_aftertouch(MidiDevice* device, uint8_t chan, uint8_t note_num, uint8_t amt) { device->send_func(device, 3, MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK), note_num & 0x7F, amt & 0x7F); }
  77. // XXX does this work right?
  78. // amt in range -0x2000, 0x1fff
  79. // uAmt should be in range..
  80. // 0x0000 to 0x3FFF
  81. void midi_send_pitchbend(MidiDevice* device, uint8_t chan, int16_t amt) {
  82. uint16_t uAmt;
  83. // check range
  84. if (amt > 0x1fff) {
  85. uAmt = 0x3FFF;
  86. } else if (amt < -0x2000) {
  87. uAmt = 0;
  88. } else {
  89. uAmt = amt + 0x2000;
  90. }
  91. device->send_func(device, 3, MIDI_PITCHBEND | (chan & MIDI_CHANMASK), uAmt & 0x7F, (uAmt >> 7) & 0x7F);
  92. }
  93. void midi_send_programchange(MidiDevice* device, uint8_t chan, uint8_t num) { device->send_func(device, 2, MIDI_PROGCHANGE | (chan & MIDI_CHANMASK), num & 0x7F, 0); }
  94. void midi_send_channelpressure(MidiDevice* device, uint8_t chan, uint8_t amt) { device->send_func(device, 2, MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK), amt & 0x7F, 0); }
  95. void midi_send_clock(MidiDevice* device) { device->send_func(device, 1, MIDI_CLOCK, 0, 0); }
  96. void midi_send_tick(MidiDevice* device) { device->send_func(device, 1, MIDI_TICK, 0, 0); }
  97. void midi_send_start(MidiDevice* device) { device->send_func(device, 1, MIDI_START, 0, 0); }
  98. void midi_send_continue(MidiDevice* device) { device->send_func(device, 1, MIDI_CONTINUE, 0, 0); }
  99. void midi_send_stop(MidiDevice* device) { device->send_func(device, 1, MIDI_STOP, 0, 0); }
  100. void midi_send_activesense(MidiDevice* device) { device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0); }
  101. void midi_send_reset(MidiDevice* device) { device->send_func(device, 1, MIDI_RESET, 0, 0); }
  102. void midi_send_tcquarterframe(MidiDevice* device, uint8_t time) { device->send_func(device, 2, MIDI_TC_QUARTERFRAME, time & 0x7F, 0); }
  103. // XXX is this right?
  104. void midi_send_songposition(MidiDevice* device, uint16_t pos) { device->send_func(device, 3, MIDI_SONGPOSITION, pos & 0x7F, (pos >> 7) & 0x7F); }
  105. void midi_send_songselect(MidiDevice* device, uint8_t song) { device->send_func(device, 2, MIDI_SONGSELECT, song & 0x7F, 0); }
  106. void midi_send_tunerequest(MidiDevice* device) { device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0); }
  107. void midi_send_byte(MidiDevice* device, uint8_t b) { device->send_func(device, 1, b, 0, 0); }
  108. void midi_send_data(MidiDevice* device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
  109. // ensure that the count passed along is always 3 or lower
  110. if (count > 3) {
  111. // TODO how to do this correctly?
  112. }
  113. device->send_func(device, count, byte0, byte1, byte2);
  114. }
  115. void midi_send_array(MidiDevice* device, uint16_t count, uint8_t* array) {
  116. uint16_t i;
  117. for (i = 0; i < count; i += 3) {
  118. uint8_t b[3] = {0, 0, 0};
  119. uint16_t to_send = count - i;
  120. to_send = (to_send > 3) ? 3 : to_send;
  121. memcpy(b, array + i, to_send);
  122. midi_send_data(device, to_send, b[0], b[1], b[2]);
  123. }
  124. }
  125. void midi_register_cc_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_cc_callback = func; }
  126. void midi_register_noteon_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_noteon_callback = func; }
  127. void midi_register_noteoff_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_noteoff_callback = func; }
  128. void midi_register_aftertouch_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_aftertouch_callback = func; }
  129. void midi_register_pitchbend_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_pitchbend_callback = func; }
  130. void midi_register_songposition_callback(MidiDevice* device, midi_three_byte_func_t func) { device->input_songposition_callback = func; }
  131. void midi_register_progchange_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_progchange_callback = func; }
  132. void midi_register_chanpressure_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_chanpressure_callback = func; }
  133. void midi_register_songselect_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_songselect_callback = func; }
  134. void midi_register_tc_quarterframe_callback(MidiDevice* device, midi_two_byte_func_t func) { device->input_tc_quarterframe_callback = func; }
  135. void midi_register_realtime_callback(MidiDevice* device, midi_one_byte_func_t func) { device->input_realtime_callback = func; }
  136. void midi_register_tunerequest_callback(MidiDevice* device, midi_one_byte_func_t func) { device->input_tunerequest_callback = func; }
  137. void midi_register_sysex_callback(MidiDevice* device, midi_sysex_func_t func) { device->input_sysex_callback = func; }
  138. void midi_register_fallthrough_callback(MidiDevice* device, midi_var_byte_func_t func) { device->input_fallthrough_callback = func; }
  139. void midi_register_catchall_callback(MidiDevice* device, midi_var_byte_func_t func) { device->input_catchall_callback = func; }