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

Author Topic: Questions about sound in a game  (Read 3985 times)

0 Members and 1 Guest are viewing this topic.

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Questions about sound in a game
« on: July 01, 2012, 11:29:58 am »
Hey there :)

I'm considering using SFML audio classes to handle sounds and music in a game. I have a few questions about this.

- I want to play a music, and automatically starts a new music when this one is done. Is there a way to know when the streamed music is about to end ? Like a callback or something ?
- I want to play a sound everytime someone shoots. So one SoundBuffer for all of them, but how do I dynamically create the Sound objects ? Do I need to keep a list of all active sounds, and delete them when they're finished ? Is there some auto-delete flag ?

All I really want is the ability to chain musics, and to starts sounds at anytime, without knowing in advance how many Sound object I will need.

I'm not decided on a version yet, so I'll probably go with 2.0 if nothing's wrong with it.

Thanks :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Questions about sound in a game
« Reply #1 on: July 01, 2012, 03:12:19 pm »
Quote
Is there a way to know when the streamed music is about to end ? Like a callback or something ?
No, sorry.

Quote
I want to play a sound everytime someone shoots. So one SoundBuffer for all of them, but how do I dynamically create the Sound objects ? Do I need to keep a list of all active sounds, and delete them when they're finished ? Is there some auto-delete flag ?
No auto-delete flag, you have to manage their lifetime yourself.

Quote
I'm not decided on a version yet, so I'll probably go with 2.0 if nothing's wrong with it.
Yeah, don't choose SFML 1.6, it's already outdated.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Questions about sound in a game
« Reply #2 on: July 01, 2012, 03:35:48 pm »
I recommend to have a std::queue<sf::Sound>. If you play a sound, you push a sf::Sound object to the queue and play it. In regular intervals you check if the sounds at the front of the queue have stopped and pop them.

By the way, I plan to implement a sound player in Thor 2.1, but I still have a lot to do before.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Questions about sound in a game
« Reply #3 on: July 01, 2012, 06:14:03 pm »
- I want to play a music, and automatically starts a new music when this one is done. Is there a way to know when the streamed music is about to end ? Like a callback or something ?
I think you'd have to periodically (perhaps at the end of your main loop, or in other thread) checking to see that the music is still playing.

- I want to play a sound everytime someone shoots. So one SoundBuffer for all of them, but how do I dynamically create the Sound objects ? Do I need to keep a list of all active sounds, and delete them when they're finished ? Is there some auto-delete flag ?
In order to avoid having to check if a sound is finished, I used a boost::circular_buffer rather than a queue as Nexus recommended. It does mean that sounds may be cut off occasionally, and it limits how many sounds you can play at once, but depending on your needs it may be sufficient. If you expect to have more then ten or twenty sounds simultaneously, though, it's probably more space-efficient to go with a non-fixed-size container like the queue.

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Questions about sound in a game
« Reply #4 on: July 03, 2012, 11:03:11 am »
Ok, thanks...

Sounds a bit complicated :-S
I don't really have an explicit event loop. I guess I'll stick with Phonon for now :(

 

anything