prepare for release, beta test, and missing features to implement

pull/32/head
midilab 10 months ago
parent a3aeae9c83
commit b35ba50dc1
  1. 55
      README.md
  2. 2
      library.json
  3. 2
      library.properties
  4. 234
      src/uClock.cpp
  5. 267
      src/uClock.h

@ -9,15 +9,46 @@ The absence of real-time features necessary for creating professional-level embe
With uClock, you gain the ability to create professional-grade sequencers, sync boxes, or generate a precise BPM clock for external devices in the realms of music, audio/video productions, performances, or tech art installations. The library offers an external synchronization schema that enables you to generate an internal clock based on an external clock source, allowing you to master your entire MIDI setup or any other protocols according to your specific preferences and requirements. With uClock, you gain the ability to create professional-grade sequencers, sync boxes, or generate a precise BPM clock for external devices in the realms of music, audio/video productions, performances, or tech art installations. The library offers an external synchronization schema that enables you to generate an internal clock based on an external clock source, allowing you to master your entire MIDI setup or any other protocols according to your specific preferences and requirements.
## Interface ## Interface
The uClock library interfaces through an attached callback function that runs on a hardware interrupt and provides support for the following resolutions: The uClock library API operates through attached callback functions mechanism:
1. **16PPQN** 16 Pulses Per Quarter Note 1. **onPPQNCallback(uint32_t tick)** calls on each new pulse based on selected PPQN resolution(if no PPQN set, the default is 96PPQN)
2. **32PPQN** 32 Pulses Per Quarter Note 2. **onStepCallback(uint32_t step)** good way to code old style step sequencer based on 16th note schema(not dependent on PPQN resolution)
3. **96PPQN** 96 Pulses Per Quarter Note 3. **onSync24Callback(uint32_t tick)** good way to code a clock machine, or keep your gears synced with your device
4. **onSync48Callback(uint32_t tick)** there are some 48ppqn based sync devices out there
5. **onClockStartCallback()** there are some 48ppqn based sync devices out there
6. **onClockStopCallback()** there are some 48ppqn based sync devices out there
To generate a MIDI sync signal and synchronize external MIDI devices, you can start working with the resolution of 96PPQN, which aligns with the clocking standards of modern MIDI-syncable devices commonly available in the market. This resolution is based on the 24PPQN (24 multiplied by 4 equals 96). By sending a sync signal every 96PPQN interval, you can ensure effective synchronization among your MIDI devices. ```c++
// avaliable resolutions
// [ uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ]
// not mandatory to call, the default is 96PPQN if not set
uClock.setPPQN(uClock.PPQN_96);
// you need to use at least one!
uClock.setOnPPQN(onPPQNCallback);
uClock.setOnStep(onStepCallback);
uClock.setOnSync24(onSync24Callback);
uClock.setOnSync48(onSync48Callback);
uClock.setOnClockStart(onClockStartCallback);
uClock.setOnClockStop(onClockStopCallback);
uClock.init();
```
Resolutions: set youw own resolution for your clock needs
1. **PPQN_24** 24 Pulses Per Quarter Note
2. **PPQN_48** 48 Pulses Per Quarter Note
3. **PPQN_96** 96 Pulses Per Quarter Note
1. **PPQN_384** 384 Pulses Per Quarter Note
2. **PPQN_480** 480 Pulses Per Quarter Note
3. **PPQN_960** 96 Pulses Per Quarter Note
If you are working on the development of a vintage-style step sequencer, utilizing a resolution of 16PPQN is a fitting option to initiate the coding process. This resolution ensures that each 16PPQN call corresponds to a step played note or event.
To generate a MIDI sync signal and synchronize external MIDI devices, you can start working with the resolution of 24PPQN, which aligns with the clocking standards of modern MIDI-syncable devices commonly available in the market. By sending 24 pulses per quarter note interval, you can ensure effective synchronization among your MIDI devices.
If you are working on the development of a vintage-style step sequencer, utilizing a resolution of 96PPQN is a fitting option to initiate the coding process. Then you can use onStepCallback call wich corresponds to a step played note or event.
Furthermore, it is possible to utilize all three resolutions simultaneously, allowing for flexibility based on your specific requirements and preferences. Furthermore, it is possible to utilize all three resolutions simultaneously, allowing for flexibility based on your specific requirements and preferences.
@ -38,7 +69,7 @@ Here is an example on how to create a simple MIDI Sync Box on Arduino boards
#define MIDI_STOP 0xFC #define MIDI_STOP 0xFC
// The callback function wich will be called by Clock each Pulse of 96PPQN clock resolution. // The callback function wich will be called by Clock each Pulse of 96PPQN clock resolution.
void ClockOut96PPQN(uint32_t tick) { void onSync24Callback(uint32_t tick) {
// Send MIDI_CLOCK to external gears // Send MIDI_CLOCK to external gears
Serial.write(MIDI_CLOCK); Serial.write(MIDI_CLOCK);
} }
@ -60,8 +91,8 @@ void setup() {
// Inits the clock // Inits the clock
uClock.init(); uClock.init();
// Set the callback function for the clock output to send MIDI Sync message. // Set the callback function for the clock output to send MIDI Sync message based on 24PPQN
uClock.setClock96PPQNOutput(ClockOut96PPQN); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStartOutput(onClockStart); uClock.setOnClockStartOutput(onClockStart);
uClock.setOnClockStopOutput(onClockStop); uClock.setOnClockStopOutput(onClockStop);
@ -85,7 +116,7 @@ An example on how to create a simple MIDI Sync Box on Teensy boards and USB Midi
#include <uClock.h> #include <uClock.h>
// The callback function wich will be called by Clock each Pulse of 96PPQN clock resolution. // The callback function wich will be called by Clock each Pulse of 96PPQN clock resolution.
void ClockOut96PPQN(uint32_t tick) { void onSync24Callback(uint32_t tick) {
// Send MIDI_CLOCK to external gears // Send MIDI_CLOCK to external gears
usbMIDI.sendRealTime(usbMIDI.Clock); usbMIDI.sendRealTime(usbMIDI.Clock);
} }
@ -103,8 +134,8 @@ void onClockStop() {
void setup() { void setup() {
// Inits the clock // Inits the clock
uClock.init(); uClock.init();
// Set the callback function for the clock output to send MIDI Sync message. // Set the callback function for the clock output to send MIDI Sync message. based on 24PPQN
uClock.setClock96PPQNOutput(ClockOut96PPQN); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStartOutput(onClockStart); uClock.setOnClockStartOutput(onClockStart);
uClock.setOnClockStopOutput(onClockStop); uClock.setOnClockStopOutput(onClockStop);

@ -1,6 +1,6 @@
{ {
"name": "uClock", "name": "uClock",
"version": "1.5.1", "version": "2.0.0",
"description": "A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0. ESP32 and STM32)", "description": "A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0. ESP32 and STM32)",
"keywords": "bpm, clock, timing, tick, music, generator", "keywords": "bpm, clock, timing, tick, music, generator",
"repository": "repository":

@ -1,5 +1,5 @@
name=uClock name=uClock
version=1.5.1 version=2.0.0
author=Romulo Silva <contact@midilab.co> author=Romulo Silva <contact@midilab.co>
maintainer=Romulo Silva <contact@midilab.co> maintainer=Romulo Silva <contact@midilab.co>
sentence=BPM clock generator for Arduino platform. sentence=BPM clock generator for Arduino platform.

@ -2,10 +2,10 @@
* @file uClock.cpp * @file uClock.cpp
* Project BPM clock generator for Arduino * Project BPM clock generator for Arduino
* @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32) * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32)
* @version 1.5.1 * @version 2.0.0
* @author Romulo Silva * @author Romulo Silva
* @date 10/06/2017 * @date 10/06/2017
* @license MIT - (c) 2022 - Romulo Silva - contact@midilab.co * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"), * copy of this software and associated documentation files (the "Software"),
@ -66,15 +66,13 @@
// header of this file // header of this file
void uclockInitTimer() void uclockInitTimer()
{ {
// begin at 120bpm (20833us) // begin at 120bpm
initTimer(20833); initTimer(uClock.bpmToMicroSeconds(120.00));
} }
void setTimerTempo(float bpm) void setTimerTempo(float bpm)
{ {
// convert bpm float into 96 ppqn resolution microseconds interval setTimer(uClock.bpmToMicroSeconds(bpm));
uint32_t us_interval = (60000000 / 24 / bpm);
setTimer(us_interval);
} }
namespace umodular { namespace clock { namespace umodular { namespace clock {
@ -103,11 +101,14 @@ uClockClass::uClockClass()
mode = INTERNAL_CLOCK; mode = INTERNAL_CLOCK;
resetCounters(); resetCounters();
onClock96PPQNCallback = NULL; onPPQNCallback = nullptr;
onClock32PPQNCallback = NULL; onSync24Callback = nullptr;
onClock16PPQNCallback = NULL; onSync48Callback = nullptr;
onClockStartCallback = NULL; onStepCallback = nullptr;
onClockStopCallback = NULL; onClockStartCallback = nullptr;
onClockStopCallback = nullptr;
// first ppqn references calculus
setPPQN(PPQN_96);
} }
void uClockClass::init() void uClockClass::init()
@ -117,6 +118,23 @@ void uClockClass::init()
setTempo(tempo); setTempo(tempo);
} }
uint32_t uClockClass::bpmToMicroSeconds(float bpm)
{
return (60000000 / ppqn / bpm);
}
void uClockClass::setPPQN(PPQNResolution resolution)
{
// stop clock to make it safe changing those references
// so we avoid volatile then and ATOMIC everyone
stop();
ppqn = resolution;
// calculate the mod24, mod48 and mod_step tick reference trigger
mod24_ref = ppqn / 24;
mod48_ref = ppqn / 48;
mod_step_ref = ppqn / 4;
}
void uClockClass::start() void uClockClass::start()
{ {
resetCounters(); resetCounters();
@ -169,20 +187,19 @@ void uClockClass::setTempo(float bpm)
) )
setTimerTempo(bpm); setTimerTempo(bpm);
} }
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/24.0) * 60.0); return (float)((float)(usecs/(float)ppqn) * 60.0);
} }
float uClockClass::getTempo() float uClockClass::getTempo()
{ {
if (mode == EXTERNAL_CLOCK) { if (mode == EXTERNAL_CLOCK) {
uint32_t acc = 0; uint32_t acc = 0;
// wait the buffer 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) {
return tempo; return tempo;
} }
@ -196,7 +213,7 @@ float uClockClass::getTempo()
return tempo; return tempo;
} }
void uClockClass::setMode(uint8_t tempo_mode) void uClockClass::setMode(SyncMode tempo_mode)
{ {
mode = tempo_mode; mode = tempo_mode;
} }
@ -217,16 +234,16 @@ void uClockClass::clockMe()
void uClockClass::resetCounters() void uClockClass::resetCounters()
{ {
external_clock = 0; tick = 0;
internal_tick = 0; int_clock_tick = 0;
external_tick = 0; sync48_tick = 0;
div32th_counter = 0; ext_clock_tick = 0;
div16th_counter = 0; ext_clock_us = 0;
mod6_counter = 0;
indiv32th_counter = 0;
indiv16th_counter = 0;
inmod6_counter = 0;
ext_interval_idx = 0; ext_interval_idx = 0;
mod24_counter = 0;
mod48_counter = 0;
mod_step_counter = 0;
step_counter = 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; ext_interval_buffer[i] = 0;
} }
@ -277,9 +294,10 @@ int8_t uClockClass::getShuffleLength()
{ {
return shuffle_length_ctrl; return shuffle_length_ctrl;
} }
/*
int8_t inline uClockClass::processShuffle() int8_t inline uClockClass::processShuffle()
{ {
// mod6_counter will become mod_step_counter
int8_t mod6_shuffle_counter; int8_t mod6_shuffle_counter;
if (!shuffle.active) { if (!shuffle.active) {
mod6_shuffle_counter = mod6_counter; mod6_shuffle_counter = mod6_counter;
@ -309,41 +327,26 @@ int8_t inline uClockClass::processShuffle()
} }
return mod6_shuffle_counter; return mod6_shuffle_counter;
} }
*/
// it is expected to be called in 24PPQN
void uClockClass::handleExternalClock() void uClockClass::handleExternalClock()
{ {
switch (state) { switch (state) {
case PAUSED: case PAUSED:
break; break;
case STARTING: case STARTING:
state = STARTED; state = STARTED;
external_clock = micros(); ext_clock_us = micros();
break; break;
case STARTED: case STARTED:
uint32_t now_clock_us = micros();
last_interval = clock_diff(ext_clock_us, now_clock_us);
ext_clock_us = now_clock_us;
uint32_t u_timer = micros(); // external clock tick me!
last_interval = clock_diff(external_clock, u_timer); ext_clock_tick++;
external_clock = u_timer;
if (inmod6_counter == 0) {
indiv16th_counter++;
indiv32th_counter++;
}
if (inmod6_counter == 3) {
indiv32th_counter++;
}
// slave tick me!
external_tick++;
inmod6_counter++;
if (inmod6_counter == 6) {
inmod6_counter = 0;
}
// accumulate interval incomming ticks data for getTempo() smooth reads on slave mode // accumulate interval incomming ticks data for getTempo() smooth reads on slave mode
if(++ext_interval_idx >= EXT_INTERVAL_BUFFER_SIZE) { if(++ext_interval_idx >= EXT_INTERVAL_BUFFER_SIZE) {
@ -351,10 +354,10 @@ void uClockClass::handleExternalClock()
} }
ext_interval_buffer[ext_interval_idx] = last_interval; ext_interval_buffer[ext_interval_idx] = last_interval;
if (external_tick == 1) { if (ext_clock_tick == 1) {
interval = last_interval; ext_interval = last_interval;
} else { } else {
interval = (((uint32_t)interval * (uint32_t)PLL_X) + (uint32_t)(256 - PLL_X) * (uint32_t)last_interval) >> 8; ext_interval = (((uint32_t)ext_interval * (uint32_t)PLL_X) + (uint32_t)(256 - PLL_X) * (uint32_t)last_interval) >> 8;
} }
break; break;
} }
@ -362,65 +365,89 @@ void uClockClass::handleExternalClock()
void uClockClass::handleTimerInt() void uClockClass::handleTimerInt()
{ {
if (mode == EXTERNAL_CLOCK) { // External sync is handled here... test if clock check on each tick instead when
// sync tick position with external tick clock // mod24_counter kicks in will help or worst slave timing sync quality
if ((internal_tick < external_tick) || (internal_tick > (external_tick + 1))) { if (mod24_counter == mod24_ref) {
internal_tick = external_tick; if (mode == EXTERNAL_CLOCK) {
div32th_counter = indiv32th_counter; // sync tick position with external tick clock
div16th_counter = indiv16th_counter; if ((int_clock_tick < ext_clock_tick) || (int_clock_tick > (ext_clock_tick + 1))) {
mod6_counter = inmod6_counter; int_clock_tick = ext_clock_tick;
} }
uint32_t counter = interval; uint32_t counter = ext_interval;
uint32_t u_timer = micros(); uint32_t now_clock_us = micros();
sync_interval = clock_diff(external_clock, u_timer); sync_interval = clock_diff(ext_clock_us, now_clock_us);
if (internal_tick <= external_tick) { if (int_clock_tick <= ext_clock_tick) {
counter -= phase_mult(sync_interval); counter -= phase_mult(sync_interval);
} else { } else {
if (counter > sync_interval) { if (counter > sync_interval) {
counter += phase_mult(counter - sync_interval); counter += phase_mult(counter - sync_interval);
}
} }
}
// update internal clock timer frequency // update internal clock timer frequency
float bpm = freqToBpm(counter); float bpm = freqToBpm(counter);
if (bpm != tempo) { if (bpm != tempo) {
if (bpm >= MIN_BPM && bpm <= MAX_BPM) { if (bpm >= MIN_BPM && bpm <= MAX_BPM) {
tempo = bpm; tempo = bpm;
setTimerTempo(bpm); setTimerTempo(bpm);
}
} }
} }
// callback to inform about sync24 event
if (onSync24Callback) {
onSync24Callback(int_clock_tick);
}
// reset counter
mod24_counter = 0;
// internal clock tick me! sync24 tick too
++int_clock_tick;
} }
if (onClock96PPQNCallback) { // sync signals first please...
onClock96PPQNCallback(internal_tick); if (onSync48Callback) {
if (mod48_counter == mod48_ref) {
onSync48Callback(sync48_tick);
// reset counter
mod48_counter = 0;
// sync48 tick me!
++sync48_tick;
}
} }
// 16PPQN call and shuffle processing if enabled // PPQNCallback time!
if (processShuffle() == 0) { if (onPPQNCallback) {
if (onClock16PPQNCallback) { onPPQNCallback(tick);
onClock16PPQNCallback(div16th_counter); }
if (onStepCallback) {
// we can add a time signature here for call setup based on mod_step_ref
// basic will be 16ths, but let the option to handle unusual sequences
if (mod_step_counter == mod_step_ref) {
onStepCallback(step_counter);
// reset counter
mod_step_counter = 0;
// going forward to the next step call
++step_counter;
} }
div16th_counter++;
shuffle_shoot_ctrl = false;
} }
// 32PPQN call. does anyone uses it? /* // TODO: port it from 24PPQN to ppqn set
if (mod6_counter == 3 || mod6_counter == 6) { if (processShuffle() == 0) {
if (onClock32PPQNCallback) { //if (mod_step_counter == signature) {
onClock32PPQNCallback(div32th_counter); if (onStepCallback) {
onStepCallback(mod_step_counter);
} }
div32th_counter++; shuffle_shoot_ctrl = false;
} } */
// tick me! // tick me!
internal_tick++; ++tick;
mod6_counter++; // increment mod counters
++mod24_counter;
if (mod6_counter == 6) { ++mod48_counter;
mod6_counter = 0; ++mod_step_counter;
}
} }
// elapsed time support // elapsed time support
@ -429,7 +456,7 @@ uint8_t uClockClass::getNumberOfSeconds(uint32_t time)
if ( time == 0 ) { if ( time == 0 ) {
return time; return time;
} }
return ((_timer - time) / 1000) % SECS_PER_MIN; return ((_millis - time) / 1000) % SECS_PER_MIN;
} }
uint8_t uClockClass::getNumberOfMinutes(uint32_t time) uint8_t uClockClass::getNumberOfMinutes(uint32_t time)
@ -437,7 +464,7 @@ uint8_t uClockClass::getNumberOfMinutes(uint32_t time)
if ( time == 0 ) { if ( time == 0 ) {
return time; return time;
} }
return (((_timer - time) / 1000) / SECS_PER_MIN) % SECS_PER_MIN; return (((_millis - time) / 1000) / SECS_PER_MIN) % SECS_PER_MIN;
} }
uint8_t uClockClass::getNumberOfHours(uint32_t time) uint8_t uClockClass::getNumberOfHours(uint32_t time)
@ -445,7 +472,7 @@ uint8_t uClockClass::getNumberOfHours(uint32_t time)
if ( time == 0 ) { if ( time == 0 ) {
return time; return time;
} }
return (((_timer - time) / 1000) % SECS_PER_DAY) / SECS_PER_HOUR; return (((_millis - time) / 1000) % SECS_PER_DAY) / SECS_PER_HOUR;
} }
uint8_t uClockClass::getNumberOfDays(uint32_t time) uint8_t uClockClass::getNumberOfDays(uint32_t time)
@ -453,12 +480,12 @@ uint8_t uClockClass::getNumberOfDays(uint32_t time)
if ( time == 0 ) { if ( time == 0 ) {
return time; return time;
} }
return ((_timer - time) / 1000) / SECS_PER_DAY; return ((_millis - time) / 1000) / SECS_PER_DAY;
} }
uint32_t uClockClass::getNowTimer() uint32_t uClockClass::getNowTimer()
{ {
return _timer; return _millis;
} }
uint32_t uClockClass::getPlayTime() uint32_t uClockClass::getPlayTime()
@ -470,12 +497,11 @@ uint32_t uClockClass::getPlayTime()
umodular::clock::uClockClass uClock; umodular::clock::uClockClass uClock;
volatile uint32_t _timer = 0; volatile uint32_t _millis = 0;
// //
// TIMER INTERRUPT HANDLER // TIMER HANDLER
// //
//
#if defined(ARDUINO_ARCH_AVR) #if defined(ARDUINO_ARCH_AVR)
ISR(TIMER1_COMPA_vect) ISR(TIMER1_COMPA_vect)
#else #else
@ -483,7 +509,7 @@ void uClockHandler()
#endif #endif
{ {
// global timer counter // global timer counter
_timer = millis(); _millis = millis();
if (uClock.state == uClock.STARTED) { if (uClock.state == uClock.STARTED) {
uClock.handleTimerInt(); uClock.handleTimerInt();

@ -2,10 +2,10 @@
* @file uClock.h * @file uClock.h
* Project BPM clock generator for Arduino * Project BPM clock generator for Arduino
* @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32) * @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32)
* @version 1.5.1 * @version 2.0.0
* @author Romulo Silva * @author Romulo Silva
* @date 10/06/2017 * @date 10/06/2017
* @license MIT - (c) 2022 - Romulo Silva - contact@midilab.co * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"), * copy of this software and associated documentation files (the "Software"),
@ -34,6 +34,16 @@
namespace umodular { namespace clock { namespace umodular { namespace clock {
// for extended steps in memory style and make use of 96ppqn for record propurse we can
// keep array[step] memory layout and add new information about note possition to be check for the entire ppqn pulse
// example: for a whole 24 pulses we only check array[step].offset that can vary from 0 to 24(ppqn/4)
// time/tick notation and representation notes:
// one quarter note == 4 steps in 16th notes step sequencer style
// PPQN / 4 = pulses in between steps(from step sequencer perspective, a quarter note have 4 steps)
// 24 PPQN (6 pulses per step)
// 48 PPQN (12 pulses per step)
// 96 PPQN (24 pulses per step)
// min: 2 step, max: 16 steps // min: 2 step, max: 16 steps
// adjust the size of you template if more than 16 needed // adjust the size of you template if more than 16 needed
// step adjust goes min: -5, max: 5 // step adjust goes min: -5, max: 5
@ -61,120 +71,142 @@ typedef struct {
#define SECS_PER_DAY (SECS_PER_HOUR * 24L) #define SECS_PER_DAY (SECS_PER_HOUR * 24L)
class uClockClass { class uClockClass {
private:
public:
float inline freqToBpm(uint32_t freq); enum SyncMode {
INTERNAL_CLOCK = 0,
// shuffle EXTERNAL_CLOCK
int8_t inline processShuffle(); };
void (*onClock96PPQNCallback)(uint32_t tick); enum ClockState {
void (*onClock32PPQNCallback)(uint32_t tick); PAUSED = 0,
void (*onClock16PPQNCallback)(uint32_t tick); STARTING,
void (*onClockStartCallback)(); STARTED
void (*onClockStopCallback)(); };
// internal clock control enum PPQNResolution {
uint32_t internal_tick; PPQN_24 = 24,
uint32_t div32th_counter; PPQN_48 = 48,
uint32_t div16th_counter; PPQN_96 = 96,
uint8_t mod6_counter; PPQN_384 = 384,
PPQN_480 = 480,
// external clock control PPQN_960 = 960
volatile uint32_t external_clock; };
volatile uint32_t external_tick;
volatile uint32_t indiv32th_counter; uint8_t state;
volatile uint32_t indiv16th_counter;
volatile uint8_t inmod6_counter; uClockClass();
volatile uint32_t interval;
uint32_t last_interval; void setOnPPQN(void (*callback)(uint32_t tick)) {
uint32_t sync_interval; onPPQNCallback = callback;
}
float tempo;
uint32_t start_timer; void setOnStep(void (*callback)(uint32_t step)) {
uint8_t mode; onStepCallback = callback;
}
volatile uint32_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE];
uint16_t ext_interval_idx; void setOnSync24(void (*callback)(uint32_t tick)) {
onSync24Callback = callback;
// shuffle implementation that applies to 16PPQN callback }
volatile SHUFFLE_TEMPLATE shuffle;
bool shuffle_shoot_ctrl = true; void setOnSync48(void (*callback)(uint32_t tick)) {
volatile int8_t shuffle_length_ctrl = 0; onSync48Callback = callback;
}
public:
void setOnClockStart(void (*callback)()) {
enum { onClockStartCallback = callback;
INTERNAL_CLOCK = 0, }
EXTERNAL_CLOCK
}; void setOnClockStop(void (*callback)()) {
onClockStopCallback = callback;
enum { }
PAUSED = 0,
STARTING, void init();
STARTED void setPPQN(PPQNResolution resolution);
};
void handleTimerInt();
uint8_t state; void handleExternalClock();
void resetCounters();
uClockClass();
// external class control
void setClock96PPQNOutput(void (*callback)(uint32_t tick)) { void start();
onClock96PPQNCallback = callback; void stop();
} void pause();
void setTempo(float bpm);
void setClock32PPQNOutput(void (*callback)(uint32_t tick)) { float getTempo();
onClock32PPQNCallback = callback;
} // external timming control
void setMode(SyncMode tempo_mode);
void setClock16PPQNOutput(void (*callback)(uint32_t tick)) { uint8_t getMode();
onClock16PPQNCallback = callback; void clockMe();
}
// shuffle
void setOnClockStartOutput(void (*callback)()) { void setShuffle(bool active);
onClockStartCallback = callback; bool isShuffled();
} void setShuffleSize(uint8_t size);
void setShuffleData(uint8_t step, int8_t tick);
void setOnClockStopOutput(void (*callback)()) { void setShuffleTemplate(int8_t * shuff);
onClockStopCallback = callback; // use this to know how many positive or negative ticks to add to current note length
} int8_t getShuffleLength();
void init(); // todo!
void handleTimerInt(); void tap();
void handleExternalClock();
void resetCounters(); // elapsed time support
uint8_t getNumberOfSeconds(uint32_t time);
// external class control uint8_t getNumberOfMinutes(uint32_t time);
void start(); uint8_t getNumberOfHours(uint32_t time);
void stop(); uint8_t getNumberOfDays(uint32_t time);
void pause(); uint32_t getNowTimer();
void setTempo(float bpm); uint32_t getPlayTime();
float getTempo();
uint32_t bpmToMicroSeconds(float bpm);
// external timming control
void setMode(uint8_t tempo_mode); private:
uint8_t getMode(); float inline freqToBpm(uint32_t freq);
void clockMe();
// shuffle
// shuffle //int8_t inline processShuffle();
void setShuffle(bool active);
bool isShuffled(); void (*onPPQNCallback)(uint32_t tick);
void setShuffleSize(uint8_t size); void (*onStepCallback)(uint32_t step);
void setShuffleData(uint8_t step, int8_t tick); void (*onSync24Callback)(uint32_t tick);
void setShuffleTemplate(int8_t * shuff); void (*onSync48Callback)(uint32_t tick);
// use this to know how many positive or negative ticks to add to current note length void (*onClockStartCallback)();
int8_t getShuffleLength(); void (*onClockStopCallback)();
// todo! // internal clock control
void tap(); // uint16_t ppqn;
PPQNResolution ppqn = PPQN_96;
// elapsed time support uint32_t tick;
uint8_t getNumberOfSeconds(uint32_t time); uint32_t int_clock_tick;
uint8_t getNumberOfMinutes(uint32_t time); uint32_t sync48_tick;
uint8_t getNumberOfHours(uint32_t time); uint8_t mod24_counter;
uint8_t getNumberOfDays(uint32_t time); uint8_t mod24_ref;
uint32_t getNowTimer(); uint8_t mod48_counter;
uint32_t getPlayTime(); uint8_t mod48_ref;
uint8_t mod_step_counter;
uint8_t mod_step_ref;
uint32_t step_counter; // should we go uint16_t?
// external clock control
volatile uint32_t ext_clock_us;
volatile uint32_t ext_clock_tick;
volatile uint32_t ext_interval;
uint32_t last_interval;
uint32_t sync_interval;
float tempo;
uint32_t start_timer;
uint8_t mode;
volatile uint32_t ext_interval_buffer[EXT_INTERVAL_BUFFER_SIZE];
uint16_t ext_interval_idx;
// shuffle implementation that applies to 16PPQN callback
volatile SHUFFLE_TEMPLATE shuffle;
bool shuffle_shoot_ctrl = true;
volatile int8_t shuffle_length_ctrl = 0;
}; };
} } // end namespace umodular::clock } } // end namespace umodular::clock
@ -182,8 +214,7 @@ public:
extern umodular::clock::uClockClass uClock; extern umodular::clock::uClockClass uClock;
extern "C" { extern "C" {
extern volatile uint16_t _clock; extern volatile uint32_t _millis;
extern volatile uint32_t _timer;
} }
#endif /* __U_CLOCK_H__ */ #endif /* __U_CLOCK_H__ */

Loading…
Cancel
Save