process_midi.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. 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 = 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. midi_config.velocity--;
  118. dprintf("midi velocity %d\n", midi_config.velocity);
  119. }
  120. return false;
  121. case MI_VELU:
  122. if (record->event.pressed) {
  123. midi_config.velocity++;
  124. dprintf("midi velocity %d\n", midi_config.velocity);
  125. }
  126. return false;
  127. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  128. if (record->event.pressed) {
  129. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  130. dprintf("midi channel %d\n", midi_config.channel);
  131. }
  132. return false;
  133. case MI_CHD:
  134. if (record->event.pressed) {
  135. midi_config.channel--;
  136. dprintf("midi channel %d\n", midi_config.channel);
  137. }
  138. return false;
  139. case MI_CHU:
  140. if (record->event.pressed) {
  141. midi_config.channel++;
  142. dprintf("midi channel %d\n", midi_config.channel);
  143. }
  144. return false;
  145. case MI_ALLOFF:
  146. if (record->event.pressed) {
  147. midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
  148. dprintf("midi all notes off\n");
  149. }
  150. return false;
  151. case MI_SUS:
  152. midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
  153. dprintf("midi sustain %d\n", record->event.pressed);
  154. return false;
  155. case MI_PORT:
  156. midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
  157. dprintf("midi portamento %d\n", record->event.pressed);
  158. return false;
  159. case MI_SOST:
  160. midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
  161. dprintf("midi sostenuto %d\n", record->event.pressed);
  162. return false;
  163. case MI_SOFT:
  164. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  165. dprintf("midi soft %d\n", record->event.pressed);
  166. return false;
  167. case MI_LEG:
  168. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  169. dprintf("midi legato %d\n", record->event.pressed);
  170. return false;
  171. case MI_MOD:
  172. midi_modulation_step = record->event.pressed ? 1 : -1;
  173. return false;
  174. case MI_MODSD:
  175. if (record->event.pressed) {
  176. midi_config.modulation_interval++;
  177. // prevent overflow
  178. if (midi_config.modulation_interval == 0) midi_config.modulation_interval--;
  179. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  180. }
  181. return false;
  182. case MI_MODSU:
  183. if (record->event.pressed && midi_config.modulation_interval > 0) {
  184. midi_config.modulation_interval--;
  185. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  186. }
  187. return false;
  188. case MI_BENDD:
  189. if (record->event.pressed) {
  190. midi_send_pitchbend(&midi_device, midi_config.channel, -0x2000);
  191. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, -0x2000);
  192. } else {
  193. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  194. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  195. }
  196. return false;
  197. case MI_BENDU:
  198. if (record->event.pressed) {
  199. midi_send_pitchbend(&midi_device, midi_config.channel, 0x1fff);
  200. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0x1fff);
  201. } else {
  202. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  203. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  204. }
  205. return false;
  206. };
  207. return true;
  208. }
  209. # endif // MIDI_ADVANCED
  210. void midi_task(void) {
  211. midi_device_process(&midi_device);
  212. # ifdef MIDI_ADVANCED
  213. if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) return;
  214. midi_modulation_timer = timer_read();
  215. if (midi_modulation_step != 0) {
  216. dprintf("midi modulation %d\n", midi_modulation);
  217. midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
  218. if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
  219. midi_modulation = 0;
  220. midi_modulation_step = 0;
  221. return;
  222. }
  223. midi_modulation += midi_modulation_step;
  224. if (midi_modulation > 127) midi_modulation = 127;
  225. }
  226. # endif
  227. }
  228. #endif // MIDI_ENABLE