class Topaz::Clock

The main tempo clock

Attributes

event[R]
midi_output[R]
source[R]
trigger[R]

Public Class Methods

new(tempo_or_input, options = {}, &tick_event) click to toggle source

@param [Fixnum, UniMIDI::Input] tempo_or_input @param [Hash] options @option options [Boolean] :midi_transport Whether to respect start/stop MIDI commands from a MIDI input @param [Proc] tick_event

# File lib/topaz/clock.rb, line 14
def initialize(tempo_or_input, options = {}, &tick_event)
  # The MIDI clock output is initialized regardless of whether there are devices
  # so that it is ready if any are added during the running process.
  @midi_output = MIDIClockOutput.new(:devices => options[:midi])
  @event = Event.new
  @trigger = EventTrigger.new
  @source = TempoSource.new(tempo_or_input, options.merge({ :event => @event }))
  initialize_events(&tick_event)
end

Public Instance Methods

start(options = {}) click to toggle source

This will start the clock source

In the case that external midi tempo is being used, this will instead start the process of waiting for a start or clock message

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

# File lib/topaz/clock.rb, line 46
def start(options = {})
  @start_time = Time.now
  begin
    @source.start(options)
  rescue SystemExit, Interrupt => exception
    stop
    raise exception
  end
  true
end
stop(options = {}) click to toggle source

This will stop the clock source @param [Hash] options @return [Boolean]

# File lib/topaz/clock.rb, line 60
def stop(options = {})
  @source.stop(options)
  @start_time = nil
  true
end
tempo=(value) click to toggle source

Set the tempo

If external MIDI tempo is being used, this will switch to internal tempo at the desired rate.

@param [Fixnum] value @return [ExternalMIDITempo, InternalTempo]

# File lib/topaz/clock.rb, line 30
def tempo=(value)
  if @source.respond_to?(:tempo=)
    @source.tempo = value
  else
    @source = TempoSource.new(event, tempo_or_input)
  end
end
time() click to toggle source

Seconds since start was called @return [Float]

# File lib/topaz/clock.rb, line 68
def time
  (Time.now - @start_time).to_f unless @start_time.nil?
end

Private Instance Methods

initialize_events(&block) click to toggle source

Initialize the tick and MIDI clock events so that they can be passed to the source and fired when needed @param [Proc] block @return [Clock::Event]

# File lib/topaz/clock.rb, line 78
def initialize_events(&block)
  @event.tick << block if block_given?
  clock = proc do
    if @trigger.stop?
      stop
    else
      @midi_output.do_clock
    end
  end
  @event.clock = clock
  @event.start << proc { @midi_output.do_start }
  @event.stop << proc { @midi_output.do_stop }
  @event
end