Module: MIDICommunications::Device::ClassMethods

Includes:
Enumerable
Included in:
Input, Output
Defined in:
lib/midi-communications/device.rb

Overview

Class methods shared by both Input and Output classes.

Provides device discovery and selection methods including enumeration, listing, searching by name, and interactive selection.

Instance Method Summary collapse

Instance Method Details

#at(index) ⇒ Input, Output Also known as: []

Returns the device at the given index without opening it.

Examples:

device = MIDICommunications::Output.at(0)
device.open if device

Parameters:

  • index (Integer)

    device index

Returns:



140
141
142
# File 'lib/midi-communications/device.rb', line 140

def at(index)
  all[index]
end

#each {|device| ... } ⇒ Enumerator

Iterates over all devices of this type.

Examples:

MIDICommunications::Output.each { |o| puts o.name }

Yields:

  • (device)

    each device

Yield Parameters:

Returns:

  • (Enumerator)

    if no block given



26
27
28
# File 'lib/midi-communications/device.rb', line 26

def each(&block)
  all.each(&block)
end

#find_by_name(name) ⇒ Input, ...

Finds a device by its name.

Examples:

output = MIDICommunications::Output.find_by_name("IAC Driver Bus 1")

Parameters:

  • name (String, Symbol)

    the device name to search for

Returns:

  • (Input, Output, nil)

    the matching device or nil if not found



53
54
55
# File 'lib/midi-communications/device.rb', line 53

def find_by_name(name)
  all.find { |device| name.to_s == device.name }
end

#first {|device| ... } ⇒ Input, Output

Selects and opens the first available device.

Examples:

output = MIDICommunications::Output.first

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Returns:



97
98
99
# File 'lib/midi-communications/device.rb', line 97

def first(&block)
  use_device(all.first, &block)
end

#gets {|device| ... } ⇒ Input, Output

Interactive console prompt for device selection.

Displays available devices and waits for user input. When a valid selection is received, the device is opened and returned.

Examples:

output = MIDICommunications::Output.gets
# Select a MIDI output...
# 0) IAC Driver Bus 1
# > 0

Yields:

  • (device)

    optional block to execute with the opened device

Yield Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/midi-communications/device.rb', line 71

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 {|device| ... } ⇒ Input, Output

Selects and opens the last available device.

Examples:

output = MIDICommunications::Output.last

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Returns:



109
110
111
# File 'lib/midi-communications/device.rb', line 109

def last(&block)
  use_device(all.last, &block)
end

#listArray<String>

Prints the ID and name of each device to the console.

Examples:

MIDICommunications::Output.list
# 0) IAC Driver Bus 1
# 1) USB MIDI Device

Returns:

  • (Array<String>)

    array of formatted device names



38
39
40
41
42
43
44
# File 'lib/midi-communications/device.rb', line 38

def list
  all.map do |device|
    name = "#{device.id}) #{device.display_name}"
    puts(name)
    name
  end
end

#use(index) {|device| ... } ⇒ Input, Output Also known as: open

Selects and opens the device at the given index.

Examples:

output = MIDICommunications::Output.use(0)

Parameters:

  • index (Integer, Symbol)

    device index or :first/:last

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Returns:



122
123
124
125
126
127
128
129
# File 'lib/midi-communications/device.rb', line 122

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