Class: MIDIEye::Listener
- Inherits:
-
Object
- Object
- MIDIEye::Listener
- Defined in:
- lib/midi-eye/listener.rb
Constant Summary collapse
- LISTEN_INTERVAL =
1.0 / 1000
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#sources ⇒ Object
Returns the value of attribute sources.
Instance Method Summary collapse
-
#add_input(inputs) ⇒ Array<MIDIEye::Source>
(also: #add_inputs)
Add a MIDI source.
-
#close ⇒ MIDIEye::Listener
(also: #stop)
Stop listening for MIDI messages.
-
#delete_event(event_name) ⇒ Boolean
Deletes the event with the given name (for backwards compat).
-
#initialize(inputs) ⇒ Listener
constructor
A new instance of Listener.
-
#join ⇒ MIDIEye::Listener
Join the listener if it’s being run in the background.
-
#listen_for(options = {}, &callback) ⇒ MIDIEye::Listener
(also: #on_message)
Add an event to listen for.
-
#poll ⇒ Object
Poll the input source for new input.
-
#remove_input(inputs) ⇒ Array<MIDIEye::Source>
(also: #remove_inputs)
Remove a MIDI source.
-
#run(options = {}) ⇒ MIDIEye::Listener
(also: #start)
Start listening for MIDI messages.
-
#running? ⇒ Boolean
Is the listener running?.
-
#uses_input?(input) ⇒ Boolean
Does this listener use the given input?.
Constructor Details
Instance Attribute Details
#event ⇒ Object (readonly)
Returns the value of attribute event.
7 8 9 |
# File 'lib/midi-eye/listener.rb', line 7 def event @event end |
#sources ⇒ Object
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
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 |
#close ⇒ MIDIEye::Listener Also known as: stop
Stop listening for MIDI messages.
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)
90 91 92 |
# File 'lib/midi-eye/listener.rb', line 90 def delete_event(event_name) !@event.delete(event_name).nil? end |
#join ⇒ MIDIEye::Listener
Join the listener if it’s being run in the background.
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
97 98 99 100 101 |
# File 'lib/midi-eye/listener.rb', line 97 def listen_for( = {}, &callback) raise "Listener must have a block" if callback.nil? @event.add(, &callback) self end |
#poll ⇒ Object
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| = [batch[:messages]].flatten.compact .each do || data = { :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
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
52 53 54 55 56 |
# File 'lib/midi-eye/listener.rb', line 52 def run( = {}) listen join unless !![:background] self end |
#running? ⇒ Boolean
Is the listener running?
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?
21 22 23 |
# File 'lib/midi-eye/listener.rb', line 21 def uses_input?(input) @sources.any? { |source| source.uses?(input) } end |