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

Author Topic: playing sounds simultaneously  (Read 2669 times)

0 Members and 1 Guest are viewing this topic.

Reborn

  • Newbie
  • *
  • Posts: 25
    • View Profile
playing sounds simultaneously
« on: October 16, 2016, 11:41:58 pm »
Hey,
I was looking for a good way to play sounds simultaneously with SFML.
After googeling I found a post from nexus (http://en.sfml-dev.org/forums/index.php?topic=6697.0). He only gave a hint: Push the playing sounds into a queue and pop them if they reached the status sf::Sound::Status::Stopped.
I thought, that's a good idea, so I tryed to programm a general solution to this problem:

(click to show/hide)

(click to show/hide)

Everything is working fine and anyone can feel free to use it.
The thing I want to know is, what do you think about this solution? May be there are some better out there?

regards

Julian
« Last Edit: October 16, 2016, 11:58:44 pm by Reborn »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: playing sounds simultaneously
« Reply #1 on: October 18, 2016, 09:06:32 pm »
Why does this use a thread? The manager can be updated whenever any function is called (such as "play new sound" or explicitly "update") at which point, all the ended sounds can be popped at once.

I mean, it might work fine and everything and that's great; I just think that adding the extra complexity of using a thread is not necessary here.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Reborn

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: playing sounds simultaneously
« Reply #2 on: February 07, 2017, 04:11:35 pm »
Thank you for your post.

Is it established practice to do it like you suggested?
I mean, it's like let clean up the gabrage of anyone's work from anyone else.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: playing sounds simultaneously
« Reply #3 on: February 07, 2017, 05:31:43 pm »
I would say everyone can approach tasks in their own way and often do so I don't think there's only one way ("established practice") to do this.

I was merely questioning if the extra thread is required here when you can achieve a similar result without it.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: playing sounds simultaneously
« Reply #4 on: February 15, 2017, 09:33:54 pm »
SFML sounds are played in a different thread internally, so you don't have to put it in another thread yourself.  Whenever you register a sound, you can check for ones that finished at that time. Sleeping for only 1ms then looping through will eat up CPU time. Even though it's in a different thread it's still associated with your process and could be scheduled less often if the OS thinks you're eating up too much time.

I think that's what Hapax is getting at.

Like Hapax said, you could periodically call "Update" on your manager (maybe every few seconds) so it can flush finished audio. It would loop through its container less often, thus eating up less CPU time.
Or you could have it running in another thread if you're worried about hitching and use a condition variable to wake up on command.  Just another idea.  No read right/wrong here.

Reborn

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: playing sounds simultaneously
« Reply #5 on: February 25, 2017, 05:29:12 pm »
That is what I was looking for. Some other views of the same point. Thank you.

 

anything