XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

Audio slows game

Last post 04-09-2008 4:44 PM by chrisjubb. 8 replies.
  • 04-07-2008 10:44 AM

    Audio slows game

    I'm using XACT to play four waves at the same time in a 'sound'. I then use SoundBank.PlayCue() here's the code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public void playMusic(bool start)
    {
    if (useMusic == false)
    return;

    try
    {
    if (start)
    {
    try
    {
    soundBank.PlayCue(soundMusic);
    }
    catch (Exception) { }
    }
    else
    {
    Cue c = soundBank.GetCue(soundMusic);
    if (c != null)
    c.Stop(AudioStopOptions.Immediate);
    }
    }
    catch (Exception)
    {
    return;
    }
    }

     

    Then in my Update method I set the variables in my XACT project:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    try
    {
    Cue c = soundBank.GetCue(soundMusic);
    if (c != null)
    {
    try
    {
    c.SetVariable("T1vol", levelColours[0] * 100.0f);//set 0..100.0f
    c.SetVariable("T2vol", levelColours[1] * 100.0f);
    c.SetVariable("T3vol", levelColours[2] * 100.0f);
    c.SetVariable("T4vol", levelColours[3] * 100.0f);
    }
    catch (Exception)
    {
    #if !XBOX360
    System.Diagnostics.Debug.Print("CANNOT SET VOLUME");
    #endif
    }
    }//end if cue is ok
    }
    catch (Exception)
    {
    }
     
     
    Sometimes retrieving the Cue fails but I don't think this is too much of a problem.
     
    Am I doing something wrong in the code?
     
    Thanks
     
    Chris
  • 04-07-2008 12:19 PM In reply to

    Re: Audio slows game

    chrisjubb:

    Am I doing something wrong in the code?


    I have no idea - what happens when you run tihs code?

    You forgot to say what your actual problem is :-)
    XNA Framework Developer - blog - homepage
  • 04-07-2008 12:34 PM In reply to

    Re: Audio slows game

    The problem is that the game runs for a few seconds and then dramatically slows down. Without the SetVariable() code the game runs fine.

  • 04-07-2008 1:32 PM In reply to

    Re: Audio slows game

    Are you calling AudioEngine.Update regularly?

    Also, that code doesn't actually appear to do anything useful. You look up a temporary cue object, then set some variables on it, then immediately leak this reference, but you never actually start playing the cue that you set these variables on...
    XNA Framework Developer - blog - homepage
  • 04-07-2008 3:46 PM In reply to

    Re: Audio slows game

    How often does this code get run? If you create a cue each call in Update(), say, you'll probably start causing massive problems when you run out of voices.
    Also, your "stop" function doesn't do the right thing. Each call to GetCue() creates a new cue instance. You want to hold on to your returned Cue object when it's created, and then set the parameters on that instance (as well as use that instance to stop the playing sound).

    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 04-08-2008 3:17 PM In reply to

    Re: Audio slows game

    Why do you have try-catch blocks around everything?  If something goes wrong in the method, you won't know about it.

  • 04-09-2008 2:28 PM In reply to

    Re: Audio slows game

    I have noticed in my experience that Try-Catch blocks can cause considerable slow down (Sometimes you just can't avoid using them, in which case they are elegantly beautiful, but otherwise they're attrocious).

    You ought to just having the game check if something will cause an error(or if it should do something) before it does it. It ought to speed up the game considerably.

  • 04-09-2008 2:57 PM In reply to

    Re: Audio slows game

    A try/catch block is actually pretty much free as long as no exception is thrown. It is only if your code actually does throw an exception that things get expensive. And agreed, you want to avoid throwing exceptions while your game is running: these should be reserved for truly rare ("exceptional") situations.
    XNA Framework Developer - blog - homepage
  • 04-09-2008 4:44 PM In reply to

    Re: Audio slows game

    Thanks for your replies.

    Yes I should hold onto my music Cue. Now I set the Cue at the start, and then every so often update the volumes:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    if (soundMusicCue != null)
    {
    try
    {
    soundMusicCue.SetVariable("T1vol", levelColours[0] * 100.0f);//set 0..100.0f
    soundMusicCue.SetVariable("T2vol", levelColours[1] * 100.0f);
    soundMusicCue.SetVariable("T3vol", levelColours[2] * 100.0f);
    soundMusicCue.SetVariable("T4vol", levelColours[3] * 100.0f);
    }
    catch (Exception)
    {
    #if !XBOX360
    System.Diagnostics.Debug.Print("CANNOT SET VOLUME");
    #endif
    }
    }//end if cue is ok
     
    Thanks for the help. :)
Page 1 of 1 (9 items) Previous Next