Page 1 of 2

how to play one sound multiple times

Posted: Sat Jan 28, 2012 1:25 pm
by gfreak
i have an sound (1.wav)
and i want to play it every time i press "s"

easy:

Code: Select all

s = love.audio.newSource( '1.wav', 'static' )

function love.keypressed(k)
   if k == "s"
      love.audio.stop(s)
      love.audio.play(s)
end
but i dont want the stop and i ask my self is there is a better way then:

Code: Select all

function love.keypressed(k)
   if k == "s"
			local s= love.audio.newSource("1.wav", "static")
			love.audio.play(s)
end
i dont want to load the same data over and over again (feels stupid)

so will working with love.sound.newSoundData fix my problem?

Re: how to play one sound multiple times

Posted: Sat Jan 28, 2012 3:00 pm
by tentus
The first way is the right way to do it.

Re: how to play one sound multiple times

Posted: Sat Jan 28, 2012 5:03 pm
by gfreak
tentus wrote:The first way is the right way to do it.
if i want it yes

but i want to have multiple "1.wav"s running
i want if the engine caches it or it will be loaded multiple times and if love.sound.newSoundData is the right solution

Re: how to play one sound multiple times

Posted: Sat Jan 28, 2012 5:09 pm
by Robin
This should probably work right:

Code: Select all

function newsource()
    return love.audio.newSource( '1.wav', 'static' )
end

sources = {}

function playsource()
      for i, s in ipairs(sources) do
         if s:isStopped() then
            love.audio.play(s)
            return
         end
       end
       table.insert(sources, newsource())
       love.audio.play(sources[#sources])
end

function love.keypressed(k)
   if k == "s" then
      playsource()
   end 
end
It only creates a new source if there is no stopped source available.

Re: how to play one sound multiple times

Posted: Sat Jan 28, 2012 5:32 pm
by Taehl
Ensayia and I created TEsound to make things like this painless. It also makes things like playing random sounds, looping music, and volume control easier.

Re: how to play one sound multiple times

Posted: Sat Jan 28, 2012 5:47 pm
by kikito
There is also slam.

Re: how to play one sound multiple times

Posted: Sat Jan 28, 2012 6:18 pm
by gfreak
kikito wrote:There is also slam.
thanks!

but:

will multiple plays lead to multiple love.audio.newSource => more ram or even worse: more I/O by reading the same file more than once ?

Re: how to play one sound multiple times

Posted: Sat Jan 28, 2012 8:22 pm
by nevon
For sounds, you should load them once as sounddata, and then use that sounddata to create a new source when you need to play it.

Re: how to play one sound multiple times

Posted: Sun Jan 29, 2012 2:25 pm
by gfreak
nevon wrote:For sounds, you should load them once as sounddata, and then use that sounddata to create a new source when you need to play it.
thanks that what was what i want to hear

i try to patch slam for this (only for static ones of course)

Re: how to play one sound multiple times

Posted: Mon Jan 30, 2012 11:11 am
by vrld
gfreak wrote:
nevon wrote:For sounds, you should load them once as sounddata, and then use that sounddata to create a new source when you need to play it.
i try to patch slam for this (only for static ones of course)
Slam already does this for you. In fact, this is kind of slam's point ;)