Sync to sound

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

Sync to sound

Post by NÖÖB »

New, much improved version in post #6 :)

Hi! Here's a demo of syncing pictures to sound; using source:tell("samples"), I'm able to tell if the currently playing audio track is at this and that position, and then show pictures when at that position. This demo is hardcoded to 120 bpm. It's a mess, and it eventually goofs up when the audio loops a number of times.
Let me know if it works on your computer :) It could need some more error handling, such as if dragging the window around the time the audio is about to loop (64 counts), the routine will not work again. And if anyone know why it sometimes messes up when the audio loops, please share it here -- hopefully someone can use this concept and make it more usable..
[edit] changing this line "if position <= 1024 and position < 1409024 then looped = 0 end" to "if position <= 11008 and position < 1409024 then looped = 0 end" makes the looping more stable.
Last edited by NÖÖB on Sun Jul 08, 2012 10:18 pm, edited 2 times in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Sync to sound

Post by Robin »

That's the same as if position < 1409024 then looped = 0 end, unless you meant >= instead of <=.
Help us help you: attach a .love.
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

Re: Sync to sound

Post by NÖÖB »

Wow.. hehe :P

if position <= 11008 should be enough..
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Sync to sound

Post by Robin »

By the way, I see now I was wrong. It's an and, not an or. So the first one means if position <= 1024 then ... and the other if position <= 11008 then ....
Help us help you: attach a .love.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Sync to sound

Post by coffee »

This is nice. I think the command idea is good and even useful. However the concept lost himself because the track have always same tone and bpm. Another good demo would be track sudden change mood to a slow thing and you issue a command to do something different.
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

Re: Sync to sound

Post by NÖÖB »

New version :) do download the .love, as the code depends on the audio file inside.

Code: Select all

-- Synchronizing graphics to music.
-- --------------------------------------
--	In order for this to work, the music you're using has to be a perfect loop, with constant intervals between beats, and no silence at the beginning or the end of the music.
-- You have to know a  number of things about the music: In this example, there are 352800 samples; 4 beats per bar (or measure), and 4 bars (4/4 time signature) at 120 bpm.
-- So, to sync at every beat: 4 beats * 4 bars = 16 beats. 352800 samples / 16 beats = 22050 samples.
-- With the supplied music, use 22050 samples in the ssyncUpdate function call to sync every quarter note. Divide by 32 and use 11025 samples to sync every eight note.

function love.load()
	ch0 = love.audio.newSource("120.ogg", "stream")			ch0:setLooping(true)
	love.audio.play(ch0)

	image1 = love.graphics.newImage("skel1.png")				image1:setFilter("nearest", "nearest")	
	image2 = love.graphics.newImage("skel2.png")				image2:setFilter("nearest", "nearest")
	imageCur = image1
end

function love.update(dt)
	ssyncUpdate(ch0, 22050)
	if ssyncPos % 2 == 0 then imageCur = image1 else imageCur = image2 end
end

function love.draw()
	love.graphics.print("ssyncSample: " .. ssyncSample, 10, 10)
	love.graphics.print("ssyncPos: " .. ssyncPos, 160, 10)
	love.graphics.draw(imageCur, 336, 236, 0, 4, 4)
end

function ssyncUpdate(ssyncChannel, ssyncSpeed)
	ssyncSample = ssyncChannel:tell("samples")
	ssyncPos = math.floor(ssyncSample / ssyncSpeed)
end
coffee wrote:This is nice. I think the command idea is good and even useful. However the concept lost himself because the track have always same tone and bpm. Another good demo would be track sudden change mood to a slow thing and you issue a command to do something different.
If you know that the track changes tempo from, say 120bpm to 150bpm at sample 705600, then in this new version you'd just change ssyncUpdate(ch0, 22050) to ssyncUpdate(ch0, 17640) when you reach that sample. Well, it's a little more complicated than that; you'd have to have a script for that one piece of music, but I guess it's doable.
Attachments
ssync.love
(9.78 KiB) Downloaded 211 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 47 guests