From 81b189186d63c4918ae5d44344cfeda23218b4c0 Mon Sep 17 00:00:00 2001 From: midilab Date: Fri, 1 Dec 2017 13:35:44 -0200 Subject: [PATCH] Added external clock support. Based on MIDI standard clock pulse. Use it via clockMe() on each received clock midi message --- src/uClock.cpp | 152 ++++++++++++++++++++++++++----------------------- src/uClock.h | 6 +- 2 files changed, 83 insertions(+), 75 deletions(-) diff --git a/src/uClock.cpp b/src/uClock.cpp index c895e5c..2773f9f 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -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,35 +93,53 @@ 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(); + } } 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) { diff --git a/src/uClock.h b/src/uClock.h index 1a8a32c..220f78f 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -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();