c66df16644
* Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
276 lines
9.3 KiB
C
276 lines
9.3 KiB
C
/* Copyright 2020 Rodolphe Belouin
|
||
*
|
||
* 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 "sequencer.h"
|
||
|
||
#ifdef MIDI_ENABLE
|
||
# include "process_midi.h"
|
||
#endif
|
||
|
||
#ifdef MIDI_MOCKED
|
||
# include "tests/midi_mock.h"
|
||
#endif
|
||
|
||
sequencer_config_t sequencer_config = {
|
||
false, // enabled
|
||
{false}, // steps
|
||
{0}, // track notes
|
||
60, // tempo
|
||
SQ_RES_4, // resolution
|
||
};
|
||
|
||
sequencer_state_t sequencer_internal_state = {0, 0, 0, 0, SEQUENCER_PHASE_ATTACK};
|
||
|
||
bool is_sequencer_on(void) { return sequencer_config.enabled; }
|
||
|
||
void sequencer_on(void) {
|
||
dprintln("sequencer on");
|
||
sequencer_config.enabled = true;
|
||
sequencer_internal_state.current_track = 0;
|
||
sequencer_internal_state.current_step = 0;
|
||
sequencer_internal_state.timer = timer_read();
|
||
sequencer_internal_state.phase = SEQUENCER_PHASE_ATTACK;
|
||
}
|
||
|
||
void sequencer_off(void) {
|
||
dprintln("sequencer off");
|
||
sequencer_config.enabled = false;
|
||
sequencer_internal_state.current_step = 0;
|
||
}
|
||
|
||
void sequencer_toggle(void) {
|
||
if (is_sequencer_on()) {
|
||
sequencer_off();
|
||
} else {
|
||
sequencer_on();
|
||
}
|
||
}
|
||
|
||
void sequencer_set_track_notes(const uint16_t track_notes[SEQUENCER_TRACKS]) {
|
||
for (uint8_t i = 0; i < SEQUENCER_TRACKS; i++) {
|
||
sequencer_config.track_notes[i] = track_notes[i];
|
||
}
|
||
}
|
||
|
||
bool is_sequencer_track_active(uint8_t track) { return (sequencer_internal_state.active_tracks >> track) & true; }
|
||
|
||
void sequencer_set_track_activation(uint8_t track, bool value) {
|
||
if (value) {
|
||
sequencer_internal_state.active_tracks |= (1 << track);
|
||
} else {
|
||
sequencer_internal_state.active_tracks &= ~(1 << track);
|
||
}
|
||
dprintf("sequencer: track %d is %s\n", track, value ? "active" : "inactive");
|
||
}
|
||
|
||
void sequencer_toggle_track_activation(uint8_t track) { sequencer_set_track_activation(track, !is_sequencer_track_active(track)); }
|
||
|
||
void sequencer_toggle_single_active_track(uint8_t track) {
|
||
if (is_sequencer_track_active(track)) {
|
||
sequencer_internal_state.active_tracks = 0;
|
||
} else {
|
||
sequencer_internal_state.active_tracks = 1 << track;
|
||
}
|
||
}
|
||
|
||
bool is_sequencer_step_on(uint8_t step) { return step < SEQUENCER_STEPS && (sequencer_config.steps[step] & sequencer_internal_state.active_tracks) > 0; }
|
||
|
||
bool is_sequencer_step_on_for_track(uint8_t step, uint8_t track) { return step < SEQUENCER_STEPS && (sequencer_config.steps[step] >> track) & true; }
|
||
|
||
void sequencer_set_step(uint8_t step, bool value) {
|
||
if (step < SEQUENCER_STEPS) {
|
||
if (value) {
|
||
sequencer_config.steps[step] |= sequencer_internal_state.active_tracks;
|
||
} else {
|
||
sequencer_config.steps[step] &= ~sequencer_internal_state.active_tracks;
|
||
}
|
||
dprintf("sequencer: step %d is %s\n", step, value ? "on" : "off");
|
||
} else {
|
||
dprintf("sequencer: step %d is out of range\n", step);
|
||
}
|
||
}
|
||
|
||
void sequencer_toggle_step(uint8_t step) {
|
||
if (is_sequencer_step_on(step)) {
|
||
sequencer_set_step_off(step);
|
||
} else {
|
||
sequencer_set_step_on(step);
|
||
}
|
||
}
|
||
|
||
void sequencer_set_all_steps(bool value) {
|
||
for (uint8_t step = 0; step < SEQUENCER_STEPS; step++) {
|
||
if (value) {
|
||
sequencer_config.steps[step] |= sequencer_internal_state.active_tracks;
|
||
} else {
|
||
sequencer_config.steps[step] &= ~sequencer_internal_state.active_tracks;
|
||
}
|
||
}
|
||
dprintf("sequencer: all steps are %s\n", value ? "on" : "off");
|
||
}
|
||
|
||
uint8_t sequencer_get_tempo(void) { return sequencer_config.tempo; }
|
||
|
||
void sequencer_set_tempo(uint8_t tempo) {
|
||
if (tempo > 0) {
|
||
sequencer_config.tempo = tempo;
|
||
dprintf("sequencer: tempo set to %d bpm\n", tempo);
|
||
} else {
|
||
dprintln("sequencer: cannot set tempo to 0");
|
||
}
|
||
}
|
||
|
||
void sequencer_increase_tempo(void) {
|
||
// Handling potential uint8_t overflow
|
||
if (sequencer_config.tempo < UINT8_MAX) {
|
||
sequencer_set_tempo(sequencer_config.tempo + 1);
|
||
} else {
|
||
dprintf("sequencer: cannot set tempo above %d\n", UINT8_MAX);
|
||
}
|
||
}
|
||
|
||
void sequencer_decrease_tempo(void) { sequencer_set_tempo(sequencer_config.tempo - 1); }
|
||
|
||
sequencer_resolution_t sequencer_get_resolution(void) { return sequencer_config.resolution; }
|
||
|
||
void sequencer_set_resolution(sequencer_resolution_t resolution) {
|
||
if (resolution >= 0 && resolution < SEQUENCER_RESOLUTIONS) {
|
||
sequencer_config.resolution = resolution;
|
||
dprintf("sequencer: resolution set to %d\n", resolution);
|
||
} else {
|
||
dprintf("sequencer: resolution %d is out of range\n", resolution);
|
||
}
|
||
}
|
||
|
||
void sequencer_increase_resolution(void) { sequencer_set_resolution(sequencer_config.resolution + 1); }
|
||
|
||
void sequencer_decrease_resolution(void) { sequencer_set_resolution(sequencer_config.resolution - 1); }
|
||
|
||
uint8_t sequencer_get_current_step(void) { return sequencer_internal_state.current_step; }
|
||
|
||
void sequencer_phase_attack(void) {
|
||
dprintf("sequencer: step %d\n", sequencer_internal_state.current_step);
|
||
dprintf("sequencer: time %d\n", timer_read());
|
||
|
||
if (sequencer_internal_state.current_track == 0) {
|
||
sequencer_internal_state.timer = timer_read();
|
||
}
|
||
|
||
if (timer_elapsed(sequencer_internal_state.timer) < sequencer_internal_state.current_track * SEQUENCER_TRACK_THROTTLE) {
|
||
return;
|
||
}
|
||
|
||
#if defined(MIDI_ENABLE) || defined(MIDI_MOCKED)
|
||
if (is_sequencer_step_on_for_track(sequencer_internal_state.current_step, sequencer_internal_state.current_track)) {
|
||
process_midi_basic_noteon(midi_compute_note(sequencer_config.track_notes[sequencer_internal_state.current_track]));
|
||
}
|
||
#endif
|
||
|
||
if (sequencer_internal_state.current_track < SEQUENCER_TRACKS - 1) {
|
||
sequencer_internal_state.current_track++;
|
||
} else {
|
||
sequencer_internal_state.phase = SEQUENCER_PHASE_RELEASE;
|
||
}
|
||
}
|
||
|
||
void sequencer_phase_release(void) {
|
||
if (timer_elapsed(sequencer_internal_state.timer) < SEQUENCER_PHASE_RELEASE_TIMEOUT + sequencer_internal_state.current_track * SEQUENCER_TRACK_THROTTLE) {
|
||
return;
|
||
}
|
||
#if defined(MIDI_ENABLE) || defined(MIDI_MOCKED)
|
||
if (is_sequencer_step_on_for_track(sequencer_internal_state.current_step, sequencer_internal_state.current_track)) {
|
||
process_midi_basic_noteoff(midi_compute_note(sequencer_config.track_notes[sequencer_internal_state.current_track]));
|
||
}
|
||
#endif
|
||
if (sequencer_internal_state.current_track > 0) {
|
||
sequencer_internal_state.current_track--;
|
||
} else {
|
||
sequencer_internal_state.phase = SEQUENCER_PHASE_PAUSE;
|
||
}
|
||
}
|
||
|
||
void sequencer_phase_pause(void) {
|
||
if (timer_elapsed(sequencer_internal_state.timer) < sequencer_get_step_duration()) {
|
||
return;
|
||
}
|
||
|
||
sequencer_internal_state.current_step = (sequencer_internal_state.current_step + 1) % SEQUENCER_STEPS;
|
||
sequencer_internal_state.phase = SEQUENCER_PHASE_ATTACK;
|
||
}
|
||
|
||
void matrix_scan_sequencer(void) {
|
||
if (!sequencer_config.enabled) {
|
||
return;
|
||
}
|
||
|
||
if (sequencer_internal_state.phase == SEQUENCER_PHASE_PAUSE) {
|
||
sequencer_phase_pause();
|
||
}
|
||
|
||
if (sequencer_internal_state.phase == SEQUENCER_PHASE_RELEASE) {
|
||
sequencer_phase_release();
|
||
}
|
||
|
||
if (sequencer_internal_state.phase == SEQUENCER_PHASE_ATTACK) {
|
||
sequencer_phase_attack();
|
||
}
|
||
}
|
||
|
||
uint16_t sequencer_get_beat_duration(void) { return get_beat_duration(sequencer_config.tempo); }
|
||
|
||
uint16_t sequencer_get_step_duration(void) { return get_step_duration(sequencer_config.tempo, sequencer_config.resolution); }
|
||
|
||
uint16_t get_beat_duration(uint8_t tempo) {
|
||
// Don’t crash in the unlikely case where the given tempo is 0
|
||
if (tempo == 0) {
|
||
return get_beat_duration(60);
|
||
}
|
||
|
||
/**
|
||
* Given
|
||
* t = tempo and d = duration, both strictly greater than 0
|
||
* When
|
||
* t beats / minute = 1 beat / d ms
|
||
* Then
|
||
* t beats / 60000ms = 1 beat / d ms
|
||
* d ms = 60000ms / t
|
||
*/
|
||
return 60000 / tempo;
|
||
}
|
||
|
||
uint16_t get_step_duration(uint8_t tempo, sequencer_resolution_t resolution) {
|
||
/**
|
||
* Resolution cheatsheet:
|
||
* 1/2 => 2 steps per 4 beats
|
||
* 1/2T => 3 steps per 4 beats
|
||
* 1/4 => 4 steps per 4 beats
|
||
* 1/4T => 6 steps per 4 beats
|
||
* 1/8 => 8 steps per 4 beats
|
||
* 1/8T => 12 steps per 4 beats
|
||
* 1/16 => 16 steps per 4 beats
|
||
* 1/16T => 24 steps per 4 beats
|
||
* 1/32 => 32 steps per 4 beats
|
||
*
|
||
* The number of steps for binary resolutions follows the powers of 2.
|
||
* The ternary variants are simply 1.5x faster.
|
||
*/
|
||
bool is_binary = resolution % 2 == 0;
|
||
uint8_t binary_steps = 2 << (resolution / 2);
|
||
uint16_t binary_step_duration = get_beat_duration(tempo) * 4 / binary_steps;
|
||
|
||
return is_binary ? binary_step_duration : 2 * binary_step_duration / 3;
|
||
}
|