Inserting current time from audio

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
1u4
Prole
Posts: 16
Joined: Tue May 01, 2012 6:27 am
Location: Oh no, not Finland!

Inserting current time from audio

Post by 1u4 »

Hi, I'm have a stab at löve-making and was wondering how to display the current time (elapsed) from an audio file via a key press call. Here is my attempt (I'm not even sure if tried to call the right function here) -- The error is about using a nil value so I guess I'm not calling the value correctly.

Code: Select all

function love.keypressed(key)
	if key == "f1" then
		x, y, z = love.audio.getPosition()
text = text .. string.x
text = text .. string.y
text = text .. string.z
		love.audio.pause(audioSource)
		status = "PAUSED"
	end
	if key == "f2" then
		love.audio.resume(audioSource)
		-- NOTE TO FUTURE SELF:
		-- Put resume / pause in array. Call array index, then index
		-- switch so if key is pressed again, result is different. 
		status = "RESUMED"
	end
	if key == "escape" then -- quit is always the last check
		love.event.push("quit") 
	end
end

function love.load()
	audioSource = love.audio.newSource("audioFile.mp3")
	love.audio.play(audioSource) 
	text = ">: "
	status = "AUTO-STARTED"
end

function love.draw()
    love.graphics.printf(text, 0, 0, 800)
    love.graphics.printf(status, 100, 100, 800)
end
About the api -- Löve is quite cool, but I wish it has some short example underneath each of the functions so it's clear how to use and call each correctly.

Thanks.
There is a story of a farmer whose horse ran away. That evening the neighbors gathered to commiserate with him since this was such bad luck.
He said, "May be." Continued...
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Inserting current time from audio

Post by bartbes »

You're looking for Source:tell. As to why your code errors:
1u4 wrote:

Code: Select all

--snip
		x, y, z = love.audio.getPosition()
text = text .. string.x
text = text .. string.y
text = text .. string.z
--snip
You assign the result of getPosition to (global) x, y and z, but then try to use it as string.x, string.y and string.z, that won't work. You could directly append x, y and z, or if you really want to make sure it's converted to a string, use tostring(x) (same for y and z, of course). In any case, getPosition isn't the right function.
User avatar
1u4
Prole
Posts: 16
Joined: Tue May 01, 2012 6:27 am
Location: Oh no, not Finland!

Re: Inserting current time from audio

Post by 1u4 »

Thanks. I'm pretty new to lua and brand new to löve, but it's really hard to abstain until my lua skills have come of age.
Here's what I did with your help. It works nice :)

Code: Select all

function love.keypressed(key)
	if key == "f1" then
		love.audio.pause(audioSource)
		position = audioSource:tell( "seconds" )
		status = "PAUSED"
	end
	if key == "f2" then
		love.audio.resume(audioSource)
		-- NOTE TO FUTURE SELF:
		-- Put resume / pause in array. Call array index, then index
		-- switch so if key is pressed again, result is different. 
		status = "RESUMED"
	end
	if key == "escape" then -- quit is always the last check
		love.event.push("quit") 
	end
end

function love.load()
	audioSource = love.audio.newSource("audioFile.mp3")
	love.audio.play(audioSource) 
	text = "Last paused at: "
	status = "AUTO-STARTED"
end

function love.draw()
    love.graphics.printf(text .. tostring(position), 10, 0, 800)
    love.graphics.printf(status, 10, 25, 800)
end
There is a story of a farmer whose horse ran away. That evening the neighbors gathered to commiserate with him since this was such bad luck.
He said, "May be." Continued...
User avatar
1u4
Prole
Posts: 16
Joined: Tue May 01, 2012 6:27 am
Location: Oh no, not Finland!

Re: Inserting current time from audio

Post by 1u4 »

I'm trying to swap a table index like so...

Code: Select all

playPause[1], playPause[2] = playPause[2], playPause[1] -- correct?
...but the behaviour is not correct in my app...

Code: Select all

	
if key == "f2" then
	playPause = {love.audio.pause(audioSource), love.audio.resume(audioSource)}
	controlPress = playPause[1] -- execute the index function
	playPause[1], playPause[2] = playPause[2], playPause[1] -- swap table index
	status = "PLAY/PAUSE"
end
Is it my löve code or my index switch code?

Thanks.
There is a story of a farmer whose horse ran away. That evening the neighbors gathered to commiserate with him since this was such bad luck.
He said, "May be." Continued...
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Inserting current time from audio

Post by Nixola »

Code: Select all

playPause = {love.audio.pause(audioSource), love.audio.resume(audioSource)}
With this you first pause the source, store the returned value (none) in playPause[1], then resume the source and store the returned value (none) in playPause[2], so it's the LÖVE code
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
1u4
Prole
Posts: 16
Joined: Tue May 01, 2012 6:27 am
Location: Oh no, not Finland!

Re: Inserting current time from audio

Post by 1u4 »

Ah, I see :crazy: -- Thanks. I also realised I shouldn't have my playPause table there either. I changed it to...

Code: Select all

	
if key == "f2" then
	if playPause[1] == "pause" then
		love.audio.pause(audioSource)
	else
		love.audio.resume(audioSource)	
	end	
	playPause[1], playPause[2] = playPause[2], playPause[1] -- swap table index
	status = "PLAY/PAUSE"
end
...moving playPause = {"pause", "play"} to the load() area.
There is a story of a farmer whose horse ran away. That evening the neighbors gathered to commiserate with him since this was such bad luck.
He said, "May be." Continued...
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Inserting current time from audio

Post by Nixola »

You could use booleans, it would be like this:

Code: Select all

--in love.load()
playing = true
--in Keypressed
if key == 'f2' then
    if playing then -- playing is true, so this is a valid condition and the chunk below will be executed
        love.audio.pause(Source)
    elseif not playing then --the elseif is useless, for if play is not true it is surely false
        love.audio.resume(Source)
    end
    playing = not playing --if playing is true it becomes false, if it's false it becomes true: 'not' changes a variable between true and false
end
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests