Merge pull request #57 from midilab/develop

v2.3.0
main
midilab 1 week ago committed by GitHub
commit 8a4b45cf90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 89
      README.md
  2. 26
      examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino
  3. 40
      examples/AcidStepSequencer/AcidStepSequencer.ino
  4. 26
      examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino
  5. 19
      examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino
  6. 38
      examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino
  7. 18
      examples/MidiClock/MidiClock.ino
  8. 16
      examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino
  9. 24
      examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino
  10. 18
      examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino
  11. 20
      examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino
  12. 40
      examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino
  13. 22
      examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino
  14. 116
      src/uClock.cpp
  15. 38
      src/uClock.h

@ -11,15 +11,23 @@ With uClock, you can create professional-grade sequencers, sync boxes, or genera
## Interface ## Interface
The uClock library API operates through an attached callback function mechanism: The uClock library API operates through an attached callback function mechanism:
1. **setOnOutputPPQN(onPPQNCallback) > onOutputPPQNCallback(uint32_t tick)** Calls are made on each new output pulse based on the selected PPQN resolution (if no PPQN is set, the default is 96 PPQN). 1. **setOnOutputPPQN(onPPQNCallback) > onOutputPPQNCallback(uint32_t tick)** Callback are made on each new output pulse based on the selected PPQN resolution (if no PPQN is set, the default is 96 PPQN).
2. **setOnInputPPQN(onPPQNCallback) > onInputPPQNCallback(uint32_t tick)** Set the expected input PPQN (Pulses Per Quarter Note) resolution for external clock sync. 2. **setOnStep(onStepCallback) > onStepCallback(uint32_t step)** A good way to code an old-style step sequencer based on a 16th-note schema, which is not dependent on PPQN (Pulses Per Quarter Note) output config.
3. **setOnStep(onStepCallback) > onStepCallback(uint32_t step)** A good way to code an old-style step sequencer based on a 16th-note schema, which is not dependent on PPQN (Pulses Per Quarter Note) output config. 3. **setOnSync24(onSync24Callback) > onSync24Callback(uint32_t tick)** A good way to code a clock machine or keep your devices in sync with your system is to use setOnSyncXX(), where XX represents the PPQN (Pulses Per Quarter Note) value you want to use. MIDI specifications typically expect 24 PPQN, but if you're working with other devices that are not MIDI standard, you can choose a different PPQN value. Please refer to the supported PPQNs to select from. You can use one or more setOnSyncXX callbacks for different sync output signatures.
4. **setOnSync24(onSync24Callback) > onSync24Callback(uint32_t tick)** A good way to code a clock machine or keep your devices in sync with your system is to use setOnSyncXX(), where XX represents the PPQN (Pulses Per Quarter Note) value you want to use. MIDI specifications typically expect 24 PPQN, but if you're working with other devices that are not MIDI standard, you can choose a different PPQN value. Please refer to the supported PPQNs to select from. You can use one or more setOnSyncXX callbacks for different sync output signatures. 4. **setOnClockStart(onClockStartCallback) > onClockStartCallback()** On the uClock Start event.
5. **setOnClockStart(onClockStartCallback) > onClockStartCallback()** On the uClock Start event. 5. **setOnClockStop(onClockStopCallback) > onClockStopCallback()** On the uClock Stop event.
6. **setOnClockStop(onClockStopCallback) > onClockStopCallback()** On the uClock Stop event.
### Clock input/output resolutions ### Clock input/output resolutions
You can setup multiple clock resolutions for clock generation and clock sync signals.
#### Set internal output clock and external input clock resolution
1. **void setOutputPPQN(PPQNResolution resolution);** sets the main clock output(independent from clock input).
2. **void setInputPPQN(PPQNResolution resolution);** sets the expected external clock resolution for input(independent from clock output).
#### PPQNResolution Avaliable Resolutions
1. **PPQN_1** 1 Pulses Per Quarter Note (only input) 1. **PPQN_1** 1 Pulses Per Quarter Note (only input)
2. **PPQN_2** 2 Pulses Per Quarter Note (only input) 2. **PPQN_2** 2 Pulses Per Quarter Note (only input)
3. **PPQN_4** 4 Pulses Per Quarter Note 3. **PPQN_4** 4 Pulses Per Quarter Note
@ -82,7 +90,7 @@ You will find more complete examples on examples/ folder:
bool _external_sync_on = false; bool _external_sync_on = false;
// the main uClock PPQN resolution ticking // the main uClock PPQN resolution ticking
void onPPQNCallback(uint32_t tick) { void onOutputPPQNCallback(uint32_t tick) {
// tick your sequencers or tickable devices... // tick your sequencers or tickable devices...
} }
@ -90,11 +98,31 @@ void onStepCallback(uint32_t step) {
// triger step data for sequencer device... // triger step data for sequencer device...
} }
// The callback function called by uClock each Pulse of 1PPQN clock resolution.
void onSync1Callback(uint32_t tick) {
// send sync signal to...
}
// The callback function called by uClock each Pulse of 2PPQN clock resolution.
void onSync2Callback(uint32_t tick) {
// send sync signal to...
}
// The callback function called by uClock each Pulse of 4PPQN clock resolution.
void onSync4Callback(uint32_t tick) {
// send sync signal to...
}
// The callback function called by uClock each Pulse of 24PPQN clock resolution. // The callback function called by uClock each Pulse of 24PPQN clock resolution.
void onSync24Callback(uint32_t tick) { void onSync24Callback(uint32_t tick) {
// send sync signal to... // send sync signal to...
} }
// The callback function called by uClock each Pulse of 48PPQN clock resolution.
void onSync48Callback(uint32_t tick) {
// send sync signal to...
}
// The callback function called when clock starts by using uClock.start() method. // The callback function called when clock starts by using uClock.start() method.
void onClockStartCallback() { void onClockStartCallback() {
// send start signal to... // send start signal to...
@ -106,28 +134,41 @@ void onClockStopCallback() {
} }
void setup() { void setup() {
// setup clock library
// inits the clock library // avaliable output resolutions
uClock.init(); // [ uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ]
// 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 // not mandatory to call, the default is 96PPQN if not set
uClock.setPPQN(uClock.PPQN_96); uClock.setOutputPPQN(uClock.PPQN_96);
// you need to use at least one! // you need to use at least one!
uClock.setOnOutputPPQN(onPPQNCallback); uClock.setOnOutputPPQN(onOutputPPQNCallback);
uClock.setOnStep(onStepCallback); uClock.setOnStep(onStepCallback);
// multi sync output signatures avaliable
// normaly used by eurorack modular modules
uClock.setOnSync1(onSync1Callback);
uClock.setOnSync2(onSync2Callback);
uClock.setOnSync4(onSync4Callback);
// midi sync standard
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// some korg machines do 48ppqn
uClock.setOnSync48(onSync48Callback);
uClock.setOnClockStart(onClockStartCallback); uClock.setOnClockStart(onClockStartCallback);
uClock.setOnClockStop(onClockStopCallback); uClock.setOnClockStop(onClockStopCallback);
// set external sync mode? // set external sync mode?
if (_external_sync_on) { if (_external_sync_on) {
uClock.setMode(uClock.EXTERNAL_CLOCK); uClock.setClockMode(uClock.EXTERNAL_CLOCK);
// what is the clock of incomming signal to sync with?
// not mandatory to call, the default is 24PPQN if not set
// avaliable input resolutions - should be always InputPPQN <= OutputPPQN
// [ uClock.PPQN_1, uClock.PPQN_2, uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ]
uClock.setInputPPQN(uClock.PPQN_24);
} }
// inits the clock library
uClock.init();
// starts clock // starts clock
uClock.start(); uClock.start();
} }
@ -184,8 +225,6 @@ void setup() {
// Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication: // Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication:
Serial.begin(31250); Serial.begin(31250);
// Inits the clock
uClock.init();
// Set the callback function for the clock output to send MIDI Sync message based on 24PPQN // Set the callback function for the clock output to send MIDI Sync message based on 24PPQN
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
@ -194,6 +233,9 @@ void setup() {
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
// Inits the clock
uClock.init();
// Starts the clock, tick-tac-tick-tac... // Starts the clock, tick-tac-tick-tac...
uClock.start(); uClock.start();
@ -227,8 +269,6 @@ void onClockStop() {
} }
void setup() { void setup() {
// Inits the clock
uClock.init();
// Set the callback function for the clock output to send MIDI Sync message. based on 24PPQN // Set the callback function for the clock output to send MIDI Sync message. based on 24PPQN
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
@ -236,6 +276,9 @@ void setup() {
uClock.setOnClockStopOutput(onClockStop); uClock.setOnClockStopOutput(onClockStop);
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
// Inits the clock
uClock.init();
// Starts the clock, tick-tac-tick-tac... // Starts the clock, tick-tac-tick-tac...
uClock.start(); uClock.start();
} }
@ -395,9 +438,6 @@ void setup()
// the default MIDI serial speed communication at 31250 bits per second // the default MIDI serial speed communication at 31250 bits per second
Serial.begin(31250); Serial.begin(31250);
// Inits the clock
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.
uClock.setOnOutputPPQN(onPPQNCallback); uClock.setOnOutputPPQN(onPPQNCallback);
@ -428,6 +468,9 @@ void setup()
// pins, buttons, leds and pots config // pins, buttons, leds and pots config
//configureYourUserInterface(); //configureYourUserInterface();
// Inits the clock
uClock.init();
// start sequencer // start sequencer
uClock.start(); uClock.start();
} }

@ -1,10 +1,10 @@
/* Uart MIDI Sync Slave Box Monitor /* Uart MIDI Sync Slave Box Monitor
* *
* This example demonstrates how to create a * This example demonstrates how to create a
* MIDI slave clock box with * MIDI slave clock box with
* monitor support using oled display * monitor support using oled display
* *
* MIDI in must be provided via an opto-isolator to pin RX/D0 * MIDI in must be provided via an opto-isolator to pin RX/D0
* Tested on an Arduino Uno. * Tested on an Arduino Uno.
* *
* You need the following libraries to make it work * You need the following libraries to make it work
@ -103,20 +103,28 @@ void setup() {
//u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
u8x8->begin(); u8x8->begin();
u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->setFont(u8x8_font_pressstart2p_r);
u8x8->clear(); u8x8->clear();
u8x8->setFlipMode(true); u8x8->setFlipMode(true);
u8x8->drawUTF8(0, 0, "uClock"); u8x8->drawUTF8(0, 0, "uClock");
// //
// uClock Setup // uClock Setup
// //
uClock.init();
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// For MIDI Sync Start and Stop // For MIDI Sync Start and Stop
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
uClock.setMode(uClock.EXTERNAL_CLOCK); uClock.setClockMode(uClock.EXTERNAL_CLOCK);
// 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
uClock.setExtIntervalBuffer(128);
// inits uClock
uClock.init();
//uClock.setTempo(136.5); //uClock.setTempo(136.5);
//uClock.start(); //uClock.start();
} }
@ -124,7 +132,7 @@ void setup() {
void loop() { void loop() {
while(MIDI.read()) {} while(MIDI.read()) {}
// DO NOT ADD MORE PROCESS HERE AT THE COST OF LOSING CLOCK SYNC // DO NOT ADD MORE PROCESS HERE AT THE COST OF LOSING CLOCK SYNC
// Since arduino make use of Serial RX interruption we need to // Since arduino make use of Serial RX interruption we need to
// read Serial as fast as we can on the loop // read Serial as fast as we can on the loop
if (bpm != uClock.getTempo()) { if (bpm != uClock.getTempo()) {
bpm = uClock.getTempo(); bpm = uClock.getTempo();

@ -54,7 +54,7 @@ bool _playing = false;
uint16_t _step = 0; uint16_t _step = 0;
void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2) void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2)
{ {
// send midi message // send midi message
command = command | (uint8_t)MIDI_CHANNEL; command = command | (uint8_t)MIDI_CHANNEL;
Serial.write(command); Serial.write(command);
@ -63,14 +63,14 @@ void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2)
} }
// Each call represents exactly one step. // Each call represents exactly one step.
void onStepCallback(uint32_t tick) void onStepCallback(uint32_t tick)
{ {
uint16_t step; uint16_t step;
uint16_t length = NOTE_LENGTH; uint16_t length = NOTE_LENGTH;
// get actual step. // get actual step.
_step = tick % _step_length; _step = tick % _step_length;
// send note on only if this step are not in rest mode // send note on only if this step are not in rest mode
if ( _sequencer[_step].rest == false ) { if ( _sequencer[_step].rest == false ) {
@ -93,15 +93,15 @@ void onStepCallback(uint32_t tick)
_note_stack[i].note = _sequencer[_step].note; _note_stack[i].note = _sequencer[_step].note;
_note_stack[i].length = length; _note_stack[i].length = length;
// send note on // send note on
sendMidiMessage(NOTE_ON, _sequencer[_step].note, _sequencer[_step].accent ? ACCENT_VELOCITY : NOTE_VELOCITY); sendMidiMessage(NOTE_ON, _sequencer[_step].note, _sequencer[_step].accent ? ACCENT_VELOCITY : NOTE_VELOCITY);
return; return;
} }
} }
} }
} }
// The callback function wich will be called by uClock each Pulse of 96PPQN clock resolution. // The callback function wich will be called by uClock each Pulse of 96PPQN clock resolution.
void onOutputPPQNCallback(uint32_t tick) void onOutputPPQNCallback(uint32_t tick)
{ {
// handle note on stack // handle note on stack
for ( uint8_t i = 0; i < NOTE_STACK_SIZE; i++ ) { for ( uint8_t i = 0; i < NOTE_STACK_SIZE; i++ ) {
@ -111,7 +111,7 @@ void onOutputPPQNCallback(uint32_t tick)
sendMidiMessage(NOTE_OFF, _note_stack[i].note, 0); sendMidiMessage(NOTE_OFF, _note_stack[i].note, 0);
_note_stack[i].length = -1; _note_stack[i].length = -1;
} }
} }
} }
// user feedback about sequence time events // user feedback about sequence time events
@ -124,14 +124,14 @@ void onSync24Callback(uint32_t tick) {
} }
// The callback function wich will be called when clock starts by using Clock.start() method. // The callback function wich will be called when clock starts by using Clock.start() method.
void onClockStart() void onClockStart()
{ {
Serial.write(MIDI_START); Serial.write(MIDI_START);
_playing = true; _playing = true;
} }
// The callback function wich will be called when clock stops by using Clock.stop() method. // The callback function wich will be called when clock stops by using Clock.stop() method.
void onClockStop() void onClockStop()
{ {
Serial.write(MIDI_STOP); Serial.write(MIDI_STOP);
// send all note off on sequencer stop // send all note off on sequencer stop
@ -142,21 +142,18 @@ void onClockStop()
_playing = false; _playing = false;
} }
void setup() void setup()
{ {
// Initialize serial communication // Initialize serial communication
#ifdef MIDI_MODE #ifdef MIDI_MODE
// the default MIDI serial speed communication at 31250 bits per second // the default MIDI serial speed communication at 31250 bits per second
Serial.begin(31250); Serial.begin(31250);
#endif #endif
#ifdef SERIAL_MODE #ifdef SERIAL_MODE
// for usage with a PC with a serial to MIDI bridge // for usage with a PC with a serial to MIDI bridge
Serial.begin(115200); Serial.begin(115200);
#endif #endif
// Inits the clock
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.
uClock.setOnOutputPPQN(onOutputPPQNCallback); uClock.setOnOutputPPQN(onOutputPPQNCallback);
@ -164,12 +161,15 @@ void setup()
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for the step sequencer on 16ppqn // Set the callback function for the step sequencer on 16ppqn
uClock.setOnStep(onStepCallback); uClock.setOnStep(onStepCallback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// Inits the clock
uClock.init();
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
@ -192,7 +192,7 @@ void setup()
} }
// User interaction goes here // User interaction goes here
void loop() void loop()
{ {
processInterface(); processInterface();
} }

@ -1,12 +1,12 @@
/* Uart MIDI Sync Box /* Uart MIDI Sync Box
* *
* This example demonstrates how to change the Uart MIDI * This example demonstrates how to change the Uart MIDI
* device name on ESP32 family. * device name on ESP32 family.
* *
* This example code is in the public domain. * This example code is in the public domain.
* *
* ... * ...
* *
*/ */
#include <uClock.h> #include <uClock.h>
@ -54,15 +54,17 @@ void setup() {
// A led to count bpms // A led to count bpms
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
// Setup our clock system // Setup our clock system
// Inits the clock
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.
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// Inits the clock
uClock.init();
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
// Starts the clock, tick-tac-tick-tac... // Starts the clock, tick-tac-tick-tac...
@ -71,5 +73,5 @@ void setup() {
// Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment... // Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment...
void loop() { void loop() {
} }

@ -48,12 +48,9 @@ void onClockStopCallback() {
} }
void setup() { void setup() {
// setup clock library
// inits the clock library // avaliable output resolutions
uClock.init(); // [ uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ]
// avaliable output PPQN resolutions for this example
// [ 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 // not mandatory to call, the default is 96PPQN if not set
uClock.setOutputPPQN(uClock.PPQN_96); uClock.setOutputPPQN(uClock.PPQN_96);
@ -76,8 +73,16 @@ void setup() {
// set external sync mode? // set external sync mode?
if (_external_sync_on) { if (_external_sync_on) {
uClock.setClockMode(uClock.EXTERNAL_CLOCK); uClock.setClockMode(uClock.EXTERNAL_CLOCK);
// what is the clock of incomming signal to sync with?
// not mandatory to call, the default is 24PPQN if not set
// avaliable input resolutions - should be always InputPPQN <= OutputPPQN
// [ uClock.PPQN_1, uClock.PPQN_2, uClock.PPQN_4, uClock.PPQN_8, uClock.PPQN_12, uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ]
uClock.setInputPPQN(uClock.PPQN_24);
} }
// inits the clock library
uClock.init();
// starts clock // starts clock
uClock.start(); uClock.start();
} }
@ -92,4 +97,4 @@ void loop() {
uClock.clockMe(); uClock.clockMe();
} }
} }
} }

