public class Clock extends java.lang.Object implements ScheduledEventListener
HBScheduler class. If the HBScheduler has it's time adjusted, all Clock objects will follow the time of the HBScheduler .
Clock.ClockTickListener to it through the addClockTickListener(ClockTickListener). More than one listener can be added to a clock
Clock testclock1 = new Clock(1000);
testclock1.addClockTickListener((offset, clock) - > {
long number_ticks = clock.getNumberTicks();
System.out.println("Num Ticks: " + number_ticks);
});
testclock1.start();
stop() or when the sketch is reset
BPM2Interval(double) and Interval2BPM(double) facilitate converting intervals to and from Beats per minute
shiftTime(double), change the interval through setInterval(double), synchronise to another Clock using synchronizeClock(Clock), or cause the clock to restart with a zero count using reset()
getNumberTicks(), getClockInterval() and isRunning()| Modifier and Type | Class and Description |
|---|---|
static interface |
Clock.ClockTickListener
ClockTick interface for receiving clock Tick events
The Clock.ClockTickListener.clockTick(double, Clock) receives the scheduled event and is usually represented as a Lambda. |
| Constructor and Description |
|---|
Clock(double interval)
Constructor using default Scheduler and an interval
|
Clock(double interval,
HBScheduler scheduler)
Constructor
|
| Modifier and Type | Method and Description |
|---|---|
Clock |
addClockTickListener(Clock.ClockTickListener listener)
Add a
Clock.ClockTickListener for this clock. |
static double |
BPM2Interval(double beats_per_minute)
Calculate the interval required to create a clock with a defined beats per minute
Will throw an illegal argument exception if you pass in zero
|
void |
clearClockTickListener()
Clears all
Clock.ClockTickListener for this clock |
void |
doScheduledEvent(double scheduledTime,
java.lang.Object param)
Notification interface that a scheduled event is occuring
|
double |
getClockInterval()
The interval of the clock in milliseconds
|
long |
getNumberTicks()
Return the number of clock ticks since the clock was started
|
static double |
Interval2BPM(double milliseconds)
Calculate number of Beats Per Minute given an interval between beats
|
boolean |
isRunning()
See if clock is running
|
void |
removeClockTickListener(Clock.ClockTickListener listener)
Removes a
Clock.ClockTickListener for this clock previously added using addClockTickListener(ClockTickListener) |
void |
reset()
Reset clock.
|
void |
setInterval(double interval)
Change the interval of the clock and start from now
|
void |
shiftTime(double shift_time)
Shift the clock forwards or backwards an amount of time
|
Clock |
start()
Start the clock
|
void |
stop() |
double |
synchronizeClock(Clock master)
Synchronise this clock to another one
|
public Clock(double interval)
interval - the interval in milliseconds Must be greater than 2 milliseconds otherwise could lock uppublic Clock(double interval,
HBScheduler scheduler)
interval - the interval in milliseconds Must be greater than 2 milliseconds otherwise could lock upscheduler - the scheduler to usepublic double getClockInterval()
public long getNumberTicks()
public boolean isRunning()
public void reset()
public void stop()
public void setInterval(double interval)
interval - the new clock intervalpublic Clock start()
public Clock addClockTickListener(Clock.ClockTickListener listener)
Clock.ClockTickListener for this clock. More than one listener can be attached to a Clock and they
can be added after the clock is already started. For example:
final int TICKS_PER_BEAT = 4;
double interval = Clock.BPM2Interval(120 * TICKS_PER_BEAT);
Clock testclock1 = new Clock(interval);
// create a listener for each Beat
testclock1.addClockTickListener((offset, clock) - > {
if (clock.getNumberTicks() % TICKS_PER_BEAT == 0){
System.out.println("Beat " + clock.getNumberTicks() / TICKS_PER_BEAT);
}
});
testclock1.start();
// We can even add listeners after clock is started
// Create a Listener for every Tick not on the beat
testclock1.addClockTickListener((offset, clock) - > {
long tickNum = clock.getNumberTicks() % TICKS_PER_BEAT;
System.out.println("tick " + tickNum);
});
Listeners are removed using removeClockTickListener(ClockTickListener)listener - the listener to addpublic void removeClockTickListener(Clock.ClockTickListener listener)
Clock.ClockTickListener for this clock previously added using addClockTickListener(ClockTickListener)listener - the listener to removepublic void clearClockTickListener()
Clock.ClockTickListener for this clockpublic void shiftTime(double shift_time)
shift_time - the amount of milliseconds to shift clockpublic static double BPM2Interval(double beats_per_minute)
throws java.lang.IllegalArgumentException
beats_per_minute - beats per minutejava.lang.IllegalArgumentException - if the beats_per minute is zeropublic static double Interval2BPM(double milliseconds)
throws java.lang.IllegalArgumentException
milliseconds - the number of milliseconds between beatsjava.lang.IllegalArgumentException - if milliseconds is zeropublic double synchronizeClock(Clock master)
master - the clock we are synchronising withpublic void doScheduledEvent(double scheduledTime,
java.lang.Object param)
ScheduledEventListenerdoScheduledEvent in interface ScheduledEventListenerscheduledTime - the time that was scheduled for the callback to occurparam - the parameter to pass to the listener.