Class: CoreMIDI::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/coremidi/device.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Device) initialize(id, device_pointer, options = {})



9
10
11
12
13
14
15
16
# File 'lib/coremidi/device.rb', line 9

def initialize(id, device_pointer, options = {})
  include_if_offline = options.fetch(:include_offline, false)
  @id = id
  @resource = device_pointer
  @entities = []
  populate_name
  populate_entities(:include_offline => include_if_offline)
end

Instance Attribute Details

- (Object) entities (readonly)

Returns the value of attribute entities



5
6
7
# File 'lib/coremidi/device.rb', line 5

def entities
  @entities
end

- (Object) id (readonly)

Returns the value of attribute id



5
6
7
# File 'lib/coremidi/device.rb', line 5

def id
  @id
end

- (Object) name (readonly)

Returns the value of attribute name



5
6
7
# File 'lib/coremidi/device.rb', line 5

def name
  @name
end

Class Method Details

+ (Array<Device>) all(options = {})

All cached devices

Options Hash (options):

  • :cache (Boolean)

    If false, the device list will never be cached. This would be useful if one needs to alter the device list (e.g. plug in a USB MIDI interface) while their program is running.

  • :include_offline (Boolean)

    If true, devices marked offline by coremidi will be included in the list



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/coremidi/device.rb', line 42

def self.all(options = {})
  use_cache = options[:cache] || true
  include_offline = options[:include_offline] || false
  if @devices.nil? || @devices.empty? || !use_cache
    @devices = []
    i = 0
    while !(device_pointer = Map.MIDIGetDevice(i)).null?
      device = new(i, device_pointer, :include_offline => include_offline)
      @devices << device
      i+=1
    end
    populate_endpoint_ids
  end
  @devices
end

+ (Array<Device>) refresh

Refresh the Device cache. This is needed if, for example a USB MIDI device is plugged in while the program is running



60
61
62
63
# File 'lib/coremidi/device.rb', line 60

def self.refresh
  @devices.clear
  @devices
end

Instance Method Details

- (Array<Endpoint>) endpoints

Endpoints for this device



20
21
22
23
24
25
26
# File 'lib/coremidi/device.rb', line 20

def endpoints
  endpoints = { :source => [], :destination => [] }
  endpoints.keys.each do |k|
    endpoints[k] += entities.map { |entity| entity.endpoints[k] }.flatten
  end
  endpoints
end

- (Integer) populate_endpoint_ids(last_id)

Assign all of this Device's endpoints an consecutive local id



31
32
33
34
35
# File 'lib/coremidi/device.rb', line 31

def populate_endpoint_ids(last_id)
  i = 0
  entities.each { |entity| i += entity.populate_endpoint_ids(i + last_id) }
  i
end