Created
January 7, 2020 09:28
-
-
Save fluffy-critter/2de489091a05f6a3746bef44e46e3025 to your computer and use it in GitHub Desktop.
Audio sync test for LÖVE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ Just an audio sync test I wrote for | |
https://bitbucket.org/rude/love/issues/1418/audio-source-tell-gets-out-of-sync-for | |
Issue has been long-since fixed but it might be useful to keep around for other testing purposes later? | |
]] | |
local stream, static | |
function love.load() | |
stream = love.audio.newSource('01-little-bouncing-ball.mp3', 'stream') | |
static = love.audio.newSource('01-little-bouncing-ball.mp3', 'static') | |
stream:setVolume(0.5) | |
static:setVolume(0.5) | |
stream:play() | |
static:play() | |
end | |
function getpos(player) | |
-- 132BPM = 132/60 beats per second | |
local time = player:tell()*132/60 | |
return math.floor(time/4), math.floor(time % 4), time % 1 | |
end | |
function love.draw() | |
local measure, beat, ofs = getpos(stream) | |
local brt = math.pow(1 - ofs, 3) | |
love.graphics.clear(0, 0, brt) | |
love.graphics.setColor(1, 1, 1) | |
love.graphics.print(string.format('%4d:%d:%f drift=%f', measure, beat, ofs, stream:tell() - static:tell())) | |
love.graphics.setColor(0, 0, 1) | |
drawBall(getpos(static)) | |
love.graphics.setColor(1, 1, 0) | |
drawBall(measure, beat, ofs) | |
end | |
function drawBall(measure, beat, ofs) | |
local w = love.graphics.getWidth() | |
local h = love.graphics.getHeight() | |
local x = ofs*w/2 | |
if beat % 2 == 1 then | |
x = w*3/4 - x | |
else | |
x = w/4 + x | |
end | |
local y = (1 + 3*(ofs - 1)*ofs)*h - 16 | |
love.graphics.circle("fill", x, y, 16) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment