Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Stopping all sounds in my game at once creates significant lag.  (Read 4076 times)

0 Members and 1 Guest are viewing this topic.

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stopping all sounds in my game at once creates significant lag.
« Reply #1 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.
Laurent Gomila - SFML developer

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Stopping all sounds in my game at once creates significant lag.
« Reply #2 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?
« Last Edit: April 22, 2015, 11:26:02 pm by gws923 »

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: Stopping all sounds in my game at once creates significant lag.
« Reply #3 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!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Stopping all sounds in my game at once creates significant lag.
« Reply #4 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stopping all sounds in my game at once creates significant lag.
« Reply #5 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 :)
Laurent Gomila - SFML developer