Added external clock support. Based on MIDI standard clock pulse. Use it via clockMe() on each received clock midi message

pull/7/head
midilab 7 years ago
parent 9db6fafa7b
commit 81b189186d
  1. 152
      src/uClock.cpp
  2. 6
      src/uClock.h

@ -79,67 +79,6 @@ uint16_t clock_diff(uint16_t old_clock, uint16_t new_clock)
} }
} }
void uClockClass::handleClock()
{
uint16_t cur_clock = _clock;
uint16_t diff;
if (cur_clock > last_clock) {
diff = cur_clock - last_clock;
} else {
diff = cur_clock + (65535 - last_clock);
}
last_interval = diff;
last_clock = cur_clock;
indiv96th_counter++;
inmod6_counter++;
if (inmod6_counter == 6) {
inmod6_counter = 0;
}
switch (state) {
case PAUSED:
break;
case STARTING:
state = STARTED;
break;
case STARTED:
if (indiv96th_counter == 2) {
interval = diff;
} else {
interval = (((uint32_t)interval * (uint32_t)pll_x) + (uint32_t)(256 - pll_x) * (uint32_t)diff) >> 8;
}
break;
}
}
void uClockClass::handleStart()
{
if (mode == EXTERNAL_CLOCK) {
init();
state = STARTING;
mod6_counter = 0;
div96th_counter = 0;
div32th_counter = 0;
div16th_counter = 0;
counter = 0;
}
}
void uClockClass::handleStop()
{
if (mode == EXTERNAL_CLOCK) {
state = PAUSED;
}
}
#define PHASE_FACTOR 16 #define PHASE_FACTOR 16
static uint32_t phase_mult(uint32_t val) static uint32_t phase_mult(uint32_t val)
{ {
@ -154,35 +93,53 @@ void uClockClass::start()
div96th_counter = 0; div96th_counter = 0;
div32th_counter = 0; div32th_counter = 0;
div16th_counter = 0; div16th_counter = 0;
if (onClockStartCallback) { } else {
onClockStartCallback(); //if (mode == EXTERNAL_CLOCK) {
} init();
} state = STARTING;
mod6_counter = 0;
div96th_counter = 0;
div32th_counter = 0;
div16th_counter = 0;
counter = 0;
}
if (onClockStartCallback) {
onClockStartCallback();
}
} }
void uClockClass::stop() void uClockClass::stop()
{ {
if (mode == INTERNAL_CLOCK) { if (mode == INTERNAL_CLOCK) {
state = PAUSED; state = PAUSED;
if (onClockStopCallback) { } else {
onClockStopCallback(); //if (mode == EXTERNAL_CLOCK) {
} state = PAUSED;
} }
if (onClockStopCallback) {
onClockStopCallback();
}
} }
void uClockClass::pause() void uClockClass::pause()
{ {
if (mode == INTERNAL_CLOCK) { //if (mode == INTERNAL_CLOCK) {
if (state == PAUSED) { if (state == PAUSED) {
start(); start();
} else { } else {
stop(); stop();
} }
} //}
} }
void uClockClass::setTempo(uint16_t _tempo) void uClockClass::setTempo(uint16_t _tempo)
{ {
if (mode == EXTERNAL_CLOCK) {
return;
}
if ( tempo == _tempo ) { if ( tempo == _tempo ) {
return; return;
} }
@ -198,6 +155,11 @@ uint16_t uClockClass::getTempo()
return tempo; return tempo;
} }
void uClockClass::setMode(uint8_t tempo_mode)
{
mode = tempo_mode;
}
void uClockClass::clockMe() void uClockClass::clockMe()
{ {
if (uClock.mode == uClock.EXTERNAL_CLOCK) { if (uClock.mode == uClock.EXTERNAL_CLOCK) {
@ -217,6 +179,54 @@ void uClockClass::shuffle()
// shuffle me // shuffle me
} }
void uClockClass::handleClock()
{
uint16_t cur_clock = _clock;
uint16_t diff;
if (cur_clock > last_clock) {
diff = cur_clock - last_clock;
} else {
diff = cur_clock + (65535 - last_clock);
}
last_interval = diff;
last_clock = cur_clock;
indiv96th_counter++;
inmod6_counter++;
if (inmod6_counter == 6) {
inmod6_counter = 0;
}
switch (state) {
case PAUSED:
break;
case STARTING:
state = STARTED;
break;
case STARTED:
if (indiv96th_counter == 2) {
interval = diff;
} else {
interval = (((uint32_t)interval * (uint32_t)pll_x) + (uint32_t)(256 - pll_x) * (uint32_t)diff) >> 8;
}
break;
/*
interval = (uint32_t)((uint32_t)156250 / tempo) - 16;
interval = x(156250 / tempo) - 16;
x(156250 / tempo) = -16
*/
}
}
void uClockClass::handleTimerInt() void uClockClass::handleTimerInt()
{ {
if (counter == 0) { if (counter == 0) {

@ -65,8 +65,7 @@ class uClockClass {
} state; } state;
enum { enum {
OFF = 0, INTERNAL_CLOCK = 0,
INTERNAL_CLOCK,
EXTERNAL_CLOCK EXTERNAL_CLOCK
} mode; } mode;
@ -94,8 +93,6 @@ class uClockClass {
void init(); void init();
void handleClock(); void handleClock();
void handleStart();
void handleStop();
void handleTimerInt(); void handleTimerInt();
// external class control // external class control
@ -106,6 +103,7 @@ class uClockClass {
uint16_t getTempo(); uint16_t getTempo();
// External timming control // External timming control
void setMode(uint8_t tempo_mode);
void clockMe(); void clockMe();
void shuffle(); void shuffle();

Loading…
Cancel
Save