Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add step sequencer feature #9703

Merged
merged 19 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ endif

include common_features.mk
include $(TMK_PATH)/common.mk
include $(QUANTUM_PATH)/sequencer/tests/rules.mk
include $(QUANTUM_PATH)/serial_link/tests/rules.mk
ifneq ($(filter $(FULL_TESTS),$(TEST)),)
include build_full_test.mk
Expand Down
1 change: 1 addition & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ COMMON_VPATH += $(QUANTUM_PATH)/keymap_extras
COMMON_VPATH += $(QUANTUM_PATH)/audio
COMMON_VPATH += $(QUANTUM_PATH)/process_keycode
COMMON_VPATH += $(QUANTUM_PATH)/api
COMMON_VPATH += $(QUANTUM_PATH)/sequencer
COMMON_VPATH += $(DRIVER_PATH)
13 changes: 11 additions & 2 deletions common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ ifeq ($(strip $(AUDIO_ENABLE)), yes)
SRC += $(QUANTUM_DIR)/audio/luts.c
endif

ifeq ($(strip $(SEQUENCER_ENABLE)), yes)
OPT_DEFS += -DSEQUENCER_ENABLE
MUSIC_ENABLE = yes
SRC += $(QUANTUM_DIR)/sequencer/sequencer.c
SRC += $(QUANTUM_DIR)/process_keycode/process_sequencer.c
endif

ifeq ($(strip $(MIDI_ENABLE)), yes)
OPT_DEFS += -DMIDI_ENABLE
MUSIC_ENABLE = yes
Expand Down Expand Up @@ -428,8 +435,10 @@ ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
# Functions added via QUANTUM_LIB_SRC are only included in the final binary if they're called.
# Unused functions are pruned away, which is why we can add multiple drivers here without bloat.
ifeq ($(PLATFORM),AVR)
QUANTUM_LIB_SRC += i2c_master.c \
i2c_slave.c
ifneq ($(NO_I2C),yes)
QUANTUM_LIB_SRC += i2c_master.c \
i2c_slave.c
endif
endif

SERIAL_DRIVER ?= bitbang
Expand Down
1 change: 1 addition & 0 deletions docs/_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
* [One Shot Keys](one_shot_keys.md)
* [Pointing Device](feature_pointing_device.md)
* [Raw HID](feature_rawhid.md)
* [Sequencer](feature_sequencer.md)
* [Swap Hands](feature_swap_hands.md)
* [Tap Dance](feature_tap_dance.md)
* [Tap-Hold Configuration](tap_hold.md)
Expand Down
88 changes: 88 additions & 0 deletions docs/feature_sequencer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Sequencer

