process_music.c 6.2 KB

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