process_music.c 9.2 KB

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