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 = {})

A new instance of Device



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/coremidi/device.rb', line 13

def initialize(id, device_pointer, options = {})
  include_if_offline = options[:include_offline] || false
  @id = id
  @resource = device_pointer
  @entities = []
  
  prop = Map::CF.CFStringCreateWithCString( nil, "name", 0 )
  name = Map::CF.CFStringCreateWithCString( nil, id.to_s, 0 )
  Map::MIDIObjectGetStringProperty(@resource, prop, name)

  @name = Map::CF.CFStringGetCStringPtr(name.read_pointer, 0).read_string
  populate_entities(:include_offline => include_if_offline)
end

Instance Attribute Details

- (Object) entities (readonly)

Returns the value of attribute entities



7
8
9
# File 'lib/coremidi/device.rb', line 7

def entities
  @entities
end

- (Object) id (readonly)

Returns the value of attribute id



7
8
9
# File 'lib/coremidi/device.rb', line 7

def id
  @id
end

- (Object) name (readonly)

Returns the value of attribute name



7
8
9
# File 'lib/coremidi/device.rb', line 7

def name
  @name
end

Class Method Details

+ (Object) all(options = {})

returns all devices which are cached in an instance variable @devices on the Device class

options:

  • cache: 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: if true, devices marked offline by coremidi will be included in the list



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/coremidi/device.rb', line 34

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

+ (Object) refresh

Refresh the Device cash. You'll need to do this if, for instance, you plug in a USB MIDI device while the program is running



52
53
54
# File 'lib/coremidi/device.rb', line 52

def self.refresh
  @devices.clear
end

Instance Method Details

- (Object) endpoints



56
57
58
59
60
61
62
# File 'lib/coremidi/device.rb', line 56

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

- (Object) populate_endpoint_ids(starting_id)



64
65
66
67
68
# File 'lib/coremidi/device.rb', line 64

def populate_endpoint_ids(starting_id)
  i = 0
  entities.each_with_index { |entity| i += entity.populate_endpoint_ids(i + starting_id) }
  i
end