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. 148
      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
static uint32_t phase_mult(uint32_t val)
{
@ -154,9 +93,19 @@ void uClockClass::start()
div96th_counter = 0;
div32th_counter = 0;
div16th_counter = 0;
if (onClockStartCallback) {
onClockStartCallback();
}
} else {
//if (mode == EXTERNAL_CLOCK) {
init();
state = STARTING;
mod6_counter = 0;
div96th_counter = 0;
div32th_counter = 0;
div16th_counter = 0;
counter = 0;
}
if (onClockStartCallback) {
onClockStartCallback();
}
}
@ -164,25 +113,33 @@ void uClockClass::stop()
{
if (mode == INTERNAL_CLOCK) {
state = PAUSED;
if (onClockStopCallback) {
onClockStopCallback();
}
} else {
//if (mode == EXTERNAL_CLOCK) {
state = PAUSED;
}
if (onClockStopCallback) {
onClockStopCallback();
}
}
void uClockClass::pause()
{
if (mode == INTERNAL_CLOCK) {
//if (mode == INTERNAL_CLOCK) {
if (state == PAUSED) {
start();
} else {
stop();
}
}
//}
}
void uClockClass::setTempo(uint16_t _tempo)
{
if (mode == EXTERNAL_CLOCK) {
return;
}
if ( tempo == _tempo ) {
return;
}
@ -198,6 +155,11 @@ uint16_t uClockClass::getTempo()
return tempo;
}
void uClockClass::setMode(uint8_t tempo_mode)
{
mode = tempo_mode;
}
void uClockClass::clockMe()
{
if (uClock.mode == uClock.EXTERNAL_CLOCK) {
@ -217,6 +179,54 @@ void uClockClass::shuffle()
// 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()
{
if (counter == 0) {

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

Loading…
Cancel
Save