class PM::InputInstrument

When a connection is started, it adds itself to this InputInstrument's +@connections+. When it ends, it removes itself.

Attributes

connections[RW]
listener[R]
triggers[RW]

Public Class Methods

new(sym, name, port_num, use_midi=true) click to toggle source

If port is nil (the normal case), creates either a real or a mock port

Calls superclass method PM::Instrument::new
# File lib/patchmaster/instrument.rb, line 25
def initialize(sym, name, port_num, use_midi=true)
  super(sym, name, port_num, input_port(port_num, use_midi))
  @connections = []
  @triggers = []
  @listener = nil
end

Public Instance Methods

add_connection(conn) click to toggle source
# File lib/patchmaster/instrument.rb, line 32
def add_connection(conn)
  @connections << conn
end
midi_in(bytes) click to toggle source

Passes MIDI bytes on to triggers and to each output connection.

# File lib/patchmaster/instrument.rb, line 58
def midi_in(bytes)
  @triggers.each { |trigger| trigger.signal(bytes) }
  @connections.each { |conn| conn.midi_in(bytes) }
end
remove_connection(conn) click to toggle source
# File lib/patchmaster/instrument.rb, line 36
def remove_connection(conn)
  @connections.delete(conn)
end
start() click to toggle source

Poll for more MIDI input and process it.

# File lib/patchmaster/instrument.rb, line 41
def start
  PatchMaster.instance.debug("instrument #{name} start")
  @port.clear_buffer
  @listener = MIDIEye::Listener.new(@port).listen_for { |event| midi_in(event[:message].to_bytes) }
  @listener.run(:background => true)
end
stop() click to toggle source
# File lib/patchmaster/instrument.rb, line 48
def stop
  PatchMaster.instance.debug("instrument #{name} stop")
  @port.clear_buffer
  if @listener
    @listener.close
    @listener = nil
  end
end

Private Instance Methods

input_port(port_num, use_midi=true) click to toggle source
# File lib/patchmaster/instrument.rb, line 65
def input_port(port_num, use_midi=true)
  if use_midi
    UniMIDI::Input.all[port_num].open
  else
    MockInputPort.new(port_num)
  end
end