From 017a81072fddd8f8838a0633d351bd36d74cdde2 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Thu, 6 Mar 2008 21:51:04 +0000 Subject: [PATCH] Rollback yesterday's commit to use a calibration interval since we're actually pretty good about only having the ticker call getElapsed. It actually appears that the problems arise from the time between calibrations being influenced by timer time. Since we're explicitly trying to work around someone jacking their timer to several times normal speed, relying on it to decide when to calibrate is a bad idea as it can lead to tiny, quick calibrations with varying numbers of underlying millis. Instead, schedule calibrations with an Interval on the AWT thread since that will be run based on System.currentTimeMillis and hope the third time's the charm. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@435 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../threerings/media/timer/CalibratingTimer.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/java/com/threerings/media/timer/CalibratingTimer.java b/src/java/com/threerings/media/timer/CalibratingTimer.java index 1325bfb0..5f17a5fc 100644 --- a/src/java/com/threerings/media/timer/CalibratingTimer.java +++ b/src/java/com/threerings/media/timer/CalibratingTimer.java @@ -1,6 +1,8 @@ package com.threerings.media.timer; import com.samskivert.Log; +import com.samskivert.util.Interval; +import com.samskivert.util.RunQueue; /** * Calibrates timing values from a subclass' implementation of current against those returned by @@ -19,11 +21,14 @@ public abstract class CalibratingTimer protected void init (long milliDivider, long microDivider) { _milliDivider = milliDivider; - _calibrationInterval = 5 * 1000 * _milliDivider;// Calibrate every 5 seconds _microDivider = microDivider; reset(); Log.info("Using " + getClass() + " timer [mfreq=" + _milliDivider + ", ufreq=" + _microDivider + ", start=" + _startStamp + "]."); + new Interval(RunQueue.AWT) { + @Override public void expired () { + calibrate(); + }}.schedule(5000, true); } // documentation inherited from interface @@ -50,9 +55,6 @@ public abstract class CalibratingTimer protected long elapsed () { long current = current(); - if ((current - _driftTimerStamp) > _calibrationInterval) { - calibrate(); - } if (_driftRatio != 1.0) { long elapsed = current - _priorCurrent; _startStamp += (elapsed - (elapsed * _driftRatio)); @@ -64,7 +66,7 @@ public abstract class CalibratingTimer /** Calculates the drift factor from the time elapsed from the last calibrate call. */ protected void calibrate () { - long elapsedTimer = (current() - _driftTimerStamp); + double elapsedTimer = (current() - _driftTimerStamp); double elapsedMillis = System.currentTimeMillis() - _driftMilliStamp; double drift = elapsedMillis / (elapsedTimer / _milliDivider); if (drift > 1.25 || drift < 0.75) { @@ -102,8 +104,4 @@ public abstract class CalibratingTimer /** Ratio of currentTimeMillis to timer millis. */ protected double _driftRatio = 1.0; - - /** Amount of timer ticks that elapse between calibrations. */ - protected long _calibrationInterval; - }