I think the most accurate way would be to set the Sync rate to 0, and also measure from the start of the program, not between beeps. If you measure between beeps, you magnify any inaccuracies.
I just created a quick test, I can get it accurate to within 0.2% (0.0009 of a second), although most of the time it is accurate to within 0.05% (0.0002 of a second).
Medialess, so no beep:
// Project: Temp
// Created: 2016-08-26
// set window properties
SetWindowTitle( "Temp" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate(0,0)
// BPM Configuration
bpm = 131
timeDiff# = 60.0/bpm
//Image
spr = createSprite(0)
SetSpriteSize(spr,10, 400)
SetSpriteOffset(spr,5,380)
SetSpritePositionByOffset(spr, GetVirtualWidth() / 2, GetVirtualHeight() - 100)
//Tween
tween = CreateTweenSprite(timeDiff#)
lastAngle# = -30
nextAngle# = 30
ResetTimer()
lastBeep# = 0
nextBeep# = 0
do
if timer() >= nextBeep#
if GetTweenSpritePlaying(tween, spr) = 1
StopTweenSprite(tween,spr)
endif
lastBeep# = nextBeep#
nextBeep# = lastBeep# + timeDiff#
lastAngle# = lastAngle# * -1
nextAngle# = nextAngle# * -1
Drift# = (timer() - lastBeep#) / timeDiff# * 100.0
SetTweenSpriteAngle(tween,lastAngle#, nextAngle#, TweenEaseOut2())
PlayTweenSprite(tween,spr,0)
endif
print("BPM : " + str(bpm))
print("Rate : " + str(timeDiff#,4) + "Seconds")
Print("Drift = " + str(Drift#) + "%")
UpdateAllTweens(GetFrameTime())
Sync()
loop
Edit: Slightly easier to visualise
// Project: Temp
// Created: 2016-08-26
// set window properties
SetWindowTitle( "Temp" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate(0,0)
enableclearcolor(0)
// BPM Configuration
bpm = 131
timeDiff# = 60.0/bpm
//background
bg = createSprite(0)
bg2 = createSprite(0)
SetSpriteSize(bg,GetVirtualWidth(), GetVirtualHeight() - 200)
SetSpriteSize(bg2,GetVirtualWidth(), 200)
SetSpritePosition(bg,0,200)
SetSpritePosition(bg2,0,0)
SetSpriteColor(bg,64,64,128,16)
SetSpriteColor(bg2,64,64,128,255)
SetSpriteDepth(bg,20)
//Image
spr = createSprite(0)
SetSpriteSize(spr,10, 400)
SetSpriteOffset(spr,5,380)
SetSpritePositionByOffset(spr, GetVirtualWidth() / 2, GetVirtualHeight() - 100)
//Tween
tween = CreateTweenSprite(timeDiff#)
setTweenSpriteBlue(tween,240,255,TweenEaseOut1())
setTweenSpriteRed(tween,0,255,TweenEaseOut1())
setTweenSpriteGreen(tween,0,255,TweenEaseOut1())
lastAngle# = -30
nextAngle# = 30
ResetTimer()
lastBeep# = 0
nextBeep# = 0
do
if timer() >= nextBeep#
if GetTweenSpritePlaying(tween, spr) = 1
StopTweenSprite(tween,spr)
endif
lastBeep# = nextBeep#
nextBeep# = lastBeep# + timeDiff#
lastAngle# = lastAngle# * -1
nextAngle# = nextAngle# * -1
Drift# = (timer() - lastBeep#) / timeDiff# * 100.0
SetTweenSpriteAngle(tween,lastAngle#, nextAngle#, TweenEaseIn1())
PlayTweenSprite(tween,spr,0)
endif
print("BPM : " + str(bpm))
print("Rate : " + str(timeDiff#,4) + "Seconds")
Print("Drift = " + str(Drift#) + "%")
UpdateAllTweens(GetFrameTime())
Sync()
loop
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt