keymap_midi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2015 Jack Humbert <jack.humb@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "keymap_common.h"
  15. #include "keymap_midi.h"
  16. #include <lufa.h>
  17. uint8_t starting_note = 0x0C;
  18. int offset = 7;
  19. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  20. {
  21. if (id != 0) {
  22. if (record->event.pressed) {
  23. midi_send_noteon(&midi_device, opt, (id & 0xFF), 127);
  24. } else {
  25. midi_send_noteoff(&midi_device, opt, (id & 0xFF), 127);
  26. }
  27. }
  28. if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
  29. if (record->event.pressed) {
  30. starting_note++;
  31. play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
  32. midi_send_cc(&midi_device, 0, 0x7B, 0);
  33. midi_send_cc(&midi_device, 1, 0x7B, 0);
  34. midi_send_cc(&midi_device, 2, 0x7B, 0);
  35. midi_send_cc(&midi_device, 3, 0x7B, 0);
  36. midi_send_cc(&midi_device, 4, 0x7B, 0);
  37. return;
  38. } else {
  39. stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)));
  40. stop_all_notes();
  41. return;
  42. }
  43. }
  44. if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
  45. if (record->event.pressed) {
  46. starting_note--;
  47. play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
  48. midi_send_cc(&midi_device, 0, 0x7B, 0);
  49. midi_send_cc(&midi_device, 1, 0x7B, 0);
  50. midi_send_cc(&midi_device, 2, 0x7B, 0);
  51. midi_send_cc(&midi_device, 3, 0x7B, 0);
  52. midi_send_cc(&midi_device, 4, 0x7B, 0);
  53. return;
  54. } else {
  55. stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[0 + offset])/12.0+(MATRIX_ROWS - 1)));
  56. stop_all_notes();
  57. return;
  58. }
  59. }
  60. if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  61. offset++;
  62. midi_send_cc(&midi_device, 0, 0x7B, 0);
  63. midi_send_cc(&midi_device, 1, 0x7B, 0);
  64. midi_send_cc(&midi_device, 2, 0x7B, 0);
  65. midi_send_cc(&midi_device, 3, 0x7B, 0);
  66. midi_send_cc(&midi_device, 4, 0x7B, 0);
  67. stop_all_notes();
  68. for (int i = 0; i <= 7; i++) {
  69. play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
  70. _delay_us(80000);
  71. stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)));
  72. _delay_us(8000);
  73. }
  74. return;
  75. }
  76. if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  77. offset--;
  78. midi_send_cc(&midi_device, 0, 0x7B, 0);
  79. midi_send_cc(&midi_device, 1, 0x7B, 0);
  80. midi_send_cc(&midi_device, 2, 0x7B, 0);
  81. midi_send_cc(&midi_device, 3, 0x7B, 0);
  82. midi_send_cc(&midi_device, 4, 0x7B, 0);
  83. stop_all_notes();
  84. for (int i = 0; i <= 7; i++) {
  85. play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)), 0xC);
  86. _delay_us(80000);
  87. stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[i + offset])/12.0+(MATRIX_ROWS - 1)));
  88. _delay_us(8000);
  89. }
  90. return;
  91. }
  92. if (record->event.pressed) {
  93. // midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
  94. midi_send_noteon(&midi_device, 0, (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row), 127);
  95. play_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)), 0xF);
  96. } else {
  97. // midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
  98. midi_send_noteoff(&midi_device, 0, (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row), 127);
  99. stop_note(((double)261.6)*pow(2.0, -1.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)));
  100. }
  101. }