lenmus / lomse

A C++ library for rendering, editing and playing back music scores.
MIT License
117 stars 28 forks source link

Lag in midi event generation over time #164

Closed sensibol closed 5 years ago

sensibol commented 6 years ago

Hi, I recorded the midi sound produced using midi events generated by lomse. Comparing it with Musescore exported audio and expert rendered audio I have the below observations. 1) There are 3-4 beats added by lomse at the beginning of score after which the playback starts. This is producing a delay of around 2-3 seconds. 2) As time progresses, the audio starts to lag. Note events are generated with a lag. Differences were seen in the note onset location for the audios. While musescore and expert onsets were nearly coinciding, lomse onsets had lag.

I am not sure if this is an issue with my implementation. Please advice.

Thanks.

cecilios commented 6 years ago

There are 3-4 beats added by lomse at the beginning of score after which the playback starts. This is producing a delay of around 2-3 seconds.

Probably you have enabled, in lomse score player, the option "count off before start playback" to generate one empty measure at start with only metronome clicks (if metronome enabled) or no sound (if metronome disabled).

As time progresses, the audio starts to lag ...

Any tempo difference between the players will make one recording to delay in reference to the other one. I do not know how MuseScore and the other app. (expert rendered audio) manage tempo, so I can not tell you if there are differences with Lomse. In any case, this tempo difference might be caused by different factors, the most plausible:

In any case I do not think that this might be caused by your application. Lomse uses a dedicated thread to generate midi events and any delay in your application for processing the midi events and generate the sound will only cause a non-cumulative delay in next midi event but not a tempo shift.

If you are trying to synchronize lomse midi player tempo with another audio player, it is going to be a nearly impossible task, as I told you in answer to issue #145. The only solution for ensuring synchronization between lomse highlight and sound produced by an external audio player would be some solution such as the one requested in issue #163

By the way, you mention MuseScore and "expert rendered audio". What is the name of this "expert rendered audio" application? Can you provide me a link to their website? Thank you.

sensibol commented 6 years ago

There are 3-4 beats added by lomse at the beginning of score after which the playback starts. This is producing a delay of around 2-3 seconds.

Probably you have enabled, in lomse score player, the option "count off before start playback" to generate one empty measure at start with only metronome clicks (if metronome enabled) or no sound (if metronome disabled).

Yes had enabled "count off option". Disabled that but still noticed delay. Modified the below lines of code. Some compensation was being done for the empty measure added for count off even when count off was disabled. Now the sync is better.

//determine last metronome pulse before first note to play.
    //First note could be syncopated or an off-beat note. Round time to nearest
    //lower pulse time
    nMtrEvDeltaTime = ((events[i]->DeltaTime / m_nMtrPulseDuration) - 1 ) * m_nMtrPulseDuration;
with
   nMtrEvDeltaTime = ((events[i]->DeltaTime / m_nMtrPulseDuration)  ) * m_nMtrPulseDuration;

The above issue was specific to our application since we are synchronizing multiple audios.

Any tempo difference between the players will make one recording to delay in reference to the other one. I do not know how MuseScore and the other app. (expert rendered audio) manage tempo, so I can not tell you if there are differences with Lomse. In any case, this tempo difference might be caused by different factors, the most plausible: If using different computers, by the precision of the computer clock If it happend on the same computer it could be caused by rounding errors on the beat duration, or by the time precision used by each app. In any case I do not think that this might be caused by your application. Lomse uses a dedicated thread to generate midi events and any delay in your application for processing the midi events and generate the sound will only cause a non-cumulative delay in next midi event but not a tempo shift. If you are trying to synchronize lomse midi player tempo with another audio player, it is going to be a nearly impossible task, as I told you in answer to issue #145. The only solution for ensuring synchronization between lomse highlight and sound produced by an external audio player would be some solution such as the one requested in issue #163

Still think I need to revisit my application code. I think somehow the sound thread is getting blocked. Anyway will keep you posted in case it is issue with lomse.

By the way, you mention MuseScore and "expert rendered audio". What is the name of this "expert rendered audio" application? Can you provide me a link to their website? Thank you.

By expert I meant a trained musician. A musician was asked to play the music on a keyboard which was recorded. Sorry for the confusion.

Really glad that the issue got resolved. Thanks for lomse!

cecilios commented 5 years ago

I am closing this issue as it appears to be resolved.