[sdiy] MIDI Clock sync advice
brianw
brianw at audiobanshee.com
Fri Mar 8 22:33:01 CET 2024
Hi Tom,
You may have already designed with the following in mind, but I highly recommend taking advantage of the design of MIDI to handle clock messages in an efficient manner.
You will note that all of the System Real Time messages in MIDI are single-byte. This allows you to handle timing inside the interrupt for the serial input, without even involving the usual queue of MIDI data. Basically, your interrupt handler should extract all of the System Real Time messages out of the stream and handle them immediately, tightly integrated with your local time reference. All the remaining MIDI messages (that aren't System Real Time) can then go into the queue for non-interrupt processing.
This will require that you write your firmware as "bare metal" or at least using a Real Time Operating System (RTOS). Admittedly, I'm not familiar with the support for MIDI in higher level operating systems with serial drivers, so perhaps there are correct solutions out there.
In any case, having a good design for interrupt handling, and writing the code so that System Real Time messages are processed instantly, will ensure that your derived clock will be as accurate and stable as possible. Any slack here will result in more slop on your resulting clock.
Brian WIlloughby
p.s. If your device also sends MIDI, then it will help to similarly design your MIDI output code to prioritize MIDI sync output without any latency due to queuing. You may end up needing a queue, but keep the sync queue separate from the non-real-time queue. But, being a drum sequencer, maybe you don't have MIDI output?
On Mar 8, 2024, at 7:10 AM, Tom Wiltshire 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
> ==================
More information about the Synth-diy
mailing list