event
Class Metronome

java.lang.Object
  extended by event.Metronome
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
UDPDrivenMetronome

public class Metronome
extends java.lang.Object
implements java.lang.Runnable

A Metronome "ticks" at regular intervals, informing any registered listeners

We use a Metronome instead of a java.util.Timer or a javax.swing.Timer object for two reasons:

  1. It is more generic (i.e., the micro edition of Java does not have a javax.swing.Timer)
  2. It is instructive with regard to multi-threading, the observer pattern, the Java event-dispatch thread, and inner classes.
  3. It gives us the option of using both fixed-delay and fixed-rate execution.

The biggest disadvantage of this class is that it does not use thread sharing. Hence, an application should not use many different Metronome objects.

A Metronome may "drift" since the sleep() method is not guaranteed to return in exactly the right amount of time. A "fixed rate" metronome will attempt to fix this (by notifying listeners in quick succession).

The time that is reported to listeners will depend on the operating mode. A "fixed interval" Metronome will report the time as if it is drift-free. That is, the difference in reported times will be exactly the delay (even when the Metronome has drifted). A "fixed rate" Metronome will report the time as if it is correcting the drift. That is, the different in reported times will reflect the amount of time it tried to wait between reports. Neither will report the actual time between reports. A listener that wants to know this information must use System.currentTimeMillis() itself.

See Also:
"The Design and Implementation of Multimedia Software © 2011"

Field Summary
protected  int delay
           
protected  int multiplier
           
protected  int time
           
protected  java.lang.Thread timerThread
           
 
Constructor Summary
Metronome()
          Default Constructor
Metronome(int delay)
          Explicit Value Constructor Constructs a "fixed interval" Metronome with the given delay
Metronome(int delay, boolean adjusting)
          Explicit Value Constructor
 
Method Summary
 void addListener(MetronomeListener ml)
          Add a MetronomeListener
 int getDelay()
          Get the current delay
 int getNumberOfListeners()
          Get the number of listeners
protected  void notifyListeners()
          Notify observers in the GUI/event-dispatch thread.
 void removeListener(MetronomeListener ml)
          Remove a MetronomeListener
 void reset()
          Reset the time Note: This method should only be called when the Metronome is not running
 void run()
          The code that is executed in the timer thread (required by Runnable)
 void setMultiplier(int multiplier)
          Set the multiplier (i.e., the apparent speed-up factor) Note: This method should only be called when the Metronome is not running
 void setTime(int time)
          Set the current time Note: This method should only be called when the Metronome is not running
 void start()
          Start this Metronome
 void stop()
          Stop this Metronome "immediately"
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

delay

protected volatile int delay

multiplier

protected volatile int multiplier

time

protected volatile int time

timerThread

protected java.lang.Thread timerThread
Constructor Detail

Metronome

public Metronome()
Default Constructor


Metronome

public Metronome(int delay)
Explicit Value Constructor Constructs a "fixed interval" Metronome with the given delay

Parameters:
delay - The number of milliseconds between ticks

Metronome

public Metronome(int delay,
                 boolean adjusting)
Explicit Value Constructor

Parameters:
delay - The number of milliseconds between ticks
adjusting - true for "fixed rate"; false for "fixed interval"
Method Detail

addListener

public void addListener(MetronomeListener ml)
Add a MetronomeListener

Parameters:
ml - The MetronomeListener to add

getDelay

public int getDelay()
Get the current delay


getNumberOfListeners

public int getNumberOfListeners()
Get the number of listeners


notifyListeners

protected void notifyListeners()
Notify observers in the GUI/event-dispatch thread. Note: Listeners are notified in the REVERSE order in which they are added.


removeListener

public void removeListener(MetronomeListener ml)
Remove a MetronomeListener

Parameters:
ml - The MetronomeListener to remove

reset

public void reset()
Reset the time Note: This method should only be called when the Metronome is not running


run

public void run()
The code that is executed in the timer thread (required by Runnable)

Specified by:
run in interface java.lang.Runnable

setMultiplier

public void setMultiplier(int multiplier)
Set the multiplier (i.e., the apparent speed-up factor) Note: This method should only be called when the Metronome is not running

Parameters:
multiplier - The multiplier

setTime

public void setTime(int time)
Set the current time Note: This method should only be called when the Metronome is not running

Parameters:
time - The current time

start

public void start()
Start this Metronome


stop

public void stop()
Stop this Metronome "immediately"



Design and Implementation of Multimedia Software, Jones and Bartlett Publishers