pedrolcl / dmidiplayer

Drumstick MIDI File Player Multiplatform (mirror)
https://dmidiplayer.sourceforge.io
GNU General Public License v3.0
22 stars 6 forks source link

High density of MIDI events slows down playback tempo #20

Closed mrbumpy409 closed 6 days ago

mrbumpy409 commented 1 month ago

Where there are a lot of MIDI events (for example, rapid MIDI CC modulation), dmidiplayer slows the tempo down, sometimes considerably so. This happens regardless if sending MIDI to an external player or using the internal FluidSynth. When this happens, the player's CPU usage remains quite low, so it doesn't seem to be a CPU limitation.

I have attached dmidiplayer event density test.zip for reproducing the issue. It contains the following files:

The SoundFont used in the recordings is the release candidate for GeneralUser GS v2.0.0, but any GM/GS SoundFont should suffice for testing this bug. Also, this bug happens in both Linux and Windows on my system, which is detailed below.

My System

pedrolcl commented 1 month ago

Hi Christian! Thanks for the report and the sample song. I can confirm the bug of tempo slowed down, and that it does not depend on the CPU. It seems some miscalculation. But I think that the rhythm lights are also affected. To me, the "leds" tick well synchronized with the music: from bar 10 the snare drum sounds exactly on the second and fourth beats synced to the corresponding "leds".

mrbumpy409 commented 1 month ago

To me, the "leds" tick well synchronized with the music: from bar 10 the snare drum sounds exactly on the second and fourth beats synced to the corresponding "leds".

Interesting. I'm seeing it completely desynchronized here using the Flatpak version in Linux. Either way, I'm glad you were able to repro the issue. dmidiplayer is everything I want in a simple MIDI player, so thank you for creating it!

pedrolcl commented 1 week ago

Sorry for not updating the ticket in a long time, but my available time was scarce, and also this issue was difficult to diagnose. Here is a little report just to inform you about my findings and tentative solution.

Your MIDI file has a fixed tempo of 138 BPM and a length of 19 bars which allows to easily calculate a theoretical duration of 31.3 seconds. Your OGG rendering using dmidiplayer is 33 seconds. In my tests with v1.7.3 rendering your clean MIDI file (removing CC7 events), the length is 31.5 seconds, but the full one is 34.8 seconds, which is noticeably slow.

The problem is an accumulation of inaccuracies in time calculations, which were made in milliseconds (1 ms = 10-3 seconds).

If you open the MIDI file with kmidimon, you may notice that the tempo event (located in the first track) says that the tempo is 434783 microseconds per beat, which is how the standard defines 138 BPM. One microsecond is a millionth of a second (10-6 seconds). The fix, applied to the 'devel' branch, calculates times in microseconds. With this precision the inaccuracies are now much smaller, and the renderings of your MIDI file are: clean=31.4 seconds, full=31.9 seconds.

There are still some pending optimizations that may improve the results in the next release, along with several other fixes and improvements. Thanks for your patience!

mrbumpy409 commented 1 week ago

I am so glad you were able to find the cause! I look forward to testing the new build. :smile:

pedrolcl commented 1 week ago

I am so glad you were able to find the cause! I look forward to testing the new build. 😄

Here is a pre-release: (removed)

mrbumpy409 commented 1 week ago

I've only tested one MIDI file so far and found that new build is playing way too slowly with lots of tempo fluctuations. Here is a zip file containing the following:

You can hear the correct tempo in FluidSynth_J-cycle.ogg vs. how much slower dmidiplayer is playing it.

pedrolcl commented 1 week ago

I've only tested one MIDI file so far and found that new build is playing way too slowly with lots of tempo fluctuations.

J-cycle.mid has only three tempo changes. It has several time signature changes, though, but they do not influence the playback, because it is only a notation meta-event:

image

The OGG rendering is has a duration of 1m 40 seconds, which is the same as this one. I've just rendered it with dmidiplayer on Linux, without issues. Here is the movie:

jcycle-2024-08-29_10.39.00.zip

pedrolcl commented 1 week ago

Testing the same MIDI file in Windows, the timing is awful. I've increased the player thread priority (which only affects Windows) in commit 1289d6b. Tell me if it improves something:

Windows: dmidiplayer-1.7.4pre3-win-x64-setup.exe Linux: dmidiplayer-1.7.4pre-x86_64.AppImage

This pre-release builds include drumstick-2.9.1 released yesterday.

mrbumpy409 commented 1 week ago

Both of those application links give me an error from SourceForge, such as: The "/v1.7.4/dmidiplayer-..e2-win-x64-setup.exe" file could not be found or is not available. Please select another file.

pedrolcl commented 1 week ago

Both of those application links give me an error from SourceForge, such as: The "/v1.7.4/dmidiplayer-..e2-win-x64-setup.exe" file could not be found or is not available. Please select another file.

Sourceforge offers a link "Problems downloading?" so you can choose a different mirror. The files take some time to propagate to all mirrors.

mrbumpy409 commented 1 week ago

Alright, I tested the latest preview builds. The Windows build still plays back with a slow and fluctuating tempo, but the Linux build seems to play perfectly in time. How odd. :thinking:

pedrolcl commented 1 week ago

Alright, I tested the latest preview builds. The Windows build still plays back with a slow and fluctuating tempo, but the Linux build seems to play perfectly in time. How odd. 🤔

What we need here is many tests, to discover if some other songs are affected by the same symptom. I can't exclude some other calculation mistake, but in that case: why the results are different in Windows and Linux? Other sample MIDI files have multiple tempo changes, like Schubert_Standchen.mid that render exactly the same on Linux and Windows.

mrbumpy409 commented 1 week ago

I'll see if I can make some useful test files over the next few days. Thank you for all the work you have been putting into this player! Hopefully I can be more of a help than a nuisance. :stuck_out_tongue:

pedrolcl commented 1 week ago

What happens if you replace the DLLs in this ZIP file into C:\Program Files\dmidiplayer\ ? cxxruntime.zip (spoiler: it fixes the timing problems for me)

pedrolcl commented 1 week ago

New Windows package: dmidiplayer-1.7.4pre3-win-x64-setup.exe

mrbumpy409 commented 6 days ago

I tested a bunch of MIDI files with the latest Windows build, and the tempo stability seems pretty solid to me. I would say the issue indeed appears to be solved! Also, everything is sounding fantastic with the new reverb & chorus settings. :smile: