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

@ -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;