class MIDIEye::Listener
Constants
- LISTEN_INTERVAL
Attributes
Public Class Methods
@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 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
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
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 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
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
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 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
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
Is the listener running? @return [Boolean]
# File lib/midi-eye/listener.rb, line 71 def running? !@listener.nil? && @listener.alive? end
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
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
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