class Xi::MIDI::Proxy

Constants

CC
MAX_CHANNELS
MAX_NOTES
NOTE_OFF
NOTE_ON

Public Class Methods

new() click to toggle source
# File lib/xi/midi/proxy.rb, line 15
def initialize
  @mutex = Mutex.new
  @outputs = RawMIDI::Output.all

  # Make sure to close outputs at exit
  at_exit do
    begin
      @outputs.each(&:close)
    rescue => err
      puts "Error when closing MIDI outputs: #{err}"
    end
  end
end

Public Instance Methods

cc(device, channel, id, value) click to toggle source
# File lib/xi/midi/proxy.rb, line 55
def cc(device, channel, id, value)
  send_bytes(device, CC + channel, id, value)
end
list() click to toggle source
# File lib/xi/midi/proxy.rb, line 29
def list
  RawMIDI::Output.all.map.with_index { |o, i| [i, o] }
end
note_off(device, channel, note) click to toggle source
# File lib/xi/midi/proxy.rb, line 51
def note_off(device, channel, note)
  send_bytes(device, NOTE_OFF + channel, note, 0)
end
note_on(device, channel, note, velocity) click to toggle source
# File lib/xi/midi/proxy.rb, line 47
def note_on(device, channel, note, velocity)
  send_bytes(device, NOTE_ON + channel, note, velocity)
end
open(device) click to toggle source
# File lib/xi/midi/proxy.rb, line 33
def open(device)
  unless @outputs[device]
    fail ArgumentError, "MIDI device #{device} not found"
  end
  @outputs[device].open
end
open?(device) click to toggle source
# File lib/xi/midi/proxy.rb, line 40
def open?(device)
  unless @outputs[device]
    fail ArgumentError, "MIDI device #{device} not found"
  end
  @outputs[device].open?
end
reset(device) click to toggle source
# File lib/xi/midi/proxy.rb, line 63
def reset(device)
  MAX_CHANNELS.times.each do |channel|
    MAX_NOTES.times.each do |note|
      note_off(device, channel, note)
    end
  end
end
reset_all() click to toggle source
# File lib/xi/midi/proxy.rb, line 59
def reset_all
  @outputs.size.times { |i| reset(i) }
end

Private Instance Methods

send_bytes(device, *bytes) click to toggle source
# File lib/xi/midi/proxy.rb, line 73
def send_bytes(device, *bytes)
  return unless open?(device)

  debug(:send_bytes, device, bytes)
  @mutex.synchronize { @outputs[device].write(bytes) }
end