process_midi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "process_midi.h"
  2. #ifdef MIDI_ENABLE
  3. #include "midi.h"
  4. #ifdef MIDI_BASIC
  5. void process_midi_basic_noteon(uint8_t note)
  6. {
  7. midi_send_noteon(&midi_device, 0, note, 128);
  8. }
  9. void process_midi_basic_noteoff(uint8_t note)
  10. {
  11. midi_send_noteoff(&midi_device, 0, note, 0);
  12. }
  13. void process_midi_all_notes_off(void)
  14. {
  15. midi_send_cc(&midi_device, 0, 0x7B, 0);
  16. }
  17. #endif // MIDI_BASIC
  18. #ifdef MIDI_ADVANCED
  19. #include "timer.h"
  20. static uint8_t tone_status[MIDI_TONE_COUNT];
  21. static uint8_t midi_modulation;
  22. static int8_t midi_modulation_step;
  23. static uint16_t midi_modulation_timer;
  24. inline uint8_t compute_velocity(uint8_t setting)
  25. {
  26. return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
  27. }
  28. void midi_init(void)
  29. {
  30. midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
  31. midi_config.transpose = 0;
  32. midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
  33. midi_config.channel = 0;
  34. midi_config.modulation_interval = 8;
  35. for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
  36. {
  37. tone_status[i] = MIDI_INVALID_NOTE;
  38. }
  39. midi_modulation = 0;
  40. midi_modulation_step = 0;
  41. midi_modulation_timer = 0;
  42. }
  43. void midi_task(void)
  44. {
  45. if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
  46. return;
  47. midi_modulation_timer = timer_read();
  48. if (midi_modulation_step != 0)
  49. {
  50. dprintf("midi modulation %d\n", midi_modulation);
  51. midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
  52. if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
  53. midi_modulation = 0;
  54. midi_modulation_step = 0;
  55. return;
  56. }
  57. midi_modulation += midi_modulation_step;
  58. if (midi_modulation > 127)
  59. midi_modulation = 127;
  60. }
  61. }
  62. uint8_t midi_compute_note(uint16_t keycode)
  63. {
  64. return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
  65. }
  66. bool process_midi(uint16_t keycode, keyrecord_t *record)
  67. {
  68. switch (keycode) {
  69. case MIDI_TONE_MIN ... MIDI_TONE_MAX:
  70. {
  71. uint8_t channel = midi_config.channel;
  72. uint8_t tone = keycode - MIDI_TONE_MIN;
  73. uint8_t velocity = compute_velocity(midi_config.velocity);
  74. if (record->event.pressed) {
  75. uint8_t note = midi_compute_note(keycode);
  76. midi_send_noteon(&midi_device, channel, note, velocity);
  77. dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
  78. tone_status[tone] = note;
  79. }
  80. else {
  81. uint8_t note = tone_status[tone];
  82. if (note != MIDI_INVALID_NOTE)
  83. {
  84. midi_send_noteoff(&midi_device, channel, note, velocity);
  85. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  86. }
  87. tone_status[tone] = MIDI_INVALID_NOTE;
  88. }
  89. return false;
  90. }
  91. case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
  92. if (record->event.pressed) {
  93. midi_config.octave = keycode - MIDI_OCTAVE_MIN;
  94. dprintf("midi octave %d\n", midi_config.octave);
  95. }
  96. return false;
  97. case MI_OCTD:
  98. if (record->event.pressed && midi_config.octave > 0) {
  99. midi_config.octave--;
  100. dprintf("midi octave %d\n", midi_config.octave);
  101. }
  102. return false;
  103. case MI_OCTU:
  104. if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
  105. midi_config.octave++;
  106. dprintf("midi octave %d\n", midi_config.octave);
  107. }
  108. return false;
  109. case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
  110. if (record->event.pressed) {
  111. midi_config.transpose = keycode - MI_TRNS_0;
  112. dprintf("midi transpose %d\n", midi_config.transpose);
  113. }
  114. return false;
  115. case MI_TRNSD:
  116. if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
  117. midi_config.transpose--;
  118. dprintf("midi transpose %d\n", midi_config.transpose);
  119. }
  120. return false;
  121. case MI_TRNSU:
  122. if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
  123. const bool positive = midi_config.transpose > 0;
  124. midi_config.transpose++;
  125. if (positive && midi_config.transpose < 0)
  126. midi_config.transpose--;
  127. dprintf("midi transpose %d\n", midi_config.transpose);
  128. }
  129. return false;
  130. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  131. if (record->event.pressed) {
  132. midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
  133. dprintf("midi velocity %d\n", midi_config.velocity);
  134. }
  135. return false;
  136. case MI_VELD:
  137. if (record->event.pressed && midi_config.velocity > 0) {
  138. midi_config.velocity--;
  139. dprintf("midi velocity %d\n", midi_config.velocity);
  140. }
  141. return false;
  142. case MI_VELU:
  143. if (record->event.pressed) {
  144. midi_config.velocity++;
  145. dprintf("midi velocity %d\n", midi_config.velocity);
  146. }
  147. return false;
  148. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  149. if (record->event.pressed) {
  150. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  151. dprintf("midi channel %d\n", midi_config.channel);
  152. }
  153. return false;
  154. case MI_CHD:
  155. if (record->event.pressed) {
  156. midi_config.channel--;
  157. dprintf("midi channel %d\n", midi_config.channel);
  158. }
  159. return false;
  160. case MI_CHU:
  161. if (record->event.pressed) {
  162. midi_config.channel++;
  163. dprintf("midi channel %d\n", midi_config.channel);
  164. }
  165. return false;
  166. case MI_ALLOFF:
  167. if (record->event.pressed) {
  168. midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
  169. dprintf("midi all notes off\n");
  170. }
  171. return false;
  172. case MI_SUS:
  173. midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
  174. dprintf("midi sustain %d\n", record->event.pressed);
  175. return false;
  176. case MI_PORT:
  177. midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
  178. dprintf("midi portamento %d\n", record->event.pressed);
  179. return false;
  180. case MI_SOST:
  181. midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
  182. dprintf("midi sostenuto %d\n", record->event.pressed);
  183. return false;
  184. case MI_SOFT:
  185. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  186. dprintf("midi soft %d\n", record->event.pressed);
  187. return false;
  188. case MI_LEG:
  189. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  190. dprintf("midi legato %d\n", record->event.pressed);
  191. return false;
  192. case MI_MOD:
  193. midi_modulation_step = record->event.pressed ? 1 : -1;
  194. return false;
  195. case MI_MODSD:
  196. if (record->event.pressed) {
  197. midi_config.modulation_interval++;
  198. // prevent overflow
  199. if (midi_config.modulation_interval == 0)
  200. midi_config.modulation_interval--;
  201. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  202. }
  203. return false;
  204. case MI_MODSU:
  205. if (record->event.pressed && midi_config.modulation_interval > 0) {
  206. midi_config.modulation_interval--;
  207. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  208. }
  209. return false;
  210. };
  211. return true;
  212. }
  213. #endif // MIDI_ADVANCED
  214. #endif // MIDI_ENABLE