process_midi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #if 0
  18. typedef struct {
  19. uint64_t low;
  20. uint64_t high;
  21. } uint128_t;
  22. #if 0
  23. static void right_shift_uint128_t(uint128_t* val, uint8_t shift)
  24. {
  25. uint64_t high_mask = ~0 >> (64 - shift);
  26. uint64_t high_bits = (val->high & high_mask) << (64 - shift);
  27. val->high = val->high >> shift;
  28. val->low = (val->low >> shift) | high_bits;
  29. }
  30. #endif
  31. static uint64_t left_shift_uint64_t(uint64_t val, uint8_t shift)
  32. {
  33. dprintf("left_shift_uint64_t(val, %c) ...\n", val, shift);
  34. while (shift > 16u) {
  35. dprintf(" left_shift_uint64_t: val=?, shift=%c\n", val, shift);
  36. val <<= 16;
  37. shift -= 16;
  38. }
  39. dprintf(" left_shift_uint64_t: val=?, shift=%c\n", val, shift);
  40. val <<= shift;
  41. return val;
  42. }
  43. static void set_bit_uint128_t(uint128_t* val, uint8_t shift)
  44. {
  45. uint64_t x = 1u;
  46. if (shift < 64)
  47. {
  48. x = left_shift_uint64_t(x, shift);
  49. dprintf("x: %d\n", x);
  50. dprintf("set_bit_uint128_t (%d): 0x%016X%016X\n", shift, 0, x);
  51. val->low = val->low | left_shift_uint64_t(1u, shift);
  52. }
  53. else
  54. {
  55. x = left_shift_uint64_t(x, shift - 64);
  56. dprintf("set_bit_uint128_t (%d): 0x%016X%016X\n", shift, x, 0);
  57. val->high = val->high | left_shift_uint64_t(1u, shift - 64);
  58. }
  59. }
  60. static void clear_bit_uint128_t(uint128_t* val, uint8_t shift)
  61. {
  62. if (shift < 64)
  63. {
  64. val->low = val->low & ~left_shift_uint64_t(1u, shift);
  65. }
  66. else
  67. {
  68. val->high = val->high & ~left_shift_uint64_t(1u, shift - 64);
  69. }
  70. }
  71. static bool is_bit_set_uint128_t(const uint128_t* val, uint8_t shift)
  72. {
  73. if (shift < 64)
  74. {
  75. return !!(val->low & (1u << shift));
  76. }
  77. else
  78. {
  79. return !!(val->high & (1u << (shift - 64)));
  80. }
  81. }
  82. uint128_t note_status = { 0, 0 };
  83. #endif
  84. #define MIDI_MAX_NOTES_ON 10
  85. typedef struct {
  86. uint8_t note;
  87. uint8_t tone;
  88. } midi_notes_on_array_entry_t;
  89. typedef struct {
  90. uint8_t length;
  91. midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
  92. } midi_notes_on_array_t;
  93. static midi_notes_on_array_t notes_on;
  94. inline uint8_t compute_velocity(uint8_t setting)
  95. {
  96. return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
  97. }
  98. void midi_init(void)
  99. {
  100. midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
  101. midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
  102. midi_config.channel = 0;
  103. notes_on.length = 0;
  104. }
  105. bool process_midi(uint16_t keycode, keyrecord_t *record)
  106. {
  107. switch (keycode) {
  108. case MIDI_TONE_MIN ... MIDI_TONE_MAX:
  109. {
  110. uint8_t channel = midi_config.channel;
  111. uint8_t tone = keycode - MIDI_TONE_MIN;
  112. uint8_t velocity = compute_velocity(midi_config.velocity);
  113. if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
  114. uint8_t note = 12 * midi_config.octave + tone;
  115. midi_send_noteon(&midi_device, channel, note, velocity);
  116. dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
  117. notes_on.values[notes_on.length].note = note;
  118. notes_on.values[notes_on.length].tone = tone;
  119. notes_on.length++;
  120. }
  121. else {
  122. for (uint8_t i = 0; i < notes_on.length; i++) {
  123. uint8_t note = notes_on.values[i].note;
  124. if (tone == notes_on.values[i].tone) {
  125. midi_send_noteoff(&midi_device, channel, note, velocity);
  126. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  127. for (uint8_t j=i; j < notes_on.length - 1; j++)
  128. {
  129. notes_on.values[j] = notes_on.values[j + 1];
  130. }
  131. notes_on.length--;
  132. break;
  133. }
  134. }
  135. }
  136. return false;
  137. }
  138. case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
  139. if (record->event.pressed)
  140. midi_config.octave = keycode - MIDI_OCTAVE_MIN;
  141. return false;
  142. case MI_OCTD:
  143. if (record->event.pressed && midi_config.octave > 0)
  144. midi_config.octave--;
  145. return false;
  146. case MI_OCTU:
  147. if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
  148. midi_config.octave++;
  149. return false;
  150. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  151. if (record->event.pressed)
  152. midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
  153. return false;
  154. case MI_VELD:
  155. if (record->event.pressed && midi_config.velocity > 0)
  156. midi_config.velocity--;
  157. return false;
  158. case MI_VELU:
  159. if (record->event.pressed)
  160. midi_config.velocity++;
  161. return false;
  162. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  163. if (record->event.pressed)
  164. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  165. return false;
  166. case MI_CHD:
  167. if (record->event.pressed)
  168. midi_config.channel--;
  169. return false;
  170. case MI_CHU:
  171. if (record->event.pressed)
  172. midi_config.channel++;
  173. return false;
  174. case MI_SUS:
  175. //TODO
  176. return false;
  177. };
  178. #if 0
  179. if (keycode == MI_ON && record->event.pressed) {
  180. midi_activated = true;
  181. #ifdef AUDIO_ENABLE
  182. music_scale_user();
  183. #endif
  184. return false;
  185. }
  186. if (keycode == MI_OFF && record->event.pressed) {
  187. midi_activated = false;
  188. midi_send_cc(&midi_device, 0, 0x7B, 0);
  189. return false;
  190. }
  191. if (midi_activated) {
  192. if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
  193. if (record->event.pressed) {
  194. midi_starting_note++; // Change key
  195. midi_send_cc(&midi_device, 0, 0x7B, 0);
  196. }
  197. return false;
  198. }
  199. if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
  200. if (record->event.pressed) {
  201. midi_starting_note--; // Change key
  202. midi_send_cc(&midi_device, 0, 0x7B, 0);
  203. }
  204. return false;
  205. }
  206. if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  207. midi_offset++; // Change scale
  208. midi_send_cc(&midi_device, 0, 0x7B, 0);
  209. return false;
  210. }
  211. if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  212. midi_offset--; // Change scale
  213. midi_send_cc(&midi_device, 0, 0x7B, 0);
  214. return false;
  215. }
  216. // basic
  217. // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
  218. // advanced
  219. // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
  220. // guitar
  221. uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
  222. // violin
  223. // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
  224. if (record->event.pressed) {
  225. // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
  226. midi_send_noteon(&midi_device, 0, note, 127);
  227. } else {
  228. // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
  229. midi_send_noteoff(&midi_device, 0, note, 127);
  230. }
  231. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  232. return false;
  233. }
  234. #endif
  235. return true;
  236. }