Page 1 of 2

Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 11:55 am
by ninwa
Hey all, I'm working on my my first love2d game and I've run into a small problem with love.audio. I'm trying to implement an enenyOnHit noise and so in my love.load I initialized the sound effect:

Code: Select all

snd_e_onhit = love.audio.newSource("e_onhit.wav")
And when an enemy is hit I do:

Code: Select all

love.audio.play(snd_e_onhit)
The problem I ran into at first was that this would only successfully play once every couple of seconds or so. I noticed on the wiki there was a love.audio.rewind(). Since I'm streaming the sound files I assumed that I may have to rewind the source to place the "stream pointer" back at the front manually faster than Love seemed to do it on its own. So I did:

Code: Select all

love.audio.rewind(snd_e_onhit)
This worked, however it also rewinds my background music and any other audio sources for that matter.

What am I doing wrong?

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:13 pm
by nevon
ninwa wrote:What am I doing wrong?
Think of sources as... Well, sources. A trumpet, for example. A trumpet can only make one sound at a time. You can't have one trumpeteer (I may just have made that word up) play two notes at the same time. The same goes for sound sources. Once you've played a sound source, you can't play it again until it has finished playing the first time.

What you do instead is to initialize your sound file as a sounddata object, and every time it's supposed to be played, you create a new source from that sounddata. Once the source has finished playing, you remove it.

One way to handle this is by writing a soundmanager that keeps track of all your sources and removes the ones that aren't needed anymore. You can view an example here: http://github.com/LOVE-Party/Love-LD18/ ... anager.lua

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:21 pm
by ninwa
nevon wrote:
ninwa wrote:What am I doing wrong?
Think of sources as... Well, sources. A trumpet, for example. A trumpet can only make one sound at a time. You can't have one trumpeteer (I may just have made that word up) play two notes at the same time. The same goes for sound sources. Once you've played a sound source, you can't play it again until it has finished playing the first time.

What you do instead is to initialize your sound file as a sounddata object, and every time it's supposed to be played, you create a new source from that sounddata. Once the source has finished playing, you remove it.

One way to handle this is by writing a soundmanager that keeps track of all your sources and removes the ones that aren't needed anymore. You can view an example here: http://github.com/LOVE-Party/Love-LD18/ ... anager.lua
Ah, thank you very much Nevon.

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:26 pm
by ninwa
My simple "for now" solution:

Code: Select all

function playSFX(file)
    local sfx = love.audio.newSource(file)
    love.audio.play(sfx)
end
Note: I only use this for one-off sound effects, not for the background music.

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:28 pm
by nevon
ninwa wrote:My simple "for now" solution:

Code: Select all

function playSFX(file)
    local sfx = love.audio.newSource(file)
    love.audio.play(sfx)
end
Note: I only use this for one-off sound effects, not for the background music.
Bad, bad idea! What will happen once you've played 1000 sounds?

EDIT: Hmm, or maybe the garbage collector will take care of it. I'm not entirely sure.

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:31 pm
by ninwa
nevon wrote:
ninwa wrote:My simple "for now" solution:

Code: Select all

function playSFX(file)
    local sfx = love.audio.newSource(file)
    love.audio.play(sfx)
end
Note: I only use this for one-off sound effects, not for the background music.
Bad, bad idea! What will happen once you've played 1000 sounds?

EDIT: Hmm, or maybe the garbage collector will take care of it. I'm not entirely sure.
GC grabbing it was my assumption as well, but after playtesting and having it crashing on me after putting this in where it's never crashed before, I'm curious wtf happened, haha. I'll sort it out :)

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:37 pm
by bartbes
It's probably the GC.

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:41 pm
by zac352
bartbes wrote:It's probably the GC.
Maybe,

Code: Select all

getmetatable(source).__gc=0
? I don't have much experience with the garbage collector... There seems like a lack of documentation.

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:50 pm
by bartbes
What? NO! Just code something that does work!

Re: Question about love.audio.rewind()

Posted: Tue Oct 12, 2010 12:59 pm
by nevon
Not that I know what that piece of code does, but I suspect it turns off the garbage collector - which certainly wouldn't be a solution.

The reason the game crashed now, I suspect, is because your sound source is being cleaned up by the garbage collector after your function has been run - before it has stopped playing. To fix it, set up a sound queue, add every sound to that, remove the sounds once they've been played. Pretty simple.