Game Development Community

Problem with onTimer callback - synchronizing to music

by Conor O Kane · in Torque Game Builder · 04/14/2008 (7:43 pm) · 4 replies

I'm trying to make a game where sound effects play in sync with the music. The music is playing at 120bpm, and I have a metronome object with onTimer callbacks every 125ms - which gives me 4 ticks per beat. So I only play sound effects during the onTimer callback function. It's working fine on a fast computer, where the game's framerate is over 100 fps

video: http://youtube.com/watch?v=_aw5ykjTK9E

However on a slower computer, the sound effects quickly go out of sync with the music. My music is split into 32 second .ogg files, and so if everything is working correctly, when the music ends the next tick of the metronome comes around and starts up the next piece of music seamlessly. However, on a slower computer there is around a 1 second delay after a piece of music ends before the next one starts. This means that either the music is playing too fast (unlikely) or the onTimer callbacks aren't arriving on time, and the errors are accumulating.

Can anyone suggest a better way to do this? Should onTimer callbacks remain on time, even if one arrives a few frames late, or do the errors really accumulate? Is there a way to grab the exact game time in milliseconds? If so I could use this in the scenegraph update to create my own accurate counter.

#1
04/15/2008 (4:27 am)
Conor,

Try to use getRealTime() to get real time in milleseconds.

Regards!
#2
04/22/2008 (6:57 am)
Thanks Dmitriy - it turns out getRealTime() doesn't update every millisecond, however getSimTime() does, so I was able to use that.

After converting my game over from an onTimer loop to an onUpdate with a counter, it turns out that the timing problem is still present on my slower computer - ie the music goes out of sync with the effects and there's a slight delay after each piece of music before the next one starts. I can only conclude that this is due to my PC playing the sound file too fast! I'll have to look into alxPlay more carefully I think.
#3
04/22/2008 (9:04 am)
Here is an idea that would ensure they are in synch. Make the metronome its own looping sound, and start them both playing at the same time.
#4
04/22/2008 (5:30 pm)
Interesting idea James, it would work if there weren't too many variables, but there are a lot of things I need to check on each metronome 'tick' and only a few of them get played. For example, the player can fire on each tick, but every 4th is a louder shot, and enemies can explode on the 2nd or 4th beat, but every 16 bars they can also explode on the 4.5 beat to add interesting rhythm to the explosions.