process_music.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. uint8_t music_mode = MUSIC_MODE_CHROMATIC;
  28. // music sequencer
  29. static bool music_sequence_recording = false;
  30. static bool music_sequence_recorded = false;
  31. static bool music_sequence_playing = false;
  32. static uint8_t music_sequence[16] = {0};
  33. static uint8_t music_sequence_count = 0;
  34. static uint8_t music_sequence_position = 0;
  35. static uint16_t music_sequence_timer = 0;
  36. static uint16_t music_sequence_interval = 100;
  37. #ifdef AUDIO_ENABLE
  38. #ifndef MUSIC_ON_SONG
  39. #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
  40. #endif
  41. #ifndef MUSIC_OFF_SONG
  42. #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
  43. #endif
  44. #ifndef CHROMATIC_SONG
  45. #define CHROMATIC_SONG SONG(CHROMATIC_SOUND)
  46. #endif
  47. #ifndef GUITAR_SONG
  48. #define GUITAR_SONG SONG(GUITAR_SOUND)
  49. #endif
  50. #ifndef VIOLIN_SONG
  51. #define VIOLIN_SONG SONG(VIOLIN_SOUND)
  52. #endif
  53. #ifndef MAJOR_SONG
  54. #define MAJOR_SONG SONG(MAJOR_SOUND)
  55. #endif
  56. float music_mode_songs[NUMBER_OF_MODES][5][2] = {
  57. CHROMATIC_SONG,
  58. GUITAR_SONG,
  59. VIOLIN_SONG,
  60. MAJOR_SONG
  61. };
  62. float music_on_song[][2] = MUSIC_ON_SONG;
  63. float music_off_song[][2] = MUSIC_OFF_SONG;
  64. #endif
  65. #ifndef MUSIC_MASK
  66. #define MUSIC_MASK keycode < 0xFF
  67. #endif
  68. static void music_noteon(uint8_t note) {
  69. #ifdef AUDIO_ENABLE
  70. process_audio_noteon(note);
  71. #endif
  72. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  73. process_midi_basic_noteon(note);
  74. #endif
  75. }
  76. static void music_noteoff(uint8_t note) {
  77. #ifdef AUDIO_ENABLE
  78. process_audio_noteoff(note);
  79. #endif
  80. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  81. process_midi_basic_noteoff(note);
  82. #endif
  83. }
  84. void music_all_notes_off(void) {
  85. #ifdef AUDIO_ENABLE
  86. process_audio_all_notes_off();
  87. #endif
  88. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  89. process_midi_all_notes_off();
  90. #endif
  91. }
  92. bool process_music(uint16_t keycode, keyrecord_t *record) {
  93. if (keycode == MU_ON && record->event.pressed) {
  94. music_on();
  95. return false;
  96. }
  97. if (keycode == MU_OFF && record->event.pressed) {
  98. music_off();
  99. return false;
  100. }
  101. if (keycode == MU_TOG && record->event.pressed) {
  102. if (music_activated) {
  103. music_off();
  104. } else {
  105. music_on();
  106. }
  107. return false;
  108. }
  109. if (keycode == MU_MOD && record->event.pressed) {
  110. music_mode_cycle();
  111. return false;
  112. }
  113. if (music_activated) {
  114. if (record->event.pressed) {
  115. if (keycode == KC_LCTL) { // Start recording
  116. music_all_notes_off();
  117. music_sequence_recording = true;
  118. music_sequence_recorded = false;
  119. music_sequence_playing = false;
  120. music_sequence_count = 0;
  121. return false;
  122. }
  123. if (keycode == KC_LALT) { // Stop recording/playing
  124. music_all_notes_off();
  125. if (music_sequence_recording) { // was recording
  126. music_sequence_recorded = true;
  127. }
  128. music_sequence_recording = false;
  129. music_sequence_playing = false;
  130. return false;
  131. }
  132. if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing
  133. music_all_notes_off();
  134. music_sequence_recording = false;
  135. music_sequence_playing = true;
  136. music_sequence_position = 0;
  137. music_sequence_timer = 0;
  138. return false;
  139. }
  140. if (keycode == KC_UP) {
  141. music_sequence_interval-=10;
  142. return false;
  143. }
  144. if (keycode == KC_DOWN) {
  145. music_sequence_interval+=10;
  146. return false;
  147. }
  148. }
  149. uint8_t note;
  150. if (music_mode == MUSIC_MODE_CHROMATIC)
  151. note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
  152. else if (music_mode == MUSIC_MODE_GUITAR)
  153. note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
  154. else if (music_mode == MUSIC_MODE_VIOLIN)
  155. note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
  156. else if (music_mode == MUSIC_MODE_MAJOR)
  157. note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
  158. else
  159. note = music_starting_note;
  160. if (record->event.pressed) {
  161. music_noteon(note);
  162. if (music_sequence_recording) {
  163. music_sequence[music_sequence_count] = note;
  164. music_sequence_count++;
  165. }
  166. } else {
  167. music_noteoff(note);
  168. }
  169. if (MUSIC_MASK)
  170. return false;
  171. }
  172. return true;
  173. }
  174. bool is_music_on(void) {
  175. return (music_activated != 0);
  176. }
  177. void music_toggle(void) {
  178. if (!music_activated) {
  179. music_on();
  180. } else {
  181. music_off();
  182. }
  183. }
  184. void music_on(void) {
  185. music_activated = 1;
  186. #ifdef AUDIO_ENABLE
  187. PLAY_SONG(music_on_song);
  188. #endif
  189. music_on_user();
  190. }
  191. void music_off(void) {
  192. music_all_notes_off();
  193. music_activated = 0;
  194. #ifdef AUDIO_ENABLE
  195. PLAY_SONG(music_off_song);
  196. #endif
  197. }
  198. void music_mode_cycle(void) {
  199. music_all_notes_off();
  200. music_mode = (music_mode + 1) % NUMBER_OF_MODES;
  201. #ifdef AUDIO_ENABLE
  202. PLAY_SONG(music_mode_songs[music_mode]);
  203. #endif
  204. }
  205. void matrix_scan_music(void) {
  206. if (music_sequence_playing) {
  207. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  208. music_sequence_timer = timer_read();
  209. uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
  210. uint8_t next_note = music_sequence[music_sequence_position];
  211. music_noteoff(prev_note);
  212. music_noteon(next_note);
  213. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  214. }
  215. }
  216. }
  217. __attribute__ ((weak))
  218. void music_on_user() {}
  219. __attribute__ ((weak))
  220. void music_scale_user() {}
  221. #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))