process_midi.c 9.9 KB

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