Class: MIDIEye::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/midi-eye/listener.rb

Constant Summary collapse

LISTEN_INTERVAL =
1.0 / 1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs) ⇒ Listener

Returns a new instance of Listener.

Parameters:

  • inputs (Array<UniMIDI::Input>, UniMIDI::Input)

    Input(s) to add to the list of sources for this listener



11
12
13
14
15
16
# File 'lib/midi-eye/listener.rb', line 11

def initialize(inputs)
  @sources = []
  @event = Event.new

  add_input(inputs)
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



7
8
9
# File 'lib/midi-eye/listener.rb', line 7

def event
  @event
end

#sourcesObject

Returns the value of attribute sources.



8
9
10
# File 'lib/midi-eye/listener.rb', line 8

def sources
  @sources
end

Instance Method Details

#add_input(inputs) ⇒ Array<MIDIEye::Source> Also known as: add_inputs

Add a MIDI source

Parameters:

  • inputs (Array<UniMIDI::Input>, UniMIDI::Input)

    Input(s) to add to the list of sources for this listener

Returns:

  • (Array<MIDIEye::Source>)

    The updated list of sources for this listener



28
29
30
31
32
33
# File 'lib/midi-eye/listener.rb', line 28

def add_input(inputs)
  inputs = [inputs].flatten.compact
  input_sources = inputs.reject { |input| uses_input?(input) }
  @sources += input_sources.map { |input| Source.new(input) }
  @sources
end

#closeMIDIEye::Listener Also known as: stop

Stop listening for MIDI messages.

Returns:



61
62
63
64
65
66
# File 'lib/midi-eye/listener.rb', line 61

def close
  @listener.kill if running?
  @event.clear
  @sources.clear
  self
end

#delete_event(event_name) ⇒ Boolean

Deletes the event with the given name (for backwards compat)

Parameters:

  • event_name (String, Symbol)

Returns:

  • (Boolean)


90
91
92
# File 'lib/midi-eye/listener.rb', line 90

def delete_event(event_name)
  !@event.delete(event_name).nil?
end

#joinMIDIEye::Listener

Join the listener if it’s being run in the background.

Returns:



77
78
79
80
81
82
83
84
85
# File 'lib/midi-eye/listener.rb', line 77

def join
  begin
    @listener.join
  rescue Exception => exception
    @listener.kill
    Thread.main.raise(exception)
  end
  self
end

#listen_for(options = {}, &callback) ⇒ MIDIEye::Listener Also known as: on_message

Add an event to listen for

Parameters:

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

Returns:



97
98
99
100
101
# File 'lib/midi-eye/listener.rb', line 97

def listen_for(options = {}, &callback)
  raise "Listener must have a block" if callback.nil?
  @event.add(options, &callback)
  self
end

#pollObject

Poll the input source for new input. This will normally be done by the background thread



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/midi-eye/listener.rb', line 105

def poll
  @sources.each do |input|
    input.poll do |objs|
      objs.each do |batch|
        messages = [batch[:messages]].flatten.compact
        messages.each do |message|
          data = {
            :message => message,
            :timestamp => batch[:timestamp]
          }
          @event.enqueue_all(data)
        end
      end
    end
  end
end

#remove_input(inputs) ⇒ Array<MIDIEye::Source> Also known as: remove_inputs

Remove a MIDI source

Parameters:

  • inputs (Array<UniMIDI::Input>, UniMIDI::Input)

    Input(s) to remove from the list of sources for this listener

Returns:

  • (Array<MIDIEye::Source>)

    The updated list of sources for this listener



39
40
41
42
43
44
45
# File 'lib/midi-eye/listener.rb', line 39

def remove_input(inputs)
  inputs = [inputs].flatten.compact
  inputs.each do |input|
    @sources.delete_if { |source| source.uses?(input) }
  end
  @sources
end

#run(options = {}) ⇒ MIDIEye::Listener Also known as: start

Start listening for MIDI messages

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :background (Boolean)

    Run in a background thread

Returns:



52
53
54
55
56
# File 'lib/midi-eye/listener.rb', line 52

def run(options = {})
  listen
  join unless !!options[:background]
  self
end

#running?Boolean

Is the listener running?

Returns:

  • (Boolean)


71
72
73
# File 'lib/midi-eye/listener.rb', line 71

def running?
  !@listener.nil? && @listener.alive?
end

#uses_input?(input) ⇒ Boolean

Does this listener use the given input?

Parameters:

  • input (UniMIDI::Input)

Returns:

  • (Boolean)


21
22
23
# File 'lib/midi-eye/listener.rb', line 21

def uses_input?(input)
  @sources.any? { |source| source.uses?(input) }
end