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.