[sdiy] MIDI Clock sync advice

Tom Wiltshire tom at electricdruid.net
Sat Mar 9 00:29:08 CET 2024


Yes, that's a very useful sanity check because that's pretty much what I'd been thinking about too.

As you say, you have to have one timer peripheral generating the "pulses" in the "Pulses per quarter note". These are then counted up to create swing/shuffle/output the current beat/whatever. Changing the timing of this timer alters the tempo. This timer variable is the ultimate destination required for our MIDI sync.

At the same time, you need something that is keeping track of how long has passed since the last MIDI Clock pulse. That's going to be *another* timer peripheral, prescaled to to give you a good number of counts for likely tempos. There's no point counting 64MHz clock pulses with a 16-bit timer if it's going to overflow five times before you even get your next pulse. Equally, you don't want to lose resolution by finishing up with counts of only a few dozen for typical tempos. If realistic tempos are in the range 30BPM to 300BPM (a decade) then we'd ideally like those tempos to give timer results between (say) 60,000 and 6000 for a 16-bit timer. Reality will intrude here and we'll take whatever we can get that's closest, but that's the general idea.

My proposed next step would be to then add some filtering to those timing results, since I suspect because of the vagaries of MIDI they won't be that tight. Maybe you get 7819, 7847, 7799, etc - maybe it's worse than that? So feed the results into a simple IIR (probably based on shifts rather than anything more complicated) and then use the result.

The output from the filter is a time-period measured in master-clock-ticks-divided-by-x (we don't know what x needs to be yet because I haven't worked it out). That time period might then need to be converted into a count value for the Tempo timer. If they're both running with the same clock and prescale value, that's trivial, but there might be some little adjustment to make.

Tom

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



> On 8 Mar 2024, at 23:06, brianw <brianw at audiobanshee.com> wrote:
> 
> Here's a quick thought:
> 
> You might use two timer peripherals on the PIC18F. Both would be fed from the same master clock source, but each might have different pre-dividers and periods.
> 
> One would implement the sequencer clock, and would not necessarily be anchored to anything but the incoming MIDI clock. As tempo changes, this timer would vary in speed.
> 
> The other timer would run 8 to 16 times higher resolution, perhaps more, and this would be a local time reference. This second timer would be how you measure the time between incoming MIDI messages, and thus it would measure the incoming clock rate. This timer would never change because it's the reference for measuring other things. Like a real-time wall clock, but hopefully more accurate. The period has to be fast enough to detect slight changes in the incoming MIDI clock.
> 
> Brian
> 
> 
> On Mar 8, 2024, at 2:59 PM, brian wrote:
>> PLLs are still a bit of a mystery to me, including software and hardware PLL and DPLL.
>> 
>> That said, any successful implementation would probably have some sort of hardware timing reference that gets tweaked from software. Basically, the solution will not be pure software, and thus the solution will depend upon the timing hardware that you have. You mentioned the PIC18F in another message, and that chip has several timers. I'm fairly certain that your sequencer timing would be based on one (or more?) hardware timer peripherals, and then the "PLL" would tweak the period of that timer.
>> 
>> I'll have more to say in reply to your message about the PIC18F platform...
>> 
>> Brian
>> 
>> 
>> On Mar 8, 2024, at 2:36 PM, Tom Wiltshire 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
>>> 
>>> On 8 Mar 2024, at 16:52, DIY DSP 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 <http://youtube.com/diydsp>
>>>> 
>>>> On Friday, March 8, 2024 at 10:20:30 AM EST, 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
> 
> 
> ________________________________________________________
> This is the Synth-diy mailing list
> Submit email to: Synth-diy at synth-diy.org <mailto:Synth-diy at synth-diy.org>
> View archive at: https://synth-diy.org/pipermail/synth-diy/ <https://synth-diy.org/pipermail/synth-diy/>
> Check your settings at: https://synth-diy.org/mailman/listinfo/synth-diy <https://synth-diy.org/mailman/listinfo/synth-diy>
> Selling or trading? Use marketplace at synth-diy.org <mailto:marketplace at synth-diy.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20240308/c3722f72/attachment.htm>


More information about the Synth-diy mailing list