From 7e09f5a15c290407aa73164549a150160c203cc7 Mon Sep 17 00:00:00 2001 From: James Gray Date: Sun, 8 Sep 2024 17:54:45 -0400 Subject: [PATCH 1/5] Add track-based callback with shuffle per track --- src/uClock.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++---- src/uClock.h | 35 +++++++++++++++- 2 files changed, 136 insertions(+), 10 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index 8196f49..d125e2f 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -1,8 +1,8 @@ /*! * @file uClock.cpp * Project BPM clock generator for Arduino - * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(RPI2040, Teensy, Seedstudio XIAO M0 and ESP32) - * @version 2.1.0 + * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32) + * @version 2.0.0 * @author Romulo Silva * @date 10/06/2017 * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co @@ -57,12 +57,6 @@ #if defined(ARDUINO_ARCH_STM32) #include "platforms/stm32.h" #endif -// -// RP2040 (Raspberry Pico) family -// -#if defined(ARDUINO_ARCH_RP2040) - #include "platforms/rp2040.h" -#endif // // Platform specific timer setup/control @@ -110,6 +104,7 @@ uClockClass::uClockClass() onPPQNCallback = nullptr; onSync24Callback = nullptr; onStepCallback = nullptr; + onTrackStepCallback = nullptr; onClockStartCallback = nullptr; onClockStopCallback = nullptr; // first ppqn references calculus @@ -247,6 +242,10 @@ void uClockClass::resetCounters() ext_clock_tick = 0; ext_clock_us = 0; ext_interval_idx = 0; + + for (uint32_t s=0; s < MAX_TRACKS; s++) { + track_step_counter[s] = 0; + } for (uint8_t i=0; i < EXT_INTERVAL_BUFFER_SIZE; i++) { ext_interval_buffer[i] = 0; @@ -264,11 +263,21 @@ void uClockClass::setShuffle(bool active) ATOMIC(shuffle.active = active) } +void uClockClass::setTrackShuffle(uint8_t track, bool active) +{ + ATOMIC(track_shuffles[track].shuffle.active = active) +} + bool uClockClass::isShuffled() { return shuffle.active; } +bool uClockClass::isTrackShuffled(uint8_t track) +{ + return track_shuffles[track].shuffle.active; +} + void uClockClass::setShuffleSize(uint8_t size) { if (size > MAX_SHUFFLE_TEMPLATE_SIZE) @@ -276,6 +285,13 @@ void uClockClass::setShuffleSize(uint8_t size) ATOMIC(shuffle.size = size) } +void uClockClass::setTrackShuffleSize(uint8_t track, uint8_t size) +{ + if (size > MAX_SHUFFLE_TEMPLATE_SIZE) + size = MAX_SHUFFLE_TEMPLATE_SIZE; + ATOMIC(track_shuffles[track].shuffle.size = size) +} + void uClockClass::setShuffleData(uint8_t step, int8_t tick) { if (step >= MAX_SHUFFLE_TEMPLATE_SIZE) @@ -283,6 +299,13 @@ void uClockClass::setShuffleData(uint8_t step, int8_t tick) ATOMIC(shuffle.step[step] = tick) } +void uClockClass::setTrackShuffleData(uint8_t track, uint8_t step, int8_t tick) +{ + if (step >= MAX_SHUFFLE_TEMPLATE_SIZE) + return; + ATOMIC(track_shuffles[track].shuffle.step[step] = tick) +} + void uClockClass::setShuffleTemplate(int8_t * shuff, uint8_t size) { //uint8_t size = sizeof(shuff) / sizeof(shuff[0]); @@ -294,11 +317,27 @@ void uClockClass::setShuffleTemplate(int8_t * shuff, uint8_t size) } } +void uClockClass::setTrackShuffleTemplate(uint8_t track, int8_t * shuff, uint8_t size) +{ + //uint8_t size = sizeof(shuff) / sizeof(shuff[0]); + if (size > MAX_SHUFFLE_TEMPLATE_SIZE) + size = MAX_SHUFFLE_TEMPLATE_SIZE; + ATOMIC(track_shuffles[track].shuffle.size = size) + for (uint8_t i=0; i < size; i++) { + setTrackShuffleData(track, i, shuff[i]); + } +} + int8_t uClockClass::getShuffleLength() { return shuffle_length_ctrl; } +int8_t uClockClass::getTrackShuffleLength(uint8_t track) +{ + return track_shuffles[track].shuffle_length_ctrl; +} + bool inline uClockClass::processShuffle() { if (!shuffle.active) { @@ -344,6 +383,51 @@ bool inline uClockClass::processShuffle() return false; } +bool inline uClockClass::processTrackShuffle(uint8_t track) +{ + if (!track_shuffles[track].shuffle.active) { + return mod_step_counter == 0; + } + + int8_t mod_shuffle = 0; + + // check shuffle template of current + int8_t shff = track_shuffles[track].shuffle.step[step_counter%track_shuffles[track].shuffle.size]; + + if (track_shuffles[track].shuffle_shoot_ctrl == false && mod_step_counter == 0) + track_shuffles[track].shuffle_shoot_ctrl = true; + + //if (mod_step_counter == mod_step_ref-1) + + if (shff >= 0) { + mod_shuffle = mod_step_counter - shff; + // any late shuffle? we should skip next mod_step_counter == 0 + if (track_shuffles[track].last_shff < 0 && mod_step_counter != 1) + return false; + } else if (shff < 0) { + mod_shuffle = mod_step_counter - (mod_step_ref + shff); + //if (last_shff < 0 && mod_step_counter != 1) + // return false; + track_shuffles[track].shuffle_shoot_ctrl = true; + } + + track_shuffles[track].last_shff = shff; + + // shuffle_shoot_ctrl helps keep track if we have shoot or not a note for the step space of ppqn/4 pulses + if (mod_shuffle == 0 && track_shuffles[track].shuffle_shoot_ctrl == true) { + // keep track of next note shuffle for current note lenght control + track_shuffles[track].shuffle_length_ctrl = track_shuffles[track].shuffle.step[(step_counter+1)%track_shuffles[track].shuffle.size]; + if (shff > 0) + track_shuffles[track].shuffle_length_ctrl -= shff; + if (shff < 0) + track_shuffles[track].shuffle_length_ctrl += shff; + track_shuffles[track].shuffle_shoot_ctrl = false; + return true; + } + + return false; +} + // it is expected to be called in 24PPQN void uClockClass::handleExternalClock() { @@ -434,6 +518,17 @@ void uClockClass::handleTimerInt() // reset step mod counter reference ? if (mod_step_counter == mod_step_ref) mod_step_counter = 0; + + if (onTrackStepCallback) { + for (uint8_t t = 0; t < MAX_TRACKS; t++) + { + if (processTrackShuffle(t)) { + onTrackStepCallback(t, track_step_counter[t]); + // going forward to the next step call + ++track_step_counter[t]; + } + } + } // step callback to support 16th old school style sequencers // with builtin shuffle for this callback only diff --git a/src/uClock.h b/src/uClock.h index 9b438c2..41caf28 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -1,8 +1,8 @@ /*! * @file uClock.h * Project BPM clock generator for Arduino - * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(RPI2040, Teensy, Seedstudio XIAO M0 and ESP32) - * @version 2.1.0 + * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32) + * @version 2.0.0 * @author Romulo Silva * @date 10/06/2017 * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co @@ -44,6 +44,10 @@ namespace umodular { namespace clock { // 48 PPQN (12 pulses per step) // 96 PPQN (24 pulses per step) +// For track-based sequencers, this controls how many tracks can be +// used with the onTrackStepCallback +#define MAX_TRACKS 16 + // min: -(ppqn/4)-1 step, max: (ppqn/4)-1 steps // adjust the size of you template if more than 16 shuffle step info needed #define MAX_SHUFFLE_TEMPLATE_SIZE 16 @@ -53,6 +57,13 @@ typedef struct { int8_t step[MAX_SHUFFLE_TEMPLATE_SIZE] = {0}; } SHUFFLE_TEMPLATE; +typedef struct { + volatile SHUFFLE_TEMPLATE shuffle; + int8_t last_shff = 0; + bool shuffle_shoot_ctrl = true; + volatile int8_t shuffle_length_ctrl = 0; +} TRACK_SHUFFLE; + // for smooth slave tempo calculate display you should raise this value // in between 64 to 128. // note: this doesn't impact on sync time, only display time getTempo() @@ -103,6 +114,10 @@ class uClockClass { void setOnStep(void (*callback)(uint32_t step)) { onStepCallback = callback; } + + void setTrackOnStep(void (*callback)(uint8_t track, uint32_t step)) { + onTrackStepCallback = callback; + } void setOnSync24(void (*callback)(uint32_t tick)) { onSync24Callback = callback; @@ -141,8 +156,19 @@ class uClockClass { void setShuffleSize(uint8_t size); void setShuffleData(uint8_t step, int8_t tick); void setShuffleTemplate(int8_t * shuff, uint8_t size); + // use this to know how many positive or negative ticks to add to current note length int8_t getShuffleLength(); + + // track shuffle + void setTrackShuffle(uint8_t track, bool active); + bool isTrackShuffled(uint8_t track); + void setTrackShuffleSize(uint8_t track, uint8_t size); + void setTrackShuffleData(uint8_t track, uint8_t step, int8_t tick); + void setTrackShuffleTemplate(uint8_t track, int8_t * shuff, uint8_t size); + + // use this to know how many positive or negative ticks to add to current note length + int8_t getTrackShuffleLength(uint8_t track); // todo! void tap(); @@ -162,9 +188,11 @@ class uClockClass { // shuffle bool inline processShuffle(); + bool inline processTrackShuffle(uint8_t track); void (*onPPQNCallback)(uint32_t tick); void (*onStepCallback)(uint32_t step); + void (*onTrackStepCallback)(uint8_t track, uint32_t step); void (*onSync24Callback)(uint32_t tick); void (*onClockStartCallback)(); void (*onClockStopCallback)(); @@ -179,6 +207,7 @@ class uClockClass { uint8_t mod_step_counter; uint8_t mod_step_ref; uint32_t step_counter; // should we go uint16_t? + uint32_t track_step_counter[MAX_TRACKS]; // external clock control volatile uint32_t ext_clock_us; @@ -195,6 +224,8 @@ class uClockClass { uint16_t ext_interval_idx; // shuffle implementation + TRACK_SHUFFLE track_shuffles[MAX_TRACKS]; + volatile SHUFFLE_TEMPLATE shuffle; int8_t last_shff = 0; bool shuffle_shoot_ctrl = true; From 1a7a984d6c8b6bc33f3195789874f72f198bc22f Mon Sep 17 00:00:00 2001 From: James Gray Date: Sun, 8 Sep 2024 18:01:52 -0400 Subject: [PATCH 2/5] pull latest, plus version bump --- src/uClock.cpp | 10 ++++++++-- src/uClock.h | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index d125e2f..a082fd9 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -1,8 +1,8 @@ /*! * @file uClock.cpp * Project BPM clock generator for Arduino - * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32) - * @version 2.0.0 + * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(RPI2040, Teensy, Seedstudio XIAO M0 and ESP32) + * @version 2.2.0 * @author Romulo Silva * @date 10/06/2017 * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co @@ -57,6 +57,12 @@ #if defined(ARDUINO_ARCH_STM32) #include "platforms/stm32.h" #endif +// +// RP2040 (Raspberry Pico) family +// +#if defined(ARDUINO_ARCH_RP2040) + #include "platforms/rp2040.h" +#endif // // Platform specific timer setup/control diff --git a/src/uClock.h b/src/uClock.h index 41caf28..fc73fe1 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -1,8 +1,8 @@ /*! * @file uClock.h * Project BPM clock generator for Arduino - * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32) - * @version 2.0.0 + * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(RPI2040, Teensy, Seedstudio XIAO M0 and ESP32) + * @version 2.2.0 * @author Romulo Silva * @date 10/06/2017 * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co From 24c8cdfb4047b20c401ef554bc900fa452fde177 Mon Sep 17 00:00:00 2001 From: James Gray Date: Sun, 8 Sep 2024 21:28:02 -0400 Subject: [PATCH 3/5] fix some bugs? --- src/uClock.cpp | 43 ++++++++++++++++++++++++++++--------------- src/uClock.h | 1 + 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index a082fd9..77abbf0 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -249,8 +249,9 @@ void uClockClass::resetCounters() ext_clock_us = 0; ext_interval_idx = 0; - for (uint32_t s=0; s < MAX_TRACKS; s++) { - track_step_counter[s] = 0; + for (uint32_t t=0; t < MAX_TRACKS; t++) { + mod_track_step_counter[t] = 0; + track_step_counter[t] = 0; } for (uint8_t i=0; i < EXT_INTERVAL_BUFFER_SIZE; i++) { @@ -392,27 +393,27 @@ bool inline uClockClass::processShuffle() bool inline uClockClass::processTrackShuffle(uint8_t track) { if (!track_shuffles[track].shuffle.active) { - return mod_step_counter == 0; + return mod_track_step_counter[track] == 0; } int8_t mod_shuffle = 0; // check shuffle template of current - int8_t shff = track_shuffles[track].shuffle.step[step_counter%track_shuffles[track].shuffle.size]; + int8_t shff = track_shuffles[track].shuffle.step[track_step_counter[track]%track_shuffles[track].shuffle.size]; - if (track_shuffles[track].shuffle_shoot_ctrl == false && mod_step_counter == 0) + if (track_shuffles[track].shuffle_shoot_ctrl == false && mod_track_step_counter[track] == 0) track_shuffles[track].shuffle_shoot_ctrl = true; - //if (mod_step_counter == mod_step_ref-1) + //if (mod_track_step_counter[track] == mod_step_ref-1) if (shff >= 0) { - mod_shuffle = mod_step_counter - shff; - // any late shuffle? we should skip next mod_step_counter == 0 - if (track_shuffles[track].last_shff < 0 && mod_step_counter != 1) + mod_shuffle = mod_track_step_counter[track] - shff; + // any late shuffle? we should skip next mod_track_step_counter == 0 + if (track_shuffles[track].last_shff < 0 && mod_track_step_counter[track] != 1) return false; } else if (shff < 0) { - mod_shuffle = mod_step_counter - (mod_step_ref + shff); - //if (last_shff < 0 && mod_step_counter != 1) + mod_shuffle = mod_track_step_counter[track] - (mod_step_ref + shff); + //if (last_shff < 0 && mod_track_step_counter[track] != 1) // return false; track_shuffles[track].shuffle_shoot_ctrl = true; } @@ -422,7 +423,7 @@ bool inline uClockClass::processTrackShuffle(uint8_t track) // shuffle_shoot_ctrl helps keep track if we have shoot or not a note for the step space of ppqn/4 pulses if (mod_shuffle == 0 && track_shuffles[track].shuffle_shoot_ctrl == true) { // keep track of next note shuffle for current note lenght control - track_shuffles[track].shuffle_length_ctrl = track_shuffles[track].shuffle.step[(step_counter+1)%track_shuffles[track].shuffle.size]; + track_shuffles[track].shuffle_length_ctrl = track_shuffles[track].shuffle.step[(track_step_counter[track]+1)%track_shuffles[track].shuffle.size]; if (shff > 0) track_shuffles[track].shuffle_length_ctrl -= shff; if (shff < 0) @@ -521,9 +522,12 @@ void uClockClass::handleTimerInt() onPPQNCallback(tick); } - // reset step mod counter reference ? - if (mod_step_counter == mod_step_ref) - mod_step_counter = 0; + for (uint8_t tm = 0; tm < MAX_TRACKS; tm++) + { + // reset track step mod counter reference ? + if (mod_track_step_counter[tm] == mod_step_ref) + mod_track_step_counter[tm] = 0; + } if (onTrackStepCallback) { for (uint8_t t = 0; t < MAX_TRACKS; t++) @@ -535,6 +539,10 @@ void uClockClass::handleTimerInt() } } } + + // reset step mod counter reference ? + if (mod_step_counter == mod_step_ref) + mod_step_counter = 0; // step callback to support 16th old school style sequencers // with builtin shuffle for this callback only @@ -552,6 +560,11 @@ void uClockClass::handleTimerInt() // increment mod counters ++mod24_counter; ++mod_step_counter; + + for (uint8_t tsm = 0; tsm < MAX_TRACKS; tsm++) + { + ++mod_track_step_counter[tsm]; + } } // elapsed time support diff --git a/src/uClock.h b/src/uClock.h index fc73fe1..3aa8d27 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -205,6 +205,7 @@ class uClockClass { uint8_t mod24_counter; uint8_t mod24_ref; uint8_t mod_step_counter; + uint8_t mod_track_step_counter[MAX_TRACKS]; uint8_t mod_step_ref; uint32_t step_counter; // should we go uint16_t? uint32_t track_step_counter[MAX_TRACKS]; From 1bdaa5c3b3ba8e1a8cf4519a7662e59b3fb54116 Mon Sep 17 00:00:00 2001 From: James Gray Date: Mon, 9 Sep 2024 22:23:17 -0400 Subject: [PATCH 4/5] wait for late shuffle to get caught up --- src/uClock.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index 77abbf0..59fd7ea 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -400,35 +400,40 @@ bool inline uClockClass::processTrackShuffle(uint8_t track) // check shuffle template of current int8_t shff = track_shuffles[track].shuffle.step[track_step_counter[track]%track_shuffles[track].shuffle.size]; - + if (track_shuffles[track].shuffle_shoot_ctrl == false && mod_track_step_counter[track] == 0) - track_shuffles[track].shuffle_shoot_ctrl = true; - - //if (mod_track_step_counter[track] == mod_step_ref-1) + track_shuffles[track].shuffle_shoot_ctrl = true; if (shff >= 0) { mod_shuffle = mod_track_step_counter[track] - shff; + // any late shuffle? we should skip next mod_track_step_counter == 0 - if (track_shuffles[track].last_shff < 0 && mod_track_step_counter[track] != 1) - return false; + if (track_shuffles[track].last_shff < 0 && mod_track_step_counter[track] != 1) { + if (track_shuffles[track].shuffle_shoot_ctrl == true) + track_shuffles[track].shuffle_shoot_ctrl = false; + + return false; + } + } else if (shff < 0) { mod_shuffle = mod_track_step_counter[track] - (mod_step_ref + shff); - //if (last_shff < 0 && mod_track_step_counter[track] != 1) - // return false; + track_shuffles[track].shuffle_shoot_ctrl = true; } track_shuffles[track].last_shff = shff; // shuffle_shoot_ctrl helps keep track if we have shoot or not a note for the step space of ppqn/4 pulses - if (mod_shuffle == 0 && track_shuffles[track].shuffle_shoot_ctrl == true) { - // keep track of next note shuffle for current note lenght control + if (mod_shuffle == 0 && track_shuffles[track].shuffle_shoot_ctrl == true) { + track_shuffles[track].shuffle_shoot_ctrl = false; + + // // keep track of next note shuffle for current note lenght control track_shuffles[track].shuffle_length_ctrl = track_shuffles[track].shuffle.step[(track_step_counter[track]+1)%track_shuffles[track].shuffle.size]; if (shff > 0) track_shuffles[track].shuffle_length_ctrl -= shff; if (shff < 0) track_shuffles[track].shuffle_length_ctrl += shff; - track_shuffles[track].shuffle_shoot_ctrl = false; + return true; } From 4161f6b124f8d00e6fe5ca1a80466eedff82c459 Mon Sep 17 00:00:00 2001 From: James Gray Date: Mon, 9 Sep 2024 22:25:40 -0400 Subject: [PATCH 5/5] apply fix to global shuffle --- src/uClock.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index 59fd7ea..ab9be49 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -364,8 +364,11 @@ bool inline uClockClass::processShuffle() if (shff >= 0) { mod_shuffle = mod_step_counter - shff; // any late shuffle? we should skip next mod_step_counter == 0 - if (last_shff < 0 && mod_step_counter != 1) - return false; + if (last_shff < 0 && mod_step_counter != 1) { + if (shuffle_shoot_ctrl == true) + shuffle_shoot_ctrl = false; + return false; + } } else if (shff < 0) { mod_shuffle = mod_step_counter - (mod_step_ref + shff); //if (last_shff < 0 && mod_step_counter != 1)