diff options
Diffstat (limited to 'quantum/process_keycode/process_music.c')
| -rw-r--r-- | quantum/process_keycode/process_music.c | 118 |
1 files changed, 61 insertions, 57 deletions
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c index 1e2648bff..f89a04ee3 100644 --- a/quantum/process_keycode/process_music.c +++ b/quantum/process_keycode/process_music.c | |||
| @@ -1,5 +1,14 @@ | |||
| 1 | #include "process_music.h" | 1 | #include "process_music.h" |
| 2 | 2 | ||
| 3 | #ifdef AUDIO_ENABLE | ||
| 4 | #include "process_audio.h" | ||
| 5 | #endif | ||
| 6 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | ||
| 7 | #include "process_midi.h" | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) | ||
| 11 | |||
| 3 | bool music_activated = false; | 12 | bool music_activated = false; |
| 4 | uint8_t music_starting_note = 0x0C; | 13 | uint8_t music_starting_note = 0x0C; |
| 5 | int music_offset = 7; | 14 | int music_offset = 7; |
| @@ -8,36 +17,41 @@ int music_offset = 7; | |||
| 8 | static bool music_sequence_recording = false; | 17 | static bool music_sequence_recording = false; |
| 9 | static bool music_sequence_recorded = false; | 18 | static bool music_sequence_recorded = false; |
| 10 | static bool music_sequence_playing = false; | 19 | static bool music_sequence_playing = false; |
| 11 | static float music_sequence[16] = {0}; | 20 | static uint8_t music_sequence[16] = {0}; |
| 12 | static uint8_t music_sequence_count = 0; | 21 | static uint8_t music_sequence_count = 0; |
| 13 | static uint8_t music_sequence_position = 0; | 22 | static uint8_t music_sequence_position = 0; |
| 14 | 23 | ||
| 15 | static uint16_t music_sequence_timer = 0; | 24 | static uint16_t music_sequence_timer = 0; |
| 16 | static uint16_t music_sequence_interval = 100; | 25 | static uint16_t music_sequence_interval = 100; |
| 17 | 26 | ||
| 18 | bool process_music(uint16_t keycode, keyrecord_t *record) { | 27 | static void music_noteon(uint8_t note) { |
| 28 | #ifdef AUDIO_ENABLE | ||
| 29 | process_audio_noteon(note); | ||
| 30 | #endif | ||
| 31 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | ||
| 32 | process_midi_basic_noteon(note); | ||
| 33 | #endif | ||
| 34 | } | ||
| 19 | 35 | ||
| 20 | if (keycode == AU_ON && record->event.pressed) { | 36 | static void music_noteoff(uint8_t note) { |
| 21 | audio_on(); | 37 | #ifdef AUDIO_ENABLE |
| 22 | return false; | 38 | process_audio_noteoff(note); |
| 23 | } | 39 | #endif |
| 40 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | ||
| 41 | process_midi_basic_noteoff(note); | ||
| 42 | #endif | ||
| 43 | } | ||
| 24 | 44 | ||
| 25 | if (keycode == AU_OFF && record->event.pressed) { | 45 | void music_all_notes_off(void) { |
| 26 | audio_off(); | 46 | #ifdef AUDIO_ENABLE |
| 27 | return false; | 47 | process_audio_all_notes_off(); |
| 28 | } | 48 | #endif |
| 49 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | ||
| 50 | process_midi_all_notes_off(); | ||
| 51 | #endif | ||
| 52 | } | ||
| 29 | 53 | ||
| 30 | if (keycode == AU_TOG && record->event.pressed) { | 54 | bool process_music(uint16_t keycode, keyrecord_t *record) { |
| 31 | if (is_audio_on()) | ||
| 32 | { | ||
| 33 | audio_off(); | ||
| 34 | } | ||
| 35 | else | ||
| 36 | { | ||
| 37 | audio_on(); | ||
| 38 | } | ||
| 39 | return false; | ||
| 40 | } | ||
| 41 | 55 | ||
| 42 | if (keycode == MU_ON && record->event.pressed) { | 56 | if (keycode == MU_ON && record->event.pressed) { |
| 43 | music_on(); | 57 | music_on(); |
| @@ -61,22 +75,10 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
| 61 | return false; | 75 | return false; |
| 62 | } | 76 | } |
| 63 | 77 | ||
| 64 | if (keycode == MUV_IN && record->event.pressed) { | ||
| 65 | voice_iterate(); | ||
| 66 | music_scale_user(); | ||
| 67 | return false; | ||
| 68 | } | ||
| 69 | |||
| 70 | if (keycode == MUV_DE && record->event.pressed) { | ||
| 71 | voice_deiterate(); | ||
| 72 | music_scale_user(); | ||
| 73 | return false; | ||
| 74 | } | ||
| 75 | |||
| 76 | if (music_activated) { | 78 | if (music_activated) { |
| 77 | 79 | ||
| 78 | if (keycode == KC_LCTL && record->event.pressed) { // Start recording | 80 | if (keycode == KC_LCTL && record->event.pressed) { // Start recording |
| 79 | stop_all_notes(); | 81 | music_all_notes_off(); |
| 80 | music_sequence_recording = true; | 82 | music_sequence_recording = true; |
| 81 | music_sequence_recorded = false; | 83 | music_sequence_recorded = false; |
| 82 | music_sequence_playing = false; | 84 | music_sequence_playing = false; |
| @@ -85,7 +87,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
| 85 | } | 87 | } |
| 86 | 88 | ||
| 87 | if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing | 89 | if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing |
| 88 | stop_all_notes(); | 90 | music_all_notes_off(); |
| 89 | if (music_sequence_recording) { // was recording | 91 | if (music_sequence_recording) { // was recording |
| 90 | music_sequence_recorded = true; | 92 | music_sequence_recorded = true; |
| 91 | } | 93 | } |
| @@ -95,7 +97,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
| 95 | } | 97 | } |
| 96 | 98 | ||
| 97 | if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing | 99 | if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing |
| 98 | stop_all_notes(); | 100 | music_all_notes_off(); |
| 99 | music_sequence_recording = false; | 101 | music_sequence_recording = false; |
| 100 | music_sequence_playing = true; | 102 | music_sequence_playing = true; |
| 101 | music_sequence_position = 0; | 103 | music_sequence_position = 0; |
| @@ -114,32 +116,34 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
| 114 | music_sequence_interval+=10; | 116 | music_sequence_interval+=10; |
| 115 | return false; | 117 | return false; |
| 116 | } | 118 | } |
| 119 | |||
| 117 | #define MUSIC_MODE_GUITAR | 120 | #define MUSIC_MODE_GUITAR |
| 118 | 121 | ||
| 119 | #ifdef MUSIC_MODE_CHROMATIC | 122 | #ifdef MUSIC_MODE_CHROMATIC |
| 120 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(MATRIX_ROWS - record->event.key.row)); | 123 | uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row); |
| 121 | #elif defined(MUSIC_MODE_GUITAR) | 124 | #elif defined(MUSIC_MODE_GUITAR) |
| 122 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 7)*5.0/12); | 125 | uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row); |
| 123 | #elif defined(MUSIC_MODE_VIOLIN) | 126 | #elif defined(MUSIC_MODE_VIOLIN) |
| 124 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 5)*7.0/12); | 127 | uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row); |
| 125 | #else | 128 | #else |
| 126 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + SCALE[record->event.key.col + music_offset])/12.0+(MATRIX_ROWS - record->event.key.row)); | 129 | uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row); |
| 127 | #endif | 130 | #endif |
| 128 | 131 | ||
| 129 | if (record->event.pressed) { | 132 | if (record->event.pressed) { |
| 130 | play_note(freq, 0xF); | 133 | music_noteon(note); |
| 131 | if (music_sequence_recording) { | 134 | if (music_sequence_recording) { |
| 132 | music_sequence[music_sequence_count] = freq; | 135 | music_sequence[music_sequence_count] = note; |
| 133 | music_sequence_count++; | 136 | music_sequence_count++; |
| 134 | } | 137 | } |
| 135 | } else { | 138 | } else { |
| 136 | stop_note(freq); | 139 | music_noteoff(note); |
| 137 | } | 140 | } |
| 138 | 141 | ||
| 139 | if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through | 142 | if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through |
| 140 | return false; | 143 | return false; |
| 141 | } | 144 | } |
| 142 | return true; | 145 | |
| 146 | return true; | ||
| 143 | } | 147 | } |
| 144 | 148 | ||
| 145 | bool is_music_on(void) { | 149 | bool is_music_on(void) { |
| @@ -161,26 +165,26 @@ void music_on(void) { | |||
| 161 | 165 | ||
| 162 | void music_off(void) { | 166 | void music_off(void) { |
| 163 | music_activated = 0; | 167 | music_activated = 0; |
| 164 | stop_all_notes(); | 168 | music_all_notes_off(); |
| 165 | } | 169 | } |
| 166 | 170 | ||
| 167 | |||
| 168 | __attribute__ ((weak)) | ||
| 169 | void music_on_user() {} | ||
| 170 | |||
| 171 | __attribute__ ((weak)) | ||
| 172 | void audio_on_user() {} | ||
| 173 | |||
| 174 | __attribute__ ((weak)) | ||
| 175 | void music_scale_user() {} | ||
| 176 | |||
| 177 | void matrix_scan_music(void) { | 171 | void matrix_scan_music(void) { |
| 178 | if (music_sequence_playing) { | 172 | if (music_sequence_playing) { |
| 179 | if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { | 173 | if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { |
| 180 | music_sequence_timer = timer_read(); | 174 | music_sequence_timer = timer_read(); |
| 181 | stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]); | 175 | uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]; |
| 182 | play_note(music_sequence[music_sequence_position], 0xF); | 176 | uint8_t next_note = music_sequence[music_sequence_position]; |
| 177 | music_noteoff(prev_note); | ||
| 178 | music_noteon(next_note); | ||
| 183 | music_sequence_position = (music_sequence_position + 1) % music_sequence_count; | 179 | music_sequence_position = (music_sequence_position + 1) % music_sequence_count; |
| 184 | } | 180 | } |
| 185 | } | 181 | } |
| 186 | } | 182 | } |
| 183 | |||
| 184 | __attribute__ ((weak)) | ||
| 185 | void music_on_user() {} | ||
| 186 | |||
| 187 | __attribute__ ((weak)) | ||
| 188 | void music_scale_user() {} | ||
| 189 | |||
| 190 | #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) \ No newline at end of file | ||
