NSSound Lag?

Discussion in 'Mac Programming' started by Darkroom, Jul 25, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i have included several .aiff sound files to my app's resources, and they are simply called though out different classes in different action methods simply by writing something like this:

    Code:
    [[NSSound soundNamed:@"mySound.aif"] play];
    
    they all play fine, but what i don't understand is that the first sound that is played always lags the IBAction it's apart of the first time that action executes... all sounds played afterward with other methods are played on time with out any lag for the IBAction, it's just the very first sound the app plays lags it's action, no matter which sound/action it is...

    why is this happening?
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Loading and playing uncached sounds is pretty slow :( It's something we've been struggling with in Adium.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    it's strange, though, as it only happens to the first sound that is executed... it's more of a freeze than a lag, as the sound happens at the same time as the action, but the action freezes for about half a second before executing (if it's the first action of the app with sound)...

    it seems that the app has to load NSSound for a first time, rather than the actual sound file, and then after the first sound (first NSSound) the NSSound is cached while the app remembers that NSSound is included and therefore plays other sounds without lagging their actions...

    is that normal?
     
  4. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    Doesn't sound normal, but I could sorta see why it might happen. It might be setting up a lot of infrastructure that's left unused until an app tries to play a sound. You could probably "cheat" and play an empty sound on startup in a background thread to hide the delay.
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    thanks... loading an empty sound at launch seems to be doing the trick... :)
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    Have you tried to use the raw OpenAL API? That might be better for instantaneous playback.
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    humm... maybe for an update later down the road i'd use that... for now that trick seems to work well with NSSound... unless of course it's as easy to code OpenAL sounds as it is for NSSound i'd do it, but after a few minutes reading about OpenAL it seems like a much more complex process – albeit far more optimized for sure than using NSSound...
     

Share This Page