added setExtIntervalBuffer(uint8_t buffer_size) for getTempo() average calculus setup. should be called before uClock.init();

pull/55/head
midilab 3 weeks ago
parent 0c20a494eb
commit 3ea0983dd6
  1. 23
      src/uClock.cpp
  2. 15
      src/uClock.h

@ -142,6 +142,9 @@ uClockClass::uClockClass()
void uClockClass::init()
{
if (ext_interval_buffer == nullptr)
setExtIntervalBuffer(1);
uclockInitTimer();
// first interval calculus
setTempo(tempo);
@ -244,14 +247,14 @@ float uClockClass::getTempo()
if (clock_mode == EXTERNAL_CLOCK) {
uint32_t acc = 0;
// 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) {
return tempo;
}
for (uint8_t i=0; i < EXT_INTERVAL_BUFFER_SIZE; i++) {
for (uint8_t i=0; i < ext_interval_buffer_size; i++) {
acc += ext_interval_buffer[i];
}
if (acc != 0) {
return freqToBpm(acc / EXT_INTERVAL_BUFFER_SIZE);
return freqToBpm(acc / ext_interval_buffer_size);
}
}
return tempo;
@ -291,6 +294,16 @@ void uClockClass::clockMe()
}
}
void uClockClass::setExtIntervalBuffer(uint8_t buffer_size)
{
if (ext_interval_buffer != nullptr)
return;
// alloc once and forever policy
ext_interval_buffer_size = buffer_size;
ext_interval_buffer = (uint32_t*) malloc( sizeof(uint32_t) * ext_interval_buffer_size );
}
void uClockClass::resetCounters()
{
tick = 0;
@ -317,7 +330,7 @@ void uClockClass::resetCounters()
mod_sync48_counter = 0;
sync48_tick = 0;
for (uint8_t i=0; i < EXT_INTERVAL_BUFFER_SIZE; i++) {
for (uint8_t i=0; i < ext_interval_buffer_size; i++) {
ext_interval_buffer[i] = 0;
}
}
@ -434,7 +447,7 @@ void uClockClass::handleExternalClock()
ext_clock_tick++;
// 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_buffer[ext_interval_idx] = last_interval;

@ -45,12 +45,6 @@ typedef struct {
int8_t step[MAX_SHUFFLE_TEMPLATE_SIZE] = {0};
} SHUFFLE_TEMPLATE;
// 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()
// if you dont want to use it, set it to 1 for memory save
#define EXT_INTERVAL_BUFFER_SIZE 128
#define MIN_BPM 1
#define MAX_BPM 400
@ -160,6 +154,11 @@ class uClockClass {
void setClockMode(ClockMode tempo_mode);
ClockMode getClockMode();
void clockMe();
// for smooth slave tempo calculate display you should raise the
// buffer_size of ext_interval_buffer in between 64 to 128. 254 max size.
// note: this doesn't impact on sync time, only display time getTempo()
// if you dont want to use it, it is default set it to 1 for memory save
void setExtIntervalBuffer(uint8_t buffer_size);
// shuffle
void setShuffle(bool active);
@ -246,7 +245,8 @@ class uClockClass {
uint32_t start_timer;
ClockMode clock_mode;
volatile uint32_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE];
volatile uint32_t * ext_interval_buffer = nullptr;
uint8_t ext_interval_buffer_size;
uint16_t ext_interval_idx;
// shuffle implementation
@ -265,4 +265,3 @@ extern "C" {
}
#endif /* __U_CLOCK_H__ */

Loading…
Cancel
Save