process_midi.c 7.8 KB

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