Geromatic / Midi-Unreal

Midi for Unreal Engine
130 stars 31 forks source link

MidiProcessor::process() is losing time #4

Closed stani closed 7 years ago

stani commented 8 years ago

I believe it's because how mMsElapsed is calculated. I replaced it by mMsElapsed = FPlatformTime::ToMilliseconds(now - mStartMs); and it works ok. (mStartMs is set in start() as mStartMs = FPlatformTime::Cycles();)

Also, I think it's better to use actor time or game time, instead of platform time. This way the time can stop when game is paused or time dilation can be applied.

Geromatic commented 8 years ago

I will check if time is big enough lose for accuracy or affects the sync of the midi file.

stani commented 8 years ago

I played a file of 230 sec. and measured time between onStart and onStop. It plays about 240 seconds -> 10 sec longer. About 2 sec lost per minute. For something like a rhythm game it's pretty huge.

stani commented 8 years ago

I figured out that my way of calculating elapsed ticks is wrong if Tempo changes more that once. So, instead I introduced 2 variables: lastTempoTime and lastTempoTick, which are initially set to zero then updated every time the Tempo is changed (set to time and tick of the Tempo event). With that elapsed ticks are calculated this way:

mTicksElapsed = lastTempoTick+ msToTicks(elapsedTime - lastTempoTime, mMPQN, mPPQ);

It seems to work well.

Geromatic commented 7 years ago

I have found a workaround that doesn't require much change. I had to apply a 4% increase in PPQ to get a more accurate playback. I tested this on a 16 minute song and the result was 1 second off the original time [using Media player to check the midi time]. Other songs I tried get like a total of 1 second off the original total time.

double MidiUtil::msToTicks(long ms, int mpqn, int ppq) { return ((ms * 1000.0) * (ppq * 1.04)) / mpqn; }

so I don't think its the tempo change but rather slower tick playback

Is is possible to send me the changes on git or through email so I can check vs my temp solution

stani commented 7 years ago

Ok, I added my changes. There is also couple of other things, like GetMidiNotes (access to notes for BP) and ProcessTo (like Process but time is controlled externally).

Geromatic commented 7 years ago

found issues with your workaround...found 1 midi that jumps forward while playing, another does not end properly.

http://www.midishrine.com/oter/ffmix1.mid [skips]

Geromatic commented 7 years ago

I introduced a variable that lets the user decide the speed playback

Geromatic commented 7 years ago

I have fixed up the timing issue. I have removed the long to double conversion