Since QMK has experimental support for MIDI, you can now turn your keyboard into a [step sequencer](https://en.wikipedia.org/wiki/Music_sequencer#Step_sequencers)!

!> **IMPORTANT:** This feature is highly experimental, it has only been tested on a Planck EZ so far. Also, the scope will be limited to support the drum machine use-case to start with.

## Enable the step sequencer

Add the following line to your `rules.mk`:

```make
SEQUENCER_ENABLE = yes
```

By default the sequencer has 16 steps, but you can override this setting in your `config.h`:

```c
#define SEQUENCER_STEPS 32
```

## Tracks

You can program up to 8 independent tracks with the step sequencer. Select the tracks you want to edit, enable or disable some steps, and start the sequence!

## Resolutions

While the tempo defines the absolute speed at which the sequencer goes through the steps, the resolution defines the granularity of these steps (from coarser to finer).

|Resolution |Description |
|---------- |----------- |
|`SQ_RES_2` |Every other beat |
|`SQ_RES_2T` |Every 1.5 beats |
|`SQ_RES_4` |Every beat |
|`SQ_RES_4T` |Three times per 2 beats|
|`SQ_RES_8` |Twice per beat |
|`SQ_RES_8T` |Three times per beat |
|`SQ_RES_16` |Four times per beat |
|`SQ_RES_16T` |Six times per beat |
|`SQ_RES_32` |Eight times per beat |

## Keycodes

|Keycode |Description |
|------- |----------- |
|`SQ_ON` |Start the step sequencer |
|`SQ_OFF` |Stop the step sequencer |
|`SQ_TOG` |Toggle the step sequencer playback |
|`SQ_SALL`|Enable all the steps |
|`SQ_SCLR`|Disable all the steps |
|`SQ_S(n)`|Toggle the step `n` |
|`SQ_TMPD`|Decrease the tempo |
|`SQ_TMPU`|Increase the tempo |
|`SQ_R(n)`|Set the resolution to n |
|`SQ_RESD`|Change to the slower resolution |
|`SQ_RESU`|Change to the faster resolution |
|`SQ_T(n)`|Set `n` as the only active track or deactivate all |

## Functions

|Function |Description |
|-------- |----------- |
|`bool is_sequencer_on(void);` |Return whether the sequencer is playing |
|`void sequencer_toggle(void);` |Toggle the step sequencer playback |
|`void sequencer_on(void);` |Start the step sequencer |
|`void sequencer_off(void);` |Stop the step sequencer |
|`bool is_sequencer_step_on(uint8_t step);` |Return whether the step is currently enabled |
|`void sequencer_set_step(uint8_t step, bool value);` |Enable or disable the step |
|`void sequencer_set_step_on();` |Enable the step |
|`void sequencer_set_step_off();` |Disable the step |
|`void sequencer_toggle_step(uint8_t step);` |Toggle the step |
|`void sequencer_set_all_steps(bool value);` |Enable or disable all the steps |
|`void sequencer_set_all_steps_on();` |Enable all the steps |
|`void sequencer_set_all_steps_off();` |Disable all the steps |
|`uint8_t sequencer_get_tempo(void);` |Return the current tempo |
|`void sequencer_set_tempo(uint8_t tempo);` |Set the tempo to `tempo` (between 1 and 255) |
|`void sequencer_increase_tempo(void);` |Increase the tempo |
|`void sequencer_decrease_tempo(void);` |Decrease the tempo |
|`sequencer_resolution_t sequencer_get_resolution(void);` |Return the current resolution |
|`void sequencer_set_resolution(sequencer_resolution_t resolution);` |Set the resolution to `resolution` |
|`void sequencer_increase_resolution(void);` |Change to the faster resolution |
|`void sequencer_decrease_resolution(void);` |Change to the slower resolution |
|`bool is_sequencer_track_active(uint8_t track);` |Return whether the track is active |
|`void sequencer_set_track_activation(uint8_t track, bool value);` |Activate or deactivate the `track` |
|`void sequencer_toggle_track_activation(uint8_t track);` |Toggle the `track` |
|`void sequencer_activate_track(uint8_t track);` |Activate the `track` |
|`void sequencer_deactivate_track(uint8_t track);` |Deactivate the `track` |
|`void sequencer_toggle_single_active_track(uint8_t track);` |Set `track` as the only active track or deactivate all |

10 changes: 5 additions & 5 deletions drivers/avr/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

#ifdef SOFT_SERIAL_PIN

# ifdef __AVR_ATmega32U4__
// if using ATmega32U4 I2C, can not use PD0 and PD1 in soft serial.
# if defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)
// if using ATmegaxxU4 I2C, can not use PD0 and PD1 in soft serial.
# ifdef USE_AVR_I2C
# if SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1
# error Using ATmega32U4 I2C, so can not use PD0, PD1
# error Using ATmegaxxU4 I2C, so can not use PD0, PD1
# endif
# endif

Expand Down Expand Up @@ -52,7 +52,7 @@
# define EICRx_BIT (~(_BV(ISC30) | _BV(ISC31)))
# define SERIAL_PIN_INTERRUPT INT3_vect
# endif
# elif SOFT_SERIAL_PIN == E6
# elif (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && SOFT_SERIAL_PIN == E6
# define EIMSK_BIT _BV(INT6)
# define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61)))
# define SERIAL_PIN_INTERRUPT INT6_vect
Expand All @@ -61,7 +61,7 @@
# endif

# else
# error serial.c now support ATmega32U4 only
# error serial.c currently only supports ATmegaxxU2 and ATmegaxxU4
# endif

# define ALWAYS_INLINE __attribute__((always_inline))
Expand Down
3 changes: 3 additions & 0 deletions quantum/mcu_selection.mk
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ ifneq (,$(filter $(MCU),atmega16u2 atmega32u2 atmega16u4 atmega32u4 at90usb646 a
ifeq (,$(filter $(NO_INTERRUPT_CONTROL_ENDPOINT),yes))
OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
endif
ifneq (,$(filter $(MCU),atmega16u2 atmega32u2))
NO_I2C = yes
endif
endif

ifneq (,$(filter $(MCU),atmega32a))
Expand Down
25 changes: 18 additions & 7 deletions quantum/process_keycode/process_midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ static int8_t midi_modulation_step;
static uint16_t midi_modulation_timer;
midi_config_t midi_config;

inline uint8_t compute_velocity(uint8_t setting) { return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); }
inline uint8_t compute_velocity(uint8_t setting) { return setting * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN)); }

