diff options
author | Zay950 <Zay950@users.noreply.github.com> | 2017-03-29 12:00:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-29 12:00:38 -0700 |
commit | 2366ebfbbdeb6ec29cc9a0facda44d666305dd6e (patch) | |
tree | 883efed0b7260f3143f5a2a879bc3844a8255e0b /quantum/process_keycode/process_music.c | |
parent | 80c5ada3394c5ad8087df00ef878eb2cbcd87d70 (diff) | |
parent | 942f2ccee44bdb2e251553e9730cd8d59307d8b2 (diff) |
Merge branch 'master' into to_push
Diffstat (limited to 'quantum/process_keycode/process_music.c')
-rw-r--r-- | quantum/process_keycode/process_music.c | 141 |
1 files changed, 85 insertions, 56 deletions
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c index 2d52e47a72..217dca2807 100644 --- a/quantum/process_keycode/process_music.c +++ b/quantum/process_keycode/process_music.c @@ -1,43 +1,72 @@ +/* Copyright 2016 Jack Humbert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ #include "process_music.h" +#ifdef AUDIO_ENABLE +#include "process_audio.h" +#endif +#if defined(MIDI_ENABLE) && defined(MIDI_BASIC) +#include "process_midi.h" +#endif + +#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) + bool music_activated = false; -uint8_t starting_note = 0x0C; -int offset = 7; +uint8_t music_starting_note = 0x0C; +int music_offset = 7; // music sequencer static bool music_sequence_recording = false; static bool music_sequence_recorded = false; static bool music_sequence_playing = false; -static float music_sequence[16] = {0}; +static uint8_t music_sequence[16] = {0}; static uint8_t music_sequence_count = 0; static uint8_t music_sequence_position = 0; static uint16_t music_sequence_timer = 0; static uint16_t music_sequence_interval = 100; -bool process_music(uint16_t keycode, keyrecord_t *record) { +static void music_noteon(uint8_t note) { + #ifdef AUDIO_ENABLE + process_audio_noteon(note); + #endif + #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) + process_midi_basic_noteon(note); + #endif +} - if (keycode == AU_ON && record->event.pressed) { - audio_on(); - return false; - } +static void music_noteoff(uint8_t note) { + #ifdef AUDIO_ENABLE + process_audio_noteoff(note); + #endif + #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) + process_midi_basic_noteoff(note); + #endif +} - if (keycode == AU_OFF && record->event.pressed) { - audio_off(); - return false; - } +void music_all_notes_off(void) { + #ifdef AUDIO_ENABLE + process_audio_all_notes_off(); + #endif + #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) + process_midi_all_notes_off(); + #endif +} - if (keycode == AU_TOG && record->event.pressed) { - if (is_audio_on()) - { - audio_off(); - } - else - { - audio_on(); - } - return false; - } +bool process_music(uint16_t keycode, keyrecord_t *record) { if (keycode == MU_ON && record->event.pressed) { music_on(); @@ -61,22 +90,10 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { return false; } - if (keycode == MUV_IN && record->event.pressed) { - voice_iterate(); - music_scale_user(); - return false; - } - - if (keycode == MUV_DE && record->event.pressed) { - voice_deiterate(); - music_scale_user(); - return false; - } - if (music_activated) { if (keycode == KC_LCTL && record->event.pressed) { // Start recording - stop_all_notes(); + music_all_notes_off(); music_sequence_recording = true; music_sequence_recorded = false; music_sequence_playing = false; @@ -85,7 +102,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { } if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing - stop_all_notes(); + music_all_notes_off(); if (music_sequence_recording) { // was recording music_sequence_recorded = true; } @@ -95,7 +112,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { } if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing - stop_all_notes(); + music_all_notes_off(); music_sequence_recording = false; music_sequence_playing = true; music_sequence_position = 0; @@ -115,21 +132,33 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { return false; } - float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)); + #define MUSIC_MODE_GUITAR + + #ifdef MUSIC_MODE_CHROMATIC + uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row); + #elif defined(MUSIC_MODE_GUITAR) + uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row); + #elif defined(MUSIC_MODE_VIOLIN) + uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row); + #else + uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row); + #endif + if (record->event.pressed) { - play_note(freq, 0xF); + music_noteon(note); if (music_sequence_recording) { - music_sequence[music_sequence_count] = freq; + music_sequence[music_sequence_count] = note; music_sequence_count++; } } else { - stop_note(freq); + music_noteoff(note); } if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through return false; } - return true; + + return true; } bool is_music_on(void) { @@ -151,26 +180,26 @@ void music_on(void) { void music_off(void) { music_activated = 0; - stop_all_notes(); + music_all_notes_off(); } - -__attribute__ ((weak)) -void music_on_user() {} - -__attribute__ ((weak)) -void audio_on_user() {} - -__attribute__ ((weak)) -void music_scale_user() {} - void matrix_scan_music(void) { if (music_sequence_playing) { if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { music_sequence_timer = timer_read(); - stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]); - play_note(music_sequence[music_sequence_position], 0xF); + uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]; + uint8_t next_note = music_sequence[music_sequence_position]; + music_noteoff(prev_note); + music_noteon(next_note); music_sequence_position = (music_sequence_position + 1) % music_sequence_count; } } } + +__attribute__ ((weak)) +void music_on_user() {} + +__attribute__ ((weak)) +void music_scale_user() {} + +#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
\ No newline at end of file |