Class: Topaz::MIDIClockInput

Inherits:
Object
  • Object
show all
Includes:
Pausable
Defined in:
lib/topaz/midi_clock_input.rb

Overview

Trigger an event based on received midi clock messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pausable

#pause, #paused?, #toggle_pause, #unpause

Constructor Details

#initialize(input, options = {}) ⇒ MIDIClockInput

Returns a new instance of MIDIClockInput.

Parameters:

  • input (UniMIDI::Input)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :event (Clock::Event)
  • :midi_transport (Boolean)

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



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/topaz/midi_clock_input.rb', line 16

def initialize(input, options = {})
  @event = options[:event]
  @use_transport = !!options[:midi_transport]
  @tick_counter = 0
  @pause = false
  @listening = false
  @running = false
  @tempo_calculator = TempoCalculator.new
  @tick_threshold = interval_to_ticks(options.fetch(:interval, 4))

  initialize_listener(input)
end

Instance Attribute Details

#clockObject (readonly)

Returns the value of attribute clock.



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

def clock
  @clock
end

#listeningObject (readonly) Also known as: listening?

Returns the value of attribute listening.



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

def listening
  @listening
end

#runningObject (readonly) Also known as: running?

Returns the value of attribute running.



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

def running
  @running
end

Instance Method Details

#intervalFixnum

Return the interval at which the tick event is fired

Returns:

  • (Fixnum)


85
86
87
# File 'lib/topaz/midi_clock_input.rb', line 85

def interval
  ticks_to_interval(@tick_threshold)
end

#interval=(interval) ⇒ Fixnum

Change the clock interval Defaults to 4, which means click once every 24 ticks or one quarter note (per MIDI spec). Therefore, to fire the on_tick event twice as often, pass 8

 1 = whole note
 2 = half note
 4 = quarter note
 6 = dotted quarter
 8 = eighth note
16 = sixteenth note
etc

Parameters:

  • interval (Fixnum)

Returns:

  • (Fixnum)


79
80
81
# File 'lib/topaz/midi_clock_input.rb', line 79

def interval=(interval)
  @tick_threshold = interval_to_ticks(interval)
end

#joinMIDIInputClock

Join the listener thread

Returns:

  • (MIDIInputClock)

    self



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

def join
  @listener.join
  self
end

#start(options = {}) ⇒ MIDIInputClock

Start the listener

Parameters:

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

Options Hash (options):

  • :background (Boolean)

    Whether to run the listener in a background process

  • :focus (Boolean) — default: or :blocking

    Whether to run the listener in a foreground process

Returns:

  • (MIDIInputClock)

    self



40
41
42
43
44
45
46
47
48
# File 'lib/topaz/midi_clock_input.rb', line 40

def start(options = {})
  @listening = true
  blocking = options[:focus] || options[:blocking]
  background = !blocking unless blocking.nil?
  background = options[:background] if background.nil?
  background = false if background.nil?
  @listener.start(:background => background)
  self
end

#stop(*a) ⇒ MIDIInputClock

Stop the listener

Returns:

  • (MIDIInputClock)

    self



52
53
54
55
56
# File 'lib/topaz/midi_clock_input.rb', line 52

def stop(*a)
  @listening = false
  @listener.stop
  self
end

#tempoFixnum

This will return a calculated tempo

Returns:

  • (Fixnum)


31
32
33
# File 'lib/topaz/midi_clock_input.rb', line 31

def tempo
  @tempo_calculator.calculate
end