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

Author Topic: Multiple sounds don't work  (Read 3431 times)

0 Members and 1 Guest are viewing this topic.

NPS

  • Newbie
  • *
  • Posts: 31
    • View Profile
Multiple sounds don't work
« on: January 18, 2013, 02:08:19 am »
SFML 2.0 (although had the same issue with 1.6). I use sounds like this:
class Sound
{
    private:
    sf::SoundBuffer soundBuffer;
    std::vector<sf::Sound> soundInstances;

    public:
    void Load(std::string filename);
    void Update(void);
    void Play(void);
};
void Sound::Load(std::string filename)
{
    bool result = soundBuffer.loadFromFile(filename);
    assert(result);
}

void Sound::Update(void)
{
    for (int i = 0 ; i < soundInstances.size() ; ++i)
    {
        if (soundInstances[i].getStatus() == sf::Sound::Stopped)
        {
            soundInstances.erase(soundInstances.begin() + i);
            --i;
        }
    }
}

void Sound::Play(void)
{
    soundInstances.push_back(sf::Sound(soundBuffer));
    soundInstances.back().play();
}

I've attached Sound::Play() to mouse clicks (just for debugging reasons). When I click once (i.e. create one sf::Sound and play it) everything's fine. However, when I click a couple of times (fast, so one sound won't finish before another starts) I get random results - either every consecutive sounds stops the previous one and only the last one plays till the end, or a couple of them start and play simultaneously but then all stop (sooner than they should), or very rarely they all play simultaneously and finish properly.

Why is that? Am I doing something wrong?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10989
    • View Profile
    • development blog
    • Email
Re: Multiple sounds don't work
« Reply #1 on: January 18, 2013, 03:23:50 am »
I just played around a bit with it and it seems to be the std::vector that is causing the problem, which makes sense after a bit thinking, because you're constantly resizing the vector and thus the sound objects get copied around while they're actually playing, which is obviously a problem. ;)

Using for instance std::deque solves the problem.

Here's a slightly modified, but at least complete example.
The events boolean can be used to switch between event and real-time mouse input.
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

#include <iostream>
#include <deque>

class Sound
{
   private:
    sf::SoundBuffer soundBuffer;
    std::deque<sf::Sound> soundInstances;

public:
    void Load(std::string filename)
    {
        soundBuffer.loadFromFile(filename);
    }

    void Update(void)
    {
        for (int i = 0 ; i < soundInstances.size() ; ++i)
        {
            if (soundInstances[i].getStatus() == sf::Sound::Stopped)
            {
                soundInstances.erase(soundInstances.begin() + i);
                --i;
            }
        }
    }

    void Play(void)
    {
        soundInstances.push_back(sf::Sound(soundBuffer));
        soundInstances.back().play();
    }

    unsigned int Count()
    {
        soundInstances.size();
    }
};

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Rotate Test");
    window.setFramerateLimit(60);

    Sound snd;
    snd.Load("laser.wav");

    sf::Event event;

    bool events = true;

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            else if(events && event.type == sf::Event::MouseButtonReleased &&
                      event.mouseButton.button == sf::Mouse::Left)
                snd.Play();

        }

        if(!events && sf::Mouse::isButtonPressed(sf::Mouse::Left))
            snd.Play();

        if(snd.Count() > 0)
            std::cout << snd.Count() << std::endl;

        snd.Update();

        window.clear();
        window.display();
    }
}
« Last Edit: January 18, 2013, 03:28:19 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NPS

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Multiple sounds don't work
« Reply #2 on: January 18, 2013, 09:34:47 am »
So obvious now that I know the answer. :P Thx!

 

anything