how to play one sound multiple times

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
gfreak
Prole
Posts: 29
Joined: Wed Jan 04, 2012 5:32 pm

how to play one sound multiple times

Post 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?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: how to play one sound multiple times

Post by tentus »

The first way is the right way to do it.
Kurosuke needs beta testers
gfreak
Prole
Posts: 29
Joined: Wed Jan 04, 2012 5:32 pm

Re: how to play one sound multiple times

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

Re: how to play one sound multiple times

Post 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.
Help us help you: attach a .love.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: how to play one sound multiple times

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: how to play one sound multiple times

Post by kikito »

There is also slam.
When I write def I mean function.
gfreak
Prole
Posts: 29
Joined: Wed Jan 04, 2012 5:32 pm

Re: how to play one sound multiple times

Post 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 ?
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: how to play one sound multiple times

Post 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.
gfreak
Prole
Posts: 29
Joined: Wed Jan 04, 2012 5:32 pm

Re: how to play one sound multiple times

Post 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)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: how to play one sound multiple times

Post 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 ;)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests