X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=quantum%2Fprocess_keycode%2Fprocess_music.c;h=c7f41cc388c5a7e40131bf0b1285d871812ed2cf;hb=58898f77e311588e15f78d3f6ed829b6010055d9;hp=43bcf973e971560064b12a52a18a9ece78c577cd;hpb=ea14ed122fb5c1b3be5f5d6edda9b39b151692e5;p=qmk_firmware.git diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c index 43bcf973e..c7f41cc38 100644 --- a/quantum/process_keycode/process_music.c +++ b/quantum/process_keycode/process_music.c @@ -1,3 +1,18 @@ +/* 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 . + */ #include "process_music.h" #ifdef AUDIO_ENABLE @@ -10,8 +25,10 @@ #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) bool music_activated = false; +bool midi_activated = false; uint8_t music_starting_note = 0x0C; int music_offset = 7; +uint8_t music_mode = MUSIC_MODE_CHROMATIC; // music sequencer static bool music_sequence_recording = false; @@ -24,30 +41,77 @@ static uint8_t music_sequence_position = 0; static uint16_t music_sequence_timer = 0; static uint16_t music_sequence_interval = 100; +#ifdef AUDIO_ENABLE + #ifndef MUSIC_ON_SONG + #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND) + #endif + #ifndef MUSIC_OFF_SONG + #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND) + #endif + #ifndef MIDI_ON_SONG + #define MIDI_ON_SONG SONG(MUSIC_ON_SOUND) + #endif + #ifndef MIDI_OFF_SONG + #define MIDI_OFF_SONG SONG(MUSIC_OFF_SOUND) + #endif + #ifndef CHROMATIC_SONG + #define CHROMATIC_SONG SONG(CHROMATIC_SOUND) + #endif + #ifndef GUITAR_SONG + #define GUITAR_SONG SONG(GUITAR_SOUND) + #endif + #ifndef VIOLIN_SONG + #define VIOLIN_SONG SONG(VIOLIN_SOUND) + #endif + #ifndef MAJOR_SONG + #define MAJOR_SONG SONG(MAJOR_SOUND) + #endif + float music_mode_songs[NUMBER_OF_MODES][5][2] = { + CHROMATIC_SONG, + GUITAR_SONG, + VIOLIN_SONG, + MAJOR_SONG + }; + float music_on_song[][2] = MUSIC_ON_SONG; + float music_off_song[][2] = MUSIC_OFF_SONG; + float midi_on_song[][2] = MIDI_ON_SONG; + float midi_off_song[][2] = MIDI_OFF_SONG; +#endif + +#ifndef MUSIC_MASK + #define MUSIC_MASK keycode < 0xFF +#endif + static void music_noteon(uint8_t note) { #ifdef AUDIO_ENABLE - process_audio_noteon(note); + if (music_activated) + process_audio_noteon(note); #endif #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) - process_midi_basic_noteon(note); + if (midi_activated) + process_midi_basic_noteon(note); #endif } static void music_noteoff(uint8_t note) { #ifdef AUDIO_ENABLE - process_audio_noteoff(note); + if (music_activated) + process_audio_noteoff(note); #endif #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) - process_midi_basic_noteoff(note); + if (midi_activated) + process_midi_basic_noteoff(note); #endif } -static void music_all_notes_off(void) { +void music_all_notes_off(void) { #ifdef AUDIO_ENABLE - process_audio_stop_all_notes(); + if (music_activated) + process_audio_all_notes_off(); #endif #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) - process_midi_basic_stop_all_notes(); + if (midi_activated) + process_midi_all_notes_off(); #endif } @@ -56,7 +120,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { if (keycode == MU_ON && record->event.pressed) { music_on(); return false; - } + } if (keycode == MU_OFF && record->event.pressed) { music_off(); @@ -64,70 +128,90 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { } if (keycode == MU_TOG && record->event.pressed) { - if (music_activated) - { + if (music_activated) { music_off(); - } - else - { + } else { music_on(); } return false; } - if (music_activated) { + if (keycode == MI_ON && record->event.pressed) { + midi_on(); + return false; + } - if (keycode == KC_LCTL && record->event.pressed) { // Start recording - music_all_notes_off(); - music_sequence_recording = true; - music_sequence_recorded = false; - music_sequence_playing = false; - music_sequence_count = 0; + if (keycode == MI_OFF && record->event.pressed) { + midi_off(); return false; - } + } - if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing - music_all_notes_off(); - if (music_sequence_recording) { // was recording - music_sequence_recorded = true; + if (keycode == MI_TOG && record->event.pressed) { + if (midi_activated) { + midi_off(); + } else { + midi_on(); } - music_sequence_recording = false; - music_sequence_playing = false; return false; - } + } - if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing - music_all_notes_off(); - music_sequence_recording = false; - music_sequence_playing = true; - music_sequence_position = 0; - music_sequence_timer = 0; - return false; - } + if (keycode == MU_MOD && record->event.pressed) { + music_mode_cycle(); + return false; + } - if (keycode == KC_UP) { - if (record->event.pressed) - music_sequence_interval-=10; - return false; - } + if (music_activated || midi_activated) { + if (record->event.pressed) { + if (keycode == KC_LCTL) { // Start recording + music_all_notes_off(); + music_sequence_recording = true; + music_sequence_recorded = false; + music_sequence_playing = false; + music_sequence_count = 0; + return false; + } - if (keycode == KC_DOWN) { - if (record->event.pressed) - music_sequence_interval+=10; - return false; - } + if (keycode == KC_LALT) { // Stop recording/playing + music_all_notes_off(); + if (music_sequence_recording) { // was recording + music_sequence_recorded = true; + } + music_sequence_recording = false; + music_sequence_playing = false; + return false; + } + + if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing + music_all_notes_off(); + music_sequence_recording = false; + music_sequence_playing = true; + music_sequence_position = 0; + music_sequence_timer = 0; + return false; + } - #define MUSIC_MODE_GUITAR + if (keycode == KC_UP) { + music_sequence_interval-=10; + return false; + } + + if (keycode == KC_DOWN) { + music_sequence_interval+=10; + return false; + } + } - #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 + uint8_t note; + if (music_mode == MUSIC_MODE_CHROMATIC) + note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row); + else if (music_mode == MUSIC_MODE_GUITAR) + note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row); + else if (music_mode == MUSIC_MODE_VIOLIN) + note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row); + else if (music_mode == MUSIC_MODE_MAJOR) + note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row); + else + note = music_starting_note; if (record->event.pressed) { music_noteon(note); @@ -139,7 +223,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { music_noteoff(note); } - if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through + if (MUSIC_MASK) return false; } @@ -160,12 +244,56 @@ void music_toggle(void) { void music_on(void) { music_activated = 1; + #ifdef AUDIO_ENABLE + PLAY_SONG(music_on_song); + #endif music_on_user(); } void music_off(void) { - music_activated = 0; music_all_notes_off(); + music_activated = 0; + #ifdef AUDIO_ENABLE + PLAY_SONG(music_off_song); + #endif +} + +bool is_midi_on(void) { + return (midi_activated != 0); +} + +void midi_toggle(void) { + if (!midi_activated) { + midi_on(); + } else { + midi_off(); + } +} + +void midi_on(void) { + midi_activated = 1; + #ifdef AUDIO_ENABLE + PLAY_SONG(midi_on_song); + #endif + midi_on_user(); +} + +void midi_off(void) { + #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) + process_midi_all_notes_off(); + #endif + midi_activated = 0; + #ifdef AUDIO_ENABLE + PLAY_SONG(midi_off_song); + #endif +} + +void music_mode_cycle(void) { + music_all_notes_off(); + music_mode = (music_mode + 1) % NUMBER_OF_MODES; + #ifdef AUDIO_ENABLE + PLAY_SONG(music_mode_songs[music_mode]); + #endif } void matrix_scan_music(void) { @@ -184,6 +312,9 @@ void matrix_scan_music(void) { __attribute__ ((weak)) void music_on_user() {} +__attribute__ ((weak)) +void midi_on_user() {} + __attribute__ ((weak)) void music_scale_user() {}