1
\$\begingroup\$

I am developing an XNA-Game, which is pretty much a runner game (side-scrolling).

Now I am in the middle of adding sounds to my game. And as it turns out, I am having quite some problems with the footstep-playback.

The following is in C#, but I am quite sure that anyone will understand it:

 public static void StartStepTimer()
 {
     _stepTimer = new DispatcherTimer();
     _stepTimer.Interval += TimeSpan.FromMilliseconds(500);
     _stepTimer.Tick += _stepTimer_Tick;
     _stepTimer.Start();

     _step = _content.Load<SoundEffect>("Sound\\World\\Summer_1");
     _stepInstance = _step.CreateInstance();
     _stepInstance.Volume = 0.4f;

 }

  private static void _stepTimer_Tick(object sender, EventArgs e)
  {
      _stepInstance.Play();
  }

As you can see, I am playing the footstep based on a timer. I am using this method, since the gamespeed gets higher during gametime (I can change the interval in that case).

But this method isn't really working. The playback lags. I thought of making a prerendered 10s soundfile and looping that in gametime, but than the playback won't fit when the gamespeed rises.

Any suggestions?

\$\endgroup\$
5
  • \$\begingroup\$ gafferongames.com/game-physics/fix-your-timestep \$\endgroup\$
    – Adam
    Sep 29, 2013 at 18:54
  • \$\begingroup\$ That guy is doing it like I am \$\endgroup\$
    – IMX
    Sep 29, 2013 at 18:58
  • \$\begingroup\$ Yeah, this approach is pretty poor. Traditionally the one part of any (modern) game that has been multi-threaded has been audio, long before we started exploiting threading for the sake of improved performance on multiple cores, audio streams were given their own child thread that would keep the sound card buffered with audio. You do not want to do this on a timer, if anything you would go the opposite extreme and use a busy-wait, which would avoid stuttering when you fail to keep the buffer full of audio samples due to timer scheduling issues. \$\endgroup\$ Sep 30, 2013 at 5:08
  • \$\begingroup\$ You really shouldn't be using event-based systems when you have a game loop (as per Roy T's answer). Getting stable, accurate timing is a tricky problem - which XNA solves for you (GameTime). Normal event-based timers are nowhere near accurate enough for "multimedia" purposes. Case in point: DispatchTimer's documentation says pretty much exactly this. \$\endgroup\$ Sep 30, 2013 at 8:00
  • \$\begingroup\$ usually I just play a looped audio and increase the playback speed if I need it to be quicker. I'm not sure how to do this in XNA but you should avoid timers for something like this. Timers are best used for events that you want to be time delayed. \$\endgroup\$ Sep 30, 2013 at 16:56

2 Answers 2

5
\$\begingroup\$

The reason your sound is lagging is because you're using a timer event. Which are imprecise as they will only update when the CPU feels like it (just like when yielding threads). The correct way to do it is by calculating how much time passes each frame and then firing the sound at the frame the foot hits the ground (or maybe a few frames before it if you still have issues).

A piece of advice. In a game the game-loop is the artery of everything time related. Try to steer away from timers and the likes as they interfere with the linearity of your game loop as you never know exactly where you will be in code when the main thread is halted to execute the timer code. Another advantage of using the game loop for everything is that you can easily speed-up and slow-down time and everything will react to it.

\$\endgroup\$
1
\$\begingroup\$

May not be the best solution, but I'll throw an idea below:

You can play the footstep sound at a specific animation frame. In my game I did it indirectly.

For example, when the player is walking, it can have 6 states (walking1, walking2, ... walking6). Every state is mapped to a particular sprite. The states changes over time, in order. So, when the character changes from state "walking1" to "walking2", it will change its sprite and also play a footstep sound. Then it will cycle through everything and repeat.

\$\endgroup\$
1
  • \$\begingroup\$ This is the basic approach used by "the pros." \$\endgroup\$ Sep 30, 2013 at 0:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .