Page 1 of 3

How to go about audio

Posted: Wed Jan 12, 2011 10:44 am
by BlackBulletIV
I'm a bit confused about how to go about playing audio in Love. I'm told that using love.audio.newSource() shouldn't be called too often, as it loads and decodes the file each time. Now if I want to play a sound each time a collision happens between a bunch of boxes, I can't use one source, as I can't play multiple instances of one source at once.

I don't think I should construct a SoundData object once and then construct multiple sources off that each time a collision occurs, because the wiki says that converting a sound file into raw sound data is expensive.

So my last option is constructing a Decoder once and then constructing multiple sources off that. However it seems that a Decoder is only used as something to decode a sound file, so the decoding would still happen each I call newSource()? Is that correct? The documentation doesn't say anything about the performance implications of this.

Also as a side note, can someone explain to me the position, orientation and velocity functions of the love.audio module? The documentation is a bit sparse on these ones and I'm not really sure how to use it (for example, what on earth is a floatArray?).

Thanks guys!

Re: How to go about audio

Posted: Wed Jan 12, 2011 2:41 pm
by TechnoCat
I'm using setPosition here: https://bitbucket.org/dannyfritz/blind- ... .lua#cl-11
and setOrientation here: https://bitbucket.org/dannyfritz/blind- ... .lua#cl-65

Very simple program to look at. The float arrays are actually just (number, number, number) I believe since all Lua knows are doubles.
The arrays are (x,y,z) because all sound is done in 3D space, not 2D.

Re: How to go about audio

Posted: Wed Jan 12, 2011 3:12 pm
by thelinx
BlackBulletIV wrote: I don't think I should construct a SoundData object once and then construct multiple sources off that each time a collision occurs, because the wiki says that converting a sound file into raw sound data is expensive.
No, that's incorrect. If you create a Source from a SoundData, it'll just use the same SoundData as every other source spawned from it.

Re: How to go about audio

Posted: Wed Jan 12, 2011 8:53 pm
by BlackBulletIV
TechnoCat wrote:I'm using setPosition here: https://bitbucket.org/dannyfritz/blind- ... .lua#cl-11
and setOrientation here: https://bitbucket.org/dannyfritz/blind- ... .lua#cl-65

Very simple program to look at. The float arrays are actually just (number, number, number) I believe since all Lua knows are doubles.
The arrays are (x,y,z) because all sound is done in 3D space, not 2D.
Oh ok. But I'm still not sure how those functions effect the sound (I guess I'll to learn by trial and error).
thelinx wrote:
BlackBulletIV wrote: I don't think I should construct a SoundData object once and then construct multiple sources off that each time a collision occurs, because the wiki says that converting a sound file into raw sound data is expensive.
No, that's incorrect. If you create a Source from a SoundData, it'll just use the same SoundData as every other source spawned from it.
What is incorrect? I realise that they'll share the same SoundData, that's the reason I thought this method may have a little bit of merit in it. Or are you saying that if they all share the same SoundData, I still won't be able to play multiple sounds over each other?

Re: How to go about audio

Posted: Wed Jan 12, 2011 9:36 pm
by nevon
BlackBulletIV wrote:
thelinx wrote:
BlackBulletIV wrote: I don't think I should construct a SoundData object once and then construct multiple sources off that each time a collision occurs, because the wiki says that converting a sound file into raw sound data is expensive.
No, that's incorrect. If you create a Source from a SoundData, it'll just use the same SoundData as every other source spawned from it.
What is incorrect? I realise that they'll share the same SoundData, that's the reason I thought this method may have a little bit of merit in it. Or are you saying that if they all share the same SoundData, I still won't be able to play multiple sounds over each other?
thelinx is generally more knowledgeable than me when it comes to... Most things. But I believe that your approach is the correct one for sounds. At least that's how me and bartbes have been doing it. First you load your sound as sounddata, and when you want to play it, you create a newSource out of it.

Re: How to go about audio

Posted: Wed Jan 12, 2011 11:34 pm
by bartbes
That should mean you're right. :P

I shall explain why: SoundData is already decoded.

Re: How to go about audio

Posted: Thu Jan 13, 2011 12:03 am
by BlackBulletIV
Oh so loading SoundData is not as bad as I thought. Well I'll do that then. I just found out that creating a new source from a file every time a collision occurs is bad news... Love started to freeze, and then crashed. Thanks guys!

Re: How to go about audio

Posted: Thu Jan 13, 2011 6:31 am
by Ensayia
Taehl and I wrote a sound module that works with sound a lot more sensibly than LOVE alone. It works completely with LOVE and requires no extra files or DLLs, just a Lua file and a require in your main file.

Go to http://love2d.org/forums/viewtopic.php?f=5&t=2334 for further information.

Re: How to go about audio

Posted: Thu Jan 13, 2011 7:00 am
by BlackBulletIV
Yeah I've actually already got that and I've been digging around in the source code to see how it's done. One question about it, you seem to be creating a new source off a file every time someone plays a sound, isn't that expensive? I know when I did that for every time I had a physics collision with a velocity greater than 100 or something, Love crashed very quickly.

Re: How to go about audio

Posted: Thu Jan 13, 2011 7:30 am
by Robin
BlackBulletIV wrote:Yeah I've actually already got that and I've been digging around in the source code to see how it's done. One question about it, you seem to be creating a new source off a file every time someone plays a sound, isn't that expensive? I know when I did that for every time I had a physics collision with a velocity greater than 100 or something, Love crashed very quickly.
I don't know about sources being expensive, but if you want to create lots of them, you could try keeping a pool of sources, and taking one out if the sound needs to be played, and putting it back in if the sound is done playing.