process_music.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 = 36;
  178. #ifdef MUSIC_MAP
  179. if (music_mode == MUSIC_MODE_CHROMATIC) {
  180. note = music_starting_note + music_offset + 36 + music_map[record->event.key.row][record->event.key.col];
  181. } else {
  182. uint8_t position = music_map[record->event.key.row][record->event.key.col];
  183. note = music_starting_note + music_offset + 36 + SCALE[position % 12] + (position / 12)*12;
  184. }
  185. #else
  186. if (music_mode == MUSIC_MODE_CHROMATIC)
  187. note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
  188. else if (music_mode == MUSIC_MODE_GUITAR)
  189. note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
  190. else if (music_mode == MUSIC_MODE_VIOLIN)
  191. note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
  192. else if (music_mode == MUSIC_MODE_MAJOR)
  193. note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
  194. else
  195. note = music_starting_note;
  196. #endif
  197. if (record->event.pressed) {
  198. music_noteon(note);
  199. if (music_sequence_recording) {
  200. music_sequence[music_sequence_count] = note;
  201. music_sequence_count++;
  202. }
  203. } else {
  204. music_noteoff(note);
  205. }
  206. if (music_mask(keycode))
  207. return false;
  208. }
  209. return true;
  210. }
  211. bool music_mask(uint16_t keycode) {
  212. #ifdef MUSIC_MASK
  213. return MUSIC_MASK;
  214. #else
  215. return music_mask_kb(keycode);
  216. #endif
  217. }
  218. __attribute__((weak))
  219. bool music_mask_kb(uint16_t keycode) {
  220. return music_mask_user(keycode);
  221. }
  222. __attribute__((weak))
  223. bool music_mask_user(uint16_t keycode) {
  224. return keycode < 0xFF;
  225. }
  226. bool is_music_on(void) {
  227. return (music_activated != 0);
  228. }
  229. void music_toggle(void) {
  230. if (!music_activated) {
  231. music_on();
  232. } else {
  233. music_off();
  234. }
  235. }
  236. void music_on(void) {
  237. music_activated = 1;
  238. #ifdef AUDIO_ENABLE
  239. PLAY_SONG(music_on_song);
  240. #endif
  241. music_on_user();
  242. }
  243. void music_off(void) {
  244. music_all_notes_off();
  245. music_activated = 0;
  246. #ifdef AUDIO_ENABLE
  247. PLAY_SONG(music_off_song);
  248. #endif
  249. }
  250. bool is_midi_on(void) {
  251. return (midi_activated != 0);
  252. }
  253. void midi_toggle(void) {
  254. if (!midi_activated) {
  255. midi_on();
  256. } else {
  257. midi_off();
  258. }
  259. }
  260. void midi_on(void) {
  261. midi_activated = 1;
  262. #ifdef AUDIO_ENABLE
  263. PLAY_SONG(midi_on_song);
  264. #endif
  265. midi_on_user();
  266. }
  267. void midi_off(void) {
  268. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  269. process_midi_all_notes_off();
  270. #endif
  271. midi_activated = 0;
  272. #ifdef AUDIO_ENABLE
  273. PLAY_SONG(midi_off_song);
  274. #endif
  275. }
  276. void music_mode_cycle(void) {
  277. music_all_notes_off();
  278. music_mode = (music_mode + 1) % NUMBER_OF_MODES;
  279. #ifdef AUDIO_ENABLE
  280. PLAY_SONG(music_mode_songs[music_mode]);
  281. #endif
  282. }
  283. void matrix_scan_music(void) {
  284. if (music_sequence_playing) {
  285. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  286. music_sequence_timer = timer_read();
  287. uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
  288. uint8_t next_note = music_sequence[music_sequence_position];
  289. music_noteoff(prev_note);
  290. music_noteon(next_note);
  291. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  292. }
  293. }
  294. }
  295. __attribute__ ((weak))
  296. void music_on_user() {}
  297. __attribute__ ((weak))
  298. void midi_on_user() {}
  299. __attribute__ ((weak))
  300. void music_scale_user() {}
  301. #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))