process_music.c 9.7 KB

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