process_midi.c 7.7 KB

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