diff options
author | James Young <18669334+noroadsleft@users.noreply.github.com> | 2020-11-28 12:02:18 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-28 12:02:18 -0800 |
commit | c66df1664497546f32662409778731143e45a552 (patch) | |
tree | da73a2d532a27685a31d932b3a44a707d4a3af81 /quantum/sequencer/sequencer.c | |
parent | 15385d4113414d42bd062c60c9de5df797d3157f (diff) | |
download | qmk_firmware-c66df1664497546f32662409778731143e45a552.tar.gz qmk_firmware-c66df1664497546f32662409778731143e45a552.zip |
2020 November 28 Breaking Changes Update (#11053)
* 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
Diffstat (limited to 'quantum/sequencer/sequencer.c')
-rw-r--r-- | quantum/sequencer/sequencer.c | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/quantum/sequencer/sequencer.c b/quantum/sequencer/sequencer.c new file mode 100644 index 000000000..0eaf3a17a --- /dev/null +++ b/quantum/sequencer/sequencer.c | |||
@@ -0,0 +1,275 @@ | |||
1 | /* Copyright 2020 Rodolphe Belouin | ||
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 | |||
17 | #include "sequencer.h" | ||
18 | |||
19 | #ifdef MIDI_ENABLE | ||
20 | # include "process_midi.h" | ||
21 | #endif | ||
22 | |||
23 | #ifdef MIDI_MOCKED | ||
24 | # include "tests/midi_mock.h" | ||
25 | #endif | ||
26 | |||
27 | sequencer_config_t sequencer_config = { | ||
28 | false, // enabled | ||
29 | {false}, // steps | ||
30 | {0}, // track notes | ||
31 | 60, // tempo | ||
32 | SQ_RES_4, // resolution | ||
33 | }; | ||
34 | |||
35 | sequencer_state_t sequencer_internal_state = {0, 0, 0, 0, SEQUENCER_PHASE_ATTACK}; | ||
36 | |||
37 | bool is_sequencer_on(void) { return sequencer_config.enabled; } | ||
38 | |||
39 | void sequencer_on(void) { | ||
40 | dprintln("sequencer on"); | ||
41 | sequencer_config.enabled = true; | ||
42 | sequencer_internal_state.current_track = 0; | ||
43 | sequencer_internal_state.current_step = 0; | ||
44 | sequencer_internal_state.timer = timer_read(); | ||
45 | sequencer_internal_state.phase = SEQUENCER_PHASE_ATTACK; | ||
46 | } | ||
47 | |||
48 | void sequencer_off(void) { | ||
49 | dprintln("sequencer off"); | ||
50 | sequencer_config.enabled = false; | ||
51 | sequencer_internal_state.current_step = 0; | ||
52 | } | ||
53 | |||
54 | void sequencer_toggle(void) { | ||
55 | if (is_sequencer_on()) { | ||
56 | sequencer_off(); | ||
57 | } else { | ||
58 | sequencer_on(); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | void sequencer_set_track_notes(const uint16_t track_notes[SEQUENCER_TRACKS]) { | ||
63 | for (uint8_t i = 0; i < SEQUENCER_TRACKS; i++) { | ||
64 | sequencer_config.track_notes[i] = track_notes[i]; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | bool is_sequencer_track_active(uint8_t track) { return (sequencer_internal_state.active_tracks >> track) & true; } | ||
69 | |||
70 | void sequencer_set_track_activation(uint8_t track, bool value) { | ||
71 | if (value) { | ||
72 | sequencer_internal_state.active_tracks |= (1 << track); | ||
73 | } else { | ||
74 | sequencer_internal_state.active_tracks &= ~(1 << track); | ||
75 | } | ||
76 | dprintf("sequencer: track %d is %s\n", track, value ? "active" : "inactive"); | ||
77 | } | ||
78 | |||
79 | void sequencer_toggle_track_activation(uint8_t track) { sequencer_set_track_activation(track, !is_sequencer_track_active(track)); } | ||
80 | |||
81 | void sequencer_toggle_single_active_track(uint8_t track) { | ||
82 | if (is_sequencer_track_active(track)) { | ||
83 | sequencer_internal_state.active_tracks = 0; | ||
84 | } else { | ||
85 | sequencer_internal_state.active_tracks = 1 << track; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | bool is_sequencer_step_on(uint8_t step) { return step < SEQUENCER_STEPS && (sequencer_config.steps[step] & sequencer_internal_state.active_tracks) > 0; } | ||
90 | |||
91 | bool is_sequencer_step_on_for_track(uint8_t step, uint8_t track) { return step < SEQUENCER_STEPS && (sequencer_config.steps[step] >> track) & true; } | ||
92 | |||
93 | void sequencer_set_step(uint8_t step, bool value) { | ||
94 | if (step < SEQUENCER_STEPS) { | ||
95 | if (value) { | ||
96 | sequencer_config.steps[step] |= sequencer_internal_state.active_tracks; | ||
97 | } else { | ||
98 | sequencer_config.steps[step] &= ~sequencer_internal_state.active_tracks; | ||
99 | } | ||
100 | dprintf("sequencer: step %d is %s\n", step, value ? "on" : "off"); | ||
101 | } else { | ||
102 | dprintf("sequencer: step %d is out of range\n", step); | ||
103 | } | ||
104 | } | ||
105 | |||
106 | void sequencer_toggle_step(uint8_t step) { | ||
107 | if (is_sequencer_step_on(step)) { | ||
108 | sequencer_set_step_off(step); | ||
109 | } else { | ||
110 | sequencer_set_step_on(step); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | void sequencer_set_all_steps(bool value) { | ||
115 | for (uint8_t step = 0; step < SEQUENCER_STEPS; step++) { | ||
116 | if (value) { | ||
117 | sequencer_config.steps[step] |= sequencer_internal_state.active_tracks; | ||
118 | } else { | ||
119 | sequencer_config.steps[step] &= ~sequencer_internal_state.active_tracks; | ||
120 | } | ||
121 | } | ||
122 | dprintf("sequencer: all steps are %s\n", value ? "on" : "off"); | ||
123 | } | ||
124 | |||
125 | uint8_t sequencer_get_tempo(void) { return sequencer_config.tempo; } | ||
126 | |||
127 | void sequencer_set_tempo(uint8_t tempo) { | ||
128 | if (tempo > 0) { | ||
129 | sequencer_config.tempo = tempo; | ||
130 | dprintf("sequencer: tempo set to %d bpm\n", tempo); | ||
131 | } else { | ||
132 | dprintln("sequencer: cannot set tempo to 0"); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | void sequencer_increase_tempo(void) { | ||
137 | // Handling potential uint8_t overflow | ||
138 | if (sequencer_config.tempo < UINT8_MAX) { | ||
139 | sequencer_set_tempo(sequencer_config.tempo + 1); | ||
140 | } else { | ||
141 | dprintf("sequencer: cannot set tempo above %d\n", UINT8_MAX); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | void sequencer_decrease_tempo(void) { sequencer_set_tempo(sequencer_config.tempo - 1); } | ||
146 | |||
147 | sequencer_resolution_t sequencer_get_resolution(void) { return sequencer_config.resolution; } | ||
148 | |||
149 | void sequencer_set_resolution(sequencer_resolution_t resolution) { | ||
150 | if (resolution >= 0 && resolution < SEQUENCER_RESOLUTIONS) { | ||
151 | sequencer_config.resolution = resolution; | ||
152 | dprintf("sequencer: resolution set to %d\n", resolution); | ||
153 | } else { | ||
154 | dprintf("sequencer: resolution %d is out of range\n", resolution); | ||
155 | } | ||
156 | } | ||
157 | |||
158 | void sequencer_increase_resolution(void) { sequencer_set_resolution(sequencer_config.resolution + 1); } | ||
159 | |||
160 | void sequencer_decrease_resolution(void) { sequencer_set_resolution(sequencer_config.resolution - 1); } | ||
161 | |||
162 | uint8_t sequencer_get_current_step(void) { return sequencer_internal_state.current_step; } | ||
163 | |||
164 | void sequencer_phase_attack(void) { | ||
165 | dprintf("sequencer: step %d\n", sequencer_internal_state.current_step); | ||
166 | dprintf("sequencer: time %d\n", timer_read()); | ||
167 | |||
168 | if (sequencer_internal_state.current_track == 0) { | ||
169 | sequencer_internal_state.timer = timer_read(); | ||
170 | } | ||
171 | |||
172 | if (timer_elapsed(sequencer_internal_state.timer) < sequencer_internal_state.current_track * SEQUENCER_TRACK_THROTTLE) { | ||
173 | return; | ||
174 | } | ||
175 | |||
176 | #if defined(MIDI_ENABLE) || defined(MIDI_MOCKED) | ||
177 | if (is_sequencer_step_on_for_track(sequencer_internal_state.current_step, sequencer_internal_state.current_track)) { | ||
178 | process_midi_basic_noteon(midi_compute_note(sequencer_config.track_notes[sequencer_internal_state.current_track])); | ||
179 | } | ||
180 | #endif | ||
181 | |||
182 | if (sequencer_internal_state.current_track < SEQUENCER_TRACKS - 1) { | ||
183 | sequencer_internal_state.current_track++; | ||
184 | } else { | ||
185 | sequencer_internal_state.phase = SEQUENCER_PHASE_RELEASE; | ||
186 | } | ||
187 | } | ||
188 | |||
189 | void sequencer_phase_release(void) { | ||
190 | if (timer_elapsed(sequencer_internal_state.timer) < SEQUENCER_PHASE_RELEASE_TIMEOUT + sequencer_internal_state.current_track * SEQUENCER_TRACK_THROTTLE) { | ||
191 | return; | ||
192 | } | ||
193 | #if defined(MIDI_ENABLE) || defined(MIDI_MOCKED) | ||
194 | if (is_sequencer_step_on_for_track(sequencer_internal_state.current_step, sequencer_internal_state.current_track)) { | ||
195 | process_midi_basic_noteoff(midi_compute_note(sequencer_config.track_notes[sequencer_internal_state.current_track])); | ||
196 | } | ||
197 | #endif | ||
198 | if (sequencer_internal_state.current_track > 0) { | ||
199 | sequencer_internal_state.current_track--; | ||
200 | } else { | ||
201 | sequencer_internal_state.phase = SEQUENCER_PHASE_PAUSE; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | void sequencer_phase_pause(void) { | ||
206 | if (timer_elapsed(sequencer_internal_state.timer) < sequencer_get_step_duration()) { | ||
207 | return; | ||
208 | } | ||
209 | |||
210 | sequencer_internal_state.current_step = (sequencer_internal_state.current_step + 1) % SEQUENCER_STEPS; | ||
211 | sequencer_internal_state.phase = SEQUENCER_PHASE_ATTACK; | ||
212 | } | ||
213 | |||
214 | void matrix_scan_sequencer(void) { | ||
215 | if (!sequencer_config.enabled) { | ||
216 | return; | ||
217 | } | ||
218 | |||
219 | if (sequencer_internal_state.phase == SEQUENCER_PHASE_PAUSE) { | ||
220 | sequencer_phase_pause(); | ||
221 | } | ||
222 | |||
223 | if (sequencer_internal_state.phase == SEQUENCER_PHASE_RELEASE) { | ||
224 | sequencer_phase_release(); | ||
225 | } | ||
226 | |||
227 | if (sequencer_internal_state.phase == SEQUENCER_PHASE_ATTACK) { | ||
228 | sequencer_phase_attack(); | ||
229 | } | ||
230 | } | ||
231 | |||
232 | uint16_t sequencer_get_beat_duration(void) { return get_beat_duration(sequencer_config.tempo); } | ||
233 | |||
234 | uint16_t sequencer_get_step_duration(void) { return get_step_duration(sequencer_config.tempo, sequencer_config.resolution); } | ||
235 | |||
236 | uint16_t get_beat_duration(uint8_t tempo) { | ||
237 | // Don’t crash in the unlikely case where the given tempo is 0 | ||
238 | if (tempo == 0) { | ||
239 | return get_beat_duration(60); | ||
240 | } | ||
241 | |||
242 | /** | ||
243 | * Given | ||
244 | * t = tempo and d = duration, both strictly greater than 0 | ||
245 | * When | ||
246 | * t beats / minute = 1 beat / d ms | ||
247 | * Then | ||
248 | * t beats / 60000ms = 1 beat / d ms | ||
249 | * d ms = 60000ms / t | ||
250 | */ | ||
251 | return 60000 / tempo; | ||
252 | } | ||
253 | |||
254 | uint16_t get_step_duration(uint8_t tempo, sequencer_resolution_t resolution) { | ||
255 | /** | ||
256 | * Resolution cheatsheet: | ||
257 | * 1/2 => 2 steps per 4 beats | ||
258 | * 1/2T => 3 steps per 4 beats | ||
259 | * 1/4 => 4 steps per 4 beats | ||
260 | * 1/4T => 6 steps per 4 beats | ||
261 | * 1/8 => 8 steps per 4 beats | ||
262 | * 1/8T => 12 steps per 4 beats | ||
263 | * 1/16 => 16 steps per 4 beats | ||
264 | * 1/16T => 24 steps per 4 beats | ||
265 | * 1/32 => 32 steps per 4 beats | ||
266 | * | ||
267 | * The number of steps for binary resolutions follows the powers of 2. | ||
268 | * The ternary variants are simply 1.5x faster. | ||
269 | */ | ||
270 | bool is_binary = resolution % 2 == 0; | ||
271 | uint8_t binary_steps = 2 << (resolution / 2); | ||
272 | uint16_t binary_step_duration = get_beat_duration(tempo) * 4 / binary_steps; | ||
273 | |||
274 | return is_binary ? binary_step_duration : 2 * binary_step_duration / 3; | ||
275 | } | ||