process_midi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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) {
  23. midi_send_noteon(&midi_device, 0, note, 127);
  24. }
  25. void process_midi_basic_noteoff(uint8_t note) {
  26. midi_send_noteoff(&midi_device, 0, note, 0);
  27. }
  28. void process_midi_all_notes_off(void) {
  29. midi_send_cc(&midi_device, 0, 0x7B, 0);
  30. }
  31. # endif // MIDI_BASIC
  32. # ifdef MIDI_ADVANCED
  33. # include "timer.h"
  34. static uint8_t tone_status[2][MIDI_TONE_COUNT];
  35. static uint8_t midi_modulation;
  36. static int8_t midi_modulation_step;
  37. static uint16_t midi_modulation_timer;
  38. midi_config_t midi_config;
  39. inline uint8_t compute_velocity(uint8_t setting) {
  40. return setting * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN));
  41. }
  42. void midi_init(void) {
  43. midi_config.octave = QK_MIDI_OCTAVE_2 - MIDI_OCTAVE_MIN;
  44. midi_config.transpose = 0;
  45. midi_config.velocity = 127;
  46. midi_config.channel = 0;
  47. midi_config.modulation_interval = 8;
  48. for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++) {
  49. tone_status[0][i] = MIDI_INVALID_NOTE;
  50. tone_status[1][i] = 0;
  51. }
  52. midi_modulation = 0;
  53. midi_modulation_step = 0;
  54. midi_modulation_timer = 0;
  55. }
  56. uint8_t midi_compute_note(uint16_t keycode) {
  57. return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
  58. }
  59. bool process_midi(uint16_t keycode, keyrecord_t *record) {
  60. switch (keycode) {
  61. case MIDI_TONE_MIN ... MIDI_TONE_MAX: {
  62. uint8_t channel = midi_config.channel;
  63. uint8_t tone = keycode - MIDI_TONE_MIN;
  64. uint8_t velocity = midi_config.velocity;
  65. if (record->event.pressed) {
  66. uint8_t note = midi_compute_note(keycode);
  67. midi_send_noteon(&midi_device, channel, note, velocity);
  68. dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
  69. tone_status[1][tone] += 1;
  70. if (tone_status[0][tone] == MIDI_INVALID_NOTE) {
  71. tone_status[0][tone] = note;
  72. }
  73. } else {
  74. uint8_t note = tone_status[0][tone];
  75. tone_status[1][tone] -= 1;
  76. if (tone_status[1][tone] == 0) {
  77. midi_send_noteoff(&midi_device, channel, note, velocity);
  78. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  79. tone_status[0][tone] = MIDI_INVALID_NOTE;
  80. }
  81. }
  82. return false;
  83. }
  84. case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
  85. if (record->event.pressed) {
  86. midi_config.octave = keycode - MIDI_OCTAVE_MIN;
  87. dprintf("midi octave %d\n", midi_config.octave);
  88. }
  89. return false;
  90. case QK_MIDI_OCTAVE_DOWN:
  91. if (record->event.pressed && midi_config.octave > 0) {
  92. midi_config.octave--;
  93. dprintf("midi octave %d\n", midi_config.octave);
  94. }
  95. return false;
  96. case QK_MIDI_OCTAVE_UP:
  97. if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
  98. midi_config.octave++;
  99. dprintf("midi octave %d\n", midi_config.octave);
  100. }
  101. return false;
  102. case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
  103. if (record->event.pressed) {
  104. midi_config.transpose = keycode - QK_MIDI_TRANSPOSE_0;
  105. dprintf("midi transpose %d\n", midi_config.transpose);
  106. }
  107. return false;
  108. case QK_MIDI_TRANSPOSE_DOWN:
  109. if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - QK_MIDI_TRANSPOSE_0)) {
  110. midi_config.transpose--;
  111. dprintf("midi transpose %d\n", midi_config.transpose);
  112. }
  113. return false;
  114. case QK_MIDI_TRANSPOSE_UP:
  115. if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - QK_MIDI_TRANSPOSE_0)) {
  116. const bool positive = midi_config.transpose > 0;
  117. midi_config.transpose++;
  118. if (positive && midi_config.transpose < 0) midi_config.transpose--;
  119. dprintf("midi transpose %d\n", midi_config.transpose);
  120. }
  121. return false;
  122. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  123. if (record->event.pressed) {
  124. midi_config.velocity = compute_velocity(keycode - MIDI_VELOCITY_MIN);
  125. dprintf("midi velocity %d\n", midi_config.velocity);
  126. }
  127. return false;
  128. case QK_MIDI_VELOCITY_DOWN:
  129. if (record->event.pressed && midi_config.velocity > 0) {
  130. if (midi_config.velocity == 127) {
  131. midi_config.velocity -= 10;
  132. } else if (midi_config.velocity > 12) {
  133. midi_config.velocity -= 13;
  134. } else {
  135. midi_config.velocity = 0;
  136. }
  137. dprintf("midi velocity %d\n", midi_config.velocity);
  138. }
  139. return false;
  140. case QK_MIDI_VELOCITY_UP:
  141. if (record->event.pressed && midi_config.velocity < 127) {
  142. if (midi_config.velocity < 115) {
  143. midi_config.velocity += 13;
  144. } else {
  145. midi_config.velocity = 127;
  146. }
  147. dprintf("midi velocity %d\n", midi_config.velocity);
  148. }
  149. return false;
  150. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  151. if (record->event.pressed) {
  152. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  153. dprintf("midi channel %d\n", midi_config.channel);
  154. }
  155. return false;
  156. case QK_MIDI_CHANNEL_DOWN:
  157. if (record->event.pressed) {
  158. midi_config.channel--;
  159. dprintf("midi channel %d\n", midi_config.channel);
  160. }
  161. return false;
  162. case QK_MIDI_CHANNEL_UP:
  163. if (record->event.pressed) {
  164. midi_config.channel++;
  165. dprintf("midi channel %d\n", midi_config.channel);
  166. }
  167. return false;
  168. case QK_MIDI_ALL_NOTES_OFF:
  169. if (record->event.pressed) {
  170. midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
  171. dprintf("midi all notes off\n");
  172. }
  173. return false;
  174. case QK_MIDI_SUSTAIN:
  175. midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
  176. dprintf("midi sustain %d\n", record->event.pressed);
  177. return false;
  178. case QK_MIDI_PORTAMENTO:
  179. midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
  180. dprintf("midi portamento %d\n", record->event.pressed);
  181. return false;
  182. case QK_MIDI_SOSTENUTO:
  183. midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
  184. dprintf("midi sostenuto %d\n", record->event.pressed);
  185. return false;
  186. case QK_MIDI_SOFT:
  187. midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
  188. dprintf("midi soft %d\n", record->event.pressed);
  189. return false;
  190. case QK_MIDI_LEGATO:
  191. midi_send_cc(&midi_device, midi_config.channel, 0x44, record->event.pressed ? 127 : 0);
  192. dprintf("midi legato %d\n", record->event.pressed);
  193. return false;
  194. case QK_MIDI_MODULATION:
  195. midi_modulation_step = record->event.pressed ? 1 : -1;
  196. return false;
  197. case QK_MIDI_MODULATION_SPEED_DOWN:
  198. if (record->event.pressed) {
  199. midi_config.modulation_interval++;
  200. // prevent overflow
  201. if (midi_config.modulation_interval == 0) midi_config.modulation_interval--;
  202. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  203. }
  204. return false;
  205. case QK_MIDI_MODULATION_SPEED_UP:
  206. if (record->event.pressed && midi_config.modulation_interval > 0) {
  207. midi_config.modulation_interval--;
  208. dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
  209. }
  210. return false;
  211. case QK_MIDI_PITCH_BEND_DOWN:
  212. if (record->event.pressed) {
  213. midi_send_pitchbend(&midi_device, midi_config.channel, -0x2000);
  214. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, -0x2000);
  215. } else {
  216. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  217. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  218. }
  219. return false;
  220. case QK_MIDI_PITCH_BEND_UP:
  221. if (record->event.pressed) {
  222. midi_send_pitchbend(&midi_device, midi_config.channel, 0x1fff);
  223. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0x1fff);
  224. } else {
  225. midi_send_pitchbend(&midi_device, midi_config.channel, 0);
  226. dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0);
  227. }
  228. return false;
  229. };
  230. return true;
  231. }
  232. # endif // MIDI_ADVANCED
  233. void midi_task(void) {
  234. midi_device_process(&midi_device);
  235. # ifdef MIDI_ADVANCED
  236. if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) return;
  237. midi_modulation_timer = timer_read();
  238. if (midi_modulation_step != 0) {
  239. dprintf("midi modulation %d\n", midi_modulation);
  240. midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
  241. if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
  242. midi_modulation = 0;
  243. midi_modulation_step = 0;
  244. return;
  245. }
  246. midi_modulation += midi_modulation_step;
  247. if (midi_modulation > 127) midi_modulation = 127;
  248. }
  249. # endif
  250. }
  251. #endif // MIDI_ENABLE