class MIDIEye::Listener

Constants

LISTEN_INTERVAL

Attributes

event[R]
sources[RW]

Public Class Methods

new(inputs) click to toggle source

@param [Array<UniMIDI::Input>, UniMIDI::Input] inputs Input(s) to add to the list of sources for this listener

# File lib/midi-eye/listener.rb, line 11
def initialize(inputs)
  @sources = []
  @event = Event.new

  add_input(inputs)
end

Public Instance Methods

add_input(inputs) click to toggle source

Add a MIDI source @param [Array<UniMIDI::Input>, UniMIDI::Input] inputs Input(s) to add to the list of sources for this listener @return [Array<MIDIEye::Source>] The updated list of sources for this listener

# 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
Also aliased as: add_inputs
add_inputs(inputs)
Alias for: add_input
close() click to toggle source

Stop listening for MIDI messages. @return [MIDIEye::Listener] self

# File lib/midi-eye/listener.rb, line 61
def close
  @listener.kill if running?
  @event.clear
  @sources.clear
  self
end
Also aliased as: stop
delete_event(event_name) click to toggle source

Deletes the event with the given name (for backwards compat) @param [String, Symbol] event_name @return [Boolean]

# File lib/midi-eye/listener.rb, line 90
def delete_event(event_name)
  !@event.delete(event_name).nil?
end
join() click to toggle source

Join the listener if it's being run in the background. @return [MIDIEye::Listener] self

# 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) click to toggle source

Add an event to listen for @param [Hash] options @return [MIDIEye::Listener] self

# 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
Also aliased as: on_message
on_message(options = {}, &callback)
Alias for: listen_for
poll() click to toggle source

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

# 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) click to toggle source

Remove a MIDI source @param [Array<UniMIDI::Input>, UniMIDI::Input] inputs Input(s) to remove from the list of sources for this listener @return [Array<MIDIEye::Source>] The updated list of sources for this listener

# 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
Also aliased as: remove_inputs
remove_inputs(inputs)
Alias for: remove_input
run(options = {}) click to toggle source

Start listening for MIDI messages @params [Hash] options @option options [Boolean] :background Run in a background thread @return [MIDIEye::Listener] self

# File lib/midi-eye/listener.rb, line 52
def run(options = {})
  listen
  join unless !!options[:background]
  self
end
Also aliased as: start
running?() click to toggle source

Is the listener running? @return [Boolean]

# File lib/midi-eye/listener.rb, line 71
def running?
  !@listener.nil? && @listener.alive?
end
start(options = {})
Alias for: run
stop()
Alias for: close
uses_input?(input) click to toggle source

Does this listener use the given input? @param [UniMIDI::Input] input @return [Boolean]

# File lib/midi-eye/listener.rb, line 21
def uses_input?(input)
  @sources.any? { |source| source.uses?(input) }
end

Private Instance Methods

listen() click to toggle source

Start the background listener thread

# File lib/midi-eye/listener.rb, line 134
def listen
  @listener = Thread.new do
    begin
      listen_loop
    rescue Exception => exception
      Thread.main.raise(exception)
    end
  end
  @listener.abort_on_exception = true
  true
end
listen_loop() click to toggle source

A loop that runs while the listener is active

# File lib/midi-eye/listener.rb, line 125
def listen_loop
  loop do
    poll
    @event.trigger_enqueued
    sleep(LISTEN_INTERVAL)
  end
end