module UniMIDI::Device::ClassMethods

Methods that are shared by both Input and Output classes

Public Instance Methods

[](index)
Alias for: at
at(index) click to toggle source

Select the device at the given index @param [Integer] index @return [Input, Output]

# File lib/unimidi/device.rb, line 81
def at(index)
  all[index]
end
Also aliased as: []
each() { |device| ... } click to toggle source

Iterate over all devices of this direction (eg Input, Output)

# File lib/unimidi/device.rb, line 12
def each(&block)
  all.each { |device| yield(device) }
end
find_by_name(name) click to toggle source

Shortcut to select a device by its name @param [String, Symbol] name @return [Input, Output]

# File lib/unimidi/device.rb, line 29
def find_by_name(name)
  all.find { |device| name.to_s == device.name }
end
first(&block) click to toggle source

Select the first device and enable it @return [Input, Output]

# File lib/unimidi/device.rb, line 55
def first(&block)
  use_device(all.first, &block)
end
gets(&block) click to toggle source

Streamlined console prompt that asks the user to select a device When their input is received, the device is selected and enabled

# File lib/unimidi/device.rb, line 35
def gets(&block)
  device = nil
  direction = get_direction
  puts ""
  puts "Select a MIDI #{direction}..."
  while device.nil?
    list
    print "> "
    selection = $stdin.gets.chomp
    if selection != ""
      selection = Integer(selection) rescue nil
      device = all.find { |d| d.id == selection } unless selection.nil?
    end
  end
  device.open(&block)
  device
end
last(&block) click to toggle source

Select the last device and enable it @return [Input, Output]

# File lib/unimidi/device.rb, line 61
def last(&block)
  use_device(all.last, &block)
end
list() click to toggle source

Prints ids and names of each device to the console @return [Array<String>]

# File lib/unimidi/device.rb, line 18
def list
  all.map do |device|
    name = device.pretty_name
    puts(name)
    name
  end
end
open(index, &block)
Alias for: use
use(index, &block) click to toggle source

Select the device at the given index and enable it @param [Integer] index @return [Input, Output]

# File lib/unimidi/device.rb, line 68
def use(index, &block)
  index = case index
          when :first then 0
          when :last then all.count - 1
          else index
          end
  use_device(at(index), &block)
end
Also aliased as: open

Private Instance Methods

get_direction() click to toggle source

The direction of the device eg “input”, “output” @return [String]

# File lib/unimidi/device.rb, line 90
def get_direction
  name.split("::").last.downcase
end
use_device(device) { |device| ... } click to toggle source

Enable the given device @param [Input, Output] device @return [Input, Output]

# File lib/unimidi/device.rb, line 97
def use_device(device, &block)
  if device.enabled?
    yield(device) if block_given?
  else
    device.open(&block)
  end
  device
end