process_midi.c 8.0 KB

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