Playing sounds makes LÖVE crash? :(

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.
Post Reply
User avatar
Nephilim
Prole
Posts: 9
Joined: Tue Feb 16, 2010 3:00 pm
Location: Netherlands

Playing sounds makes LÖVE crash? :(

Post by Nephilim »

First of all: Hello everyone! I'm new to LÖVE and this forum. I plan on using LÖVE for a little hobby project I started. I want to build a voice controlled system that will (eventually...) automate as much of my home as possible. There are commercial programs that will allow you to do this, but I want my system to have a name, a face and a voice. I was inspired by JARVIS from the movie Iron Man.
I plan on using LÖVE for the main logic, graphics and audio.

Now, I have some programming experience but it's pretty limited. Especially since it's been years since I've done programming of any sort. I started experimenting with LÖVE and yesterday ran into my first real obstacle and I hope you can tell me what I'm doing wrong.

I 'stole' some code from an example I found on this forum that (amongst other things) handles keypresses. The unmodified program works perfectly. When I add the following code to the "if key=="X" then" part:

Code: Select all

sfx = love.audio.newSource("sfx.wav", "static")
love.audio.play(sfx)
The program initially works exactly the same and plays the sound when I push the key. Nice.
However, after a few times LÖVE completely crashes. I tried taking the first line out of the keypress event, but that only made things worse. What am I doing wrong?

I'd love to hear your opinions.

I found the code I use to play the sounds here: http://love2d.org/wiki/love.audio.newSource
Programming is an art form that fights back.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Playing sounds makes LÖVE crash? :(

Post by Robin »

Try placing that first line inside love.load. IIRC, every time you call newSource, the file is loaded and decoded. I can imagine that causing trouble if done too frequently.
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Playing sounds makes LÖVE crash? :(

Post by TechnoCat »

Try something like this if you only want to play the sound after it is done playing itself.

Code: Select all

function love.load()
   sfx = love.audio.newSource("sfx.wav", "static")
end
function love.keypressed(k)
   if k==" " and sfx:isStopped() then
      love.audio.play(sfx)
   end
end
http://love2d.org/wiki/Source:isStopped
User avatar
Nephilim
Prole
Posts: 9
Joined: Tue Feb 16, 2010 3:00 pm
Location: Netherlands

Re: Playing sounds makes LÖVE crash? :(

Post by Nephilim »

Ok, I tried it with the following code now:

Code: Select all

function love.load()
   soundone = love.audio.newSource("pingon.wav", "static")
   soundtwo = love.audio.newSource("pingoff.wav", "static")
end

function love.draw()
    love.graphics.print("Hello World", 400, 300)
end

function love.keypressed(k)
   if k=="q" and soundone:isStopped() then
      love.audio.play(soundone)
   end
   if k=="w" and soundtwo:isStopped() then
      love.audio.play(soundtwo)
   end
end
The program's not crashing anymore, which is really good. So thanks for the tip!

BUT: It takes a long time before the keypress plays the sound again. The sample is really short and it takes (I'm guessing) about 4 times the length of the sample before it plays again.

Any thoughts?
Programming is an art form that fights back.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Playing sounds makes LÖVE crash? :(

Post by TechnoCat »

Nephilim wrote:BUT: It takes a long time before the keypress plays the sound again. The sample is really short and it takes (I'm guessing) about 4 times the length of the sample before it plays again.

Any thoughts?
I believe that to be a problem with the wav decoder. Try converting it to ogg or mp3.
User avatar
Nephilim
Prole
Posts: 9
Joined: Tue Feb 16, 2010 3:00 pm
Location: Netherlands

Re: Playing sounds makes LÖVE crash? :(

Post by Nephilim »

Converting the WAV files to MP3 did the trick! Many thanks!
Programming is an art form that fights back.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests