onlaj / Piano-LED-Visualizer

Piano LED Visualizer: Connect an LED strip to your Raspberry Pi and create an immersive visual experience for your piano playing
MIT License
494 stars 107 forks source link

Add accurate midi timestamps #477

Closed stephen322 closed 8 months ago

stephen322 commented 8 months ago

Add midi timestamps via Mido callback.

I originally wrote this in July/Aug, (and re-wrote now due to the many upstream changes). But never created a pull request because it's all for naught if PixelStrip's render holds the GIL for the duration. On my Pi/ledstrip setup, it's held for a good 11ms, which means any two notes could be recorded off by up to 22ms, enough to be noticeable.

This time I decided to patch rpi-ws281x to prevent GIL locking during render: https://github.com/rpi-ws281x/rpi-ws281x-python/pull/102 This should make the project much more thread-friendly regardless of the timestamps.

There's also discussion on Mido to use the backend rtmidi's delta timestamps: https://github.com/orgs/mido/discussions/485 But I'm not sure if those deltas would also be affected by PixelStrip's render lock.

The midi parsing in visualizer.py was also cleaned up a bit.

stephen322 commented 8 months ago

Updated to use time.perf_counter(): -Highest resolution time source -Monotonic -Typically lower loss of precision due to smaller number for float (although maybe perf_counter_ns() should be considered)

onlaj commented 8 months ago

I manually tested light modes, recording, playing, and sequences. Everything seems to be working fine. Thank you for your contribution, merging :)