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



144
145
146
147
148
149
150
151
152
# File 'lib/jsound/midi/device_list.rb', line 144

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:



81
82
83
84
# File 'lib/jsound/midi/device_list.rb', line 81

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:



49
50
51
52
53
54
55
# File 'lib/jsound/midi/device_list.rb', line 49

def [](criteria)
  if criteria.kind_of? Fixnum or criteria.kind_of? Range
    @devices[criteria]
  else
    find_all(criteria)
  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:



29
30
31
# File 'lib/jsound/midi/device_list.rb', line 29

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:



41
42
43
# File 'lib/jsound/midi/device_list.rb', line 41

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:



65
66
67
68
69
70
71
72
73
# File 'lib/jsound/midi/device_list.rb', line 65

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:



92
93
94
# File 'lib/jsound/midi/device_list.rb', line 92

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:



102
103
104
# File 'lib/jsound/midi/device_list.rb', line 102

def open_last
  open @devices.first
end

#to_sObject



106
107
108
# File 'lib/jsound/midi/device_list.rb', line 106

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