process_midi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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[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[i] = MIDI_INVALID_NOTE;
  42. }
  43. midi_modulation = 0;
  44. midi_modulation_step = 0;
  45. midi_modulation_timer = 0;
  46. }
  47. uint8_t midi_compute_note(uint16_t keycode) { return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose; }
  48. bool process_midi(uint16_t keycode, keyrecord_t *record) {
  49. switch (keycode) {
  50. case MIDI_TONE_MIN ... MIDI_TONE_MAX: {
  51. uint8_t channel = midi_config.channel;
  52. uint8_t tone = keycode - MIDI_TONE_MIN;
  53. uint8_t velocity = midi_config.velocity;
  54. if (record->event.pressed) {
  55. if (tone_status[tone] == MIDI_INVALID_NOTE) {
  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[tone] = note;
  60. }
  61. } else {
  62. uint8_t note = tone_status[tone];
  63. if (note != MIDI_INVALID_NOTE) {
  64. midi_send_noteoff(&midi_device, channel, note, velocity);
  65. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  66. }
  67. tone_status[tone] = MIDI_INVALID_NOTE;
  68. }
  69. return false;
  70. }
  71. case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
  72. if (record->event.pressed) {
  73. midi_config.octave = keycode - MIDI_OCTAVE_MIN;
  74. dprintf("midi octave %d\n", midi_config.octave);
  75. }
  76. return false;
  77. case MI_OCTD:
  78. if (record->event.pressed && midi_config.octave > 0) {
  79. midi_config.octave--;
  80. dprintf("midi octave %d\n", midi_config.octave);
  81. }
  82. return false;
  83. case MI_OCTU:
  84. if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
  85. midi_config.octave++;
  86. dprintf("midi octave %d\n", midi_config.octave);
  87. }
  88. return false;
  89. case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
  90. if (record->event.pressed) {
  91. midi_config.transpose = keycode - MI_TRNS_0;
  92. dprintf("midi transpose %d\n", midi_config.transpose);
  93. }
  94. return false;
  95. case MI_TRNSD:
  96. if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
  97. midi_config.transpose--;
  98. dprintf("midi transpose %d\n", midi_config.transpose);
  99. }
  100. return false;
  101. case MI_TRNSU:
  102. if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
  103. const bool positive = midi_config.transpose > 0;
  104. midi_config.transpose++;
  105. if (positive && midi_config.transpose < 0) midi_config.transpose--;
  106. dprintf("midi transpose %d\n", midi_config.transpose);
  107. }
  108. return false;
  109. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  110. if (record->event.pressed) {
  111. midi_config.velocity = compute_velocity(keycode - MIDI_VELOCITY_MIN);
  112. dprintf("midi velocity %d\n", midi_config.velocity);
  113. }
  114. return false;
  115. case MI_VELD:
  116. if (record->event.pressed && midi_config.velocity > 0) {
  117. if (midi_config.velocity == 127) {
  118. midi_config.velocity -= 10;
  119. } else if (midi_config.velocity > 12) {
  120. midi_config.velocity -= 13;
  121. } else {
  122. midi_config.velocity = 0;
  123. }
  124. dprintf("midi velocity %d\n", midi_config.velocity);
  125. }
  126. return false;
  127. case MI_VELU:
  128. if (record->event.pressed && midi_config.velocity < 127) {
  129. if (midi_config.velocity < 115) {
  130. midi_config.velocity += 13;
  131. } else {
  132. midi_config.velocity = 127;
  133. }
  134. dprintf("midi velocity %d\n", midi_config.velocity);
  135. }
  136. return false;
  137. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  138. if (record->event.pressed) {
  139. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  140. dprintf("midi channel %d\n", midi_config.channel);
  141. }
  142. return false;
  143. case MI_CHD:
  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_CHU:
  150. if (record->event.pressed) {
  151. midi_config.channel++;
  152. dprintf("midi channel %d\n", midi_config.channel);
  153. }
  154. return false;
  155. case MI_ALLOFF:
  156. if (record->event.pressed) {
  157. midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
  158. dprintf("midi all notes off\n");
  159. }
  160. return false;
  161. case MI_SUS:
  162. midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
  163. dprintf("midi sustain %d\n", record->event.pressed);
  164. return false;
  165. case MI_PORT:
  166. midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
  167. dprintf("midi portamento %d\n", record->event.pressed);
  168. return false;
  169. case MI_SOST:
  170. midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
  171. dprintf("midi sostenuto %d\n", record->event.pressed);
  172. return false;
  173. case MI_SOFT:
  174. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  175. dprintf("midi soft %d\n", record->event.pressed);
  176. return false;
  177. case MI_LEG:
  178. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  179. dprintf("midi legato %d\n", record->event.pressed);
  180. return false;
  181. case MI_MOD:
  182. midi_modulation_step = record->event.pressed ? 1 : -1;
  183. return false;
  184. case MI_MODSD:
  185. if (record->event.pressed) {
  186. midi_config.modulation_interval++;
  187. // prevent overflow
  188. if (midi_config.modulation_interval == 0) midi_config.modulation_interval--;
  189. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  190. }
  191. return false;
  192. case MI_MODSU:
  193. if (record->event.pressed && midi_config.modulation_interval > 0) {
  194. midi_config.modulation_interval--;
  195. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  196. }
  197. return false;
  198. case MI_BENDD:
  199. if (record->event.pressed) {
  200. midi_send_pitchbend(&midi_device, midi_config.channel, -0x2000);
  201. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, -0x2000);
  202. } else {
  203. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  204. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  205. }
  206. return false;
  207. case MI_BENDU:
  208. if (record->event.pressed) {
  209. midi_send_pitchbend(&midi_device, midi_config.channel, 0x1fff);
  210. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0x1fff);
  211. } else {
  212. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  213. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  214. }
  215. return false;
  216. };
  217. return true;
  218. }
  219. # endif // MIDI_ADVANCED
  220. void midi_task(void) {
  221. midi_device_process(&midi_device);
  222. # ifdef MIDI_ADVANCED
  223. if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) return;
  224. midi_modulation_timer = timer_read();
  225. if (midi_modulation_step != 0) {
  226. dprintf("midi modulation %d\n", midi_modulation);
  227. midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
  228. if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
  229. midi_modulation = 0;
  230. midi_modulation_step = 0;
  231. return;
  232. }
  233. midi_modulation += midi_modulation_step;
  234. if (midi_modulation > 127) midi_modulation = 127;
  235. }
  236. # endif
  237. }
  238. #endif // MIDI_ENABLE