process_midi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright 2016 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "process_midi.h"
  17. #ifdef MIDI_ENABLE
  18. #include "midi.h"
  19. #ifdef MIDI_BASIC
  20. void process_midi_basic_noteon(uint8_t note)
  21. {
  22. midi_send_noteon(&midi_device, 0, note, 128);
  23. }
  24. void process_midi_basic_noteoff(uint8_t note)
  25. {
  26. midi_send_noteoff(&midi_device, 0, note, 0);
  27. }
  28. void process_midi_all_notes_off(void)
  29. {
  30. midi_send_cc(&midi_device, 0, 0x7B, 0);
  31. }
  32. #endif // MIDI_BASIC
  33. #ifdef MIDI_ADVANCED
  34. #include "timer.h"
  35. static uint8_t tone_status[MIDI_TONE_COUNT];
  36. static uint8_t midi_modulation;
  37. static int8_t midi_modulation_step;
  38. static uint16_t midi_modulation_timer;
  39. inline uint8_t compute_velocity(uint8_t setting)
  40. {
  41. return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
  42. }
  43. void midi_init(void)
  44. {
  45. midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
  46. midi_config.transpose = 0;
  47. midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
  48. midi_config.channel = 0;
  49. midi_config.modulation_interval = 8;
  50. for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
  51. {
  52. tone_status[i] = MIDI_INVALID_NOTE;
  53. }
  54. midi_modulation = 0;
  55. midi_modulation_step = 0;
  56. midi_modulation_timer = 0;
  57. }
  58. void midi_task(void)
  59. {
  60. if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
  61. return;
  62. midi_modulation_timer = timer_read();
  63. if (midi_modulation_step != 0)
  64. {
  65. dprintf("midi modulation %d\n", midi_modulation);
  66. midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
  67. if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
  68. midi_modulation = 0;
  69. midi_modulation_step = 0;
  70. return;
  71. }
  72. midi_modulation += midi_modulation_step;
  73. if (midi_modulation > 127)
  74. midi_modulation = 127;
  75. }
  76. }
  77. uint8_t midi_compute_note(uint16_t keycode)
  78. {
  79. return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
  80. }
  81. bool process_midi(uint16_t keycode, keyrecord_t *record)
  82. {
  83. switch (keycode) {
  84. case MIDI_TONE_MIN ... MIDI_TONE_MAX:
  85. {
  86. uint8_t channel = midi_config.channel;
  87. uint8_t tone = keycode - MIDI_TONE_MIN;
  88. uint8_t velocity = compute_velocity(midi_config.velocity);
  89. if (record->event.pressed) {
  90. uint8_t note = midi_compute_note(keycode);
  91. midi_send_noteon(&midi_device, channel, note, velocity);
  92. dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
  93. tone_status[tone] = note;
  94. }
  95. else {
  96. uint8_t note = tone_status[tone];
  97. if (note != MIDI_INVALID_NOTE)
  98. {
  99. midi_send_noteoff(&midi_device, channel, note, velocity);
  100. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  101. }
  102. tone_status[tone] = MIDI_INVALID_NOTE;
  103. }
  104. return false;
  105. }
  106. case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
  107. if (record->event.pressed) {
  108. midi_config.octave = keycode - MIDI_OCTAVE_MIN;
  109. dprintf("midi octave %d\n", midi_config.octave);
  110. }
  111. return false;
  112. case MI_OCTD:
  113. if (record->event.pressed && midi_config.octave > 0) {
  114. midi_config.octave--;
  115. dprintf("midi octave %d\n", midi_config.octave);
  116. }
  117. return false;
  118. case MI_OCTU:
  119. if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
  120. midi_config.octave++;
  121. dprintf("midi octave %d\n", midi_config.octave);
  122. }
  123. return false;
  124. case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
  125. if (record->event.pressed) {
  126. midi_config.transpose = keycode - MI_TRNS_0;
  127. dprintf("midi transpose %d\n", midi_config.transpose);
  128. }
  129. return false;
  130. case MI_TRNSD:
  131. if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
  132. midi_config.transpose--;
  133. dprintf("midi transpose %d\n", midi_config.transpose);
  134. }
  135. return false;
  136. case MI_TRNSU:
  137. if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
  138. const bool positive = midi_config.transpose > 0;
  139. midi_config.transpose++;
  140. if (positive && midi_config.transpose < 0)
  141. midi_config.transpose--;
  142. dprintf("midi transpose %d\n", midi_config.transpose);
  143. }
  144. return false;
  145. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  146. if (record->event.pressed) {
  147. midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
  148. dprintf("midi velocity %d\n", midi_config.velocity);
  149. }
  150. return false;
  151. case MI_VELD:
  152. if (record->event.pressed && midi_config.velocity > 0) {
  153. midi_config.velocity--;
  154. dprintf("midi velocity %d\n", midi_config.velocity);
  155. }
  156. return false;
  157. case MI_VELU:
  158. if (record->event.pressed) {
  159. midi_config.velocity++;
  160. dprintf("midi velocity %d\n", midi_config.velocity);
  161. }
  162. return false;
  163. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  164. if (record->event.pressed) {
  165. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  166. dprintf("midi channel %d\n", midi_config.channel);
  167. }
  168. return false;
  169. case MI_CHD:
  170. if (record->event.pressed) {
  171. midi_config.channel--;
  172. dprintf("midi channel %d\n", midi_config.channel);
  173. }
  174. return false;
  175. case MI_CHU:
  176. if (record->event.pressed) {
  177. midi_config.channel++;
  178. dprintf("midi channel %d\n", midi_config.channel);
  179. }
  180. return false;
  181. case MI_ALLOFF:
  182. if (record->event.pressed) {
  183. midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
  184. dprintf("midi all notes off\n");
  185. }
  186. return false;
  187. case MI_SUS:
  188. midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
  189. dprintf("midi sustain %d\n", record->event.pressed);
  190. return false;
  191. case MI_PORT:
  192. midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
  193. dprintf("midi portamento %d\n", record->event.pressed);
  194. return false;
  195. case MI_SOST:
  196. midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
  197. dprintf("midi sostenuto %d\n", record->event.pressed);
  198. return false;
  199. case MI_SOFT:
  200. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  201. dprintf("midi soft %d\n", record->event.pressed);
  202. return false;
  203. case MI_LEG:
  204. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  205. dprintf("midi legato %d\n", record->event.pressed);
  206. return false;
  207. case MI_MOD:
  208. midi_modulation_step = record->event.pressed ? 1 : -1;
  209. return false;
  210. case MI_MODSD:
  211. if (record->event.pressed) {
  212. midi_config.modulation_interval++;
  213. // prevent overflow
  214. if (midi_config.modulation_interval == 0)
  215. midi_config.modulation_interval--;
  216. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  217. }
  218. return false;
  219. case MI_MODSU:
  220. if (record->event.pressed && midi_config.modulation_interval > 0) {
  221. midi_config.modulation_interval--;
  222. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  223. }
  224. return false;
  225. };
  226. return true;
  227. }
  228. #endif // MIDI_ADVANCED
  229. #endif // MIDI_ENABLE