Page 1 of 2

Correct way to play sounds?

Posted: Thu Nov 15, 2012 12:55 am
by utunnels
For example, if I have an animation that plays a sound repeatedly (a ball bounces around), what do I do to make the sounds mix in a proper way?

Do I need to creat a new source every time from the same sound data?

Re: Correct way to play sounds?

Posted: Thu Nov 15, 2012 7:28 am
by Roland_Yonaba
You don't need to. Create the source once. and then use source controls.

Dunno for sure if you're looking for that, but if you are continuously sending playing calls to a source, and you are not sure whether or not the source is actually being played, then you can rewrite love.audio.play in a manner it always rewinds the source before playing it.

Code: Select all

local old_love_audio_play = love.audio.play
function love.audio.play(source)
  if not source:isStopped() then
    source:rewind()
  else
    old_love_audio_play(source)
  end
end

Re: Correct way to play sounds?

Posted: Thu Nov 15, 2012 8:30 am
by utunnels
Perhaps I didn't say it clearly. What I want to do is to have multiple sounds playing at the same time, so I don't want the first one to stop nor want to wait for it to stop. ;)

The idea I had in mind was like this:

Code: Select all

local sdping = love.sound.newSoundData("ping.wav")

function playSound(sd)
  love.audio.newSource(sd, "static"):play()
end

playSound(sdping)

------------------------

Edit*

I tried the method I mentioned just now. It seemed to work well on this pc (no lags I could tell).

But I'm not sure if creating sound source is an acceptable way.

Re: Correct way to play sounds?

Posted: Thu Nov 15, 2012 9:02 am
by Robin
utunnels wrote:But I'm not sure if creating sound source is an acceptable way.
That is, in fact, the proper (and only) way to play a single sound multiple times simultaneously.

Re: Correct way to play sounds?

Posted: Thu Nov 15, 2012 9:30 am
by kikito
Robin wrote:That is, in fact, the proper (and only) way to play a single sound multiple times simultaneously.
And, I think LÖVE should provide a way to do that by default, since this question has been asked again and again.

Don't want to repeat myself too much: LÖVE needs a first-class "Sound" entity that acts as a "Source manager". When a Sound is played, a new (or existing in the Sound's pool) Source should be returned by love.play. This is too basic to need external libraries.

Re: Correct way to play sounds?

Posted: Thu Nov 15, 2012 4:15 pm
by SiENcE
I aggree to kikito.

Re: Correct way to play sounds?

Posted: Thu Nov 15, 2012 5:04 pm
by dreadkillz
The way sound is handled isn't very intuitive if you're new. It's not like an image object where you can just draw as many copies of an image as you want. I too share the same sentiment as kikito.

Re: Correct way to play sounds?

Posted: Fri Nov 16, 2012 12:24 am
by utunnels
It shouldn't be hard to write a a wrapper in lua.

Another thing I'm concerning is the unlimited new sound source may cause booms. It works well on my pc, but lags and booms on android if too many sounds are created. Though android version is not an official release, perhaps it is still necessary to have some limit.

For example, have a pool with limited size, when a new source is played, check the pool, if it is full, find a source which is already stopped or has lower priority, replace it with the new source.

Re: Correct way to play sounds?

Posted: Fri Nov 16, 2012 12:44 am
by kikito
utunnels wrote:It shouldn't be hard to write a a wrapper in lua.
I didn't say "it is hard to write a wrapper in Lua". I said "it should be done by LÖVE". The fact that is (allegedly) simple to do doesn't mean that it should be done by default. It's not a matter of difficulty, it's a matter of convenience and expectatives.
utunnels wrote:Another thing I'm concerning is the unlimited new sound source may cause booms. It works well on my pc, but lags and booms on android if too many sounds are created. Though android version is not an official release, perhaps it is still necessary to have some limit.
It shouldn't be hard to write a wrapper in Lua (see? :P )

Now, seriously. As you say the Android version is even less official than LÖVE. If worst comes to worst, the android version could add a limit for Android.
utunnels wrote:For example, have a pool with limited size, when a new source is played, check the pool, if it is full, find a source which is already stopped or has lower priority, replace it with the new source.
I was thinking about having an optional `sources` param at the end of love.audio.newSound, maybe default to 4. Or a constant in love.audio (love.audio.setSourcePoolSize(...)). I'm open to anything on that regard.

Re: Correct way to play sounds?

Posted: Fri Nov 16, 2012 1:16 am
by Dattorz
kikito wrote:Don't want to repeat myself too much: LÖVE needs a first-class "Sound" entity that acts as a "Source manager".
I think that's overkill, and it will just add to the confusion (we already have enough sound-related classes as it is). A much simpler solution would be to just allow love.audio.play() to take either a Source or a SoundData as the parameter.