Class: JSound::Midi::DeviceList

Inherits:
Object
  • Object
show all
Defined in:
lib/jsound/midi/device_list.rb

Overview

A collection of MIDI Devices that provides various methods to lookup (and optionally auto-open) specific devices

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list = []) ⇒ DeviceList

Returns a new instance of DeviceList.



13
14
15
# File 'lib/jsound/midi/device_list.rb', line 13

def initialize(list=[])
  @devices = list
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)

Pass method calls through to the underlying Array. If Array doesn’t understand the method, call #open with a case-insensitive regexp match against description, treating underscore as a wildcard.

Examples:

# INPUTS.Akai => open the first “Akai” input device connected to this computer

# INPUTS.Akai_2 => open an input device matching /Akai.*2/i



158
159
160
161
162
163
164
165
166
# File 'lib/jsound/midi/device_list.rb', line 158

def method_missing(sym, *args, &block)
  if @devices.respond_to? sym
    @devices.send(sym, *args, &block)
  elsif args.length == 0 and block.nil?
    self.open /#{sym.to_s.sub '_','.*'}/i
  else
    super
  end
end

Instance Attribute Details

#devicesObject (readonly)

The devices within this collection.



11
12
13
# File 'lib/jsound/midi/device_list.rb', line 11

def devices
  @devices
end

Instance Method Details

#/(regexp) ⇒ Device

Find and open the first device with a description matching the argument.

Returns:

Raises:

  • an error if no device can be found

See Also:



87
88
89
90
# File 'lib/jsound/midi/device_list.rb', line 87

def /(regexp)
  regexp = Regexp.new(regexp.to_s, Regexp::IGNORECASE) if not regexp.kind_of? Regexp
  open regexp
end

#[](criteria) ⇒ Object

Acts like Array#[] for Numeric and Range arguments. Otherwise acts like #find_all.

See Also:



55
56
57
58
59
60
61
# File 'lib/jsound/midi/device_list.rb', line 55

def [](criteria)
  if criteria.kind_of? Fixnum or criteria.kind_of? Range
    @devices[criteria]
  else
    find_all(criteria)
  end
end

#eachObject



17
18
19
20
21
# File 'lib/jsound/midi/device_list.rb', line 17

def each
  for device in devices
    yield device
  end
end

#find(criteria) ⇒ Device

Note:

I/O devices need to be opened before they can be used (see JSound::Midi::Device#open)

Find the first JSound::Midi::Device matching the criteria

Examples:

OUTPUTS.find “IAC Driver Bus 1” #=> find the first output with the exact description “IAC Driver Bus 1”

OUTPUTS.find /IAC/ #=> find the first output with a description containing “IAC”

OUTPUTS.find :vendor => “Apple Inc.” #=> find the first output with the exact vendor field “Apple Inc.”

OUTPUTS.find :vendor => /Apple / #=> find the first output with a vendor field containing “Apple”

Parameters:

  • criteria

    matches description against for a Regexp argument, matches the device field against the value for a Hash argument, otherwise checks for an equal description

Returns:

See Also:



35
36
37
# File 'lib/jsound/midi/device_list.rb', line 35

def find(criteria)
  search :find, criteria
end

#find_all(criteria) ⇒ Array

Note:

I/O devices need to be opened before they can be used (see JSound::Midi::Device#open)

Find all JSound::Midi::Devices matching the criteria

Parameters:

  • criteria

    matches description against for a Regexp argument, matches the device field against the value for a Hash argument, otherwise checks for an equal description

Returns:

See Also:



47
48
49
# File 'lib/jsound/midi/device_list.rb', line 47

def find_all(criteria)
  search :find_all, criteria
end

#open(device) ⇒ Device

Find the first device matching the argument and open it.

Parameters:

  • device

    matches description against for a Regexp argument, matches the device field against the value for a Hash argument, otherwise checks for an equal description

Returns:

Raises:

  • an error if no device can be found

See Also:



71
72
73
74
75
76
77
78
79
# File 'lib/jsound/midi/device_list.rb', line 71

def open(device)
  device = find device unless device.is_a? Device
  if device
    device.open
  else
    raise "Device note found: #{device}"
  end
  device
end

#open_firstDevice

open the first device in this list

Returns:

Raises:

  • an error if no device can be found

See Also:



98
99
100
# File 'lib/jsound/midi/device_list.rb', line 98

def open_first
  open @devices.first
end

#open_lastDevice

open and return the last device in this list

Returns:

Raises:

  • an error if no device can be found

See Also:



108
109
110
# File 'lib/jsound/midi/device_list.rb', line 108

def open_last
  open @devices.first
end

#output=(output) ⇒ Object Also known as: >>

Connect the output to all devices in this device list



113
114
115
116
117
# File 'lib/jsound/midi/device_list.rb', line 113

def output= output
  for device in devices
    device.output = output
  end
end

#to_sObject



120
121
122
# File 'lib/jsound/midi/device_list.rb', line 120

def to_s
  @devices.join("\n")
end