@ -1,9 +1,9 @@
/* USB MIDI Sync Slave Box Monitor /* USB MIDI Sync Slave Box Monitor
* *
* This example demonstrates how to create a * This example demonstrates how to create a
* MIDI hid compilant slave clock box with * MIDI hid compilant slave clock box with
* monitor support using oled displays * monitor support using oled displays
* *
* You need the following libraries to make it work * You need the following libraries to make it work
* - Midi Library * - Midi Library
* - USB-MIDI and MIDIUSB * - USB-MIDI and MIDIUSB
@ -94,20 +94,28 @@ void setup() {
//u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
u8x8->begin(); u8x8->begin();
u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->setFont(u8x8_font_pressstart2p_r);
u8x8->clear(); u8x8->clear();
u8x8->setFlipMode(true); u8x8->setFlipMode(true);
u8x8->drawUTF8(0, 0, "uClock"); u8x8->drawUTF8(0, 0, "uClock");
// //
// uClock Setup // uClock Setup
// //
uClock.init();
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// For MIDI Sync Start and Stop // For MIDI Sync Start and Stop
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
uClock.setMode(uClock.EXTERNAL_CLOCK); uClock.setClockMode(uClock.EXTERNAL_CLOCK);
// 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
uClock.setExtIntervalBuffer(128);
// init uClock
uClock.init();
//uClock.setTempo(136.5); //uClock.setTempo(136.5);
//uClock.start(); //uClock.start();
} }
@ -115,7 +123,7 @@ void setup() {
void loop() { void loop() {
while(MIDI.read()) {} while(MIDI.read()) {}
// DO NOT ADD MORE PROCESS HERE AT THE COST OF LOSING CLOCK SYNC // DO NOT ADD MORE PROCESS HERE AT THE COST OF LOSING CLOCK SYNC
// Since arduino make use of Serial RX interruption we need to // Since arduino make use of Serial RX interruption we need to
// read Serial as fast as we can on the loop // read Serial as fast as we can on the loop
if (bpm != uClock.getTempo()) { if (bpm != uClock.getTempo()) {
bpm = uClock.getTempo(); bpm = uClock.getTempo();
@ -127,20 +135,20 @@ void loop() {
u8x8->drawUTF8(8+4, 7, " "); u8x8->drawUTF8(8+4, 7, " ");
} }
} }
if (clock_state != uClock.state) { if (clock_state != uClock.state) {
clock_state = uClock.state; clock_state = uClock.state;
if (clock_state >= 1) { if (clock_state >= 1) {
u8x8->drawUTF8(0, 7, "Playing"); u8x8->drawUTF8(0, 7, "Playing");
} else { } else {
u8x8->drawUTF8(0, 7, "Stopped"); u8x8->drawUTF8(0, 7, "Stopped");
} }
} }
if (clock_mode != uClock.getMode()) { if (clock_mode != uClock.getMode()) {
clock_mode = uClock.getMode(); clock_mode = uClock.getMode();
if (clock_mode == uClock.EXTERNAL_CLOCK) { if (clock_mode == uClock.EXTERNAL_CLOCK) {
u8x8->drawUTF8(10, 0, "Slave "); u8x8->drawUTF8(10, 0, "Slave ");
} else { } else {
u8x8->drawUTF8(10, 0, "Master"); u8x8->drawUTF8(10, 0, "Master");
} }
} }
} }

@ -7,37 +7,39 @@
#define MIDI_STOP 0xFC #define MIDI_STOP 0xFC
// The callback function wich will be called by Clock each Pulse of 24PPQN clock resolution. // The callback function wich will be called by Clock each Pulse of 24PPQN clock resolution.
void onSync24Callback(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);
} }
// The callback function wich will be called when clock starts by using Clock.start() method. // The callback function wich will be called when clock starts by using Clock.start() method.
void onClockStart() void onClockStart()
{ {
Serial.write(MIDI_START); Serial.write(MIDI_START);
} }
// The callback function wich will be called when clock stops by using Clock.stop() method. // The callback function wich will be called when clock stops by using Clock.stop() method.
void onClockStop() void onClockStop()
{ {
Serial.write(MIDI_STOP); Serial.write(MIDI_STOP);
} }
void setup() void setup()
{ {
// Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication: // Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication:
Serial.begin(31250); Serial.begin(31250);
// Inits the clock
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.
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// Inits the clock
uClock.init();
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
@ -47,7 +49,7 @@ void setup()
} }
// Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment... // Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment...
void loop() void loop()
{ {
} }

@ -1,8 +1,8 @@
/* /*
* USB/Uart MIDI Sync Box * USB/Uart MIDI Sync Box
* *
* This example code is in the public domain. * This example code is in the public domain.
* *
*/ */
#include <Adafruit_TinyUSB.h> #include <Adafruit_TinyUSB.h>
@ -68,13 +68,15 @@ void setup() {
initBlinkLed(); initBlinkLed();
// Setup our clock system // Setup our clock system
// Inits the clock
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.
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// Inits the clock
uClock.init();
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
// Starts the clock, tick-tac-tick-tac.. // Starts the clock, tick-tac-tick-tac..

@ -1,13 +1,13 @@
/* Uart MIDI out /* Uart MIDI out
* *
* This example demonstrates how to send MIDI data via Uart * This example demonstrates how to send MIDI data via Uart
* interface on STM32 family. * interface on STM32 family.
* *
* This example code is in the public domain. * This example code is in the public domain.
* *
* Requires STM32Duino board manager to be installed. * Requires STM32Duino board manager to be installed.
* *
* Define HardwareSerial using any available UART/USART. * Define HardwareSerial using any available UART/USART.
* Nucleo boards have UART/USART pins that are used by the ST-LINK interface (unless using solder bridging). * Nucleo boards have UART/USART pins that are used by the ST-LINK interface (unless using solder bridging).
* *
* Tested on Nucleo-F401RE and Nucleo-F072RB (PA9=D8 PA10=D2 on the Arduino pins) * Tested on Nucleo-F401RE and Nucleo-F072RB (PA9=D8 PA10=D2 on the Arduino pins)
@ -62,15 +62,17 @@ void setup() {
// An led to display BPM // An led to display BPM
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
// Setup our clock system // Setup our clock system
// Inits the clock
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.
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// Inits the clock
uClock.init();
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(120); uClock.setTempo(120);
// Starts the clock, tick-tac-tick-tac... // Starts the clock, tick-tac-tick-tac...
@ -79,5 +81,5 @@ void setup() {
// Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment... // Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment...
void loop() { void loop() {
} }

@ -1,6 +1,6 @@
/* USB MIDI Sync Box /* USB MIDI Sync Box
* *
* This example demonstrates how to change the USB MIDI * This example demonstrates how to change the USB MIDI
* device name on Teensy LC, 3.x and 4.x. When creating more * device name on Teensy LC, 3.x and 4.x. When creating more
* that one MIDI device, custom names are much easier to * that one MIDI device, custom names are much easier to
* use when selecting each device in MIDI software on * use when selecting each device in MIDI software on
@ -12,9 +12,9 @@
* steps to get your operating system to "forget" the * steps to get your operating system to "forget" the
* cached info. (TODO: wanted... can anyone contribute * cached info. (TODO: wanted... can anyone contribute
* instructions for these systems) * instructions for these systems)
* *
* You must select MIDI from the "Tools > USB Type" menu * You must select MIDI from the "Tools > USB Type" menu
* *
* This example code is in the public domain. * This example code is in the public domain.
*/ */
@ -53,15 +53,17 @@ void onClockStop() {
void setup() { void setup() {
// A led to count bpms // A led to count bpms
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
// Setup our clock system // Setup our clock system
// Inits the clock
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.
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// Inits the clock
uClock.init();
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
// Starts the clock, tick-tac-tick-tac... // Starts the clock, tick-tac-tick-tac...

@ -1,6 +1,6 @@
/* USB MIDI Sync Slave Box /* USB MIDI Sync Slave Box
* *
* This example demonstrates how to change the USB MIDI * This example demonstrates how to change the USB MIDI
* device name on Teensy LC, 3.x and 4.x. When creating more * device name on Teensy LC, 3.x and 4.x. When creating more
* that one MIDI device, custom names are much easier to * that one MIDI device, custom names are much easier to
* use when selecting each device in MIDI software on * use when selecting each device in MIDI software on
@ -12,9 +12,9 @@
* steps to get your operating system to "forget" the * steps to get your operating system to "forget" the
* cached info. (TODO: wanted... can anyone contribute * cached info. (TODO: wanted... can anyone contribute
* instructions for these systems) * instructions for these systems)
* *
* You must select MIDI from the "Tools > USB Type" menu * You must select MIDI from the "Tools > USB Type" menu
* *
* This example code is in the public domain. * This example code is in the public domain.
*/ */
@ -69,22 +69,24 @@ void onExternalStop()
void setup() { void setup() {
// A led to count bpms // A led to count bpms
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
// Setup realtime midi event handlers // Setup realtime midi event handlers
usbMIDI.setHandleClock(onExternalClock); usbMIDI.setHandleClock(onExternalClock);
usbMIDI.setHandleStart(onExternalStart); usbMIDI.setHandleStart(onExternalStart);
usbMIDI.setHandleStop(onExternalStop); usbMIDI.setHandleStop(onExternalStop);
// Setup our clock system // Setup our clock system
// Inits the clock
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.
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// set to external sync mode // set to external sync mode
uClock.setMode(1); uClock.setClockMode(uClock.EXTERNAL_CLOCK);
// Inits the clock
uClock.init();
} }
void loop() { void loop() {

@ -1,20 +1,20 @@
/* USB MIDI Sync Slave Box Monitor /* USB MIDI Sync Slave Box Monitor
* *
* This example demonstrates how to create a * This example demonstrates how to create a
* MIDI hid compilant slave clock box using * MIDI hid compilant slave clock box using
* Teensy LC, 3.x and 4.x with * Teensy LC, 3.x and 4.x with
* monitor support using oled displays * monitor support using oled displays
* *
* Making use of a 250 usceconds timer to * Making use of a 250 usceconds timer to
* handle MIDI input to avoid jitter on clock * handle MIDI input to avoid jitter on clock
* *
* You need the following libraries to make it work * You need the following libraries to make it work
* - u8g2 * - u8g2
* - uClock * - uClock
* *
* This example code is in the public domain. * This example code is in the public domain.
*/ */
#include <U8x8lib.h> #include <U8x8lib.h>
// //
@ -100,23 +100,31 @@ void setup() {
//u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE);
u8x8->begin(); u8x8->begin();
u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->setFont(u8x8_font_pressstart2p_r);
u8x8->clear(); u8x8->clear();
u8x8->setFlipMode(true); u8x8->setFlipMode(true);
u8x8->drawUTF8(0, 0, "uClock"); u8x8->drawUTF8(0, 0, "uClock");
// //
// uClock Setup // uClock Setup
// //
// Setup our clock system // Setup our clock system
uClock.init();
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// For MIDI Sync Start and Stop // For MIDI Sync Start and Stop
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
uClock.setMode(uClock.EXTERNAL_CLOCK); uClock.setClockMode(uClock.EXTERNAL_CLOCK);
// 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
uClock.setExtIntervalBuffer(128);
// inits uClock
uClock.init();
// make use of 250us timer to handle midi input sync // make use of 250us timer to handle midi input sync
teensyTimer.begin(handleMidiInput, 250); teensyTimer.begin(handleMidiInput, 250);
teensyTimer.priority(80); teensyTimer.priority(80);
} }
@ -136,20 +144,20 @@ void loop() {
u8x8->drawUTF8(8+4, 7, " "); u8x8->drawUTF8(8+4, 7, " ");
} }
} }
if (clock_state != uClock.state) { if (clock_state != uClock.state) {
clock_state = uClock.state; clock_state = uClock.state;
if (clock_state >= 1) { if (clock_state >= 1) {
u8x8->drawUTF8(0, 7, "Playing"); u8x8->drawUTF8(0, 7, "Playing");
} else { } else {
u8x8->drawUTF8(0, 7, "Stopped"); u8x8->drawUTF8(0, 7, "Stopped");
} }
} }
if (clock_mode != uClock.getMode()) { if (clock_mode != uClock.getMode()) {
clock_mode = uClock.getMode(); clock_mode = uClock.getMode();
if (clock_mode == uClock.EXTERNAL_CLOCK) { if (clock_mode == uClock.EXTERNAL_CLOCK) {
u8x8->drawUTF8(10, 0, "Slave "); u8x8->drawUTF8(10, 0, "Slave ");
} else { } else {
u8x8->drawUTF8(10, 0, "Master"); u8x8->drawUTF8(10, 0, "Master");
} }
} }
} }

