Class: Topaz::Clock

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/topaz/clock.rb

Overview

The main tempo clock

Defined Under Namespace

Classes: Event, EventTrigger

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

included, #time_since_start

Constructor Details

#initialize(tempo_or_input, options = {}, &tick_event) ⇒ Clock

Returns a new instance of Clock.

Parameters:

  • tempo_or_input (Fixnum, UniMIDI::Input)
  • options (Hash) (defaults to: {})
  • tick_event (Proc)

Options Hash (options):

  • :midi_transport (Boolean)

    Whether to respect start/stop MIDI commands from a MIDI input



14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



8
9
10
# File 'lib/topaz/clock.rb', line 8

def event
  @event
end

#midi_outputObject (readonly)

Returns the value of attribute midi_output.



8
9
10
# File 'lib/topaz/clock.rb', line 8

def midi_output
  @midi_output
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/topaz/clock.rb', line 8

def source
  @source
end

#triggerObject (readonly)

Returns the value of attribute trigger.



8
9
10
# File 'lib/topaz/clock.rb', line 8

def trigger
  @trigger
end

Instance Method Details

#start(options = {}) ⇒ Boolean

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

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :background (Boolean)

    Whether to run the timer in a background thread (default: false)

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
# 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 = {}) ⇒ Boolean

This will stop the clock source

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/topaz/clock.rb', line 60

def stop(options = {})
  @source.stop(options)
  @start_time = nil
  true
end

#tempo=(value) ⇒ ExternalMIDITempo, InternalTempo

Set the tempo

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

Parameters:

  • value (Fixnum)

Returns:

  • (ExternalMIDITempo, InternalTempo)


30
31
32
33
34
35
36
# 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

#timeFloat

Seconds since start was called

Returns:

  • (Float)


68
69
70
# File 'lib/topaz/clock.rb', line 68

def time
  (Time.now - @start_time).to_f unless @start_time.nil?
end