1 package tim.prune.function.autoplay;
4 * Class to hold a list of points and hold a running position
8 /** Array of milliseconds for each point */
9 private long[] _millis = null;
10 /** Array of indexes of corresponding points */
11 private int[] _indexes = null;
12 /** Array index of current position */
13 private int _currentItem = 0;
14 /** Max array index */
15 private int _maxItem = 0;
19 * @param inNumPoints number of points
21 public PointList(int inNumPoints)
23 _millis = new long[inNumPoints];
24 _indexes = new int[inNumPoints];
26 _maxItem = inNumPoints - 1;
30 * Add a point to the array
31 * @param inMillis milliseconds since start
32 * @param inIndex point index
34 public void setPoint(long inMillis, int inIndex)
36 _millis[_currentItem] = inMillis;
37 _indexes[_currentItem] = inIndex;
42 * Set the position using the current milliseconds
43 * @param inMillis milliseconds since start
45 public void set(long inMillis)
47 if (isFinished() || inMillis < _millis[_currentItem])
52 while (_currentItem < _maxItem && _millis[_currentItem + 1] < inMillis)
59 * Normalize the list to cover the requested number of seconds duration
60 * @param inSeconds length of autoplay sequence in seconds
62 public void normalize(int inSeconds)
66 return; // nothing to normalize
68 long currentDuration = _millis[_maxItem] - _millis[0];
69 if (currentDuration > 0L)
71 double multFactor = inSeconds * 1000.0 / currentDuration;
72 for (int i=0; i<=_maxItem; i++)
74 _millis[i] = (long) (_millis[i] * multFactor);
79 /** @return the milliseconds of the current point */
80 public long getCurrentMilliseconds()
82 if (isAtStart() || isFinished()) {
85 return _millis[_currentItem];
88 /** @return the index of the current point */
89 public int getCurrentPointIndex()
91 return _indexes[_currentItem];
94 /** @return true if we're on the first point */
95 public boolean isAtStart() {
96 return _currentItem == 0;
99 /** @return true if we're on the last point */
100 public boolean isFinished() {
101 return _currentItem >= _maxItem;
105 * @param inCurrentMillis current time in milliseconds since start
106 * @return number of milliseconds to wait until next point is due
108 public long getMillisUntilNextPoint(long inCurrentMillis)
110 if (isFinished() || _millis[_currentItem+1] < _millis[_currentItem]) {
111 return 0; // no next point
113 return _millis[_currentItem+1] - inCurrentMillis;