Add per-channel Swing configuration (#7)

Select swing amount from a percentage range of the beat starting a 50% (unchanged) to a max swing amount of 95% (about 1/32nd note before end of period). Swing percentage shows an indicator marker when the percentage lines up with a quantized note on the grid.

This is probably going to be the last feature because it is pushing up against the limits of available dynamic memory.

Out of scope changes:
- selecting parameters / values no longer wraps
- reduce dynamic memory used in processClockTick
- various readability formatting

Reviewed-on: https://git.pinkduck.xyz/adam/libGravity/pulls/7
Co-authored-by: Adam Wonak <adam.wonak@gmail.com>
Co-committed-by: Adam Wonak <adam.wonak@gmail.com>
This commit is contained in:
2025-06-22 18:38:51 +00:00
committed by adam
parent c8e42c7448
commit 973c13b8ef
6 changed files with 95 additions and 32 deletions

View File

@ -21,8 +21,8 @@
#include "app_state.h"
#include "channel.h"
#include "save_state.h"
#include "display.h"
#include "save_state.h"
AppState app;
StateManager stateManager;
@ -210,6 +210,9 @@ void editChannelParameter(int val) {
case PARAM_CH_OFFSET:
ch.setOffset(ch.getOffset() + val);
break;
case PARAM_CH_SWING:
ch.setSwing(ch.getSwing() + val);
break;
case PARAM_CH_CV_SRC: {
int source = static_cast<int>(ch.getCvSource());
updateSelection(source, val, CV_LAST);
@ -226,8 +229,7 @@ void editChannelParameter(int val) {
}
void updateSelection(int& param, int change, int maxValue) {
// This formula correctly handles positive and negative wrapping.
param = (param + change % maxValue + maxValue) % maxValue;
param = constrain(param + change, 0, maxValue - 1);
}
//

View File

@ -14,6 +14,7 @@ struct AppState {
int selected_param = 0;
int selected_sub_param = 0;
byte selected_channel = 0; // 0=tempo, 1-6=output channel
byte selected_shuffle = 0;
Clock::Source selected_source = Clock::SOURCE_INTERNAL;
Channel channel[Gravity::OUTPUT_COUNT];
};
@ -37,6 +38,7 @@ enum ParamsChannelPage {
PARAM_CH_PROB,
PARAM_CH_DUTY,
PARAM_CH_OFFSET,
PARAM_CH_SWING,
PARAM_CH_CV_SRC,
PARAM_CH_CV_DEST,
PARAM_CH_LAST,

View File

@ -18,6 +18,7 @@ enum CvDestination {
CV_DEST_PROB,
CV_DEST_DUTY,
CV_DEST_OFFSET,
CV_DEST_SWING,
CV_DEST_LAST,
};
@ -39,6 +40,7 @@ class Channel {
base_probability = 100;
base_duty_cycle = 50;
base_offset = 0;
base_swing = 50;
cv_source = CV_NONE;
cv_destination = CV_DEST_NONE;
@ -46,6 +48,7 @@ class Channel {
cvmod_probability = base_probability;
cvmod_duty_cycle = base_duty_cycle;
cvmod_offset = base_offset;
cvmod_swing = base_swing;
}
// Setters (Set the BASE value)
@ -55,7 +58,8 @@ class Channel {
}
void setProbability(int prob) { base_probability = constrain(prob, 0, 100); }
void setDutyCycle(int duty) { base_duty_cycle = constrain(duty, 1, 99); }
void setOffset(int off) { base_offset = constrain(off, 0, 100); }
void setOffset(int off) { base_offset = constrain(off, 0, 99); }
void setSwing(int val) { base_swing = constrain(val, 50, 95); }
void setCvSource(CvSource source) { cv_source = source; }
void setCvDestination(CvDestination dest) { cv_destination = dest; }
@ -64,6 +68,7 @@ class Channel {
int getProbability(bool withCvMod = false) const { return withCvMod ? cvmod_probability : base_probability; }
int getDutyCycle(bool withCvMod = false) const { return withCvMod ? cvmod_duty_cycle : base_duty_cycle; }
int getOffset(bool withCvMod = false) const { return withCvMod ? cvmod_offset : base_offset; }
int getSwing(bool withCvMod = false) const { return withCvMod ? cvmod_swing : base_swing; }
int getClockMod(bool withCvMod = false) const { return clock_mod[getClockModIndex(withCvMod)]; }
int getClockModIndex(bool withCvMod = false) const { return withCvMod ? cvmod_clock_mod_index : base_clock_mod_index; }
CvSource getCvSource() { return cv_source; }
@ -77,21 +82,31 @@ class Channel {
*/
void processClockTick(uint32_t tick, DigitalOutput& output) {
// Calculate output duty cycle state using cv modded values to determine pulse counts.
const uint32_t mod_pulses = clock_mod_pulses[cvmod_clock_mod_index];
const uint32_t duty_pulses = max((long)((mod_pulses * (100L - cvmod_duty_cycle)) / 100L), 1L);
const uint32_t offset_pulses = (long)((mod_pulses * (100L - cvmod_offset)) / 100L);
const uint16_t mod_pulses = clock_mod_pulses[cvmod_clock_mod_index];
const uint16_t duty_pulses = max((long)((mod_pulses * (100L - cvmod_duty_cycle)) / 100L), 1L);
const uint16_t offset_pulses = (long)((mod_pulses * (100L - cvmod_offset)) / 100L);
const uint32_t current_tick_offset = tick + offset_pulses;
uint16_t swing_pulses = 0;
// Check step increment for odd beats.
if (cvmod_swing > 50 && (tick / mod_pulses) % 2 == 1) {
int shifted_swing = cvmod_swing - 50;
swing_pulses = (long)((mod_pulses * (100L - shifted_swing)) / 100L);
}
// Duty cycle high check
if (current_tick_offset % mod_pulses == 0) {
if (cvmod_probability >= random(0, 100)) {
output.High();
const uint32_t current_tick_offset = tick + offset_pulses + swing_pulses;
// Step check
if (!output.On()) {
if (current_tick_offset % mod_pulses == 0) {
// Duty cycle high check
if (cvmod_probability >= random(0, 100)) {
output.High();
}
}
}
// Duty cycle low check
const uint32_t duty_cycle_end_tick = tick + duty_pulses + offset_pulses;
const uint32_t duty_cycle_end_tick = tick + duty_pulses + offset_pulses + swing_pulses;
if (duty_cycle_end_tick % mod_pulses == 0) {
output.Low();
}
@ -104,6 +119,7 @@ class Channel {
cvmod_probability = base_probability;
cvmod_duty_cycle = base_duty_cycle;
cvmod_offset = base_offset;
cvmod_swing = base_swing;
return;
}
@ -113,21 +129,30 @@ class Channel {
// Calculate and store cv modded values using bipolar mapping.
// Default to base value if not the current CV destination.
cvmod_clock_mod_index = (cv_destination == CV_DEST_MOD)
? constrain(base_clock_mod_index + map(value, -512, 512, -10, 10), 0, MOD_CHOICE_SIZE - 1)
: base_clock_mod_index;
cvmod_clock_mod_index =
(cv_destination == CV_DEST_MOD)
? constrain(base_clock_mod_index + map(value, -512, 512, -10, 10), 0, MOD_CHOICE_SIZE - 1)
: base_clock_mod_index;
cvmod_probability = (cv_destination == CV_DEST_PROB)
? constrain(base_probability + map(value, -512, 512, -50, 50), 0, 100)
: base_probability;
cvmod_probability =
(cv_destination == CV_DEST_PROB)
? constrain(base_probability + map(value, -512, 512, -50, 50), 0, 100)
: base_probability;
cvmod_duty_cycle = (cv_destination == CV_DEST_DUTY)
? constrain(base_duty_cycle + map(value, -512, 512, -50, 50), 1, 99)
: base_duty_cycle;
cvmod_duty_cycle =
(cv_destination == CV_DEST_DUTY)
? constrain(base_duty_cycle + map(value, -512, 512, -50, 50), 1, 99)
: base_duty_cycle;
cvmod_offset = (cv_destination == CV_DEST_OFFSET)
? constrain(base_offset + map(value, -512, 512, -50, 50), 0, 99)
: base_offset;
cvmod_offset =
(cv_destination == CV_DEST_OFFSET)
? constrain(base_offset + map(value, -512, 512, -50, 50), 0, 99)
: base_offset;
cvmod_swing =
(cv_destination == CV_DEST_SWING)
? constrain(base_swing + map(value, -512, 512, -25, 25), 50, 95)
: base_swing;
}
private:
@ -136,12 +161,14 @@ class Channel {
byte base_probability;
byte base_duty_cycle;
byte base_offset;
byte base_swing;
// Base value with cv mod applied.
byte cvmod_clock_mod_index;
byte cvmod_probability;
byte cvmod_duty_cycle;
byte cvmod_offset;
byte cvmod_swing;
// CV configuration
CvSource cv_source = CV_NONE;

View File

@ -96,16 +96,16 @@ void drawRightAlignedText(const char* text, int y) {
void drawSelectHero() {
gravity.display.setDrawColor(1);
const int tickSize = 3;
const int heroWidth = SCREEN_WIDTH/2;
const int heroWidth = SCREEN_WIDTH / 2;
const int heroHeight = 49;
gravity.display.drawLine(0, 0, tickSize, 0);
gravity.display.drawLine(0, 0, 0, tickSize);
gravity.display.drawLine(heroWidth, 0, heroWidth-tickSize, 0);
gravity.display.drawLine(heroWidth, 0, heroWidth - tickSize, 0);
gravity.display.drawLine(heroWidth, 0, heroWidth, tickSize);
gravity.display.drawLine(heroWidth, heroHeight, heroWidth, heroHeight-tickSize);
gravity.display.drawLine(heroWidth, heroHeight, heroWidth-tickSize, heroHeight);
gravity.display.drawLine(heroWidth, heroHeight, heroWidth, heroHeight - tickSize);
gravity.display.drawLine(heroWidth, heroHeight, heroWidth - tickSize, heroHeight);
gravity.display.drawLine(0, heroHeight, tickSize, heroHeight);
gravity.display.drawLine(0, heroHeight, 0, heroHeight-tickSize);
gravity.display.drawLine(0, heroHeight, 0, heroHeight - tickSize);
gravity.display.setDrawColor(2);
}
@ -147,6 +147,24 @@ void drawMenuItems(const char* menu_items[], int menu_size) {
}
}
// Display an indicator when swing percentage matches a musical note.
void swingDivisionMark() {
auto& ch = GetSelectedChannel();
switch (ch.getSwing()) {
case 58: // 1/32nd
case 66: // 1/16th
case 75: // 1/8th
gravity.display.drawBox(56, 4, 4, 4);
break;
case 54: // 1/32nd tripplet
case 62: // 1/16th tripplet
case 71: // 1/8th tripplet
gravity.display.drawBox(56, 4, 4, 4);
gravity.display.drawBox(57, 5, 2, 2);
break;
}
}
// Main display functions
void DisplayMainPage() {
@ -244,6 +262,13 @@ void DisplayChannelPage() {
sprintf(mainText, "%d%%", ch.getOffset(withCvMod));
subText = "SHIFT HIT";
break;
case PARAM_CH_SWING:
ch.getSwing() == 50
? sprintf(mainText, "OFF")
: sprintf(mainText, "%d%%", ch.getSwing(withCvMod));
subText = "DOWN BEAT";
swingDivisionMark();
break;
case PARAM_CH_CV_SRC: {
switch (ch.getCvSource()) {
case CV_NONE:
@ -283,6 +308,10 @@ void DisplayChannelPage() {
sprintf(mainText, "DEST");
subText = "OFFSET";
break;
case CV_DEST_SWING:
sprintf(mainText, "DEST");
subText = "SWING";
break;
}
break;
}
@ -293,7 +322,7 @@ void DisplayChannelPage() {
// Draw Channel Page menu items
const char* menu_items[PARAM_CH_LAST] = {
"MOD", "PROBABILITY", "DUTY", "OFFSET", "CV SOURCE", "CV DEST"};
"MOD", "PROBABILITY", "DUTY", "OFFSET", "SWING", "CV SOURCE", "CV DEST"};
drawMenuItems(menu_items, PARAM_CH_LAST);
}

View File

@ -27,6 +27,7 @@ bool StateManager::initialize(AppState& app) {
ch.setProbability(saved_ch_state.base_probability);
ch.setDutyCycle(saved_ch_state.base_duty_cycle);
ch.setOffset(saved_ch_state.base_offset);
ch.setSwing(saved_ch_state.base_shuffle);
ch.setCvSource(static_cast<CvSource>(saved_ch_state.cv_source));
ch.setCvDestination(static_cast<CvDestination>(saved_ch_state.cv_destination));
}
@ -105,6 +106,7 @@ void StateManager::_saveState(const AppState& app) {
save_ch.base_probability = ch.getProbability(false);
save_ch.base_duty_cycle = ch.getDutyCycle(false);
save_ch.base_offset = ch.getOffset(false);
save_ch.base_shuffle = ch.getSwing();
save_ch.cv_source = static_cast<byte>(ch.getCvSource());
save_ch.cv_destination = static_cast<byte>(ch.getCvDestination());
}

View File

@ -9,7 +9,7 @@ struct AppState;
// Define the constants for the current firmware.
const char SKETCH_NAME[] = "Gravity";
const byte SKETCH_VERSION = 3;
const byte SKETCH_VERSION = 4;
// Define the minimum amount of time between EEPROM writes.
static const unsigned long SAVE_DELAY_MS = 2000;
@ -41,6 +41,7 @@ class StateManager {
byte base_probability;
byte base_duty_cycle;
byte base_offset;
byte base_shuffle;
byte cv_source; // Cast the CvSource enum to a byte for storage
byte cv_destination; // Cast the CvDestination enum as a byte for storage
};