Question about love.audio.rewind()

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.
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Question about love.audio.rewind()

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

Re: Question about love.audio.rewind()

Post 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
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Question about love.audio.rewind()

Post 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.
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Question about love.audio.rewind()

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

Re: Question about love.audio.rewind()

Post 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.
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Question about love.audio.rewind()

Post 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 :)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Question about love.audio.rewind()

Post by bartbes »

It's probably the GC.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Question about love.audio.rewind()

Post 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.
Hello, I am not dead.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Question about love.audio.rewind()

Post by bartbes »

What? NO! Just code something that does work!
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Question about love.audio.rewind()

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests