Framerate independent timer class

Hi Guys,

I’m looking for a way to either construct or incorporate a timer class into a project that is not driven by the overall ‘Tick’ function and can thus run independently and will not be affected by framerate fluctuation.

I know there are some pretty robust timer classes in other C++ APIs so would it be possible to extend the functionality of the U4 API and incorporate one of these? I’m thinking JUCE or something similar.

The timer will form the basis for a generative music system I am constructing where notes will need to be triggered with up to 1ms accuracy and kept in constant time regardless of framerate.

I’m fairly new to this so not sure if this is something which can be done or maybe I’m overcomplicating the issue but any help would be greatly appreciated!

Many Thanks,

I’m not giving the answer to “exactly” what you asked for, but maybe this can help:

If you check the API help by UWorld::Tick you’ll notice this:

// Audio always plays at real-time regardless of time dilation, but only when NOT paused
if( !bIsPaused )
{
AudioTimeSeconds += DeltaSeconds;
}

Also UWorld has a method to get this property…

Returns time in seconds since world was brought up for play, IS stopped when game pauses, NOT dilated/clamped.

Source

float UWorld::GetAudioTimeSeconds() const
{
return AudioTimeSeconds;
}

And we have a global variable called GWorld that we can access anywhere…

Maybe you have already all you’ll need to write your timer without mess with external libraries… :smiley:

Did you tried UE4 timers?

You can access via GetWorldTimerManager()

But if you really want to use external liberies… UE4 does not stop you to do it :slight_smile: heres a guide how to do it:

I’m pretty sure these are just added to a list and called from FTimerManager::Tick, which is framerate dependant.

Can you show me where you see that?

Tick is the way to get frame independent time. Yes one tick = single frame render time.
But you can accumulate that time to get time in seconds. I dunno what else you might need.

If you look at /Runtime/Engine/Private/TimerManager.cpp you can follow what SetTimer does, it creates a new TimerData instance and adds it to the list. Then every Tick, it will check whether the timer at the top of the list has expired and call the delegate if it did.

interesting subject, subscribing to this one
for those of you wondering why Tick is not an option for audio check this vid
http://www.youtube.com/watch?v=Iew7bvu2wQo
it was done in ue3 so im really hoping ue4 (specially with source) has a way round this problem

What you want is to run code on a separate thread. However, there’s a whole lot of stuff that cannot be done directly from other threads (I don’t think you can multithread blueprint code, for example).

Thanks for your responses guys, some really interesting comments!

From what I can see, Zeblote is correct about the SetTimer function, it does seem to be linked to the framerate.

tegleg’s videos is a nice demonstration of why a Tick-based timer isn’t really an option when sequencing audio at run-time. As the framerate drops (around 0:35) the drumbeat glides slightly out of time making everything sound slightly wonky. I think for an application such as this a timer which is locked and accurate to the millisecond is needed.

Thanks for the info on linking external libraries , that’s really helpful. I think I’ll try and incorporate the timer class from the JUCE API and see how that works out.

Will post back when I’ve got something together!

Cheers!

Cheers pedro, this is interesting, will have to do a bit of research into multi-threading.

Streamed sound assets using vorbis are decoded in a separate thread, so if your intention is to generate audio it should at least be possible to write to an audio buffer that way.

Hey guys,

So after trying for a quite a while to link the JUCE API with UE4 I’ve decided to abandon using a third party timing system for the moment. I attempted to link the PortAudio library in an attempt to access sample-accurate data directly from the sound card but this somewhat pickled my brain too! I think I managed to link it ok but found that UE4 uses XAudio2 which isn’t supported by PA. So I think that was a dead end.

Has anybody had any luck linking third-party libraries yet? I read somewhere that there might be some problems at the moment with this system.

I looked into multi-threading but it seems other threads are only really appropriate for performing calculations and not running a timer or creating/editing UOBJECTS. This would mean I would only be able to access the thread with a tick-based timer which kind of defeats the object…

At the moment I’m using a timer based on the GetAccurateRealTime node which I’m dividing by 4 and trying to use the modulus of that number (when = = 0) to trigger audio events. I’ve sort of got this running in BP but would ideally like to get working with code.

Time to get my head further into the UE4 API methinks…

Will continue to post updates as I find them for anyone who’s interested!

Sorry to resurrect this thread but if any one is interested ROLI have made a UE4 JUCE bridge that can be found here: /WeAreROLI/JUCEUnrealBridge

They made it for 4.14 but it’s relatively painless getting it to compile for 4.19…

They have included a UE4 metronome which in theory should provide near sample frame accurate timing which fires a multicast delegate and it is working perfectly for me when using it to trigger a step sequencer.