Sha256: a04be7b342627b4342e2bd51b07c884b629ba4d188a813b5df364089e1ecc967

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# Ports are UniMIDI inputs or outputs.
module PM

class MIDIDevice

  attr_reader :name, :port_num, :port

  def initialize(name, port_num, port)
    @name, @port_num, @port = name, port_num, port
  end

end

# When a connection is started, it adds itself to this InputDevice's
# +@connections+. When it ends, it removes itself.
class InputDevice < MIDIDevice

  attr_accessor :connections

  # If +port+ is nil (the normal case), creates either a real or a mock port
  def initialize(name, port_num, no_midi=false)
    super(name, port_num, input_port(port_num, no_midi))
    @connections = []
  end

  def add_connection(conn)
    @connections << conn
  end

  def remove_connection(conn)
    @connections.delete(conn)
  end

  # Poll for more MIDI input and process it.
  def gets_data
    @port.gets_data.each { |bytes| midi_in(bytes) }
  end

  # Passes MIDI bytes on to each output connection
  def midi_in(bytes)
    @connections.each { |conn| conn.midi_in(bytes) }
  end

  private

  def input_port(port_num, no_midi=false)
    if no_midi
      MockInputPort.new
    else
      UniMIDI::Input.all[port_num].open
    end
  end

end

class OutputDevice < MIDIDevice

  def initialize(name, port_num, no_midi=false)
    super(name, port_num, output_port(port_num, no_midi))
  end

  def midi_out(bytes)
    @port.puts bytes
  end

  private

  def output_port(port_num, no_midi)
    if no_midi
      MockOutputPort.new
    else
      UniMIDI::Output.all[port_num].open
    end
  end
end

class MockInputPort
  def gets_data
    []
  end
end

class MockOutputPort
  def puts(data)
  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
patchmaster-0.0.0 lib/patchmaster/io.rb