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

Author Topic: 'Stopped' callback in sf::Sound  (Read 5051 times)

0 Members and 1 Guest are viewing this topic.

victorclf

  • Newbie
  • *
  • Posts: 2
    • View Profile
'Stopped' callback in sf::Sound
« on: July 18, 2011, 12:55:16 am »
Hello,

I believe sf::Sound lacks a callback for when it finishes playing. Unlike sf::Sprite, it can't be destroyed before it finishes playing, therefore managing sf::Sound instances is tricky.

Currently, you need to manually manage each sf::Sound instance. The best solution I come up with so far was to keep track of all sf::Sound instances created and periodically iterate through them, deleting the ones that have Status::Stopped.

I do not believe this an optimal solution. Having sf::Sound call your callback object when it's done playing would give you the opportunity to delete the now useless sf::Sound instead of having to manually check if it's stopped.

IIRC SDL had support for such a callback.


Cheers and keep up the excellent work,
Victor Freire

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
'Stopped' callback in sf::Sound
« Reply #1 on: July 18, 2011, 07:36:45 am »
OpenAL doesn't provide such a callback, there's no way for SFML to know when a sound finishes. So I would have to create a thread that polls every sound instance to find out which ones have their status "Stopped". This is not better than what you do, so I prefer to let users do it if they need to -- and not do it if they don't need this feature ;)
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
'Stopped' callback in sf::Sound
« Reply #2 on: July 18, 2011, 08:00:39 am »
Actually I would see it more preferable not to have a callback for deleting the sound.

Letting the sound object stay in memory is actually preferable since there is a limited amount of sound sources that can be playing at any given time. So what we do instead is to keep a central sound manager where you request free sound objects from. Which will be any sound that has finished playing. So the sounds stay in memory and is instead borrowed out instead of creating and destroying them over time.

What I think though could be appreciated is a way to see if a sound is finished playing because just because it is stopped doesn't mean that the sound has been played. So a special sf::Sound::Status that is returned when the sound has reached the end and is stopped.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
'Stopped' callback in sf::Sound
« Reply #3 on: July 18, 2011, 09:00:43 am »
Quote
Actually I would see it more preferable not to have a callback for deleting the sound.

The callback would not delete the sound, it would just signal that the sound has finished playing. Then it's up to the user to do whatever he wants with the sound.

Quote
What I think though could be appreciated is a way to see if a sound is finished playing because just because it is stopped doesn't mean that the sound has been played. So a special sf::Sound::Status that is returned when the sound has reached the end and is stopped.

Is it really relevant? What difference does it make?
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
'Stopped' callback in sf::Sound
« Reply #4 on: July 18, 2011, 09:12:42 am »
Nevermind, I thought I had a great reason but since the sound was stopped you could just acquire a new one anyway.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

victorclf

  • Newbie
  • *
  • Posts: 2
    • View Profile
'Stopped' callback in sf::Sound
« Reply #5 on: July 18, 2011, 09:32:15 pm »
Hmm ok, I'm not familiar with OpenAL API. I thought it had such funcionality implemented.

In that case, I agree it's probably best to leave it to the user to handle it.

I will analyze what will work best in my case (creating/deleting sounds or reusing them as Groogy suggested).

Ty.

 

anything