Multiple audio.newSource of same fileI

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.
Post Reply
domo
Prole
Posts: 1
Joined: Fri Mar 28, 2025 1:07 pm

Multiple audio.newSource of same fileI

Post by domo »

I usually load assets into memory just once and reuse them. However, I now have multiple instances that need to play the same sound simultaneously. The problem is that the sound can only be played once at a time. How can I handle this?

It seems that I need to create multiple sources of the same file, like love.audio.newSource('assets/break.ogg', 'static'). But is this memory-efficient? Does love2d handle this automatically, or does it just load the file into memory again and again?

Currently, a sound source in love2d can only play once at a time. If you call play multiple times, it only plays once and then again after the playback is complete. To restart the sound, you need to stop and then play it again, which means that you can never have more than one instance of the sound playing at the same time. Should I create a function that spawns a new Source and plays it?

Is this the way to go in love2d? If so, does the same apply to newImage and other assets? Does love2d manage this efficiently, or does calling newSource or newImage repeatedly load the file from disk every time rather than using the data already in memory? And when does it free it?
User avatar
BrotSagtMist
Party member
Posts: 680
Joined: Fri Aug 06, 2021 10:30 pm

Re: Multiple audio.newSource of same fileI

Post by BrotSagtMist »

Uff for example for footsteps i am using
step:clone():play()
Which indeed reads like this will end in a memory problem, but it never happened so its probably fine (i hope)
obey
User avatar
marclurr
Party member
Posts: 164
Joined: Fri Apr 22, 2022 9:25 am

Re: Multiple audio.newSource of same fileI

Post by marclurr »

I've also used the clone method in the past. The clones will get garbage collected as long as there are no long lived references to them and they reference the data in the original source so data duplication is fairly low. You can also release the clones manually but to be fair I think this isn't that useful as they don't tie up many resources and you still need to wait for the gc to decide it's time for them to go.

In short, I think it's generally fine to create clones on demand and just let the gc clean up after you, but you do have the option of pooling the sources of you want.
User avatar
zorg
Party member
Posts: 3477
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multiple audio.newSource of same fileI

Post by zorg »

For one of your latter questions, this is specific to audio source objects; Images don't exactly have issues with you drawing multiple of them out, unlike you expecting to be able to play a source multiple times in a way that they would overlap.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
dusoft
Party member
Posts: 765
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Multiple audio.newSource of same fileI

Post by dusoft »

If you need to force garbage collection immediately after sound cloned and played, you can use:

Code: Select all

collectgarbage("collect")
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 9 guests