Sign in to follow this  
darkwolf79

Synch Game Events with Music

Recommended Posts

Hey all, well, I am just tossing ideas around for a small one day project coming up, soon. (We're having a game dev jam). One of my ideas however requires me to synch every event in the game (sound fx, visual fx) with the beat of a song (that has yet to be written). Of course there are complex methods out there to do exactly that, but this is strictly a one day project and so somewhat limited. So my first idea was to compose the song (friend & artist will help here) exactly to specifications I set in advance (in terms of regular beat etc.) and then just hardcode the game so that events happen in synch with the beat. (but i might run out of synch here in the long run i guess, unless I manually synch it every now and then) That at least was my idea. I'm now looking for some ideas/input if someone knows a way to achieve this in a more dynamic way. Are there simple ways to analyse a music track? (probably in wav or mp3 form) Ideas appreciated... :)

Share this post


Link to post
Share on other sites
You may find problems with that method, a quick calculation just showed me that to have an 'event' on any from, locked to 64 frames/sec you would have each frame sync to a 16/th note in 4/4 time at 240 beats/sec. Thats a freaking fast track to keep too. On the other hand, if you kept to, say, 32 FPS then you could do the same, but at a track tempo of only 120 BPS, much more manageable. Keep in mind of course that a 16th note is not going to feel very in time with the music for major events which would feel more 'right' synced to a beat or even start of a measure.

Share this post


Link to post
Share on other sites
If you use a tracked music format (like .it) then you can key in-game actions to the notes and events. If you use FMod for playback then this is easy to do.

If you want to use a "proper" audio format like mp3 or ogg then you're going to have to encode the events separately, possibly in an xml file. You'll have to be careful that they don't drift out of sync over time.

Share this post


Link to post
Share on other sites
I've just written a small project that does just this (see the IOTD

As with your case, I just wanted to throw some game ideas about without things getting out of hand and so opted for a simple method.

basically. I start with a desired beat rate (BPM). Then go through the following steps.


1. At the start of the game (when I start my mp3 track) I set my game clock to zero. This can be a timer of any sort. I have a convenient GetElapsedTime function that'll return the number of seconds that have passed since the game was started (or the last time the timer was reset).

2. Create a function called GetCurrentBeat or some such. i.e.

unsigned int GetCurrentBeat(float BPM)
{
float elapsedMins = (GetElapsedSeconds() / 60.0f);

return (unsigned int) (elapsedMins * BPM);
}



3. Store the result from this function when you start your game/level/etc.

4. As the game progresses each frame and events are generated. store them in a list or some such.

5. Each frame call GetCurrentBeat. if it returns a value that is different from the stored one then a beat has happened. Process all the stored events and update the stored beat value with the new value.


That's basically it. In my case I created another additional function that returned how far I was through a given beat each frame as a fraction (0.0f to 1.0f). this allowed me to tie all the motions within the game to the beat rate thus enhancing the impression of synchronisation.

Everything works out what it's going to do as the beat changes then spends the whole beat doing it, saving me the bother of storing a list of events. The fraction functions means I can slide things (or shrink them or grow them) in time with the beat from their old state (or position) into their newly chosen one.


That's it basically.

Share this post


Link to post
Share on other sites
Thanks everyone for the ideas...

@DarkPsychosis
That sounds like a good solution considering its a one day project. I'll be able to read out the elapsed time since the song was started (we're using our own 2D engine for the jam event) which enables me to change songs on the fly without running into danger of going out of sync. Just need to feed in the new BPM and sorted. That'll be close enough for what I have in mind.

Now I just need to figure out how to make a half decent game out of the collection of ideas I have... :)

Share this post


Link to post
Share on other sites
Well, its probably going to be less of a game and more of an interactive music thing. sort of. lets see, i got 2 more weeks to think about this...
and then one day of insane coding... :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this