Array of sounds and looping them

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
imnotanerd
Prole
Posts: 6
Joined: Thu Dec 30, 2010 6:55 am
Contact:

Array of sounds and looping them

Post by imnotanerd »

How would i iterate through an array of sounds and loop through them? Here's what I have for audio:

Code: Select all

src1 = {love.audio.newSource("harmoney.mp3"), love.audio.newSource("ghosts-n-stuff.mp3")}

src1:setVolume(1.0)
src1:setLooping(true)

function love.keypressed(key, unicode)
	if key == 'p' then
		src1:pause()
	end
	
	if key == 'r' then
		src1:resume()
	end
	for i = 1, src1.getn(src1) do
		if key == 's' then
			love.audio.play(src1[i])
		end
	end
end
I feel like i'm doing everything wrong. :-/
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Array of sounds and looping them

Post by tentus »

You need to do some rearranging. Also, I'd avoid src1, since it implies that it is which is isn't. It's not the first source, it all the sources, so I renamed it in the code below.

Code: Select all

function love.load()
	sources = {
		love.audio.newSource("harmoney.mp3"),
		love.audio.newSource("ghosts-n-stuff.mp3")
	}
	for i=1, #sources do
		sources[i]:setVolume(1.0)
		sources[i]:setLooping(true)
		love.audio.play(sources[i])
	end
end

function love.keypressed(key, unicode)
	if key == 'p' then
		for i=1, #sources do
			sources[i]:pause()
		end
	end
	if key == 'r' then
		for i=1, #sources do
			sources[i]:resume()
		end
	end
end
Basically, you need to loop through all of your sources and do the gruntwork to each of them individually. A table can't have a volume, but a sound in a table can.
Last edited by tentus on Thu Dec 30, 2010 10:47 pm, edited 2 times in total.
Kurosuke needs beta testers
imnotanerd
Prole
Posts: 6
Joined: Thu Dec 30, 2010 6:55 am
Contact:

Re: Array of sounds and looping them

Post by imnotanerd »

Great! That fixed it, but now everything is running simultaneously. Is there a love.audio method or Source: method that can check to see which source is playing?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Array of sounds and looping them

Post by tentus »

Not that I know of, you'll want to keep another table to tell you that. See what I did below:

Code: Select all

function love.load()
	sources = {
		love.audio.newSource("harmoney.mp3"),
		love.audio.newSource("ghosts-n-stuff.mp3")
	}
	selections = {
		true,
		true
	}
	for i=1, #sources do
		sources[i]:setVolume(1.0)
		sources[i]:setLooping(true)
		love.audio.play(sources[i])
	end
end

function love.draw()
	for i=1, #sources do
		local text = "Source "..i.." is playing"
		if not selections[i] then
			text = "Source "..i.." is not playing"
		end
		love.graphics.print(text, 8, 32 * i)
	end
end

function love.keypressed(key, unicode)
	if key == 'p' then
		for i=1, #sources do
			pauseSource(i)
		end
	end
	if key == 'r' then
		for i=1, #sources do
			resumeSource(i)
		end
	end
	if key == '1' or key == '2' then
		local keyNumber  = tonumber(key)
		if selections[keyNumber] then
			pauseSource(keyNumber)
		else
			resumeSource(keyNumber)
		end
	end
end

function pauseSource(which)
	sources[which]:pause()
	selections[which] = false
end

function resumeSource(which)
	sources[which]:resume()
	selections[which] = true
end
Several things to get from this. One, you can have tables which parallel to each other, so you can reuse key values. Two, it became a good idea to define some functions to do the pausing and resuming. Three, tonumber() is awesome. And four, having a draw function to show us what is happening is also awesome.

If you have any questions about the above code, please ask (what are locals, why does #sources work, etc). I'm not sure how much you know and I don't want to step on any toes, or just hand you solutions that work and you don't know how.
Kurosuke needs beta testers
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Array of sounds and looping them

Post by BlackBulletIV »

Can I ask what that hash (#) before the name of the sources table does? Is it the length of the table?
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Array of sounds and looping them

Post by thelinx »

Yeah.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Array of sounds and looping them

Post by BlackBulletIV »

Thanks.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 69 guests