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?
Multiple audio.newSource of same fileI
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- BrotSagtMist
- Party member
- Posts: 680
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Multiple audio.newSource of same fileI
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)
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
Re: Multiple audio.newSource of same fileI
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.
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.
- zorg
- Party member
- Posts: 3477
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Multiple audio.newSource of same fileI
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
True 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.

Re: Multiple audio.newSource of same fileI
If you need to force garbage collection immediately after sound cloned and played, you can use:
Code: Select all
collectgarbage("collect")

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