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.

API:

  • public

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:

  • device index

Returns:

  • the device at the given index

API:

  • public



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:

  • if no block given

API:

  • public



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:

  • the device name to search for

Returns:

  • the matching device or nil if not found

API:

  • public



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:

  • the first device, opened

API:

  • public



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:

  • the selected and opened device

API:

  • public



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:

  • the last device, opened

API:

  • public



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 of formatted device names

API:

  • public



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:

  • device index or :first/:last

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Returns:

  • the selected device, opened

API:

  • public



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