Class: MIDIInstrument::Listener

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/midi-instrument/listener.rb

Overview

A light wrapper for MIDIEye::Listener

Instance Method Summary collapse

Constructor Details

#initialize(sources) ⇒ Listener

Returns a new instance of Listener.

Parameters:

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


12
13
14
# File 'lib/midi-instrument/listener.rb', line 12

def initialize(sources)
  @listener = MIDIEye::Listener.new([sources].flatten)
end

Instance Method Details

#add(*messages) ⇒ Array<MIDIMessage> Also known as: <<

Manually add messages to the MIDI input buffer

Parameters:

  • args (Array<MIDIMessage>, MIDIMessage, *MIDIMessage)

Returns:

  • (Array<MIDIMessage>)


19
20
21
22
23
24
25
26
27
28
# File 'lib/midi-instrument/listener.rb', line 19

def add(*messages)
  [messages].flatten.map do |message|
    report = { 
      :message => message, 
      :timestamp => Time.now.to_f 
    }
    @listener.event.enqueue_all(report)
    report
  end
end

#inputsArray<UniMIDI::Input>

The active inputs

Returns:

  • (Array<UniMIDI::Input>)


39
40
41
# File 'lib/midi-instrument/listener.rb', line 39

def inputs
  @listener.sources
end

#joinObject

Join the listener thread



32
33
34
35
# File 'lib/midi-instrument/listener.rb', line 32

def join
  start if !@listener.running?
  @listener.join
end

#receive(match = {}, &callback) ⇒ Boolean

Bind a message callback

Parameters:

  • match (Hash) (defaults to: {})
  • callback (Proc)

Returns:

  • (Boolean)


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

def receive(match = {}, &callback)
  @listener.listen_for(match, &callback)
  start if !@listener.running?
  true
end

#startObject

Start the listener



44
45
46
# File 'lib/midi-instrument/listener.rb', line 44

def start
  @listener.start(:background => true)
end