Add track-based callback with shuffle per track

pull/42/head
James Gray 2 months ago
parent c0196f31a2
commit 7e09f5a15c
  1. 111
      src/uClock.cpp
  2. 35
      src/uClock.h

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

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

Loading…
Cancel
Save