SFML community forums

Help => Audio => Topic started by: gws923 on April 22, 2015, 10:38:21 pm

Title: Stopping all sounds in my game at once creates significant lag.
Post by: gws923 on April 22, 2015, 10:38:21 pm
Hi there.  Y'all have been very helpful in the past, so I turn to you again! I hope this question hasn't been asked many times.  I did a search and didn't see anything on it so...

When the player in my game dies, I want all sf::Sounds and sf::Musics playing to stop for a (hopefully) dramatic effect.  Kind of like when you die in MegaMan.

When I call to my SoundManager class (which is very basic, mostly std::maps to hold sf::Sound and sf::Musics) to stop all playing sounds and musics, it works, but then my game lags like crazy.

Here is the relevant code, as far as I can tell:

    void SoundManager::stopAll()
    {
        stopAllSounds();
        stopAllMusic();
    }

    void SoundManager::stopAllSounds()
    {
        if(_soundVolume == 0)
            return;

        std::map<string, Sound::Ptr>::iterator itr;
        for (itr = _sounds.begin(); itr != _sounds.end(); itr++)
        {
            itr->second->stop();
        }

    }

    void SoundManager::stopAllMusic()
    {
        if(_musicVolume == 0)
            return;

        std::map<string, sf::Music*>::iterator itr;
        for (itr = _musics.begin(); itr != _musics.end(); itr++)
        {
            itr->second->stop();
        }
    }

 

Now, I'm wondering whether iterating over the map is just eating my memory, or if I'm not supposed to be storing and using Sounds this way and should instead be using a soundBuffer... but who knows.  I don't really understand what the point of a SoundBuffer is, even though I've read the documentation.  I am not well versed in sound file technology and terminology.

Any help would be much appreciated!
Title: Re: Stopping all sounds in my game at once creates significant lag.
Post by: Laurent on April 22, 2015, 11:10:03 pm
The first thing to do is obviously to test whether it is sounds or musics or both that cause the lag.
Title: Re: Stopping all sounds in my game at once creates significant lag.
Post by: gws923 on April 22, 2015, 11:22:39 pm
Yes, will do.  Results forthcoming.

EDIT:  Wow.  Surprisingly, it's stopping the music that is creating the lag!  I wouldn't have expected that.  Does that provide any insight?
Title: Re: Stopping all sounds in my game at once creates significant lag.
Post by: gws923 on April 23, 2015, 01:02:15 am
Well, I've switched around my code a bit so that I keep track of what music is playing at any given time with a string, so when I call stopAllMusic() it stops the currently playing music only by referencing each music's key in the map.  This limits the number of musics I can play at a time to a pre-determined number.

Not a big deal and pretty much solves my problem, but I am interested if anyone has any insight into anything stupid I did, just so I can avoid doing it in the future.

Let me know!
Title: Re: Stopping all sounds in my game at once creates significant lag.
Post by: eXpl0it3r on April 23, 2015, 02:06:33 am
There's still a high chance that you're doing something odd, but without complete and minimal example we can only guess.
Title: Re: Stopping all sounds in my game at once creates significant lag.
Post by: Laurent on April 23, 2015, 08:03:34 am
Quote
Wow.  Surprisingly, it's stopping the music that is creating the lag!  I wouldn't have expected that.  Does that provide any insight?
That was expected. sf::Music uses a separate thread to feed the audio samples to the audio engine, and stopping it involves some synchronization and waiting between the calling thread and the music thread. I guess we could check the corresponding code on our side, to make sure that we're not doing something stupid as well :)