process_midi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "process_midi.h"
  2. #if 0
  3. bool midi_activated = false;
  4. uint8_t midi_starting_note = 0x0C;
  5. int midi_offset = 7;
  6. #endif
  7. typedef union {
  8. uint16_t raw;
  9. struct {
  10. uint8_t octave :4;
  11. uint8_t velocity :4;
  12. uint8_t channel :4;
  13. };
  14. } midi_config_t;
  15. midi_config_t midi_config;
  16. #define MIDI_INVALID_NOTE 0xFF
  17. #define MIDI_USE_NOTE_ON_ARRAY
  18. #ifdef MIDI_USE_NOTE_ON_ARRAY
  19. #define MIDI_MAX_NOTES_ON 10
  20. typedef struct {
  21. uint8_t note;
  22. uint8_t tone;
  23. } midi_notes_on_array_entry_t;
  24. typedef struct {
  25. uint8_t length;
  26. midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
  27. } midi_notes_on_array_t;
  28. static midi_notes_on_array_t notes_on;
  29. #else
  30. #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
  31. static uint8_t tone_status[MIDI_TONE_COUNT];
  32. #endif
  33. inline uint8_t compute_velocity(uint8_t setting)
  34. {
  35. return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
  36. }
  37. void midi_init(void)
  38. {
  39. midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
  40. midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
  41. midi_config.channel = 0;
  42. #ifdef MIDI_USE_NOTE_ON_ARRAY
  43. notes_on.length = 0;
  44. #else
  45. for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
  46. {
  47. tone_status[i] = MIDI_INVALID_NOTE;
  48. }
  49. #endif
  50. }
  51. bool process_midi(uint16_t keycode, keyrecord_t *record)
  52. {
  53. switch (keycode) {
  54. case MIDI_TONE_MIN ... MIDI_TONE_MAX:
  55. {
  56. uint8_t channel = midi_config.channel;
  57. uint8_t tone = keycode - MIDI_TONE_MIN;
  58. uint8_t velocity = compute_velocity(midi_config.velocity);
  59. #ifdef MIDI_USE_NOTE_ON_ARRAY
  60. if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
  61. #else
  62. if (record->event.pressed) {
  63. #endif
  64. uint8_t note = 12 * midi_config.octave + tone;
  65. midi_send_noteon(&midi_device, channel, note, velocity);
  66. dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
  67. #ifdef MIDI_USE_NOTE_ON_ARRAY
  68. notes_on.values[notes_on.length].note = note;
  69. notes_on.values[notes_on.length].tone = tone;
  70. notes_on.length++;
  71. #else
  72. tone_status[tone] = note;
  73. #endif
  74. }
  75. else {
  76. #ifdef MIDI_USE_NOTE_ON_ARRAY
  77. for (uint8_t i = 0; i < notes_on.length; i++) {
  78. uint8_t note = notes_on.values[i].note;
  79. if (tone == notes_on.values[i].tone) {
  80. midi_send_noteoff(&midi_device, channel, note, velocity);
  81. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  82. for (uint8_t j=i; j < notes_on.length - 1; j++)
  83. {
  84. notes_on.values[j] = notes_on.values[j + 1];
  85. }
  86. notes_on.length--;
  87. break;
  88. }
  89. }
  90. #else
  91. uint8_t note = tone_status[tone];
  92. if (note != MIDI_INVALID_NOTE)
  93. {
  94. midi_send_noteoff(&midi_device, channel, note, velocity);
  95. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  96. }
  97. tone_status[tone] = MIDI_INVALID_NOTE;
  98. #endif
  99. }
  100. return false;
  101. }
  102. case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
  103. if (record->event.pressed)
  104. midi_config.octave = keycode - MIDI_OCTAVE_MIN;
  105. return false;
  106. case MI_OCTD:
  107. if (record->event.pressed && midi_config.octave > 0)
  108. midi_config.octave--;
  109. return false;
  110. case MI_OCTU:
  111. if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
  112. midi_config.octave++;
  113. return false;
  114. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  115. if (record->event.pressed)
  116. midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
  117. return false;
  118. case MI_VELD:
  119. if (record->event.pressed && midi_config.velocity > 0)
  120. midi_config.velocity--;
  121. return false;
  122. case MI_VELU:
  123. if (record->event.pressed)
  124. midi_config.velocity++;
  125. return false;
  126. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  127. if (record->event.pressed)
  128. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  129. return false;
  130. case MI_CHD:
  131. if (record->event.pressed)
  132. midi_config.channel--;
  133. return false;
  134. case MI_CHU:
  135. if (record->event.pressed)
  136. midi_config.channel++;
  137. return false;
  138. case MI_SUS:
  139. //TODO
  140. return false;
  141. };
  142. #if 0
  143. if (keycode == MI_ON && record->event.pressed) {
  144. midi_activated = true;
  145. #ifdef AUDIO_ENABLE
  146. music_scale_user();
  147. #endif
  148. return false;
  149. }
  150. if (keycode == MI_OFF && record->event.pressed) {
  151. midi_activated = false;
  152. midi_send_cc(&midi_device, 0, 0x7B, 0);
  153. return false;
  154. }
  155. if (midi_activated) {
  156. if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
  157. if (record->event.pressed) {
  158. midi_starting_note++; // Change key
  159. midi_send_cc(&midi_device, 0, 0x7B, 0);
  160. }
  161. return false;
  162. }
  163. if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
  164. if (record->event.pressed) {
  165. midi_starting_note--; // Change key
  166. midi_send_cc(&midi_device, 0, 0x7B, 0);
  167. }
  168. return false;
  169. }
  170. if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  171. midi_offset++; // Change scale
  172. midi_send_cc(&midi_device, 0, 0x7B, 0);
  173. return false;
  174. }
  175. if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  176. midi_offset--; // Change scale
  177. midi_send_cc(&midi_device, 0, 0x7B, 0);
  178. return false;
  179. }
  180. // basic
  181. // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
  182. // advanced
  183. // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
  184. // guitar
  185. uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
  186. // violin
  187. // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
  188. if (record->event.pressed) {
  189. // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
  190. midi_send_noteon(&midi_device, 0, note, 127);
  191. } else {
  192. // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
  193. midi_send_noteoff(&midi_device, 0, note, 127);
  194. }
  195. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  196. return false;
  197. }
  198. #endif
  199. return true;
  200. }