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):



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

def initialize(input, options = {})
  @event = options[:event]
  @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)


83
84
85
# File 'lib/topaz/midi_clock_input.rb', line 83

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)


77
78
79
# File 'lib/topaz/midi_clock_input.rb', line 77

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

#joinMIDIInputClock

Join the listener thread

Returns:

  • (MIDIInputClock)

    self



58
59
60
61
# File 'lib/topaz/midi_clock_input.rb', line 58

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



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

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



50
51
52
53
54
# File 'lib/topaz/midi_clock_input.rb', line 50

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

#tempoFixnum

This will return a calculated tempo

Returns:

  • (Fixnum)


29
30
31
# File 'lib/topaz/midi_clock_input.rb', line 29

def tempo
  @tempo_calculator.calculate
end