process_music.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_MAJOR;
  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. static void music_noteon(uint8_t note) {
  75. #ifdef AUDIO_ENABLE
  76. if (music_activated)
  77. process_audio_noteon(note);
  78. #endif
  79. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  80. if (midi_activated)
  81. process_midi_basic_noteon(note);
  82. #endif
  83. }
  84. static void music_noteoff(uint8_t note) {
  85. #ifdef AUDIO_ENABLE
  86. if (music_activated)
  87. process_audio_noteoff(note);
  88. #endif
  89. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  90. if (midi_activated)
  91. process_midi_basic_noteoff(note);
  92. #endif
  93. }
  94. void music_all_notes_off(void) {
  95. #ifdef AUDIO_ENABLE
  96. if (music_activated)
  97. process_audio_all_notes_off();
  98. #endif
  99. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  100. if (midi_activated)
  101. process_midi_all_notes_off();
  102. #endif
  103. }
  104. bool process_music(uint16_t keycode, keyrecord_t *record) {
  105. if (keycode == MU_ON && record->event.pressed) {
  106. music_on();
  107. return false;
  108. }
  109. if (keycode == MU_OFF && record->event.pressed) {
  110. music_off();
  111. return false;
  112. }
  113. if (keycode == MU_TOG && record->event.pressed) {
  114. if (music_activated) {
  115. music_off();
  116. } else {
  117. music_on();
  118. }
  119. return false;
  120. }
  121. if (keycode == MI_ON && record->event.pressed) {
  122. midi_on();
  123. return false;
  124. }
  125. if (keycode == MI_OFF && record->event.pressed) {
  126. midi_off();
  127. return false;
  128. }
  129. if (keycode == MI_TOG && record->event.pressed) {
  130. if (midi_activated) {
  131. midi_off();
  132. } else {
  133. midi_on();
  134. }
  135. return false;
  136. }
  137. if (keycode == MU_MOD && record->event.pressed) {
  138. music_mode_cycle();
  139. return false;
  140. }
  141. if (music_activated || midi_activated) {
  142. if (record->event.pressed) {
  143. if (keycode == KC_LCTL) { // Start recording
  144. music_all_notes_off();
  145. music_sequence_recording = true;
  146. music_sequence_recorded = false;
  147. music_sequence_playing = false;
  148. music_sequence_count = 0;
  149. return false;
  150. }
  151. if (keycode == KC_LALT) { // Stop recording/playing
  152. music_all_notes_off();
  153. if (music_sequence_recording) { // was recording
  154. music_sequence_recorded = true;
  155. }
  156. music_sequence_recording = false;
  157. music_sequence_playing = false;
  158. return false;
  159. }
  160. if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing
  161. music_all_notes_off();
  162. music_sequence_recording = false;
  163. music_sequence_playing = true;
  164. music_sequence_position = 0;
  165. music_sequence_timer = 0;
  166. return false;
  167. }
  168. if (keycode == KC_UP) {
  169. music_sequence_interval-=10;
  170. return false;
  171. }
  172. if (keycode == KC_DOWN) {
  173. music_sequence_interval+=10;
  174. return false;
  175. }
  176. }
  177. uint8_t note;
  178. if (music_mode == MUSIC_MODE_CHROMATIC)
  179. note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
  180. else if (music_mode == MUSIC_MODE_GUITAR)
  181. note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
  182. else if (music_mode == MUSIC_MODE_VIOLIN)
  183. note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
  184. else if (music_mode == MUSIC_MODE_MAJOR)
  185. note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
  186. else
  187. note = music_starting_note;
  188. if (record->event.pressed) {
  189. music_noteon(note);
  190. if (music_sequence_recording) {
  191. music_sequence[music_sequence_count] = note;
  192. music_sequence_count++;
  193. }
  194. } else {
  195. music_noteoff(note);
  196. }
  197. if (music_mask(keycode))
  198. return false;
  199. }
  200. return true;
  201. }
  202. bool music_mask(uint16_t keycode) {
  203. #ifdef MUSIC_MASK
  204. return MUSIC_MASK;
  205. #else
  206. return music_mask_kb(keycode);
  207. #endif
  208. }
  209. __attribute__((weak))
  210. bool music_mask_kb(uint16_t keycode) {
  211. return music_mask_user(keycode);
  212. }
  213. __attribute__((weak))
  214. bool music_mask_user(uint16_t keycode) {
  215. return keycode < 0xFF;
  216. }
  217. bool is_music_on(void) {
  218. return (music_activated != 0);
  219. }
  220. void music_toggle(void) {
  221. if (!music_activated) {
  222. music_on();
  223. } else {
  224. music_off();
  225. }
  226. }
  227. void music_on(void) {
  228. music_activated = 1;
  229. #ifdef AUDIO_ENABLE
  230. PLAY_SONG(music_on_song);
  231. #endif
  232. music_on_user();
  233. }
  234. void music_off(void) {
  235. music_all_notes_off();
  236. music_activated = 0;
  237. #ifdef AUDIO_ENABLE
  238. PLAY_SONG(music_off_song);
  239. #endif
  240. }
  241. bool is_midi_on(void) {
  242. return (midi_activated != 0);
  243. }
  244. void midi_toggle(void) {
  245. if (!midi_activated) {
  246. midi_on();
  247. } else {
  248. midi_off();
  249. }
  250. }
  251. void midi_on(void) {
  252. midi_activated = 1;
  253. #ifdef AUDIO_ENABLE
  254. PLAY_SONG(midi_on_song);
  255. #endif
  256. midi_on_user();
  257. }
  258. void midi_off(void) {
  259. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  260. process_midi_all_notes_off();
  261. #endif
  262. midi_activated = 0;
  263. #ifdef AUDIO_ENABLE
  264. PLAY_SONG(midi_off_song);
  265. #endif
  266. }
  267. void music_mode_cycle(void) {
  268. music_all_notes_off();
  269. music_mode = (music_mode + 1) % NUMBER_OF_MODES;
  270. #ifdef AUDIO_ENABLE
  271. PLAY_SONG(music_mode_songs[music_mode]);
  272. #endif
  273. }
  274. void matrix_scan_music(void) {
  275. if (music_sequence_playing) {
  276. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  277. music_sequence_timer = timer_read();
  278. uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
  279. uint8_t next_note = music_sequence[music_sequence_position];
  280. music_noteoff(prev_note);
  281. music_noteon(next_note);
  282. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  283. }
  284. }
  285. }
  286. __attribute__ ((weak))
  287. void music_on_user() {}
  288. __attribute__ ((weak))
  289. void midi_on_user() {}
  290. __attribute__ ((weak))
  291. void music_scale_user() {}
  292. #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))