# [sdiy] MIDI Clock sync advice

DIY DSP diydsp at yahoo.com
Sat Mar 9 00:20:06 CET 2024

```Unfortunately the code I wrote previously is for a company who would not be pleased if I shared it...   but I took a minute to ask ChatGPT and it came with something halfway reasonable.  I chatted with it a bit to refine.  I'll paste it below.  Note that it's working on sinusoids.  You'll probably adapt it to work with timestamps/deltas instead.  I tried to get chatgpt to do that, but it kept taking crazy shortcuts

#include <stdio.h>
#include <math.h>

// Constants
#define PI 3.14159265

// PLL parameters
double phase = 0.0;             // Current phase
double frequency = 4.0;         // Output frequency (four times input)
double phaseError = 0.0;        // Phase error
double phaseIncrement = 0.01;   // Phase increment

// Reference signal
double referenceFrequency = 2.0;   // Input frequency
double referencePhase = 0.0;

// Function to generate reference signal
double referenceSignal(double time) {
return sin(2 * PI * referenceFrequency * time + referencePhase);
}

// Function to update phase and frequency using PLL
void updatePLL(double time) {
double referenceValue = referenceSignal(time);
double inputSignal = sin(2 * PI * frequency * time + phase);

// Compute phase error using difference of squares
phaseError = referenceValue * inputSignal;

// Update phase using phase error
phase += phaseIncrement * phaseError;

// Update output frequency to be four times the input frequency
frequency = 4 * referenceFrequency;
}

int main() {
double time = 0.0;
int i;

// Simulate for 100 time steps
for (i = 0; i < 100; i++) {
// Update PLL
updatePLL(time);

// Increment time
time += 0.1;  // Time step

// Print phase and frequency
printf("Time: %.2f, Phase: %.2f, Frequency: %.2f\n", time, phase, frequency);
}

return 0;
}

Noah Vawter's futuristic electronic music instrument lab: youtube.com/diydsp

On Friday, March 8, 2024 at 05:36:19 PM EST, Tom Wiltshire <tom at electricdruid.net> wrote:

Thanks. Do you have any resources to point to that I could look up about software PLLs? All the stuff I've seen seems to assume it's a hardware solution. While some of that is clearly transferable, something that had software in mind would help a lot.
Tom
==================
Electric Druid
Synth & Stompbox DIY
==================

On 8 Mar 2024, at 16:52, DIY DSP <diydsp at yahoo.com> wrote:

I've done this the "easy" way with a software PLL, and the hard way with a Kalman filter.  Technically what you need depends on the noise/error profile of the source clock.But start with the software PLL.. On every 4th input clock, use the error to update the phase and period of the output clock.For this application, since the signals will converge very quickly relative to how often they chance, I recommend using only the P term from the PID to compute the error and reset the phase of the output clock on the spot to the input clock.

Noah Vawter's futuristic electronic music instrument lab: youtube.com/diydsp

On Friday, March 8, 2024 at 10:20:30 AM EST, Tom Wiltshire <tom at electricdruid.net> wrote:

Hi All,

Has anyone got any experience dealing with writing software to sync to MIDI clock that they can share?

I'm working on a drum sequencer which will run at 96PPQN, and it'd be nice if it could sync to incoming 24PPQN MIDI Clock messages.

I can see a couple of ways to do this:

1) Some sort of PID controller, where we compare the internal timing and the incoming clock timing and derive some error signals.
2) IIR filtering. We measure the time between incoming clocks and then use an IIR filter to provide some averaging and smoothing. We then set the internal clock based on the filter's output.

(2) seems like the simpler approach. Clearly it will introduce some lag when changing tempo, but I'm not sure I see this as a fault - smooth tempo changes could be a feature. And depending on how much filtering is required, that lag might actually be quite short. What's a reasonable time constant for such a thing?

How has this been approached in the past? I know that I'm not the first person to do this, so I'm just trying to avoid re-inventing the stone-age MIDI wheel!

Many thanks for any ideas/pointers offered,
Tom

==================
Electric Druid
Synth & Stompbox DIY
==================

________________________________________________________
This is the Synth-diy mailing list
Submit email to: Synth-diy at synth-diy.org
View archive at: https://synth-diy.org/pipermail/synth-diy/