diff --git a/src/uClock.cpp b/src/uClock.cpp index 9f27935..3ffaf0e 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -128,6 +128,7 @@ uClockClass::uClockClass() onPPQNCallback = nullptr; onSync24Callback = nullptr; onStepCallback = nullptr; + onTrackStepCallback = nullptr; onClockStartCallback = nullptr; onClockStopCallback = nullptr; // first ppqn references calculus @@ -274,6 +275,11 @@ void uClockClass::resetCounters() ext_clock_tick = 0; ext_clock_us = 0; ext_interval_idx = 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++) { ext_interval_buffer[i] = 0; @@ -291,11 +297,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) @@ -303,6 +319,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) @@ -310,6 +333,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]); @@ -321,11 +351,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) { @@ -345,8 +391,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) @@ -371,6 +420,56 @@ bool inline uClockClass::processShuffle() return false; } +bool inline uClockClass::processTrackShuffle(uint8_t track) +{ + if (!track_shuffles[track].shuffle.active) { + 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[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 (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) { + 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); + + 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) { + 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; + + return true; + } + + return false; +} + // it is expected to be called in 24PPQN void uClockClass::handleExternalClock() { @@ -458,6 +557,24 @@ void uClockClass::handleTimerInt() onPPQNCallback(tick); } + 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++) + { + if (processTrackShuffle(t)) { + onTrackStepCallback(t, track_step_counter[t]); + // going forward to the next step call + ++track_step_counter[t]; + } + } + } + // reset step mod counter reference ? if (mod_step_counter == mod_step_ref) mod_step_counter = 0; @@ -478,6 +595,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 df1a08d..f9727ba 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -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; @@ -144,8 +159,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(); @@ -165,9 +191,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)(); @@ -180,8 +208,10 @@ 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]; // external clock control volatile uint32_t ext_clock_us; @@ -198,6 +228,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;