Correct way to play sounds?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
utunnels
Citizen
Posts: 75
Joined: Fri Jan 06, 2012 5:20 pm

Correct way to play sounds?

Post 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?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Correct way to play sounds?

Post 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
utunnels
Citizen
Posts: 75
Joined: Fri Jan 06, 2012 5:20 pm

Re: Correct way to play sounds?

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Correct way to play sounds?

Post 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.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Correct way to play sounds?

Post 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.
When I write def I mean function.
User avatar
SiENcE
Party member
Posts: 792
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Correct way to play sounds?

Post by SiENcE »

I aggree to kikito.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Correct way to play sounds?

Post 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.
utunnels
Citizen
Posts: 75
Joined: Fri Jan 06, 2012 5:20 pm

Re: Correct way to play sounds?

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Correct way to play sounds?

Post 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.
When I write def I mean function.
User avatar
Dattorz
Citizen
Posts: 66
Joined: Sat Oct 27, 2012 12:32 am
Contact:

Re: Correct way to play sounds?

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 103 guests