Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

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?

nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

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

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

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.

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

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.

nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

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.

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

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

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.

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

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

It's probably the GC.

zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

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.

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

What? NO! Just code something that does work!

nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact: