process_music.c 6.1 KB

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