class Topaz::Timer
Attributes
Public Class Methods
@param [Fixnum] tempo @param [Hash] options @option options [Clock::Event] :event
# 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
The timer’s click interval @return [Fixnum]
# File lib/topaz/timer.rb, line 44 def interval @interval * 4 end
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 the timer thread @return [Timer]
# File lib/topaz/timer.rb, line 58 def join super() self end
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 the timer @return [Timer]
# File lib/topaz/timer.rb, line 50 def stop(*a) super() !@event.nil? && @event.do_stop self end
Protected Instance Methods
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
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
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
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 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 the timer function @return [Boolean]
# File lib/topaz/timer.rb, line 98 def perform dispatch advance true end
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
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