class Topaz::Timer

Attributes

phase[R]
running[R]
running?[R]

Public Class Methods

new(tempo, options = {}) click to toggle source

@param [Fixnum] tempo @param [Hash] options @option options [Clock::Event] :event

Calls superclass method
# File lib/topaz/timer.rb, line 13
def initialize(tempo, options = {})
  @event = options[:event]
  @last_tick_event = 0
  @last_midi_clock = 0
  @pause = false
  @running = false
  self.interval = options[:interval] || 4 
  
  super({ :tempo => tempo })
end

Public Instance Methods

interval() click to toggle source

The timer’s click interval @return [Fixnum]

# File lib/topaz/timer.rb, line 44
def interval
  @interval * 4
end
interval=(value) click to toggle source

Set the timer’s click interval @param [Fixnum] value @return [Fixnum]

# File lib/topaz/timer.rb, line 38
def interval=(value)
  @interval = value / 4
end
join() click to toggle source

Join the timer thread @return [Timer]

Calls superclass method
# File lib/topaz/timer.rb, line 58
def join
  super()
  self
end
start(options = {}) click to toggle source

Start the internal timer @param [Hash] options @option options [Boolean] :background Whether to run the timer in a background thread (default: false) @return [Timer]

# File lib/topaz/timer.rb, line 28
def start(options = {})
  run
  !@event.nil? && @event.do_start
  join unless !!options[:background]
  self
end
stop(*a) click to toggle source

Stop the timer @return [Timer]

Calls superclass method
# File lib/topaz/timer.rb, line 50
def stop(*a)
  super()
  !@event.nil? && @event.do_stop
  self
end

Protected Instance Methods

dispatch() click to toggle source

Run all ready tasks. @return [Boolean]

# File lib/topaz/timer.rb, line 86
def dispatch
  # Stuff to do on every tick
  clock if clock?
  # Stuff to do on @interval
  tick if tick?
  true
end
run() click to toggle source

Initialize the scheduler’s clock, and begin executing tasks @return [Boolean]

# File lib/topaz/timer.rb, line 67
def run
  unless @running
    @thread = Thread.new do
      begin
        initialize_running_state
        loop { perform } 
      rescue Exception => exception
        Thread.main.raise(exception)
      end
    end
    @thread.abort_on_exception = true
    true
  else
    false
  end
end

Private Instance Methods

clock() click to toggle source

Perform MIDI clock @return [Boolean]

# File lib/topaz/timer.rb, line 114
def clock
  !@event.nil? && @event.do_clock
  @last_midi_clock = (@phase * 24).to_i
  true
end
clock?() click to toggle source

Is the current phase appropriate for MIDI clock output? @return [Boolean]

# File lib/topaz/timer.rb, line 131
def clock?
  phase = (@phase * 24).to_i
  !@last_midi_clock.eql?(phase)
end
initialize_running_state() click to toggle source

Initialize the variables that handle the running process of the timer @return [Boolean]

# File lib/topaz/timer.rb, line 122
def initialize_running_state
  @running = true
  @phase = 0.0
  @origin = @time = Time.now.to_f
  true
end
perform() click to toggle source

Perform the timer function @return [Boolean]

# File lib/topaz/timer.rb, line 98
def perform
  dispatch 
  advance
  true
end
tick() click to toggle source

Perform a tick @return [Boolean]

# File lib/topaz/timer.rb, line 106
def tick
  !@event.nil? && !@pause && @event.do_tick
  @last_tick_event = (@phase * @interval).to_i
  true
end
tick?() click to toggle source

Is the current phase appropriate for the tick event? @return [Boolean]

# File lib/topaz/timer.rb, line 138
def tick?
  phase = (@phase * @interval).to_i
  !@last_tick_event.eql?(phase)
end