after input and output clock setup support, rename some methods of uClock to avoid API usage miss intepretation.

develop
midilab 5 days ago
parent 45ecc4d2ca
commit 0d5ab3733c
  1. 80
      src/uClock.cpp
  2. 26
      src/uClock.h

@ -121,8 +121,8 @@ uClockClass::uClockClass()
start_timer = 0; start_timer = 0;
last_interval = 0; last_interval = 0;
sync_interval = 0; sync_interval = 0;
state = PAUSED; clock_state = PAUSED;
mode = INTERNAL_CLOCK; clock_mode = INTERNAL_CLOCK;
resetCounters(); resetCounters();
onPPQNCallback = nullptr; onPPQNCallback = nullptr;
@ -136,7 +136,7 @@ uClockClass::uClockClass()
onStepCallback = nullptr; onStepCallback = nullptr;
onClockStartCallback = nullptr; onClockStartCallback = nullptr;
onClockStopCallback = nullptr; onClockStopCallback = nullptr;
// first ppqn references calculus for ppqn and clock resolution // initialize reference data
calculateReferencedata(); calculateReferencedata();
} }
@ -149,38 +149,38 @@ void uClockClass::init()
uint32_t uClockClass::bpmToMicroSeconds(float bpm) uint32_t uClockClass::bpmToMicroSeconds(float bpm)
{ {
return (60000000.0f / (float)ppqn / bpm); return (60000000.0f / (float)output_ppqn / bpm);
} }
void uClockClass::calculateReferencedata() void uClockClass::calculateReferencedata()
{ {
mod_clock_ref = ppqn / clock_ppqn; mod_clock_ref = output_ppqn / input_ppqn;
mod_sync1_ref = ppqn / PPQN_1; mod_sync1_ref = output_ppqn / PPQN_1;
mod_sync2_ref = ppqn / PPQN_2; mod_sync2_ref = output_ppqn / PPQN_2;
mod_sync4_ref = ppqn / PPQN_4; mod_sync4_ref = output_ppqn / PPQN_4;
mod_sync8_ref = ppqn / PPQN_8; mod_sync8_ref = output_ppqn / PPQN_8;
mod_sync12_ref = ppqn / PPQN_12; mod_sync12_ref = output_ppqn / PPQN_12;
mod_sync24_ref = ppqn / PPQN_24; mod_sync24_ref = output_ppqn / PPQN_24;
mod_sync48_ref = ppqn / PPQN_48; mod_sync48_ref = output_ppqn / PPQN_48;
mod_step_ref = ppqn / 4; mod_step_ref = output_ppqn / 4;
} }
void uClockClass::setPPQN(PPQNResolution resolution) void uClockClass::setOutputPPQN(PPQNResolution resolution)
{ {
// dont allow PPQN lower than 4 for output clock (to avoid problems with mod_step_ref) // dont allow PPQN lower than PPQN_4 for output clock (to avoid problems with mod_step_ref)
if (resolution < PPQN_4) if (resolution < PPQN_4)
return; return;
ATOMIC( ATOMIC(
ppqn = resolution; output_ppqn = resolution;
calculateReferencedata(); calculateReferencedata();
) )
} }
void uClockClass::setClockPPQN(PPQNResolution resolution) void uClockClass::setInputPPQN(PPQNResolution resolution)
{ {
ATOMIC( ATOMIC(
clock_ppqn = resolution; input_ppqn = resolution;
calculateReferencedata(); calculateReferencedata();
) )
} }
@ -194,16 +194,16 @@ void uClockClass::start()
onClockStartCallback(); onClockStartCallback();
} }
if (mode == INTERNAL_CLOCK) { if (clock_mode == INTERNAL_CLOCK) {
state = STARTED; clock_state = STARTED;
} else { } else {
state = STARTING; clock_state = STARTING;
} }
} }
void uClockClass::stop() void uClockClass::stop()
{ {
state = PAUSED; clock_state = PAUSED;
start_timer = 0; start_timer = 0;
resetCounters(); resetCounters();
if (onClockStopCallback) { if (onClockStopCallback) {
@ -213,8 +213,8 @@ void uClockClass::stop()
void uClockClass::pause() void uClockClass::pause()
{ {
if (mode == INTERNAL_CLOCK) { if (clock_mode == INTERNAL_CLOCK) {
if (state == PAUSED) { if (clock_state == PAUSED) {
start(); start();
} else { } else {
stop(); stop();
@ -224,7 +224,7 @@ void uClockClass::pause()
void uClockClass::setTempo(float bpm) void uClockClass::setTempo(float bpm)
{ {
if (mode == EXTERNAL_CLOCK) { if (clock_mode == EXTERNAL_CLOCK) {
return; return;
} }
@ -241,7 +241,7 @@ void uClockClass::setTempo(float bpm)
float uClockClass::getTempo() float uClockClass::getTempo()
{ {
if (mode == EXTERNAL_CLOCK) { if (clock_mode == EXTERNAL_CLOCK) {
uint32_t acc = 0; uint32_t acc = 0;
// wait the buffer to get full // wait the buffer to get full
if (ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE-1] == 0) { if (ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE-1] == 0) {
@ -269,22 +269,22 @@ void uClockClass::run()
float inline uClockClass::freqToBpm(uint32_t freq) float inline uClockClass::freqToBpm(uint32_t freq)
{ {
float usecs = 1/((float)freq/1000000.0); float usecs = 1/((float)freq/1000000.0);
return (float)((float)(usecs/(float)clock_ppqn) * 60.0); return (float)((float)(usecs/(float)input_ppqn) * 60.0);
} }
void uClockClass::setMode(SyncMode tempo_mode) void uClockClass::setMode(ClockMode tempo_mode)
{ {
mode = tempo_mode; clock_mode = tempo_mode;
} }
uClockClass::SyncMode uClockClass::getMode() uClockClass::ClockMode uClockClass::getMode()
{ {
return mode; return clock_mode;
} }
void uClockClass::clockMe() void uClockClass::clockMe()
{ {
if (mode == EXTERNAL_CLOCK) { if (clock_mode == EXTERNAL_CLOCK) {
ATOMIC( ATOMIC(
handleExternalClock() handleExternalClock()
) )
@ -325,8 +325,8 @@ void uClockClass::resetCounters()
void uClockClass::tap() void uClockClass::tap()
{ {
// we can make use of mod_sync1_ref for tap // we can make use of mod_sync1_ref for tap
//uint8_t mod_tap_ref = ppqn / PPQN_1; //uint8_t mod_tap_ref = output_ppqn / PPQN_1;
// we only set tap if SyncMode is INTERNAL_CLOCK // we only set tap if ClockMode is INTERNAL_CLOCK
} }
void uClockClass::setShuffle(bool active) void uClockClass::setShuffle(bool active)
@ -399,7 +399,7 @@ bool inline uClockClass::processShuffle()
last_shff = shff; 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 // shuffle_shoot_ctrl helps keep track if we have shoot or not a note for the step space of output_ppqn/4 pulses
if (mod_shuffle == 0 && shuffle_shoot_ctrl == true) { if (mod_shuffle == 0 && shuffle_shoot_ctrl == true) {
// keep track of next note shuffle for current note lenght control // keep track of next note shuffle for current note lenght control
shuffle_length_ctrl = shuffle.step[(step_counter+1)%shuffle.size]; shuffle_length_ctrl = shuffle.step[(step_counter+1)%shuffle.size];
@ -416,12 +416,12 @@ bool inline uClockClass::processShuffle()
void uClockClass::handleExternalClock() void uClockClass::handleExternalClock()
{ {
switch (state) { switch (clock_state) {
case PAUSED: case PAUSED:
break; break;
case STARTING: case STARTING:
state = STARTED; clock_state = STARTED;
ext_clock_us = micros(); ext_clock_us = micros();
break; break;
@ -433,7 +433,7 @@ void uClockClass::handleExternalClock()
// external clock tick me! // external clock tick me!
ext_clock_tick++; ext_clock_tick++;
// accumulate interval incomming ticks data for getTempo() smooth reads on slave mode // accumulate interval incomming ticks data for getTempo() smooth reads on slave clock_mode
if(++ext_interval_idx >= EXT_INTERVAL_BUFFER_SIZE) { if(++ext_interval_idx >= EXT_INTERVAL_BUFFER_SIZE) {
ext_interval_idx = 0; ext_interval_idx = 0;
} }
@ -457,7 +457,7 @@ void uClockClass::handleTimerInt()
// process sync signals first please... // process sync signals first please...
if (mod_clock_counter == 0) { if (mod_clock_counter == 0) {
if (mode == EXTERNAL_CLOCK) { if (clock_mode == EXTERNAL_CLOCK) {
// sync tick position with external tick clock // sync tick position with external tick clock
if ((int_clock_tick < ext_clock_tick) || (int_clock_tick > (ext_clock_tick + 1))) { if ((int_clock_tick < ext_clock_tick) || (int_clock_tick > (ext_clock_tick + 1))) {
int_clock_tick = ext_clock_tick; int_clock_tick = ext_clock_tick;
@ -649,7 +649,7 @@ void uClockHandler()
// global timer counter // global timer counter
_millis = millis(); _millis = millis();
if (uClock.state == uClock.STARTED) { if (uClock.clock_state == uClock.STARTED) {
uClock.handleTimerInt(); uClock.handleTimerInt();
} }
} }

@ -34,7 +34,9 @@
namespace umodular { namespace clock { namespace umodular { namespace clock {
// min: -(ppqn/4)-1 step, max: (ppqn/4)-1 steps // Shuffle templates are specific for each PPQN output resolution
// min: -(output_ppqn/4)-1 ticks
// max: (output_ppqn/4)-1 ticks
// adjust the size of you template if more than 16 shuffle step info needed // adjust the size of you template if more than 16 shuffle step info needed
#define MAX_SHUFFLE_TEMPLATE_SIZE 16 #define MAX_SHUFFLE_TEMPLATE_SIZE 16
typedef struct { typedef struct {
@ -62,7 +64,7 @@ typedef struct {
class uClockClass { class uClockClass {
public: public:
enum SyncMode { enum ClockMode {
INTERNAL_CLOCK = 0, INTERNAL_CLOCK = 0,
EXTERNAL_CLOCK EXTERNAL_CLOCK
}; };
@ -87,11 +89,11 @@ class uClockClass {
PPQN_960 = 960 PPQN_960 = 960
}; };
ClockState state; ClockState clock_state;
uClockClass(); uClockClass();
void setOnPPQN(void (*callback)(uint32_t tick)) { void setOnOutputPPQN(void (*callback)(uint32_t tick)) {
onPPQNCallback = callback; onPPQNCallback = callback;
} }
@ -137,8 +139,8 @@ class uClockClass {
} }
void init(); void init();
void setPPQN(PPQNResolution resolution); void setOutputPPQN(PPQNResolution resolution);
void setClockPPQN(PPQNResolution resolution); void setInputPPQN(PPQNResolution resolution);
void handleTimerInt(); void handleTimerInt();
void handleExternalClock(); void handleExternalClock();
@ -155,8 +157,8 @@ class uClockClass {
void run(); void run();
// external timming control // external timming control
void setMode(SyncMode tempo_mode); void setClockMode(ClockMode tempo_mode);
SyncMode getMode(); ClockMode getClockMode();
void clockMe(); void clockMe();
// shuffle // shuffle
@ -200,9 +202,9 @@ class uClockClass {
void (*onClockStartCallback)(); void (*onClockStartCallback)();
void (*onClockStopCallback)(); void (*onClockStopCallback)();
// clock control // clock input/output control
PPQNResolution ppqn = PPQN_96; PPQNResolution output_ppqn = PPQN_96;
PPQNResolution clock_ppqn = PPQN_24; PPQNResolution input_ppqn = PPQN_24;
// output and internal counters, ticks and references // output and internal counters, ticks and references
uint32_t tick; uint32_t tick;
uint32_t int_clock_tick; uint32_t int_clock_tick;
@ -242,7 +244,7 @@ class uClockClass {
float tempo; float tempo;
uint32_t start_timer; uint32_t start_timer;
SyncMode mode; ClockMode clock_mode;
volatile uint32_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE]; volatile uint32_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE];
uint16_t ext_interval_idx; uint16_t ext_interval_idx;

Loading…
Cancel
Save