I’m making a music game and need to tightly sync music playback time with gameplay time so I can fire events off at the correct times in tick.
I’ve tried keeping track of time in engine (just adding delta times) and starting playback the first time my tick function is run, however the music tends to stutter a bit and get out of sync with gameplay time.
What I’d like to do is use the music playback time (the elapsed time of the actual music file) to drive all my events in tick to ensure it is in sync as possible.
Only problem is I can’t see any API call for this, except for in MediaPlayer in 4.5 (which I just tried and, in 4.5 preview, has very limited support for playing audio).
Any ideas on how I could achieve this? I’m a programmer so I don’t mind getting my hands dirty, although I’d prefer to keep things as simple as possible since this is my hobby project.
If the tracks are known you could implement a Matinee which is used for cut-scenes. A Matinee has tracks for various uses, such as audio, movements and more. You can add events onto a track at a specific moment and so synchronize the audio with your events. The good thing is that the audio and the events are using the same time and will be in-sync.
Hmm, interesting idea Moss. I was originally going to use Matinee for the works, but ended up writing my own data backend to hold all the events (because I wanted to create the data with my own level editing tool).
Do you think I could put the music in a Matinee, and use the Matinee’s time to accurately fire my events?
If the Matinee and music are in perfect sync I imagine it would work just as well as actually having the events in the matinee (which won’t work for me since mine are data-driven).
I think you can, this will give you the flexibility of your own system and all the engine features from the Matinee system.
The current time of the matinee is stored in the InterpPosition member. Each tick the Matinee updates it using: NewPosition = InterpPosition + (DeltaTime * PlayRate);
How can I get a reference to my MatineeActor in C++ so I can access its InterPosition member?
I was thinking I could just declare a Blueprintable property in my class, however AMatineeActor itself is coming up as undefined. Pulling up the source, it’s in Source\Runtime\Engine\Classes\Matinee\ and I think only headers in Source\Runtime\Engine\Public can be included in Engine.h?
Also AMatineeActor has “NotBlueprintable” in its UCLASS definition… does that mean I’ll need to spawn the Matinee Actor myself or something to get a pointer to it?
Sure would be nice if I could access Position from Blueprints (only Set Position is available).
Woo! Nevermind, I just included MatineActor.h with an absolute path (what’s the correct way to reference this?) and that worked. Setting it with Blueprints was fine, and I’m now getting the correct time from the Matinee in tick in C++, and then sending that into Blueprints with an event.