qmk_midi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <LUFA/Drivers/USB/USB.h>
  2. #include "qmk_midi.h"
  3. #include "sysex_tools.h"
  4. #include "midi.h"
  5. #include "usb_descriptor.h"
  6. #include "process_midi.h"
  7. #if API_SYSEX_ENABLE
  8. # include "api_sysex.h"
  9. #endif
  10. /*******************************************************************************
  11. * MIDI
  12. ******************************************************************************/
  13. MidiDevice midi_device;
  14. #define SYSEX_START_OR_CONT 0x40
  15. #define SYSEX_ENDS_IN_1 0x50
  16. #define SYSEX_ENDS_IN_2 0x60
  17. #define SYSEX_ENDS_IN_3 0x70
  18. #define SYS_COMMON_1 0x50
  19. #define SYS_COMMON_2 0x20
  20. #define SYS_COMMON_3 0x30
  21. static void usb_send_func(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
  22. MIDI_EventPacket_t event;
  23. event.Data1 = byte0;
  24. event.Data2 = byte1;
  25. event.Data3 = byte2;
  26. uint8_t cable = 0;
  27. // if the length is undefined we assume it is a SYSEX message
  28. if (midi_packet_length(byte0) == UNDEFINED) {
  29. switch (cnt) {
  30. case 3:
  31. if (byte2 == SYSEX_END)
  32. event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_3);
  33. else
  34. event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
  35. break;
  36. case 2:
  37. if (byte1 == SYSEX_END)
  38. event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_2);
  39. else
  40. event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
  41. break;
  42. case 1:
  43. if (byte0 == SYSEX_END)
  44. event.Event = MIDI_EVENT(cable, SYSEX_ENDS_IN_1);
  45. else
  46. event.Event = MIDI_EVENT(cable, SYSEX_START_OR_CONT);
  47. break;
  48. default:
  49. return; // invalid cnt
  50. }
  51. } else {
  52. // deal with 'system common' messages
  53. // TODO are there any more?
  54. switch (byte0 & 0xF0) {
  55. case MIDI_SONGPOSITION:
  56. event.Event = MIDI_EVENT(cable, SYS_COMMON_3);
  57. break;
  58. case MIDI_SONGSELECT:
  59. case MIDI_TC_QUARTERFRAME:
  60. event.Event = MIDI_EVENT(cable, SYS_COMMON_2);
  61. break;
  62. default:
  63. event.Event = MIDI_EVENT(cable, byte0);
  64. break;
  65. }
  66. }
  67. send_midi_packet(&event);
  68. }
  69. static void usb_get_midi(MidiDevice* device) {
  70. MIDI_EventPacket_t event;
  71. while (recv_midi_packet(&event)) {
  72. midi_packet_length_t length = midi_packet_length(event.Data1);
  73. uint8_t input[3];
  74. input[0] = event.Data1;
  75. input[1] = event.Data2;
  76. input[2] = event.Data3;
  77. if (length == UNDEFINED) {
  78. // sysex
  79. if (event.Event == MIDI_EVENT(0, SYSEX_START_OR_CONT) || event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_3)) {
  80. length = 3;
  81. } else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_2)) {
  82. length = 2;
  83. } else if (event.Event == MIDI_EVENT(0, SYSEX_ENDS_IN_1)) {
  84. length = 1;
  85. } else {
  86. // XXX what to do?
  87. }
  88. }
  89. // pass the data to the device input function
  90. if (length != UNDEFINED) midi_device_input(device, length, input);
  91. }
  92. }
  93. static void fallthrough_callback(MidiDevice* device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) {
  94. #ifdef AUDIO_ENABLE
  95. if (cnt == 3) {
  96. switch (byte0 & 0xF0) {
  97. case MIDI_NOTEON:
  98. play_note(((double)261.6) * pow(2.0, -4.0) * pow(2.0, (byte1 & 0x7F) / 12.0), (byte2 & 0x7F) / 8);
  99. break;
  100. case MIDI_NOTEOFF:
  101. stop_note(((double)261.6) * pow(2.0, -4.0) * pow(2.0, (byte1 & 0x7F) / 12.0));
  102. break;
  103. }
  104. }
  105. if (byte0 == MIDI_STOP) {
  106. stop_all_notes();
  107. }
  108. #endif
  109. }
  110. static void cc_callback(MidiDevice* device, uint8_t chan, uint8_t num, uint8_t val) {
  111. // sending it back on the next channel
  112. // midi_send_cc(device, (chan + 1) % 16, num, val);
  113. }
  114. #ifdef API_SYSEX_ENABLE
  115. uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
  116. static void sysex_callback(MidiDevice* device, uint16_t start, uint8_t length, uint8_t* data) {
  117. // SEND_STRING("\n");
  118. // send_word(start);
  119. // SEND_STRING(": ");
  120. // Don't store the header
  121. int16_t pos = start - 4;
  122. for (uint8_t place = 0; place < length; place++) {
  123. // send_byte(*data);
  124. if (pos >= 0) {
  125. if (*data == 0xF7) {
  126. // SEND_STRING("\nRD: ");
  127. // for (uint8_t i = 0; i < start + place + 1; i++){
  128. // send_byte(midi_buffer[i]);
  129. // SEND_STRING(" ");
  130. // }
  131. const unsigned decoded_length = sysex_decoded_length(pos);
  132. uint8_t decoded[API_SYSEX_MAX_SIZE];
  133. sysex_decode(decoded, midi_buffer, pos);
  134. process_api(decoded_length, decoded);
  135. return;
  136. } else if (pos >= MIDI_SYSEX_BUFFER) {
  137. return;
  138. }
  139. midi_buffer[pos] = *data;
  140. }
  141. // SEND_STRING(" ");
  142. data++;
  143. pos++;
  144. }
  145. }
  146. #endif
  147. void midi_init(void);
  148. void setup_midi(void) {
  149. #ifdef MIDI_ADVANCED
  150. midi_init();
  151. #endif
  152. midi_device_init(&midi_device);
  153. midi_device_set_send_func(&midi_device, usb_send_func);
  154. midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
  155. midi_register_fallthrough_callback(&midi_device, fallthrough_callback);
  156. midi_register_cc_callback(&midi_device, cc_callback);
  157. #ifdef API_SYSEX_ENABLE
  158. midi_register_sysex_callback(&midi_device, sysex_callback);
  159. #endif
  160. }