logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_midi.c (10809B)


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