Introduce basic Euclidean Rhythm #9

Merged
awonak merged 8 commits from refs/pull/9/head into main 2025-06-30 17:23:56 +00:00
6 changed files with 153 additions and 19 deletions
Showing only changes of commit 17afa9882b - Show all commits

View File

@ -58,11 +58,17 @@ void loop() {
gravity.Process(); gravity.Process();
// Read CVs and call the update function for each channel. // Read CVs and call the update function for each channel.
if (!app.editing_param) {
int cv1 = gravity.cv1.Read(); int cv1 = gravity.cv1.Read();
int cv2 = gravity.cv2.Read(); int cv2 = gravity.cv2.Read();
for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) { for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) {
// Only apply CV to the channel when the current channel has cv
// mod configured.
if (app.channel[i].isCvModActive()) {
app.channel[i].applyCvMod(cv1, cv2); app.channel[i].applyCvMod(cv1, cv2);
} }
}
}
// Check for dirty state eligible to be saved. // Check for dirty state eligible to be saved.
stateManager.update(app); stateManager.update(app);

View File

@ -20,6 +20,8 @@ enum CvDestination {
CV_DEST_DUTY, CV_DEST_DUTY,
CV_DEST_OFFSET, CV_DEST_OFFSET,
CV_DEST_SWING, CV_DEST_SWING,
CV_DEST_EUC_STEPS,
CV_DEST_EUC_HITS,
CV_DEST_LAST, CV_DEST_LAST,
}; };
@ -80,11 +82,10 @@ class Channel {
bool isCvModActive() const { return cv_source != CV_NONE && cv_destination != CV_DEST_NONE; } bool isCvModActive() const { return cv_source != CV_NONE && cv_destination != CV_DEST_NONE; }
// Euclidean // Euclidean
void setSteps(byte val) { pattern.SetSteps(val); } void setSteps(int val) { pattern.SetSteps(val); }
void setHits(byte val) { pattern.SetHits(val); } void setHits(int val) { pattern.SetHits(val); }
byte getSteps() { return pattern.GetSteps(); }
byte getSteps() { pattern.GetSteps(); } byte getHits() { return pattern.GetHits(); }
byte getHits() { pattern.GetHits(); }
/** /**
* @brief Processes a clock tick and determines if the output should be high or low. * @brief Processes a clock tick and determines if the output should be high or low.
@ -111,19 +112,14 @@ class Channel {
// Step check // Step check
if (current_tick_offset % mod_pulses == 0) { if (current_tick_offset % mod_pulses == 0) {
bool hit = cvmod_probability >= random(0, 100); bool hit = cvmod_probability >= random(0, 100);
if (pattern.IsActive()) {
// Euclidean rhythm check // Euclidean rhythm check
switch (pattern.NextStep()) { switch (pattern.NextStep()) {
case Pattern::REST: // Rest when active or fall back to probability case Pattern::REST: // Rest when active or fall back to probability
hit = pattern.IsActive() ? false : hit; hit = false;
break; break;
case Pattern::HIT: // Hit if probability is true case Pattern::HIT: // Hit if probability is true
hit &= true; hit &= true;
break; break;
case Pattern::PADDING: // Padding returns only when active, always rest)
hit = false;
break;
}
} }
if (hit) { if (hit) {
output.High(); output.High();
@ -139,16 +135,6 @@ class Channel {
} }
void applyCvMod(int cv1_value, int cv2_value) { void applyCvMod(int cv1_value, int cv2_value) {
if (!isCvModActive()) {
// If CV is off, ensure cv modded values match the base values.
cvmod_clock_mod_index = base_clock_mod_index;
cvmod_probability = base_probability;
cvmod_duty_cycle = base_duty_cycle;
cvmod_offset = base_offset;
cvmod_swing = base_swing;
return;
}
// Use the CV value for current selected cv source. // Use the CV value for current selected cv source.
int value = (cv_source == CV_1) ? cv1_value : cv2_value; int value = (cv_source == CV_1) ? cv1_value : cv2_value;
@ -179,6 +165,14 @@ class Channel {
(cv_destination == CV_DEST_SWING) (cv_destination == CV_DEST_SWING)
? constrain(base_swing + map(value, -512, 512, -25, 25), 50, 95) ? constrain(base_swing + map(value, -512, 512, -25, 25), 50, 95)
: base_swing; : base_swing;
if (cv_destination == CV_DEST_EUC_STEPS) {
pattern.SetSteps(map(value, -512, 512, 0, MAX_PATTERN_LEN));
}
if (cv_destination == CV_DEST_EUC_HITS) {
pattern.SetHits(map(value, -512, 512, 0, pattern.GetSteps()));
}
} }
private: private:

View File

@ -313,6 +313,12 @@ void DisplayChannelPage() {
case CV_DEST_SWING: case CV_DEST_SWING:
subText = F("SWING"); subText = F("SWING");
break; break;
case CV_DEST_EUC_STEPS:
subText = F("EUCLID STEPS");
break;
case CV_DEST_EUC_HITS:
subText = F("EUCLID HITS");
break;
} }
break; break;
} }
@ -323,8 +329,8 @@ void DisplayChannelPage() {
// Draw Channel Page menu items // Draw Channel Page menu items
String menu_items[PARAM_CH_LAST] = { String menu_items[PARAM_CH_LAST] = {
F("MOD"), F("PROBABILITY"), F("DUTY"), F("OFFSET"), F("SWING"), F("EUC STEPS"), F("MOD"), F("PROBABILITY"), F("DUTY"), F("OFFSET"), F("SWING"), F("EUCLID STEPS"),
F("EUC HITS"), F("CV SOURCE"), F("CV DEST")}; F("EUCLID HITS"), F("CV SOURCE"), F("CV DEST")};
drawMenuItems(menu_items, PARAM_CH_LAST); drawMenuItems(menu_items, PARAM_CH_LAST);
} }

View File

@ -8,7 +8,7 @@ struct PatternState {
uint8_t hits; uint8_t hits;
}; };
const PatternState DEFAULT_PATTERN = {16, 4}; const PatternState DEFAULT_PATTERN = {1, 1};
class Pattern { class Pattern {
public: public:
@ -16,93 +16,64 @@ class Pattern {
~Pattern() {} ~Pattern() {}
enum Step { enum Step {
HIT,
REST, REST,
PADDING, HIT,
}; };
void Init(PatternState state) { void Init(PatternState state) {
steps_ = constrain(state.steps, 0, MAX_PATTERN_LEN); steps_ = constrain(state.steps, 1, MAX_PATTERN_LEN);
hits_ = constrain(state.hits, 1, steps_); hits_ = constrain(state.hits, 1, steps_);
updatePattern(); updatePattern();
} }
PatternState GetState() { return {steps_, hits_}; } PatternState GetState() { return {steps_, hits_}; }
// Get the current step value and advance the euclidean rhythm step index
// to the next step in the pattern.
Step NextStep() {
byte padding_ = 0;
if (steps_ == 0) return REST;
Step value = GetCurrentStep(current_step_);
current_step_ =
(current_step_ < steps_ + padding_ - 1) ? current_step_ + 1 : 0;
return value;
}
Step GetCurrentStep(byte i) { return pattern_[i]; } Step GetCurrentStep(byte i) { return pattern_[i]; }
void SetSteps(byte steps) { void SetSteps(int steps) {
steps_ = constrain(steps, 0, MAX_PATTERN_LEN); steps_ = constrain(steps, 1, MAX_PATTERN_LEN);
hits_ = min(hits_, steps_); hits_ = min(hits_, steps_);
updatePattern(); updatePattern();
} }
void SetHits(byte hits) { void SetHits(int hits) {
hits_ = constrain(hits, 0, steps_); hits_ = constrain(hits, 1, steps_);
updatePattern();
} }
// void ChangeOffset(byte val) { void Reset() { step_index_ = 0; }
// offset_ = constrain(offset_ + val, 0, (steps_ + padding_));
// updatePattern();
// }
// void ChangePadding(byte val) { uint8_t GetSteps() { return steps_; }
// if (val == 1 && padding_ + steps_ < MAX_PATTERN_LEN) { uint8_t GetHits() { return hits_; }
// padding_++; uint8_t GetStepIndex() { return step_index_; }
// updatePattern();
// } else if (val == -1 && padding_ > 0) {
// padding_--;
// offset_ = min(offset_, (padding_ + steps_) - 1);
// updatePattern();
// }
// }
void Reset() { current_step_ = 0; } // Get the current step value and advance the euclidean rhythm step index
bool IsActive() { return steps_ != 0 && hits_ != 0; } // to the next step in the pattern.
Step NextStep() {
if (steps_ == 0) return REST;
inline uint8_t GetSteps() { return steps_; } Step value = GetCurrentStep(step_index_);
inline uint8_t GetHits() { return hits_; } step_index_ = (step_index_ < steps_ - 1) ? step_index_ + 1 : 0;
inline uint8_t GetStepIndex() { return current_step_; } return value;
}
private: private:
uint8_t steps_ = 0; uint8_t steps_ = 0;
uint8_t hits_ = 0; uint8_t hits_ = 0;
volatile uint8_t current_step_ = 0; volatile uint8_t step_index_ = 0;
Step pattern_[MAX_PATTERN_LEN]; Step pattern_[MAX_PATTERN_LEN];
// Update the euclidean rhythm pattern when attributes change. // Update the euclidean rhythm pattern when attributes change.
void updatePattern() { void updatePattern() {
// Fill current pattern with "padding" steps, then overwrite with hits
// and rests.
for (int i = 0; i < MAX_PATTERN_LEN; i++) {
pattern_[i] = PADDING;
}
// Populate the euclidean rhythm pattern according to the current
// instance variables.
byte bucket = 0; byte bucket = 0;
byte offset_ = 0; // temp disable pattern_[0] = HIT;
byte padding_ = 0; // temp disable
pattern_[offset_] = (hits_ > 0) ? HIT : REST;
for (int i = 1; i < steps_; i++) { for (int i = 1; i < steps_; i++) {
bucket += hits_; bucket += hits_;
if (bucket >= steps_) { if (bucket >= steps_) {
bucket -= steps_; bucket -= steps_;
pattern_[(i + offset_) % (steps_ + padding_)] = HIT; pattern_[i] = HIT;
} else { } else {
pattern_[(i + offset_) % (steps_ + padding_)] = REST; pattern_[i] = REST;
} }
} }
} }