process_music.c 8.3 KB

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