com.threerings.media.timer
Class CalibratingTimer

java.lang.Object
  extended by com.threerings.media.timer.CalibratingTimer
All Implemented Interfaces:
MediaTimer
Direct Known Subclasses:
NanoTimer, PerfTimer

public abstract class CalibratingTimer
extends Object
implements MediaTimer

Calibrates timing values from a subclass' implementation of current against those returned by System.currentTimeMillis. If the subclass timer is moving 1.25 times faster or .75 times slower than currentTimeMillis, hold it to the values from currentTimeMillis.


Field Summary
protected  Interval _calibrateInterval
          Interval that fires every five seconds to run the calibration.
protected static RuntimeAdjust.BooleanAdjust _debugCalibrate
          A debug hook that toggles dumping of calibration values.
protected  long _driftMilliStamp
          currentTimeMillis() value from the last time we called calibrate.
protected  float _driftRatio
          Ratio of currentTimeMillis to timer millis.
protected  long _driftTimerStamp
          current() value from the last time we called calibrate.
protected  float _maxDriftRatio
          The largest drift we've had to adjust for.
protected  long _microDivider
          Amount by which current() should be divided to get microseconds.
protected  long _milliDivider
          Amount by which current() should be divided to get milliseconds.
protected  long _priorCurrent
          current() value when elapsed was called last.
protected  long _startStamp
          current() value when the timer was started.
protected static int CALIBRATE_INTERVAL
          Milliseconds between calibrate calls.
protected  Logger log
          Used to log things.
protected static float MAX_ALLOWED_DRIFT_RATIO
          The largest drift ratio we'll allow without correcting.
protected static float MIN_ALLOWED_DRIFT_RATIO
          The smallest drift ratio we'll allow without correcting.
 
Constructor Summary
CalibratingTimer()
           
 
Method Summary
protected  void calibrate()
          Calculates the drift factor from the time elapsed from the last calibrate call.
 void clearMaxDriftRatio()
          Clears out our remembered max drift.
abstract  long current()
          Return the current value for this timer.
protected  long elapsed()
          Returns the difference between _startStamp and current()
 long getElapsedMicros()
          Returns the number of microseconds that have elapsed since the timer was created or last MediaTimer.reset().
 long getElapsedMillis()
          Returns the number of milliseconds that have elapsed since the timer was created or last MediaTimer.reset().
 float getMaxDriftRatio()
          Returns the greatest drift ratio we've had to compensate for.
protected  void init(long milliDivider, long microDivider)
          Initializes this timer.
 void reset()
          Resets the timer's monotonically increasing value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_startStamp

protected long _startStamp
current() value when the timer was started.


_priorCurrent

protected long _priorCurrent
current() value when elapsed was called last.


_milliDivider

protected long _milliDivider
Amount by which current() should be divided to get milliseconds.


_microDivider

protected long _microDivider
Amount by which current() should be divided to get microseconds.


_driftMilliStamp

protected long _driftMilliStamp
currentTimeMillis() value from the last time we called calibrate.


_driftTimerStamp

protected long _driftTimerStamp
current() value from the last time we called calibrate.


_driftRatio

protected float _driftRatio
Ratio of currentTimeMillis to timer millis.


_maxDriftRatio

protected float _maxDriftRatio
The largest drift we've had to adjust for.


_calibrateInterval

protected Interval _calibrateInterval
Interval that fires every five seconds to run the calibration.


log

protected final Logger log
Used to log things.


MAX_ALLOWED_DRIFT_RATIO

protected static final float MAX_ALLOWED_DRIFT_RATIO
The largest drift ratio we'll allow without correcting.

See Also:
Constant Field Values

MIN_ALLOWED_DRIFT_RATIO

protected static final float MIN_ALLOWED_DRIFT_RATIO
The smallest drift ratio we'll allow without correcting.

See Also:
Constant Field Values

CALIBRATE_INTERVAL

protected static final int CALIBRATE_INTERVAL
Milliseconds between calibrate calls.

See Also:
Constant Field Values

_debugCalibrate

protected static RuntimeAdjust.BooleanAdjust _debugCalibrate
A debug hook that toggles dumping of calibration values.

Constructor Detail

CalibratingTimer

public CalibratingTimer()
Method Detail

init

protected void init(long milliDivider,
                    long microDivider)
Initializes this timer. Must be called before the timer is used.

Parameters:
milliDivider - - value by which current() must be divided to get milliseconds
microDivider - - value by which current() must be divided to get microseconds

getElapsedMicros

public long getElapsedMicros()
Description copied from interface: MediaTimer
Returns the number of microseconds that have elapsed since the timer was created or last MediaTimer.reset(). Note: the accuracy of this method is highly dependent on the timer implementation used.

Specified by:
getElapsedMicros in interface MediaTimer

getElapsedMillis

public long getElapsedMillis()
Description copied from interface: MediaTimer
Returns the number of milliseconds that have elapsed since the timer was created or last MediaTimer.reset(). Note: the accuracy of this method is highly dependent on the timer implementation used.

Specified by:
getElapsedMillis in interface MediaTimer

reset

public void reset()
Description copied from interface: MediaTimer
Resets the timer's monotonically increasing value.

Specified by:
reset in interface MediaTimer

current

public abstract long current()
Return the current value for this timer.


getMaxDriftRatio

public float getMaxDriftRatio()
Returns the greatest drift ratio we've had to compensate for.


clearMaxDriftRatio

public void clearMaxDriftRatio()
Clears out our remembered max drift.


elapsed

protected long elapsed()
Returns the difference between _startStamp and current()


calibrate

protected void calibrate()
Calculates the drift factor from the time elapsed from the last calibrate call.