I am pretty new, and still trying to figure out how to work with external MIDI devices.
I have a KORG keyboard, which sends MIDI clock messages (but looks as empty messages in Sonic Pi). It works pretty well with other devices or software, but I can’t get it to work properly with Sonic Pi. By properly I mean to say, that I want this device to be the master Clock source.
Can anyone give me some clue as to how to properly sync w/an external MIDI clock?
This is my current unreliable solution, which is clearly not perfectly synced
define :midiClock do |port|
  in_thread do
    lap = nil
    t1 = nil
    ppqn = 24
    t = nil
    
    loop do
      sync port
      t = tick(:clock)
      isQuarterNote = (t%ppqn)==0
      
      if t1 == nil
        t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      elsif t1 != nil && lap == nil && isQuarterNote
        t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
        / elapsed time in milliseconds /
        lap = (t2 - t1) * 1000
        t1 = nil
      end
      
      cue :beat
      
      if lap != nil
        if lap > 0
          pps = 1000/lap
          ppm = pps*60
          puts lap, pps, ppm
          set :midibeat, [lap, ppm]
        end
        
        lap = nil
      end
    end
  end
end
midiClock '/midi/microstation_midi_1/2/clock'
The logs show tempo is out of sync most of the times (last number is ppm), considering my external MIDI clock is fixed to 100.00
{run: 6, time: 300.0842}
 └─ 575.7103539999662 1.736984567069396 104.21907402416376
 
{run: 6, time: 300.6842}
 └─ 576.0453549992235 1.7359744181972407 104.15846509183444
 
{run: 6, time: 301.2843}
 └─ 576.4124100005574 1.7348689630034735 104.0921377802084
 
{run: 6, time: 301.8843}
 └─ 575.8693120005773 1.7365051048231537 104.19030628938923
 
{run: 6, time: 302.4843}
 └─ 576.0355580005125 1.7360039430050431 104.16023658030258
 
{run: 6, time: 303.0844}
 └─ 576.1048839995055 1.7357950397116548 104.1477023826993
 
{run: 6, time: 303.6844}
 └─ 576.0853020001377 1.735854041976167 104.15124251857002
 
{run: 6, time: 304.2844}
 └─ 575.6511149993457 1.7371633163624405 104.22979898174643
 
{run: 6, time: 304.8844}
 └─ 575.8668889993714 1.7365124112928318 104.19074467756991
 
{run: 6, time: 305.4844}
 └─ 576.2161530001322 1.735459852684433 104.12759116106598
 
{run: 6, time: 306.0845}
 └─ 576.0676230001991 1.735907313783636 104.15443882701817
 
{run: 6, time: 306.6845}
 └─ 575.8704109994142 1.7365017908534588 104.19010745120752
 
{run: 6, time: 307.2846}
 └─ 576.0708109992265 1.7358977072027741 104.15386243216645
 
{run: 6, time: 307.8846}
 └─ 576.0947459984891 1.7358255858883023 104.14953515329813
 
{run: 6, time: 308.4846}
 └─ 575.8625850012322 1.7365253899901487 104.19152339940892
 
{run: 6, time: 309.0846}
 └─ 576.0855089993129 1.7358534182487009 104.15120509492205
 
{run: 6, time: 309.6846}
 └─ 576.1413180007366 1.7356852715755433 104.1411162945326
 
{run: 6, time: 310.2846}
 └─ 576.0268280009768 1.7360302530879763 104.16181518527858
Thanks!
