logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_music.c (9939B)


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