@ -1,12 +1,12 @@
/* USB MIDI Sync Box /* USB MIDI Sync Box
* *
* This example demonstrates how to change the USB MIDI * This example demonstrates how to change the USB MIDI
* device name on Seeedstudio XIAO M0. * device name on Seeedstudio XIAO M0.
* *
* This example code is in the public domain. * This example code is in the public domain.
* *
* Tested with Adafruit TinyUSB version 0.10.5 * Tested with Adafruit TinyUSB version 0.10.5
* *
*/ */
#include <Adafruit_TinyUSB.h> #include <Adafruit_TinyUSB.h>
#include <MIDI.h> #include <MIDI.h>
@ -52,15 +52,17 @@ void setup() {
// A led to count bpms // A led to count bpms
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
// Setup our clock system // Setup our clock system
// Inits the clock
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.
uClock.setOnSync24(onSync24Callback); uClock.setOnSync24(onSync24Callback);
// Set the callback function for MIDI Start and Stop messages. // Set the callback function for MIDI Start and Stop messages.
uClock.setOnClockStart(onClockStart); uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop); uClock.setOnClockStop(onClockStop);
// Inits the clock
uClock.init();
// Set the clock BPM to 126 BPM // Set the clock BPM to 126 BPM
uClock.setTempo(126); uClock.setTempo(126);
// Starts the clock, tick-tac-tick-tac... // Starts the clock, tick-tac-tick-tac...