void midi_init(void) {
midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
midi_config.transpose = 0;
midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
midi_config.velocity = 127;
midi_config.channel = 0;
midi_config.modulation_interval = 8;

Expand All @@ -66,7 +66,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) {
case MIDI_TONE_MIN ... MIDI_TONE_MAX: {
uint8_t channel = midi_config.channel;
uint8_t tone = keycode - MIDI_TONE_MIN;
uint8_t velocity = compute_velocity(midi_config.velocity);
uint8_t velocity = midi_config.velocity;
if (record->event.pressed) {
uint8_t note = midi_compute_note(keycode);
midi_send_noteon(&midi_device, channel, note, velocity);
Expand Down Expand Up @@ -122,19 +122,30 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) {
return false;
case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
if (record->event.pressed) {
midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
midi_config.velocity = compute_velocity(keycode - MIDI_VELOCITY_MIN);
dprintf("midi velocity %d\n", midi_config.velocity);
}
return false;
case MI_VELD:
if (record->event.pressed && midi_config.velocity > 0) {
midi_config.velocity--;
if (midi_config.velocity == 127) {
midi_config.velocity -= 10;
} else if (midi_config.velocity > 12) {
midi_config.velocity -= 13;
} else {
midi_config.velocity = 0;
}

dprintf("midi velocity %d\n", midi_config.velocity);
}
return false;
case MI_VELU:
if (record->event.pressed) {
midi_config.velocity++;
if (record->event.pressed && midi_config.velocity < 127) {
if (midi_config.velocity < 115) {
midi_config.velocity += 13;
} else {
midi_config.velocity = 127;
}
dprintf("midi velocity %d\n", midi_config.velocity);
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion quantum/process_keycode/process_midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef union {
struct {
uint8_t octave : 4;
int8_t transpose : 4;
uint8_t velocity : 4;
uint8_t velocity : 7;
uint8_t channel : 4;
uint8_t modulation_interval : 4;
};
Expand Down
62 changes: 62 additions & 0 deletions quantum/process_keycode/process_sequencer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* 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 "process_sequencer.h"

bool process_sequencer(uint16_t keycode, keyrecord_t *record) {
rbelouin marked this conversation as resolved.
Show resolved Hide resolved
if (record->event.pressed) {
switch (keycode) {
case SQ_ON:
sequencer_on();
return false;
case SQ_OFF:
sequencer_off();
return false;
case SQ_TOG:
sequencer_toggle();
return false;
case SQ_TMPD:
sequencer_decrease_tempo();
return false;
case SQ_TMPU:
sequencer_increase_tempo();
return false;
case SEQUENCER_RESOLUTION_MIN ... SEQUENCER_RESOLUTION_MAX:
sequencer_set_resolution(keycode - SEQUENCER_RESOLUTION_MIN);
return false;
case SQ_RESD:
sequencer_decrease_resolution();
return false;
case SQ_RESU:
sequencer_increase_resolution();
return false;
case SQ_SALL:
sequencer_set_all_steps_on();
return false;
case SQ_SCLR:
sequencer_set_all_steps_off();
return false;
case SEQUENCER_STEP_MIN ... SEQUENCER_STEP_MAX:
sequencer_toggle_step(keycode - SEQUENCER_STEP_MIN);
return false;
case SEQUENCER_TRACK_MIN ... SEQUENCER_TRACK_MAX:
sequencer_toggle_single_active_track(keycode - SEQUENCER_TRACK_MIN);
return false;
}
}

return true;
}
21 changes: 21 additions & 0 deletions quantum/process_keycode/process_sequencer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* 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/>.
*/

#pragma once

#include "quantum.h"

bool process_sequencer(uint16_t keycode, keyrecord_t *record);
7 changes: 7 additions & 0 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ bool process_record_quantum(keyrecord_t *record) {
process_record_via(keycode, record) &&
#endif
process_record_kb(keycode, record) &&
#if defined(SEQUENCER_ENABLE)
process_sequencer(keycode, record) &&
#endif
#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
process_midi(keycode, record) &&
#endif
Expand Down Expand Up @@ -633,6 +636,10 @@ void matrix_scan_quantum() {
matrix_scan_music();
#endif

#ifdef SEQUENCER_ENABLE
matrix_scan_sequencer();
#endif

#ifdef TAP_DANCE_ENABLE
matrix_scan_tap_dance();
#endif
Expand Down
5 changes: 5 additions & 0 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ extern layer_state_t default_layer_state;
extern layer_state_t layer_state;
#endif

#if defined(SEQUENCER_ENABLE)
# include "sequencer.h"
# include "process_sequencer.h"
#endif

#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
# include "process_midi.h"
#endif
Expand Down
Loading