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

Author Topic: Best way to handle sounds?  (Read 3615 times)

0 Members and 1 Guest are viewing this topic.

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Best way to handle sounds?
« on: June 16, 2014, 04:52:28 pm »
I'm getting to the point where I think I should start managing my sounds better. While I don't have many sounds now, I will eventually, and I want to start a good sound system before it's too late. I have 2 ideas for managing sounds.

First idea : manage them like a sprite sheet. Put all sounds into one file (soudbuffer) like a texture, and just set the playing offset to whichever sound I need, and stop it when that specific sound ends, like setting the texture rectangle. The thing is I have to have a sf::Time variable to comper to where the sound is constantly to stop it. But it's doable.

Second idea : make a lot of soundbuffers for each sound, and just have one sound that switches between them.

I understand that both of these methods mean you can't play multiple sounds at once, but that's how i want it. I will have multiple sounds, but each sound buffer or group of soundbuffers will be related somehow. So...anybody have any ideas or suggestions?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Best way to handle sounds?
« Reply #1 on: June 16, 2014, 04:59:50 pm »
Third idea: each sf::SoundBuffer represents a distinct sound, multiple sf::Sound instances share the same soundbuffer. Don't optimize in sprite-sheet style unless it brings clear advantages.

For sound management, you could have a look at Jesper Juhl's jukebox.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Best way to handle sounds?
« Reply #2 on: June 16, 2014, 05:13:24 pm »
I tried that once. I made a list or set of sf::Sound, and just inserted a new sound when i needed it, then erased it from the list when it was done. But for some reason it wouldn't work. I should probably try again, because that to me was the best way to manage as well.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Best way to handle sounds?
« Reply #3 on: June 16, 2014, 05:28:58 pm »
std::set is only useful if you need an ordering. When the sounds have the same length, std::queue is ideal, otherwise you could use std::list with its remove_if() member function.

The problem with std::vector (and std::deque if you manipulate it in other places than on the ends) is that sounds can be copied/moved while playing, leading to destruction of the playing sound.
« Last Edit: June 16, 2014, 05:30:40 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Best way to handle sounds?
« Reply #4 on: June 16, 2014, 06:13:23 pm »
If your app is constrained to a single sound at once, then I'd say that a single sf::Sound and multiple buffers will be a better idea, since it will enforce that constraint.
Laurent Gomila - SFML developer

 

anything