Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 3, 2026

  • Analyze DelayLockedLoop algorithm and MidiClock implementation
  • Improve DLL algorithm for faster BPM convergence with better stability
  • Optimize MidiClock parameters for more responsive BPM updates
  • Add unit tests for the improved algorithm
  • Run code review and security checks

Summary of Changes

DelayLockedLoop (src/delaylockedloop.hpp)

  • Implemented adaptive bandwidth approach: starts with high bandwidth (0.5) for fast initial convergence, then transitions to lower bandwidth (0.1) for stable tracking after ~1 beat (24 ticks)
  • Changed M_PI and M_SQRT2 from float to double for better precision
  • Added isLocked() and getPeriod() helper methods
  • Simplified the reset interface to work directly with period (in seconds) rather than period in samples

MidiClock (src/engine/midiclock.hpp, src/engine/midiclock.cpp)

  • Reduced syncPeriodTicks from 48 to 24 (signal acquired after 1 beat instead of 2)
  • Reduced BPM update interval from 1.0 seconds to 0.25 seconds (4x faster updates)
  • Added hysteresis with bpmChangeThreshold = 0.5 BPM to prevent jittery updates when tempo is stable
  • Added lastReportedBpm tracking to implement hysteresis

Unit Tests (test/engine/MidiClockTest.cpp)

  • Added tests for DelayLockedLoop: construction, reset, convergence, and locking behavior
  • Added tests for MidiClockMaster: construction and tempo changes
  • Registered new test suite in test/CMakeLists.txt

These changes should make MIDI clock sync respond immediately when receiving external clock, while maintaining stability once locked.

Original prompt

This section details on the original issue you should resolve

<issue_title>MIDI Clock SYNC Slow to Update BPM</issue_title>
<issue_description>### What happened?

Hello.
I am sending midi clock from Stage Traxx 3 to Element.
It is working but the tempo in Element doesn't update immediately, it takes time to reach the received value and often shift a bit.
All is set up correctly in Stage traxx 3 and I have try the same setup with LivProfessor as a receiver for the clock with no issue.

Version

0.46.x (Previous)

Relevant log output

```</issue_description>

<agent_instructions>see `src/delaylockedloop.hpp` <-- this algorithm probably needs improved</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@mfisher31</author><body>
It's the algorithm I'm using based on Ardour from years ago.  Admittedly it needs an update.  This is a known issue however.  Thanks for the official report.</body></comment_new>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@mfisher31
Copy link
Member

@copilot - try that again, but don't try to build the project and push to git. Not sure what you're doing there buddy, but there are mega errors. Just look at the code, make changes if needed, and a I'll pull and check locally.

Key changes:
- DelayLockedLoop: Added adaptive bandwidth (starts high for fast
  convergence, transitions to lower bandwidth for stability)
- MidiClock: Reduced sync period from 48 to 24 ticks (~1 beat)
- MidiClock: Reduced BPM update interval from 1.0s to 0.25s
- MidiClock: Added hysteresis (0.5 BPM threshold) to prevent jitter
- Changed M_PI/M_SQRT2 from float to double for precision

Co-authored-by: mfisher31 <[email protected]>
@CLAassistant
Copy link

CLAassistant commented Jan 3, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ mfisher31
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor Author

Copilot AI commented Jan 3, 2026

@copilot - try that again, but don't try to build the project and push to git. Not sure what you're doing there buddy, but there are mega errors. Just look at the code, make changes if needed, and a...

Made the code changes in commit 7411ba1. I've improved the DelayLockedLoop algorithm with adaptive bandwidth (fast initial convergence, stable tracking once locked) and updated MidiClock to update BPM every 0.25s instead of 1.0s. Ready for you to pull and test locally.

@mfisher31
Copy link
Member

Hey @copilot go ahead and write a unit test for this as well. make sure to check the CMakeLists.txt and other tests to see exactly how to do it. Make no assumptions. Don't do guess work. Look at the existing build system to see what to do. Also remember KISS. Don't be a newbie on this one, got it?

Tests cover:
- DelayLockedLoop construction, reset, convergence, and locking
- MidiClockMaster construction and tempo changes

Co-authored-by: mfisher31 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 3, 2026

Hey @copilot go ahead and write a unit test for this as well. make sure to check the CMakeLists.txt and other tests to see exactly how to do it. Make no assumptions. Don't do guess work. Look at the...

Added unit tests in commit c653f81. Created test/engine/MidiClockTest.cpp following the existing test patterns (Boost.Test with class-based test organization). Tests cover DelayLockedLoop construction/reset/convergence/locking and MidiClockMaster construction/tempo changes. Also registered the new test suite in test/CMakeLists.txt.

- Add testTempoChangeConvergence: tests DLL convergence speed (120→140 BPM)
- Add testLargeTempoSwing: tests extreme tempo change (20→120 BPM, 6x increase)
- Add testRapidTempoChanges: tests stability with large tempo shifts
- Add testTempoChangeAccuracy: tests accuracy across 60-200 BPM range
- Add testJitterRejection: tests filtering of timing jitter
- Add testExtremeTempos: tests behavior at 40 and 240 BPM
- Add MidiClockMaster tests for clock generation and tempo change response

Verifies implementation meets pro audio standards (lock within 1-4 beats)
@mfisher31 mfisher31 changed the title [WIP] Fix MIDI clock sync to update BPM immediately [WIP] Fix MIDI clock sync to update BPM Faster Jan 3, 2026
- Format the code
- fix a datapath testing problem with archlinux in docker
- Refactor code for consistency and clarity in various files
@mfisher31 mfisher31 marked this pull request as ready for review January 3, 2026 14:42
@mfisher31 mfisher31 merged commit 64a0dd7 into main Jan 3, 2026
3 of 4 checks passed
@mfisher31 mfisher31 deleted the copilot/fix-midi-sync-bpm-update branch January 3, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MIDI Clock SYNC Slow to Update BPM

3 participants