process_music.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_music.h"
  17. #ifdef AUDIO_ENABLE
  18. #include "process_audio.h"
  19. #endif
  20. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  21. #include "process_midi.h"
  22. #endif
  23. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  24. bool music_activated = false;
  25. uint8_t music_starting_note = 0x0C;
  26. int music_offset = 7;
  27. // music sequencer
  28. static bool music_sequence_recording = false;
  29. static bool music_sequence_recorded = false;
  30. static bool music_sequence_playing = false;
  31. static uint8_t music_sequence[16] = {0};
  32. static uint8_t music_sequence_count = 0;
  33. static uint8_t music_sequence_position = 0;
  34. static uint16_t music_sequence_timer = 0;
  35. static uint16_t music_sequence_interval = 100;
  36. static void music_noteon(uint8_t note) {
  37. #ifdef AUDIO_ENABLE
  38. process_audio_noteon(note);
  39. #endif
  40. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  41. process_midi_basic_noteon(note);
  42. #endif
  43. }
  44. static void music_noteoff(uint8_t note) {
  45. #ifdef AUDIO_ENABLE
  46. process_audio_noteoff(note);
  47. #endif
  48. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  49. process_midi_basic_noteoff(note);
  50. #endif
  51. }
  52. void music_all_notes_off(void) {
  53. #ifdef AUDIO_ENABLE
  54. process_audio_all_notes_off();
  55. #endif
  56. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  57. process_midi_all_notes_off();
  58. #endif
  59. }
  60. bool process_music(uint16_t keycode, keyrecord_t *record) {
  61. if (keycode == MU_ON && record->event.pressed) {
  62. music_on();
  63. return false;
  64. }
  65. if (keycode == MU_OFF && record->event.pressed) {
  66. music_off();
  67. return false;
  68. }
  69. if (keycode == MU_TOG && record->event.pressed) {
  70. if (music_activated)
  71. {
  72. music_off();
  73. }
  74. else
  75. {
  76. music_on();
  77. }
  78. return false;
  79. }
  80. if (music_activated) {
  81. if (keycode == KC_LCTL && record->event.pressed) { // Start recording
  82. music_all_notes_off();
  83. music_sequence_recording = true;
  84. music_sequence_recorded = false;
  85. music_sequence_playing = false;
  86. music_sequence_count = 0;
  87. return false;
  88. }
  89. if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
  90. music_all_notes_off();
  91. if (music_sequence_recording) { // was recording
  92. music_sequence_recorded = true;
  93. }
  94. music_sequence_recording = false;
  95. music_sequence_playing = false;
  96. return false;
  97. }
  98. if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
  99. music_all_notes_off();
  100. music_sequence_recording = false;
  101. music_sequence_playing = true;
  102. music_sequence_position = 0;
  103. music_sequence_timer = 0;
  104. return false;
  105. }
  106. if (keycode == KC_UP) {
  107. if (record->event.pressed)
  108. music_sequence_interval-=10;
  109. return false;
  110. }
  111. if (keycode == KC_DOWN) {
  112. if (record->event.pressed)
  113. music_sequence_interval+=10;
  114. return false;
  115. }
  116. #define MUSIC_MODE_GUITAR
  117. #ifdef MUSIC_MODE_CHROMATIC
  118. uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
  119. #elif defined(MUSIC_MODE_GUITAR)
  120. uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
  121. #elif defined(MUSIC_MODE_VIOLIN)
  122. uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
  123. #else
  124. uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
  125. #endif
  126. if (record->event.pressed) {
  127. music_noteon(note);
  128. if (music_sequence_recording) {
  129. music_sequence[music_sequence_count] = note;
  130. music_sequence_count++;
  131. }
  132. } else {
  133. music_noteoff(note);
  134. }
  135. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  136. return false;
  137. }
  138. return true;
  139. }
  140. bool is_music_on(void) {
  141. return (music_activated != 0);
  142. }
  143. void music_toggle(void) {
  144. if (!music_activated) {
  145. music_on();
  146. } else {
  147. music_off();
  148. }
  149. }
  150. void music_on(void) {
  151. music_activated = 1;
  152. music_on_user();
  153. }
  154. void music_off(void) {
  155. music_activated = 0;
  156. music_all_notes_off();
  157. }
  158. void matrix_scan_music(void) {
  159. if (music_sequence_playing) {
  160. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  161. music_sequence_timer = timer_read();
  162. uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
  163. uint8_t next_note = music_sequence[music_sequence_position];
  164. music_noteoff(prev_note);
  165. music_noteon(next_note);
  166. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  167. }
  168. }
  169. }
  170. __attribute__ ((weak))
  171. void music_on_user() {}
  172. __attribute__ ((weak))
  173. void music_scale_user() {}
  174. #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))