process_midi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <LUFA/Drivers/USB/USB.h>
  19. # include "midi.h"
  20. # include "qmk_midi.h"
  21. # ifdef MIDI_BASIC
  22. void process_midi_basic_noteon(uint8_t note) { midi_send_noteon(&midi_device, 0, note, 127); }
  23. void process_midi_basic_noteoff(uint8_t note) { midi_send_noteoff(&midi_device, 0, note, 0); }
  24. void process_midi_all_notes_off(void) { midi_send_cc(&midi_device, 0, 0x7B, 0); }
  25. # endif // MIDI_BASIC
  26. # ifdef MIDI_ADVANCED
  27. # include "timer.h"
  28. static uint8_t tone_status[2][MIDI_TONE_COUNT];
  29. static uint8_t midi_modulation;
  30. static int8_t midi_modulation_step;
  31. static uint16_t midi_modulation_timer;
  32. midi_config_t midi_config;
  33. inline uint8_t compute_velocity(uint8_t setting) { return setting * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN)); }
  34. void midi_init(void) {
  35. midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
  36. midi_config.transpose = 0;
  37. midi_config.velocity = 127;
  38. midi_config.channel = 0;
  39. midi_config.modulation_interval = 8;
  40. for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++) {
  41. tone_status[0][i] = MIDI_INVALID_NOTE;
  42. tone_status[1][i] = 0;
  43. }
  44. midi_modulation = 0;
  45. midi_modulation_step = 0;
  46. midi_modulation_timer = 0;
  47. }
  48. uint8_t midi_compute_note(uint16_t keycode) { return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose; }
  49. bool process_midi(uint16_t keycode, keyrecord_t *record) {
  50. switch (keycode) {
  51. case MIDI_TONE_MIN ... MIDI_TONE_MAX: {
  52. uint8_t channel = midi_config.channel;
  53. uint8_t tone = keycode - MIDI_TONE_MIN;
  54. uint8_t velocity = midi_config.velocity;
  55. if (record->event.pressed) {
  56. uint8_t note = midi_compute_note(keycode);
  57. midi_send_noteon(&midi_device, channel, note, velocity);
  58. dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
  59. tone_status[1][tone] += 1;
  60. if (tone_status[0][tone] == MIDI_INVALID_NOTE) {
  61. tone_status[0][tone] = note;
  62. }
  63. } else {
  64. uint8_t note = tone_status[0][tone];
  65. tone_status[1][tone] -= 1;
  66. if (tone_status[1][tone] == 0) {
  67. midi_send_noteoff(&midi_device, channel, note, velocity);
  68. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  69. tone_status[0][tone] = MIDI_INVALID_NOTE;
  70. }
  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) midi_config.transpose--;
  109. dprintf("midi transpose %d\n", midi_config.transpose);
  110. }
  111. return false;
  112. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  113. if (record->event.pressed) {
  114. midi_config.velocity = compute_velocity(keycode - MIDI_VELOCITY_MIN);
  115. dprintf("midi velocity %d\n", midi_config.velocity);
  116. }
  117. return false;
  118. case MI_VELD:
  119. if (record->event.pressed && midi_config.velocity > 0) {
  120. if (midi_config.velocity == 127) {
  121. midi_config.velocity -= 10;
  122. } else if (midi_config.velocity > 12) {
  123. midi_config.velocity -= 13;
  124. } else {
  125. midi_config.velocity = 0;
  126. }
  127. dprintf("midi velocity %d\n", midi_config.velocity);
  128. }
  129. return false;
  130. case MI_VELU:
  131. if (record->event.pressed && midi_config.velocity < 127) {
  132. if (midi_config.velocity < 115) {
  133. midi_config.velocity += 13;
  134. } else {
  135. midi_config.velocity = 127;
  136. }
  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_ALLOFF:
  159. if (record->event.pressed) {
  160. midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
  161. dprintf("midi all notes 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) midi_config.modulation_interval--;
  192. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  193. }
  194. return false;
  195. case MI_MODSU:
  196. if (record->event.pressed && midi_config.modulation_interval > 0) {
  197. midi_config.modulation_interval--;
  198. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  199. }
  200. return false;
  201. case MI_BENDD:
  202. if (record->event.pressed) {
  203. midi_send_pitchbend(&midi_device, midi_config.channel, -0x2000);
  204. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, -0x2000);
  205. } else {
  206. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  207. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  208. }
  209. return false;
  210. case MI_BENDU:
  211. if (record->event.pressed) {
  212. midi_send_pitchbend(&midi_device, midi_config.channel, 0x1fff);
  213. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0x1fff);
  214. } else {
  215. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  216. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  217. }
  218. return false;
  219. };
  220. return true;
  221. }
  222. # endif // MIDI_ADVANCED
  223. void midi_task(void) {
  224. midi_device_process(&midi_device);
  225. # ifdef MIDI_ADVANCED
  226. if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) return;
  227. midi_modulation_timer = timer_read();
  228. if (midi_modulation_step != 0) {
  229. dprintf("midi modulation %d\n", midi_modulation);
  230. midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
  231. if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
  232. midi_modulation = 0;
  233. midi_modulation_step = 0;
  234. return;
  235. }
  236. midi_modulation += midi_modulation_step;
  237. if (midi_modulation > 127) midi_modulation = 127;
  238. }
  239. # endif
  240. }
  241. #endif // MIDI_ENABLE