@ -23,7 +23,7 @@
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE. * DEALINGS IN THE SOFTWARE.
*/ */
#include "uClock.h" #include "uClock.h"
@ -90,23 +90,23 @@
// header of this file // header of this file
void uclockInitTimer() void uclockInitTimer()
{ {
// begin at 120bpm // begin at 120bpm
initTimer(uClock.bpmToMicroSeconds(120.00)); initTimer(uClock.bpmToMicroSeconds(120.00));
} }
void setTimerTempo(float bpm) void setTimerTempo(float bpm)
{ {
setTimer(uClock.bpmToMicroSeconds(bpm)); setTimer(uClock.bpmToMicroSeconds(bpm));
} }
namespace umodular { namespace clock { namespace umodular { namespace clock {
static inline uint32_t phase_mult(uint32_t val) static inline uint32_t phase_mult(uint32_t val)
{ {
return (val * PHASE_FACTOR) >> 8; return (val * PHASE_FACTOR) >> 8;
} }
static inline uint32_t clock_diff(uint32_t old_clock, uint32_t new_clock) static inline uint32_t clock_diff(uint32_t old_clock, uint32_t new_clock)
{ {
if (new_clock >= old_clock) { if (new_clock >= old_clock) {
return new_clock - old_clock; return new_clock - old_clock;
@ -140,21 +140,24 @@ uClockClass::uClockClass()
calculateReferencedata(); calculateReferencedata();
} }
void uClockClass::init() void uClockClass::init()
{ {
if (ext_interval_buffer == nullptr)
setExtIntervalBuffer(1);
uclockInitTimer(); uclockInitTimer();
// first interval calculus // first interval calculus
setTempo(tempo); setTempo(tempo);
} }
uint32_t uClockClass::bpmToMicroSeconds(float bpm) uint32_t uClockClass::bpmToMicroSeconds(float bpm)
{ {
return (60000000.0f / (float)output_ppqn / bpm); return (60000000.0f / (float)output_ppqn / bpm);
} }
void uClockClass::calculateReferencedata() void uClockClass::calculateReferencedata()
{ {
mod_clock_ref = output_ppqn / input_ppqn; mod_clock_ref = output_ppqn / input_ppqn;
mod_sync1_ref = output_ppqn / PPQN_1; mod_sync1_ref = output_ppqn / PPQN_1;
mod_sync2_ref = output_ppqn / PPQN_2; mod_sync2_ref = output_ppqn / PPQN_2;
mod_sync4_ref = output_ppqn / PPQN_4; mod_sync4_ref = output_ppqn / PPQN_4;
@ -185,20 +188,20 @@ void uClockClass::setInputPPQN(PPQNResolution resolution)
) )
} }
void uClockClass::start() void uClockClass::start()
{ {
resetCounters(); resetCounters();
start_timer = millis(); start_timer = millis();
if (onClockStartCallback) { if (onClockStartCallback) {
onClockStartCallback(); onClockStartCallback();
} }
if (clock_mode == INTERNAL_CLOCK) { if (clock_mode == INTERNAL_CLOCK) {
clock_state = STARTED; clock_state = STARTED;
} else { } else {
clock_state = STARTING; clock_state = STARTING;
} }
} }
void uClockClass::stop() void uClockClass::stop()
@ -211,7 +214,7 @@ void uClockClass::stop()
} }
} }
void uClockClass::pause() void uClockClass::pause()
{ {
if (clock_mode == INTERNAL_CLOCK) { if (clock_mode == INTERNAL_CLOCK) {
if (clock_state == PAUSED) { if (clock_state == PAUSED) {
@ -222,12 +225,12 @@ void uClockClass::pause()
} }
} }
void uClockClass::setTempo(float bpm) void uClockClass::setTempo(float bpm)
{ {
if (clock_mode == EXTERNAL_CLOCK) { if (clock_mode == EXTERNAL_CLOCK) {
return; return;
} }
if (bpm < MIN_BPM || bpm > MAX_BPM) { if (bpm < MIN_BPM || bpm > MAX_BPM) {
return; return;
} }
@ -239,19 +242,19 @@ void uClockClass::setTempo(float bpm)
setTimerTempo(bpm); setTimerTempo(bpm);
} }
float uClockClass::getTempo() float uClockClass::getTempo()
{ {
if (clock_mode == EXTERNAL_CLOCK) { if (clock_mode == EXTERNAL_CLOCK) {
uint32_t acc = 0; uint32_t acc = 0;
// wait the buffer to 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;
} }
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]; acc += ext_interval_buffer[i];
} }
if (acc != 0) { if (acc != 0) {
return freqToBpm(acc / EXT_INTERVAL_BUFFER_SIZE); return constrainBpm(freqToBpm(acc / ext_interval_buffer_size));
} }
} }
return tempo; return tempo;
@ -272,17 +275,22 @@ float inline uClockClass::freqToBpm(uint32_t freq)
return (float)((float)(usecs/(float)input_ppqn) * 60.0); return (float)((float)(usecs/(float)input_ppqn) * 60.0);
} }
void uClockClass::setClockMode(ClockMode tempo_mode) float inline uClockClass::constrainBpm(float bpm)
{
return (bpm < MIN_BPM) ? MIN_BPM : ( bpm > MAX_BPM ? MAX_BPM : bpm );
}
void uClockClass::setClockMode(ClockMode tempo_mode)
{ {
clock_mode = tempo_mode; clock_mode = tempo_mode;
} }
uClockClass::ClockMode uClockClass::getClockMode() uClockClass::ClockMode uClockClass::getClockMode()
{ {
return clock_mode; return clock_mode;
} }
void uClockClass::clockMe() void uClockClass::clockMe()
{ {
if (clock_mode == EXTERNAL_CLOCK) { if (clock_mode == EXTERNAL_CLOCK) {
ATOMIC( ATOMIC(
@ -291,7 +299,17 @@ void uClockClass::clockMe()
} }
} }
void uClockClass::resetCounters() 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; tick = 0;
int_clock_tick = 0; int_clock_tick = 0;
@ -316,16 +334,16 @@ void uClockClass::resetCounters()
sync24_tick = 0; sync24_tick = 0;
mod_sync48_counter = 0; mod_sync48_counter = 0;
sync48_tick = 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; ext_interval_buffer[i] = 0;
} }
} }
void uClockClass::tap() void uClockClass::tap()
{ {
// we can make use of mod_sync1_ref for tap // we can make use of mod_sync1_ref for tap
//uint8_t mod_tap_ref = output_ppqn / PPQN_1; //uint8_t mod_tap_ref = output_ppqn / PPQN_1;
// we only set tap if ClockMode is INTERNAL_CLOCK // we only set tap if ClockMode is INTERNAL_CLOCK
} }
@ -381,7 +399,7 @@ bool inline uClockClass::processShuffle()
int8_t shff = shuffle.step[step_counter%shuffle.size]; int8_t shff = shuffle.step[step_counter%shuffle.size];
if (shuffle_shoot_ctrl == false && mod_step_counter == 0) if (shuffle_shoot_ctrl == false && mod_step_counter == 0)
shuffle_shoot_ctrl = true; shuffle_shoot_ctrl = true;
//if (mod_step_counter == mod_step_ref-1) //if (mod_step_counter == mod_step_ref-1)
@ -389,11 +407,11 @@ bool inline uClockClass::processShuffle()
mod_shuffle = mod_step_counter - shff; mod_shuffle = mod_step_counter - shff;
// any late shuffle? we should skip next mod_step_counter == 0 // any late shuffle? we should skip next mod_step_counter == 0
if (last_shff < 0 && mod_step_counter != 1) if (last_shff < 0 && mod_step_counter != 1)
return false; return false;
} else if (shff < 0) { } else if (shff < 0) {
mod_shuffle = mod_step_counter - (mod_step_ref + shff); mod_shuffle = mod_step_counter - (mod_step_ref + shff);
//if (last_shff < 0 && mod_step_counter != 1) //if (last_shff < 0 && mod_step_counter != 1)
// return false; // return false;
shuffle_shoot_ctrl = true; shuffle_shoot_ctrl = true;
} }
@ -414,7 +432,7 @@ bool inline uClockClass::processShuffle()
return false; return false;
} }
void uClockClass::handleExternalClock() void uClockClass::handleExternalClock()
{ {
switch (clock_state) { switch (clock_state) {
case PAUSED: case PAUSED:
@ -434,7 +452,7 @@ void uClockClass::handleExternalClock()
ext_clock_tick++; ext_clock_tick++;
// accumulate interval incomming ticks data for getTempo() smooth reads on slave clock_mode // 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_idx = 0;
} }
ext_interval_buffer[ext_interval_idx] = last_interval; ext_interval_buffer[ext_interval_idx] = last_interval;
@ -448,7 +466,7 @@ void uClockClass::handleExternalClock()
} }
} }
void uClockClass::handleTimerInt() void uClockClass::handleTimerInt()
{ {
// track main input clock counter // track main input clock counter
if (mod_clock_counter == mod_clock_ref) if (mod_clock_counter == mod_clock_ref)
@ -479,12 +497,10 @@ void uClockClass::handleTimerInt()
} }
// update internal clock timer frequency // update internal clock timer frequency
float bpm = freqToBpm(counter); float bpm = constrainBpm(freqToBpm(counter));
if (bpm != tempo) { if (bpm != tempo) {
if (bpm >= MIN_BPM && bpm <= MAX_BPM) { tempo = bpm;
tempo = bpm; setTimerTempo(bpm);
setTimerTempo(bpm);
}
} }
} }
@ -515,7 +531,7 @@ void uClockClass::handleTimerInt()
} }
++mod_sync2_counter; ++mod_sync2_counter;
} }
// Sync4 callback // Sync4 callback
if (onSync4Callback) { if (onSync4Callback) {
if (mod_sync4_counter == mod_sync4_ref) if (mod_sync4_counter == mod_sync4_ref)
@ -583,7 +599,7 @@ void uClockClass::handleTimerInt()
if (mod_step_counter == mod_step_ref) if (mod_step_counter == mod_step_ref)
mod_step_counter = 0; mod_step_counter = 0;
// processShufle make use of mod_step_counter == 0 logic too // processShufle make use of mod_step_counter == 0 logic too
if (processShuffle()) { if (processShuffle()) {
onStepCallback(step_counter); onStepCallback(step_counter);
// going forward to the next step call // going forward to the next step call
++step_counter; ++step_counter;
@ -605,7 +621,7 @@ uint8_t uClockClass::getNumberOfMinutes(uint32_t time)
{ {
if ( time == 0 ) { if ( time == 0 ) {
return time; return time;
} }
return (((_millis - time) / 1000) / SECS_PER_MIN) % SECS_PER_MIN; return (((_millis - time) / 1000) / SECS_PER_MIN) % SECS_PER_MIN;
} }
@ -613,7 +629,7 @@ uint8_t uClockClass::getNumberOfHours(uint32_t time)
{ {
if ( time == 0 ) { if ( time == 0 ) {
return time; return time;
} }
return (((_millis - time) / 1000) % SECS_PER_DAY) / SECS_PER_HOUR; return (((_millis - time) / 1000) % SECS_PER_DAY) / SECS_PER_HOUR;
} }
@ -621,7 +637,7 @@ uint8_t uClockClass::getNumberOfDays(uint32_t time)
{ {
if ( time == 0 ) { if ( time == 0 ) {
return time; return time;
} }
return ((_millis - time) / 1000) / SECS_PER_DAY; return ((_millis - time) / 1000) / SECS_PER_DAY;
} }
@ -629,12 +645,12 @@ uint32_t uClockClass::getNowTimer()
{ {
return _millis; return _millis;
} }
uint32_t uClockClass::getPlayTime() uint32_t uClockClass::getPlayTime()
{ {
return start_timer; return start_timer;
} }
} } // end namespace umodular::clock } } // end namespace umodular::clock
umodular::clock::uClockClass uClock; umodular::clock::uClockClass uClock;
@ -642,13 +658,13 @@ umodular::clock::uClockClass uClock;
volatile uint32_t _millis = 0; volatile uint32_t _millis = 0;
// //
// TIMER HANDLER // TIMER HANDLER
// //
void uClockHandler() void uClockHandler()
{ {
// global timer counter // global timer counter
_millis = millis(); _millis = millis();
if (uClock.clock_state == uClock.STARTED) { if (uClock.clock_state == uClock.STARTED) {
uClock.handleTimerInt(); uClock.handleTimerInt();
} }

@ -23,7 +23,7 @@
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE. * DEALINGS IN THE SOFTWARE.
*/ */
#ifndef __U_CLOCK_H__ #ifndef __U_CLOCK_H__
@ -36,7 +36,7 @@ namespace umodular { namespace clock {
// Shuffle templates are specific for each PPQN output resolution // Shuffle templates are specific for each PPQN output resolution
// min: -(output_ppqn/4)-1 ticks // min: -(output_ppqn/4)-1 ticks
// max: (output_ppqn/4)-1 ticks // max: (output_ppqn/4)-1 ticks
// adjust the size of you template if more than 16 shuffle step info needed // adjust the size of you template if more than 16 shuffle step info needed
#define MAX_SHUFFLE_TEMPLATE_SIZE 16 #define MAX_SHUFFLE_TEMPLATE_SIZE 16
typedef struct { typedef struct {
@ -45,12 +45,6 @@ typedef struct {
int8_t step[MAX_SHUFFLE_TEMPLATE_SIZE] = {0}; int8_t step[MAX_SHUFFLE_TEMPLATE_SIZE] = {0};
} SHUFFLE_TEMPLATE; } 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 MIN_BPM 1
#define MAX_BPM 400 #define MAX_BPM 400
@ -90,7 +84,7 @@ class uClockClass {
}; };
ClockState clock_state; ClockState clock_state;
uClockClass(); uClockClass();
void setOnOutputPPQN(void (*callback)(uint32_t tick)) { void setOnOutputPPQN(void (*callback)(uint32_t tick)) {
@ -105,19 +99,19 @@ class uClockClass {
void setOnSync1(void (*callback)(uint32_t tick)) { void setOnSync1(void (*callback)(uint32_t tick)) {
onSync1Callback = callback; onSync1Callback = callback;
} }
void setOnSync2(void (*callback)(uint32_t tick)) { void setOnSync2(void (*callback)(uint32_t tick)) {
onSync2Callback = callback; onSync2Callback = callback;
} }
void setOnSync4(void (*callback)(uint32_t tick)) { void setOnSync4(void (*callback)(uint32_t tick)) {
onSync4Callback = callback; onSync4Callback = callback;
} }
void setOnSync8(void (*callback)(uint32_t tick)) { void setOnSync8(void (*callback)(uint32_t tick)) {
onSync8Callback = callback; onSync8Callback = callback;
} }
void setOnSync12(void (*callback)(uint32_t tick)) { void setOnSync12(void (*callback)(uint32_t tick)) {
onSync12Callback = callback; onSync12Callback = callback;
} }
@ -125,7 +119,7 @@ class uClockClass {
void setOnSync24(void (*callback)(uint32_t tick)) { void setOnSync24(void (*callback)(uint32_t tick)) {
onSync24Callback = callback; onSync24Callback = callback;
} }
void setOnSync48(void (*callback)(uint32_t tick)) { void setOnSync48(void (*callback)(uint32_t tick)) {
onSync48Callback = callback; onSync48Callback = callback;
} }
@ -145,7 +139,7 @@ class uClockClass {
void handleTimerInt(); void handleTimerInt();
void handleExternalClock(); void handleExternalClock();
void resetCounters(); void resetCounters();
// external class control // external class control
void start(); void start();
void stop(); void stop();
@ -160,6 +154,11 @@ class uClockClass {
void setClockMode(ClockMode tempo_mode); void setClockMode(ClockMode tempo_mode);
ClockMode getClockMode(); ClockMode getClockMode();
void clockMe(); 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 // shuffle
void setShuffle(bool active); void setShuffle(bool active);
@ -169,10 +168,10 @@ class uClockClass {
void setShuffleTemplate(int8_t * shuff, uint8_t size); void setShuffleTemplate(int8_t * shuff, uint8_t size);
// use this to know how many positive or negative ticks to add to current note length // use this to know how many positive or negative ticks to add to current note length
int8_t getShuffleLength(); int8_t getShuffleLength();
// todo! // todo!
void tap(); void tap();
// elapsed time support // elapsed time support
uint8_t getNumberOfSeconds(uint32_t time); uint8_t getNumberOfSeconds(uint32_t time);
uint8_t getNumberOfMinutes(uint32_t time); uint8_t getNumberOfMinutes(uint32_t time);
@ -185,6 +184,7 @@ class uClockClass {
private: private:
float inline freqToBpm(uint32_t freq); float inline freqToBpm(uint32_t freq);
float inline constrainBpm(float bpm);
void calculateReferencedata(); void calculateReferencedata();
// shuffle // shuffle
@ -246,7 +246,8 @@ class uClockClass {
uint32_t start_timer; uint32_t start_timer;
ClockMode clock_mode; 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; uint16_t ext_interval_idx;
// shuffle implementation // shuffle implementation
@ -265,4 +266,3 @@ extern "C" {
} }
#endif /* __U_CLOCK_H__ */ #endif /* __U_CLOCK_H__ */

Loading…
Cancel
Save