process_music.c 9.2 KB

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