diff --git a/README.md b/README.md index 7c52d2c..b27e88c 100755 --- a/README.md +++ b/README.md @@ -1,21 +1,48 @@ # uClock -The **uClock BPM Generator library** is designed to implement precise and reliable BPM clock tick calls using the microcontroller's timer hardware interruption. It is designed to be multi-architecture, portable, and easy to use within the open source community universe. +The **uClock BPM Generator library** is designed to implement precise and reliable BPM clock tick calls using the microcontroller's timer hardware interrupt. It is built to be multi-architecture, portable, and easy to use within the open-source ecosystem. -We have chosen PlatformIO and Arduino as our official deployment platforms. The library has been supported and tested on general **AVR boards (ATmega168/328, ATmega16u4/32u4, and ATmega2560)** as well as **ARM boards (Teensy, STM32XX, ESP32, Raspberry Pico, Seedstudio XIAO M0 and RP2040)**. +We have chosen PlatformIO and Arduino as our official deployment platforms. The library has been supported and tested on various **AVR boards (ATmega168/328, ATmega16u4/32u4, and ATmega2560)** as well as **ARM boards (Teensy, STM32XX, ESP32, Raspberry Pi Pico, Seeed Studio XIAO M0, and RP2040)**. -The absence of real-time features necessary for creating professional-level embedded devices for music and video on open source community-based platforms like Arduino led to the development of uClock. By leveraging the use of timer hardware interruptions, the library can schedule and manage real-time-like processing with safe shared resource access through its API. +The absence of real-time features necessary for creating professional-level embedded devices for music and video on open-source community-based platforms like Arduino led to the development of uClock. By leveraging timer hardware interrupts, the library can schedule and manage real-time processing with safe shared resource access through its API. -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 can create professional-grade sequencers, sync boxes, or generate a precise BPM clock for external devices in music, audio/video production, 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 control your entire MIDI setup or any other protocols according to your specific preferences and requirements. ## Interface -The uClock library API operates through attached callback functions mechanism: +The uClock library API operates through an attached callback function mechanism: -1. **setOnPPQN(onPPQNCallback) > onPPQNCallback(uint32_t tick)** calls on each new pulse based on selected PPQN resolution (if no PPQN set, the default is 96PPQN) -2. **setOnStep(onStepCallback) > onStepCallback(uint32_t step)** good way to code old style step sequencer based on 16th note schema (not dependent on PPQN resolution) -3. **setOnSync24(onSync24Callback) > onSync24Callback(uint32_t tick)** good way to code a clock machine, or keep your devices synced with your device -4. **setOnClockStart(onClockStartCallback) > onClockStartCallback()** on uClock Start event -5. **setOnClockStop(onClockStopCallback) > onClockStopCallback()** on uClock Stop event +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. **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. **setOnClockStart(onClockStartCallback) > onClockStartCallback()** On the uClock Start event. +5. **setOnClockStop(onClockStopCallback) > onClockStopCallback()** On the uClock Stop event. + +### 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) +2. **PPQN_2** 2 Pulses Per Quarter Note (only input) +3. **PPQN_4** 4 Pulses Per Quarter Note +4. **PPQN_8** 8 Pulses Per Quarter Note +5. **PPQN_12** 12 Pulses Per Quarter Note +6. **PPQN_24** 24 Pulses Per Quarter Note +7. **PPQN_48** 48 Pulses Per Quarter Note +8. **PPQN_96** 96 Pulses Per Quarter Note +9. **PPQN_384** 384 Pulses Per Quarter Note +10. **PPQN_480** 480 Pulses Per Quarter Note +11. **PPQN_960** 960 Pulses Per Quarter Note + +To generate a MIDI sync signal and synchronize external MIDI devices, you can start with a resolution of 24 PPQN, which aligns with the clocking standards of modern MIDI-syncable devices commonly available on 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 which corresponds to a step played, note or event. ### Software Timer mode - for unsupported boards (or avoiding usage of interrupts) If a supported board isn't detected during compilation then a generic fallback approach will be used. This does not utilise any interrupts and so does not ensure accurate timekeeping. This can be useful to port your projects to boards that do not have support in uClock yet, or to test if suspected bugs in your code are related to interactions with interrupts or task handling. @@ -27,7 +54,7 @@ In order for software timer mode to work, you need to add a call to your `loop() ```c++ void loop() { uClock.run(); - + // do anything else you need to do inside loop()... // you can intercalate your main processing with other uClock.run() calls to avoid timming accuracy loss. //uClock.run(); @@ -37,31 +64,16 @@ void loop() { } ``` -## Set your 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** 960 Pulses Per Quarter Note - -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 which 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. - ## uClock v2.0 Breaking Changes If you are coming from uClock version < 2.0 versions, pay attention to the breaking changes so you can update your code to reflect the new API interface: ### setCallback function name changes -- **setClock96PPQNOutput(onClock96PPQNOutputCallback)** is now _setOnPPQN(onPPQNCallback)_ and this clock depends on the PPQN setup using _setPPQN(clockPPQNResolution)_. For clock setup you now use a separated callback via _setOnSync24(onSync24Callback)_ -- **setClock16PPQNOutput(ClockOut16PPQN)** is now _setOnStep(onStepCall)_ and it's not dependent on clock PPQN resolution -- **setOnClockStartOutput(onClockStartCallback)** is now _setOnClockStart(onClockStartCallback)_ -- **setOnClockStopOutput(onClockStopCallback)** is now _setOnClockStop(onClockStopCallback)_ +- `setClock96PPQNOutput(onClock96PPQNOutputCallback)` is now renamed to **`setOnOutputPPQN(onOutputPPQNCallback)`**, and its tick count is based on the PPQN setup using **`setOutputPPQN(clockOutputPPQNResolution)`**. For clock ticks, you now use a separated callback via **`setOnSyncXX(onSyncXXCallback)`**, where XX represents one of the available PPQN values +- `setClock16PPQNOutput(ClockOut16PPQN)` is now renamed to **`setOnStep(onStepCall)`**, and it's not dependent on clock PPQN resolution. +- `setOnClockStartOutput(onClockStartCallback)` is now renamed to **`setOnClockStart(onClockStartCallback)`**. +- `setOnClockStopOutput(onClockStopCallback)` is now renamed to **`setOnClockStop(onClockStopCallback)`**. ### Tick resolution and sequencers @@ -69,7 +81,7 @@ If created a device using setClock16PPQNOutput only you just change the API call # Examples -You will find more complete examples on examples/ folder: +You will find more complete examples on examples/ folder: ```c++ #include @@ -78,7 +90,7 @@ You will find more complete examples on examples/ folder: bool _external_sync_on = false; // the main uClock PPQN resolution ticking -void onPPQNCallback(uint32_t tick) { +void onOutputPPQNCallback(uint32_t tick) { // tick your sequencers or tickable devices... } @@ -86,11 +98,31 @@ void onStepCallback(uint32_t step) { // 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. void onSync24Callback(uint32_t tick) { // 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. void onClockStartCallback() { // send start signal to... @@ -102,28 +134,41 @@ void onClockStopCallback() { } void setup() { - - // inits the clock library - uClock.init(); - - // avaliable resolutions - // [ uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] + // setup clock library + // avaliable output resolutions + // [ 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 ] // 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! - uClock.setOnPPQN(onPPQNCallback); + uClock.setOnOutputPPQN(onOutputPPQNCallback); 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); + // some korg machines do 48ppqn + uClock.setOnSync48(onSync48Callback); uClock.setOnClockStart(onClockStartCallback); uClock.setOnClockStop(onClockStopCallback); // set external sync mode? 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 uClock.start(); } @@ -180,16 +225,17 @@ void setup() { // Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication: Serial.begin(31250); - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message based on 24PPQN uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStartOutput(onClockStart); + uClock.setOnClockStartOutput(onClockStart); uClock.setOnClockStopOutput(onClockStop); // Set the clock BPM to 126 BPM uClock.setTempo(126); + // Inits the clock + uClock.init(); + // Starts the clock, tick-tac-tick-tac... uClock.start(); @@ -223,15 +269,16 @@ void onClockStop() { } void setup() { - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. based on 24PPQN uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStartOutput(onClockStart); + uClock.setOnClockStartOutput(onClockStart); uClock.setOnClockStopOutput(onClockStop); // Set the clock BPM to 126 BPM uClock.setTempo(126); + + // Inits the clock + uClock.init(); // Starts the clock, tick-tac-tick-tac... uClock.start(); } @@ -302,7 +349,7 @@ bool _playing = false; uint16_t _step = 0; void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2) -{ +{ // send midi message command = command | (uint8_t)MIDI_CHANNEL; Serial.write(command); @@ -311,14 +358,14 @@ void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2) } // The callback function called by uClock each Pulse of 16PPQN clock resolution. Each call represents exactly one step. -void onStepCallback(uint32_t tick) +void onStepCallback(uint32_t tick) { uint16_t step; uint16_t length = NOTE_LENGTH; - + // get actual step. _step = tick % _step_length; - + // send note on only if this step are not in rest mode if ( _sequencer[_step].rest == false ) { @@ -341,15 +388,15 @@ void onStepCallback(uint32_t tick) _note_stack[i].note = _sequencer[_step].note; _note_stack[i].length = length; // 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; } } - } + } } // The callback function called by uClock each Pulse of 96PPQN clock resolution. -void onPPQNCallback(uint32_t tick) +void onPPQNCallback(uint32_t tick) { // Send MIDI_CLOCK to external hardware Serial.write(MIDI_CLOCK); @@ -362,19 +409,19 @@ void onPPQNCallback(uint32_t tick) sendMidiMessage(NOTE_OFF, _note_stack[i].note, 0); _note_stack[i].length = -1; } - } + } } } // The callback function called when clock starts by using Clock.start() method. -void onClockStart() +void onClockStart() { Serial.write(MIDI_START); _playing = true; } // The callback function called when clock stops by using Clock.stop() method. -void onClockStop() +void onClockStop() { Serial.write(MIDI_STOP); // send all note off on sequencer stop @@ -385,25 +432,22 @@ void onClockStop() _playing = false; } -void setup() +void setup() { // Initialize serial communication // 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. - uClock.setOnPPQN(onPPQNCallback); - + uClock.setOnOutputPPQN(onPPQNCallback); + // 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. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); - + // Set the clock BPM to 126 BPM uClock.setTempo(126); @@ -423,13 +467,16 @@ void setup() // pins, buttons, leds and pots config //configureYourUserInterface(); - + + // Inits the clock + uClock.init(); + // start sequencer uClock.start(); } // User interaction goes here -void loop() +void loop() { // Controls your 303 engine interacting with user here... // you can change data by using _sequencer[] and _step_length only! do not mess with _note_stack[]! diff --git a/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino b/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino index 10bf4f2..c7c3cae 100644 --- a/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino +++ b/examples/AVRUartSlaveMidiClockMonitor/AVRUartSlaveMidiClockMonitor.ino @@ -1,10 +1,10 @@ /* Uart MIDI Sync Slave Box Monitor - * + * * This example demonstrates how to create a - * MIDI slave clock box with + * MIDI slave clock box with * 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. * * 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_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8->begin(); - u8x8->setFont(u8x8_font_pressstart2p_r); + u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->clear(); u8x8->setFlipMode(true); - u8x8->drawUTF8(0, 0, "uClock"); + u8x8->drawUTF8(0, 0, "uClock"); // // uClock Setup // - uClock.init(); uClock.setOnSync24(onSync24Callback); // For MIDI Sync Start and Stop uClock.setOnClockStart(onClockStart); 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.start(); } @@ -124,7 +132,7 @@ void setup() { void loop() { while(MIDI.read()) {} // 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 if (bpm != uClock.getTempo()) { bpm = uClock.getTempo(); diff --git a/examples/AcidStepSequencer/AcidStepSequencer.ino b/examples/AcidStepSequencer/AcidStepSequencer.ino index 41f3928..33efcc4 100644 --- a/examples/AcidStepSequencer/AcidStepSequencer.ino +++ b/examples/AcidStepSequencer/AcidStepSequencer.ino @@ -54,7 +54,7 @@ bool _playing = false; uint16_t _step = 0; void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2) -{ +{ // send midi message command = command | (uint8_t)MIDI_CHANNEL; Serial.write(command); @@ -63,14 +63,14 @@ void sendMidiMessage(uint8_t command, uint8_t byte1, uint8_t byte2) } // Each call represents exactly one step. -void onStepCallback(uint32_t tick) +void onStepCallback(uint32_t tick) { uint16_t step; uint16_t length = NOTE_LENGTH; - + // get actual step. _step = tick % _step_length; - + // send note on only if this step are not in rest mode if ( _sequencer[_step].rest == false ) { @@ -93,15 +93,15 @@ void onStepCallback(uint32_t tick) _note_stack[i].note = _sequencer[_step].note; _note_stack[i].length = length; // 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; } } - } + } } // The callback function wich will be called by uClock each Pulse of 96PPQN clock resolution. -void onPPQNCallback(uint32_t tick) +void onOutputPPQNCallback(uint32_t tick) { // handle note on stack for ( uint8_t i = 0; i < NOTE_STACK_SIZE; i++ ) { @@ -111,7 +111,7 @@ void onPPQNCallback(uint32_t tick) sendMidiMessage(NOTE_OFF, _note_stack[i].note, 0); _note_stack[i].length = -1; } - } + } } // 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. -void onClockStart() +void onClockStart() { Serial.write(MIDI_START); _playing = true; } // The callback function wich will be called when clock stops by using Clock.stop() method. -void onClockStop() +void onClockStop() { Serial.write(MIDI_STOP); // send all note off on sequencer stop @@ -142,34 +142,34 @@ void onClockStop() _playing = false; } -void setup() +void setup() { // Initialize serial communication #ifdef MIDI_MODE // the default MIDI serial speed communication at 31250 bits per second - Serial.begin(31250); + Serial.begin(31250); #endif #ifdef SERIAL_MODE // for usage with a PC with a serial to MIDI bridge Serial.begin(115200); #endif - // Inits the clock - uClock.init(); - // Set the callback function for the clock output to send MIDI Sync message. - uClock.setOnPPQN(onPPQNCallback); + uClock.setOnOutputPPQN(onOutputPPQNCallback); // for MIDI sync uClock.setOnSync24(onSync24Callback); // 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. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); - + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); @@ -192,7 +192,7 @@ void setup() } // User interaction goes here -void loop() +void loop() { processInterface(); } diff --git a/examples/AcidStepSequencer/DefaultUserInterface.ino b/examples/AcidStepSequencer/DefaultUserInterface.ino index 904d651..ad8bed9 100644 --- a/examples/AcidStepSequencer/DefaultUserInterface.ino +++ b/examples/AcidStepSequencer/DefaultUserInterface.ino @@ -84,15 +84,15 @@ void processInterface() processPots(); } -void tempoInterface(uint32_t * tick) +void tempoInterface(uint32_t tick) { // BPM led indicator - if ( !(*tick % (96)) || (*tick == 0) ) { // first compass step will flash longer + if ( !(tick % (96)) || (tick == 0) ) { // first compass step will flash longer _bpm_blink_timer = 8; digitalWrite(PLAY_STOP_LED_PIN , HIGH); - } else if ( !(*tick % (24)) ) { // each quarter led on + } else if ( !(tick % (24)) ) { // each quarter led on digitalWrite(PLAY_STOP_LED_PIN , HIGH); - } else if ( !(*tick % _bpm_blink_timer) ) { // get led off + } else if ( !(tick % _bpm_blink_timer) ) { // get led off digitalWrite(PLAY_STOP_LED_PIN , LOW); _bpm_blink_timer = 1; } diff --git a/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino b/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino index 25da851..134919d 100644 --- a/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino +++ b/examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino @@ -1,12 +1,12 @@ /* Uart MIDI Sync Box - * - * This example demonstrates how to change the Uart MIDI - * device name on ESP32 family. - * + * + * This example demonstrates how to change the Uart MIDI + * device name on ESP32 family. + * * This example code is in the public domain. - * + * * ... - * + * */ #include @@ -54,15 +54,17 @@ void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // 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... void loop() { - -} \ No newline at end of file + +} diff --git a/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino b/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino index 366e938..d8ae8f8 100644 --- a/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino +++ b/examples/GenericMasterOrExternalSync/GenericMasterOrExternalSync.ino @@ -4,7 +4,7 @@ bool _external_sync_on = false; // the main uClock PPQN resolution ticking -void onPPQNCallback(uint32_t tick) { +void onOutputPPQNCallback(uint32_t tick) { // tick your sequencers or tickable devices... } @@ -12,11 +12,31 @@ void onStepCallback(uint32_t step) { // 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. void onSync24Callback(uint32_t tick) { // 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. void onClockStartCallback() { // send start signal to... @@ -28,28 +48,41 @@ void onClockStopCallback() { } void setup() { - - // inits the clock library - uClock.init(); - - // avaliable resolutions - // [ uClock.PPQN_24, uClock.PPQN_48, uClock.PPQN_96, uClock.PPQN_384, uClock.PPQN_480, uClock.PPQN_960 ] + // setup clock library + // avaliable output resolutions + // [ 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 ] // 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! - uClock.setOnPPQN(onPPQNCallback); + uClock.setOnOutputPPQN(onOutputPPQNCallback); 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); + // some korg machines do 48ppqn + uClock.setOnSync48(onSync48Callback); uClock.setOnClockStart(onClockStartCallback); uClock.setOnClockStop(onClockStopCallback); // set external sync mode? 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 uClock.start(); } @@ -64,4 +97,4 @@ void loop() { uClock.clockMe(); } } -} \ No newline at end of file +} diff --git a/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino b/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino index 76b5f63..a7ac66e 100644 --- a/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino +++ b/examples/LeonardoUsbSlaveMidiClockMonitor/LeonardoUsbSlaveMidiClockMonitor.ino @@ -1,9 +1,9 @@ /* USB MIDI Sync Slave Box Monitor - * + * * 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 - * + * * You need the following libraries to make it work * - Midi Library * - USB-MIDI and MIDIUSB @@ -94,20 +94,28 @@ void setup() { //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8->begin(); - u8x8->setFont(u8x8_font_pressstart2p_r); + u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->clear(); u8x8->setFlipMode(true); - u8x8->drawUTF8(0, 0, "uClock"); + u8x8->drawUTF8(0, 0, "uClock"); // // uClock Setup // - uClock.init(); uClock.setOnSync24(onSync24Callback); // For MIDI Sync Start and Stop uClock.setOnClockStart(onClockStart); 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.start(); } @@ -115,7 +123,7 @@ void setup() { void loop() { while(MIDI.read()) {} // 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 if (bpm != uClock.getTempo()) { bpm = uClock.getTempo(); @@ -127,20 +135,20 @@ void loop() { u8x8->drawUTF8(8+4, 7, " "); } } - if (clock_state != uClock.state) { + if (clock_state != uClock.state) { clock_state = uClock.state; if (clock_state >= 1) { - u8x8->drawUTF8(0, 7, "Playing"); - } else { - u8x8->drawUTF8(0, 7, "Stopped"); + u8x8->drawUTF8(0, 7, "Playing"); + } else { + u8x8->drawUTF8(0, 7, "Stopped"); } } if (clock_mode != uClock.getMode()) { clock_mode = uClock.getMode(); if (clock_mode == uClock.EXTERNAL_CLOCK) { - u8x8->drawUTF8(10, 0, "Slave "); - } else { - u8x8->drawUTF8(10, 0, "Master"); + u8x8->drawUTF8(10, 0, "Slave "); + } else { + u8x8->drawUTF8(10, 0, "Master"); } } } diff --git a/examples/MidiClock/MidiClock.ino b/examples/MidiClock/MidiClock.ino index 169e504..29b9f5a 100755 --- a/examples/MidiClock/MidiClock.ino +++ b/examples/MidiClock/MidiClock.ino @@ -7,37 +7,39 @@ #define MIDI_STOP 0xFC // 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 Serial.write(MIDI_CLOCK); } // The callback function wich will be called when clock starts by using Clock.start() method. -void onClockStart() +void onClockStart() { Serial.write(MIDI_START); } // The callback function wich will be called when clock stops by using Clock.stop() method. -void onClockStop() +void onClockStop() { Serial.write(MIDI_STOP); } -void setup() +void setup() { // Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication: Serial.begin(31250); - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM 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... -void loop() +void loop() { } diff --git a/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino b/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino index e571da9..734d467 100644 --- a/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino +++ b/examples/RP2040UsbUartMasterClock/RP2040UsbUartMasterClock.ino @@ -1,8 +1,8 @@ -/* - * USB/Uart MIDI Sync Box - * +/* + * USB/Uart MIDI Sync Box + * * This example code is in the public domain. - * + * */ #include @@ -68,13 +68,15 @@ void setup() { initBlinkLed(); // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // Starts the clock, tick-tac-tick-tac.. diff --git a/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino b/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino index e3421c6..b91410a 100644 --- a/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino +++ b/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino @@ -1,13 +1,13 @@ /* Uart MIDI out - * - * This example demonstrates how to send MIDI data via Uart - * interface on STM32 family. - * + * + * This example demonstrates how to send MIDI data via Uart + * interface on STM32 family. + * * This example code is in the public domain. * * 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). * * 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 pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(120); // 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... void loop() { - + } diff --git a/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino b/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino index 2c814b4..d9402d8 100644 --- a/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino +++ b/examples/TeensyUsbMasterMidiClock/TeensyUsbMasterMidiClock.ino @@ -1,6 +1,6 @@ /* 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 * that one MIDI device, custom names are much easier to * use when selecting each device in MIDI software on @@ -12,9 +12,9 @@ * steps to get your operating system to "forget" the * cached info. (TODO: wanted... can anyone contribute * instructions for these systems) - * + * * You must select MIDI from the "Tools > USB Type" menu - * + * * This example code is in the public domain. */ @@ -53,15 +53,17 @@ void onClockStop() { void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // Starts the clock, tick-tac-tick-tac... diff --git a/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino b/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino index 98c1ee3..a56ed6b 100644 --- a/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino +++ b/examples/TeensyUsbSlaveMidiClock/TeensyUsbSlaveMidiClock.ino @@ -1,6 +1,6 @@ /* 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 * that one MIDI device, custom names are much easier to * use when selecting each device in MIDI software on @@ -12,9 +12,9 @@ * steps to get your operating system to "forget" the * cached info. (TODO: wanted... can anyone contribute * instructions for these systems) - * + * * You must select MIDI from the "Tools > USB Type" menu - * + * * This example code is in the public domain. */ @@ -69,22 +69,24 @@ void onExternalStop() void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup realtime midi event handlers usbMIDI.setHandleClock(onExternalClock); usbMIDI.setHandleStart(onExternalStart); usbMIDI.setHandleStop(onExternalStop); // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + // set to external sync mode - uClock.setMode(1); + uClock.setClockMode(uClock.EXTERNAL_CLOCK); + + // Inits the clock + uClock.init(); } void loop() { diff --git a/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino b/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino index 57b2989..e6dc0c0 100644 --- a/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino +++ b/examples/TeensyUsbSlaveMidiClockMonitor/TeensyUsbSlaveMidiClockMonitor.ino @@ -1,20 +1,20 @@ /* USB MIDI Sync Slave Box Monitor - * + * * This example demonstrates how to create a * 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 * * Making use of a 250 usceconds timer to * handle MIDI input to avoid jitter on clock - * + * * You need the following libraries to make it work * - u8g2 * - uClock * * This example code is in the public domain. */ - + #include // @@ -100,23 +100,31 @@ void setup() { //u8x8 = new U8X8_SH1106_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8 = new U8X8_SSD1306_128X64_NONAME_HW_I2C(U8X8_PIN_NONE); u8x8->begin(); - u8x8->setFont(u8x8_font_pressstart2p_r); + u8x8->setFont(u8x8_font_pressstart2p_r); u8x8->clear(); u8x8->setFlipMode(true); - u8x8->drawUTF8(0, 0, "uClock"); + u8x8->drawUTF8(0, 0, "uClock"); // // uClock Setup // // Setup our clock system - uClock.init(); uClock.setOnSync24(onSync24Callback); // For MIDI Sync Start and Stop uClock.setOnClockStart(onClockStart); 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 - teensyTimer.begin(handleMidiInput, 250); + teensyTimer.begin(handleMidiInput, 250); teensyTimer.priority(80); } @@ -136,20 +144,20 @@ void loop() { u8x8->drawUTF8(8+4, 7, " "); } } - if (clock_state != uClock.state) { + if (clock_state != uClock.state) { clock_state = uClock.state; if (clock_state >= 1) { - u8x8->drawUTF8(0, 7, "Playing"); - } else { - u8x8->drawUTF8(0, 7, "Stopped"); + u8x8->drawUTF8(0, 7, "Playing"); + } else { + u8x8->drawUTF8(0, 7, "Stopped"); } } if (clock_mode != uClock.getMode()) { clock_mode = uClock.getMode(); if (clock_mode == uClock.EXTERNAL_CLOCK) { - u8x8->drawUTF8(10, 0, "Slave "); - } else { - u8x8->drawUTF8(10, 0, "Master"); + u8x8->drawUTF8(10, 0, "Slave "); + } else { + u8x8->drawUTF8(10, 0, "Master"); } } } diff --git a/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino b/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino index c052f79..33a7827 100644 --- a/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino +++ b/examples/XiaoUsbMasterMidiClock/XiaoUsbMasterMidiClock.ino @@ -1,12 +1,12 @@ /* USB MIDI Sync Box - * - * This example demonstrates how to change the USB MIDI - * device name on Seeedstudio XIAO M0. - * + * + * This example demonstrates how to change the USB MIDI + * device name on Seeedstudio XIAO M0. + * * This example code is in the public domain. - * + * * Tested with Adafruit TinyUSB version 0.10.5 - * + * */ #include #include @@ -52,15 +52,17 @@ void setup() { // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); - + // Setup our clock system - // Inits the clock - uClock.init(); // Set the callback function for the clock output to send MIDI Sync message. uClock.setOnSync24(onSync24Callback); // Set the callback function for MIDI Start and Stop messages. - uClock.setOnClockStart(onClockStart); + uClock.setOnClockStart(onClockStart); uClock.setOnClockStop(onClockStop); + + // Inits the clock + uClock.init(); + // Set the clock BPM to 126 BPM uClock.setTempo(126); // Starts the clock, tick-tac-tick-tac... diff --git a/library.json b/library.json index c66d060..c0c98c2 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "uClock", - "version": "2.2.0", + "version": "2.2.1", "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, STM32XX, ESP32, Raspberry Pico, Seedstudio XIAO M0 and RP2040)", "keywords": "bpm, clock, timing, tick, music, generator", "repository": diff --git a/library.properties b/library.properties index 0f4cce0..300684b 100755 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=uClock -version=2.2.0 +version=2.2.1 author=Romulo Silva maintainer=Romulo Silva sentence=BPM clock generator for Arduino platform. diff --git a/src/platforms/avr.h b/src/platforms/avr.h index 9904c92..9931dac 100644 --- a/src/platforms/avr.h +++ b/src/platforms/avr.h @@ -6,6 +6,15 @@ // TODO: we should do this using macro guards for avrs different clocks freqeuncy setup at compile time #define AVR_CLOCK_FREQ 16000000 +// forward declaration of uClockHandler +void uClockHandler(); + +// AVR ISR Entrypoint +ISR(TIMER1_COMPA_vect) +{ + uClockHandler(); +} + void initTimer(uint32_t init_clock) { ATOMIC( diff --git a/src/platforms/esp32-nofrertos.h b/src/platforms/esp32-nofrertos.h new file mode 100644 index 0000000..70d2720 --- /dev/null +++ b/src/platforms/esp32-nofrertos.h @@ -0,0 +1,32 @@ +#include + +#define TIMER_ID 0 + +hw_timer_t * _uclockTimer = NULL; +portMUX_TYPE _uclockTimerMux = portMUX_INITIALIZER_UNLOCKED; +#define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux); + +// forward declaration of uClockHandler +void uClockHandler(); + +// ISR handler +void ARDUINO_ISR_ATTR handlerISR(void) +{ + uClockHandler(); +} + +void initTimer(uint32_t init_clock) +{ + _uclockTimer = timerBegin(init_clock); + + // attach to generic uclock ISR + timerAttachInterrupt(_uclockTimer, &handlerISR); + + // init clock tick time + timerAlarm(_uclockTimer, init_clock, true, 0); +} + +void setTimer(uint32_t us_interval) +{ + timerAlarmWrite(_uclockTimer, us_interval, true); +} \ No newline at end of file diff --git a/src/platforms/esp32.h b/src/platforms/esp32.h index 372ca93..f0ef639 100644 --- a/src/platforms/esp32.h +++ b/src/platforms/esp32.h @@ -3,11 +3,7 @@ #include // esp32-specific timer -#define TIMER_ID 0 hw_timer_t * _uclockTimer = NULL; -// mutex control for ISR -//portMUX_TYPE _uclockTimerMux = portMUX_INITIALIZER_UNLOCKED; -//#define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux); // FreeRTOS main clock task size in bytes #define CLOCK_STACK_SIZE 5*1024 // adjust for your needs, a sequencer with heavy serial handling should be large in size @@ -47,7 +43,7 @@ void initTimer(uint32_t init_clock) // create the clockTask xTaskCreate(clockTask, "clockTask", CLOCK_STACK_SIZE, NULL, 1, &taskHandle); - _uclockTimer = timerBegin(1000000); + _uclockTimer = timerBegin(init_clock); // attach to generic uclock ISR timerAttachInterrupt(_uclockTimer, &handlerISR); diff --git a/src/uClock.cpp b/src/uClock.cpp index ee8f114..f0fa23d 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -2,7 +2,7 @@ * @file uClock.cpp * 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(RPI2040, Teensy, Seedstudio XIAO M0 and ESP32) - * @version 2.2.0 + * @version 2.2.1 * @author Romulo Silva * @date 10/06/2017 * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co @@ -23,7 +23,7 @@ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * 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 - * DEALINGS IN THE SOFTWARE. + * DEALINGS IN THE SOFTWARE. */ #include "uClock.h" @@ -90,23 +90,23 @@ // header of this file void uclockInitTimer() { - // begin at 120bpm + // begin at 120bpm initTimer(uClock.bpmToMicroSeconds(120.00)); } -void setTimerTempo(float bpm) +void setTimerTempo(float bpm) { setTimer(uClock.bpmToMicroSeconds(bpm)); } 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; } -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) { return new_clock - old_clock; @@ -121,61 +121,92 @@ uClockClass::uClockClass() start_timer = 0; last_interval = 0; sync_interval = 0; - state = PAUSED; - mode = INTERNAL_CLOCK; + clock_state = PAUSED; + clock_mode = INTERNAL_CLOCK; resetCounters(); - onPPQNCallback = nullptr; + onOutputPPQNCallback = nullptr; + onSync1Callback = nullptr; + onSync2Callback = nullptr; + onSync4Callback = nullptr; + onSync8Callback = nullptr; + onSync12Callback = nullptr; onSync24Callback = nullptr; + onSync48Callback = nullptr; onStepCallback = nullptr; onClockStartCallback = nullptr; onClockStopCallback = nullptr; - // first ppqn references calculus - setPPQN(PPQN_96); + // initialize reference data + calculateReferencedata(); } -void uClockClass::init() +void uClockClass::init() { + if (ext_interval_buffer == nullptr) + setExtIntervalBuffer(1); + uclockInitTimer(); // first interval calculus setTempo(tempo); } -uint32_t uClockClass::bpmToMicroSeconds(float bpm) +uint32_t uClockClass::bpmToMicroSeconds(float bpm) +{ + return (60000000.0f / (float)output_ppqn / bpm); +} + +void uClockClass::calculateReferencedata() { - return (60000000.0f / (float)ppqn / bpm); + mod_clock_ref = output_ppqn / input_ppqn; + mod_sync1_ref = output_ppqn / PPQN_1; + mod_sync2_ref = output_ppqn / PPQN_2; + mod_sync4_ref = output_ppqn / PPQN_4; + mod_sync8_ref = output_ppqn / PPQN_8; + mod_sync12_ref = output_ppqn / PPQN_12; + mod_sync24_ref = output_ppqn / PPQN_24; + mod_sync48_ref = output_ppqn / PPQN_48; + mod_step_ref = output_ppqn / 4; } -void uClockClass::setPPQN(PPQNResolution resolution) +void uClockClass::setOutputPPQN(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 and mod_step tick reference trigger - mod24_ref = ppqn / 24; - mod_step_ref = ppqn / 4; + // dont allow PPQN lower than PPQN_4 for output clock (to avoid problems with mod_step_ref) + if (resolution < PPQN_4) + return; + + ATOMIC( + output_ppqn = resolution; + calculateReferencedata(); + ) } -void uClockClass::start() +void uClockClass::setInputPPQN(PPQNResolution resolution) +{ + ATOMIC( + input_ppqn = resolution; + calculateReferencedata(); + ) +} + +void uClockClass::start() { resetCounters(); start_timer = millis(); - + if (onClockStartCallback) { onClockStartCallback(); - } - - if (mode == INTERNAL_CLOCK) { - state = STARTED; + } + + if (clock_mode == INTERNAL_CLOCK) { + clock_state = STARTED; } else { - state = STARTING; - } + clock_state = STARTING; + } } void uClockClass::stop() { - state = PAUSED; + clock_state = PAUSED; start_timer = 0; resetCounters(); if (onClockStopCallback) { @@ -184,12 +215,12 @@ void uClockClass::stop() } void uClockClass::continue_playing() { - if (state == PAUSED) { + if (clock_state == PAUSED) { start_timer = millis(); - if (mode == INTERNAL_CLOCK) { - state = STARTED; + if (clock_mode == INTERNAL_CLOCK) { + clock_state = STARTED; } else { - state = STARTING; + clock_state = STARTING; } if (onClockContinueCallback) { onClockContinueCallback(); @@ -197,13 +228,13 @@ void uClockClass::continue_playing() { } } -void uClockClass::pause() +void uClockClass::pause() { - if (mode == INTERNAL_CLOCK) { - if (state == PAUSED) { - continue_playing(); + if (clock_mode == INTERNAL_CLOCK) { + if (clock_state == PAUSED) { + start(); } else { - state = PAUSED; + clock_state = PAUSED; if (onClockPauseCallback) { onClockPauseCallback(); } @@ -211,12 +242,12 @@ void uClockClass::pause() } } -void uClockClass::setTempo(float bpm) +void uClockClass::setTempo(float bpm) { - if (mode == EXTERNAL_CLOCK) { + if (clock_mode == EXTERNAL_CLOCK) { return; } - + if (bpm < MIN_BPM || bpm > MAX_BPM) { return; } @@ -228,19 +259,19 @@ void uClockClass::setTempo(float bpm) setTimerTempo(bpm); } -float uClockClass::getTempo() +float uClockClass::getTempo() { - if (mode == EXTERNAL_CLOCK) { + if (clock_mode == EXTERNAL_CLOCK) { uint32_t acc = 0; // 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; } - 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]; } if (acc != 0) { - return freqToBpm(acc / EXT_INTERVAL_BUFFER_SIZE); + return constrainBpm(freqToBpm(acc / ext_interval_buffer_size)); } } return tempo; @@ -255,52 +286,82 @@ void uClockClass::run() #endif } -// this function is based on sync24PPQN float inline uClockClass::freqToBpm(uint32_t freq) { float usecs = 1/((float)freq/1000000.0); - return (float)((float)(usecs/(float)24) * 60.0); + return (float)((float)(usecs/(float)input_ppqn) * 60.0); } -void uClockClass::setMode(SyncMode tempo_mode) +float inline uClockClass::constrainBpm(float bpm) { - mode = tempo_mode; + return (bpm < MIN_BPM) ? MIN_BPM : ( bpm > MAX_BPM ? MAX_BPM : bpm ); } -uClockClass::SyncMode uClockClass::getMode() +void uClockClass::setClockMode(ClockMode tempo_mode) { - return mode; + clock_mode = tempo_mode; } -void uClockClass::clockMe() +uClockClass::ClockMode uClockClass::getClockMode() { - if (mode == EXTERNAL_CLOCK) { + return clock_mode; +} + +void uClockClass::clockMe() +{ + if (clock_mode == EXTERNAL_CLOCK) { ATOMIC( handleExternalClock() ) } } -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; int_clock_tick = 0; - mod24_counter = 0; + mod_clock_counter = 0; mod_step_counter = 0; step_counter = 0; ext_clock_tick = 0; ext_clock_us = 0; ext_interval_idx = 0; - - for (uint8_t i=0; i < EXT_INTERVAL_BUFFER_SIZE; i++) { + // sync output counters + mod_sync1_counter = 0; + sync1_tick = 0; + mod_sync2_counter = 0; + sync2_tick = 0; + mod_sync4_counter = 0; + sync4_tick = 0; + mod_sync8_counter = 0; + sync8_tick = 0; + mod_sync12_counter = 0; + sync12_tick = 0; + mod_sync24_counter = 0; + sync24_tick = 0; + mod_sync48_counter = 0; + sync48_tick = 0; + + for (uint8_t i=0; i < ext_interval_buffer_size; i++) { ext_interval_buffer[i] = 0; } } -// TODO: Tap stuff -void uClockClass::tap() +void uClockClass::tap() { - // tap me + // we can make use of mod_sync1_ref for tap + //uint8_t mod_tap_ref = output_ppqn / PPQN_1; + // we only set tap if ClockMode is INTERNAL_CLOCK } void uClockClass::setShuffle(bool active) @@ -355,7 +416,7 @@ bool inline uClockClass::processShuffle() int8_t shff = shuffle.step[step_counter%shuffle.size]; 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) @@ -363,17 +424,17 @@ bool inline uClockClass::processShuffle() mod_shuffle = mod_step_counter - shff; // any late shuffle? we should skip next mod_step_counter == 0 if (last_shff < 0 && mod_step_counter != 1) - return false; + return false; } else if (shff < 0) { mod_shuffle = mod_step_counter - (mod_step_ref + shff); //if (last_shff < 0 && mod_step_counter != 1) - // return false; + // return false; shuffle_shoot_ctrl = true; } last_shff = shff; - // shuffle_shoot_ctrl helps keep track if we have shoot or not a note for the step space of ppqn/4 pulses + // shuffle_shoot_ctrl helps keep track if we have shoot or not a note for the step space of output_ppqn/4 pulses if (mod_shuffle == 0 && shuffle_shoot_ctrl == true) { // keep track of next note shuffle for current note lenght control shuffle_length_ctrl = shuffle.step[(step_counter+1)%shuffle.size]; @@ -388,15 +449,14 @@ bool inline uClockClass::processShuffle() return false; } -// it is expected to be called in 24PPQN -void uClockClass::handleExternalClock() +void uClockClass::handleExternalClock() { - switch (state) { + switch (clock_state) { case PAUSED: break; case STARTING: - state = STARTED; + clock_state = STARTED; ext_clock_us = micros(); break; @@ -408,8 +468,8 @@ void uClockClass::handleExternalClock() // external clock tick me! ext_clock_tick++; - // accumulate interval incomming ticks data for getTempo() smooth reads on slave mode - if(++ext_interval_idx >= EXT_INTERVAL_BUFFER_SIZE) { + // accumulate interval incomming ticks data for getTempo() smooth reads on slave clock_mode + if(++ext_interval_idx >= ext_interval_buffer_size) { ext_interval_idx = 0; } ext_interval_buffer[ext_interval_idx] = last_interval; @@ -423,21 +483,21 @@ void uClockClass::handleExternalClock() } } -void uClockClass::handleTimerInt() +void uClockClass::handleTimerInt() { - // reset mod24 counter reference ? - if (mod24_counter == mod24_ref) - mod24_counter = 0; + // track main input clock counter + if (mod_clock_counter == mod_clock_ref) + mod_clock_counter = 0; // process sync signals first please... - if (mod24_counter == 0) { + if (mod_clock_counter == 0) { - if (mode == EXTERNAL_CLOCK) { + if (clock_mode == EXTERNAL_CLOCK) { // sync tick position with external tick clock if ((int_clock_tick < ext_clock_tick) || (int_clock_tick > (ext_clock_tick + 1))) { int_clock_tick = ext_clock_tick; - tick = int_clock_tick * mod24_ref; - mod24_counter = tick % mod24_ref; + tick = int_clock_tick * mod_clock_ref; + mod_clock_counter = tick % mod_clock_ref; mod_step_counter = tick % mod_step_ref; } @@ -454,47 +514,115 @@ void uClockClass::handleTimerInt() } // update internal clock timer frequency - float bpm = freqToBpm(counter); + float bpm = constrainBpm(freqToBpm(counter)); if (bpm != tempo) { - if (bpm >= MIN_BPM && bpm <= MAX_BPM) { - tempo = bpm; - setTimerTempo(bpm); - } + tempo = bpm; + setTimerTempo(bpm); } } - if (onSync24Callback) { - onSync24Callback(int_clock_tick); - } - // internal clock tick me! sync24 tick too + // internal clock tick me! ++int_clock_tick; } + ++mod_clock_counter; + + // ALL OUTPUT SYNC CALLBACKS + // Sync1 callback + if (onSync1Callback) { + if (mod_sync1_counter == mod_sync1_ref) + mod_sync1_counter = 0; + if (mod_sync1_counter == 0) { + onSync1Callback(sync1_tick); + ++sync1_tick; + } + ++mod_sync1_counter; + } + + // Sync2 callback + if (onSync2Callback) { + if (mod_sync2_counter == mod_sync2_ref) + mod_sync2_counter = 0; + if (mod_sync2_counter == 0) { + onSync2Callback(sync2_tick); + ++sync2_tick; + } + ++mod_sync2_counter; + } + + // Sync4 callback + if (onSync4Callback) { + if (mod_sync4_counter == mod_sync4_ref) + mod_sync4_counter = 0; + if (mod_sync4_counter == 0) { + onSync4Callback(sync4_tick); + ++sync4_tick; + } + ++mod_sync4_counter; + } - // PPQNCallback time! - if (onPPQNCallback) { - onPPQNCallback(tick); + // Sync8 callback + if (onSync8Callback) { + if (mod_sync8_counter == mod_sync8_ref) + mod_sync8_counter = 0; + if (mod_sync8_counter == 0) { + onSync8Callback(sync8_tick); + ++sync8_tick; + } + ++mod_sync8_counter; + } + + // Sync12 callback + if (onSync12Callback) { + if (mod_sync12_counter == mod_sync12_ref) + mod_sync12_counter = 0; + if (mod_sync12_counter == 0) { + onSync12Callback(sync12_tick); + ++sync12_tick; + } + ++mod_sync12_counter; + } + + // Sync24 callback + if (onSync24Callback) { + if (mod_sync24_counter == mod_sync24_ref) + mod_sync24_counter = 0; + if (mod_sync24_counter == 0) { + onSync24Callback(sync24_tick); + ++sync24_tick; + } + ++mod_sync24_counter; + } + + // Sync48 callback + if (onSync48Callback) { + if (mod_sync48_counter == mod_sync48_ref) + mod_sync48_counter = 0; + if (mod_sync48_counter == 0) { + onSync48Callback(sync48_tick); + ++sync48_tick; + } + ++mod_sync48_counter; + } + + // main PPQNCallback + if (onOutputPPQNCallback) { + onOutputPPQNCallback(tick); + ++tick; } - // reset step mod counter reference ? - if (mod_step_counter == mod_step_ref) - mod_step_counter = 0; - // step callback to support 16th old school style sequencers // with builtin shuffle for this callback only if (onStepCallback) { + if (mod_step_counter == mod_step_ref) + mod_step_counter = 0; // processShufle make use of mod_step_counter == 0 logic too - if (processShuffle()) { + if (processShuffle()) { onStepCallback(step_counter); // going forward to the next step call ++step_counter; } + ++mod_step_counter; } - - // tick me! - ++tick; - // increment mod counters - ++mod24_counter; - ++mod_step_counter; } // elapsed time support @@ -510,7 +638,7 @@ uint8_t uClockClass::getNumberOfMinutes(uint32_t time) { if ( time == 0 ) { return time; - } + } return (((_millis - time) / 1000) / SECS_PER_MIN) % SECS_PER_MIN; } @@ -518,7 +646,7 @@ uint8_t uClockClass::getNumberOfHours(uint32_t time) { if ( time == 0 ) { return time; - } + } return (((_millis - time) / 1000) % SECS_PER_DAY) / SECS_PER_HOUR; } @@ -526,7 +654,7 @@ uint8_t uClockClass::getNumberOfDays(uint32_t time) { if ( time == 0 ) { return time; - } + } return ((_millis - time) / 1000) / SECS_PER_DAY; } @@ -534,12 +662,12 @@ uint32_t uClockClass::getNowTimer() { return _millis; } - + uint32_t uClockClass::getPlayTime() { return start_timer; } - + } } // end namespace umodular::clock umodular::clock::uClockClass uClock; @@ -547,18 +675,14 @@ umodular::clock::uClockClass uClock; volatile uint32_t _millis = 0; // -// TIMER HANDLER -// -#if defined(ARDUINO_ARCH_AVR) -ISR(TIMER1_COMPA_vect) -#else -void uClockHandler() -#endif +// TIMER HANDLER +// +void uClockHandler() { // global timer counter _millis = millis(); - - if (uClock.state == uClock.STARTED) { + + if (uClock.clock_state == uClock.STARTED) { uClock.handleTimerInt(); } } diff --git a/src/uClock.h b/src/uClock.h index 5323035..69ead1a 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -2,7 +2,7 @@ * @file uClock.h * 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(RPI2040, Teensy, Seedstudio XIAO M0 and ESP32) - * @version 2.2.0 + * @version 2.2.1 * @author Romulo Silva * @date 10/06/2017 * @license MIT - (c) 2024 - Romulo Silva - contact@midilab.co @@ -23,7 +23,7 @@ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * 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 - * DEALINGS IN THE SOFTWARE. + * DEALINGS IN THE SOFTWARE. */ #ifndef __U_CLOCK_H__ @@ -34,17 +34,9 @@ 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: -(ppqn/4)-1 step, max: (ppqn/4)-1 steps +// Shuffle templates are specific for each PPQN output resolution +// min: -(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 #define MAX_SHUFFLE_TEMPLATE_SIZE 16 typedef struct { @@ -53,14 +45,8 @@ typedef struct { int8_t step[MAX_SHUFFLE_TEMPLATE_SIZE] = {0}; } 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 24 - #define MIN_BPM 1 -#define MAX_BPM 300 +#define MAX_BPM 400 #define PHASE_FACTOR 16 #define PLL_X 220 @@ -72,7 +58,7 @@ typedef struct { class uClockClass { public: - enum SyncMode { + enum ClockMode { INTERNAL_CLOCK = 0, EXTERNAL_CLOCK }; @@ -84,6 +70,11 @@ class uClockClass { }; enum PPQNResolution { + PPQN_1 = 1, + PPQN_2 = 2, + PPQN_4 = 4, + PPQN_8 = 8, + PPQN_12 = 12, PPQN_24 = 24, PPQN_48 = 48, PPQN_96 = 96, @@ -92,22 +83,47 @@ class uClockClass { PPQN_960 = 960 }; - ClockState state; - + ClockState clock_state; + uClockClass(); - void setOnPPQN(void (*callback)(uint32_t tick)) { - onPPQNCallback = callback; + void setOnOutputPPQN(void (*callback)(uint32_t tick)) { + onOutputPPQNCallback = callback; } void setOnStep(void (*callback)(uint32_t step)) { onStepCallback = callback; } - + + // multiple output clock signatures + void setOnSync1(void (*callback)(uint32_t tick)) { + onSync1Callback = callback; + } + + void setOnSync2(void (*callback)(uint32_t tick)) { + onSync2Callback = callback; + } + + void setOnSync4(void (*callback)(uint32_t tick)) { + onSync4Callback = callback; + } + + void setOnSync8(void (*callback)(uint32_t tick)) { + onSync8Callback = callback; + } + + void setOnSync12(void (*callback)(uint32_t tick)) { + onSync12Callback = callback; + } + void setOnSync24(void (*callback)(uint32_t tick)) { onSync24Callback = callback; } + void setOnSync48(void (*callback)(uint32_t tick)) { + onSync48Callback = callback; + } + void setOnClockStart(void (*callback)()) { onClockStartCallback = callback; } @@ -125,12 +141,13 @@ class uClockClass { } void init(); - void setPPQN(PPQNResolution resolution); + void setOutputPPQN(PPQNResolution resolution); + void setInputPPQN(PPQNResolution resolution); void handleTimerInt(); void handleExternalClock(); void resetCounters(); - + // external class control void start(); void stop(); @@ -143,9 +160,14 @@ class uClockClass { void run(); // external timming control - void setMode(SyncMode tempo_mode); - SyncMode getMode(); + void setClockMode(ClockMode tempo_mode); + ClockMode getClockMode(); 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 void setShuffle(bool active); @@ -155,10 +177,10 @@ class uClockClass { void setShuffleTemplate(int8_t * shuff, uint8_t size); // use this to know how many positive or negative ticks to add to current note length int8_t getShuffleLength(); - + // todo! void tap(); - + // elapsed time support uint8_t getNumberOfSeconds(uint32_t time); uint8_t getNumberOfMinutes(uint32_t time); @@ -171,28 +193,58 @@ class uClockClass { private: float inline freqToBpm(uint32_t freq); + float inline constrainBpm(float bpm); + void calculateReferencedata(); // shuffle bool inline processShuffle(); - void (*onPPQNCallback)(uint32_t tick); + void (*onOutputPPQNCallback)(uint32_t tick); void (*onStepCallback)(uint32_t step); + void (*onSync1Callback)(uint32_t tick); + void (*onSync2Callback)(uint32_t tick); + void (*onSync4Callback)(uint32_t tick); + void (*onSync8Callback)(uint32_t tick); + void (*onSync12Callback)(uint32_t tick); void (*onSync24Callback)(uint32_t tick); + void (*onSync48Callback)(uint32_t tick); void (*onClockStartCallback)(); void (*onClockStopCallback)(); void (*onClockContinueCallback)(); void (*onClockPauseCallback)(); - // internal clock control - // uint16_t ppqn; - PPQNResolution ppqn = PPQN_96; + // clock input/output control + PPQNResolution output_ppqn = PPQN_96; + PPQNResolution input_ppqn = PPQN_24; + // output and internal counters, ticks and references uint32_t tick; uint32_t int_clock_tick; - uint8_t mod24_counter; - uint8_t mod24_ref; + uint8_t mod_clock_counter; + uint16_t mod_clock_ref; uint8_t mod_step_counter; uint8_t mod_step_ref; - uint32_t step_counter; // should we go uint16_t? + uint32_t step_counter; + uint8_t mod_sync1_counter; + uint16_t mod_sync1_ref; + uint32_t sync1_tick; + uint8_t mod_sync2_counter; + uint16_t mod_sync2_ref; + uint32_t sync2_tick; + uint8_t mod_sync4_counter; + uint16_t mod_sync4_ref; + uint32_t sync4_tick; + uint8_t mod_sync8_counter; + uint16_t mod_sync8_ref; + uint32_t sync8_tick; + uint8_t mod_sync12_counter; + uint16_t mod_sync12_ref; + uint32_t sync12_tick; + uint8_t mod_sync24_counter; + uint16_t mod_sync24_ref; + uint32_t sync24_tick; + uint8_t mod_sync48_counter; + uint16_t mod_sync48_ref; + uint32_t sync48_tick; // external clock control volatile uint32_t ext_clock_us; @@ -203,9 +255,10 @@ class uClockClass { float tempo; uint32_t start_timer; - SyncMode 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; // shuffle implementation @@ -224,4 +277,3 @@ extern "C" { } #endif /* __U_CLOCK_H__ */ -