Class: MIDICommunicationsMacOS::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/midi-communications-macos/device.rb

Overview

A MIDI device may have multiple logically distinct sub-components. For example, one device may encompass a MIDI synthesizer and a pair of MIDI ports, both addressable via a USB port. Each such element of a device is called a MIDI entity.

developer.apple.com/library/ios/documentation/CoreMidi/Reference/MIDIServices_Reference/Reference/reference.html

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, device_pointer, include_offline: false) ⇒ Device

Returns a new instance of Device.

Parameters:

  • id (Integer)

    The ID for the device

  • device_pointer (Object)

    The underlying device pointer

  • include_offline (Boolean) (defaults to: false)

    Whether to include offline entities (default: false)



15
16
17
18
19
20
# File 'lib/midi-communications-macos/device.rb', line 15

def initialize(id, device_pointer, include_offline: false)
  @id = id
  @resource = device_pointer
  @entities = []
  populate(include_offline: include_offline)
end

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



8
9
10
# File 'lib/midi-communications-macos/device.rb', line 8

def entities
  @entities
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/midi-communications-macos/device.rb', line 8

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/midi-communications-macos/device.rb', line 8

def name
  @name
end

Class Method Details

.all(options = {}) ⇒ Array<Device>

All cached devices

Parameters:

  • options (Hash) (defaults to: {})

    The options to select devices with

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 midi-communications-macos will be included in the list

Returns:

  • (Array<Device>)

    All cached devices



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/midi-communications-macos/device.rb', line 47

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

.populated?Boolean

Has the device list been populated?

Returns:

  • (Boolean)


71
72
73
# File 'lib/midi-communications-macos/device.rb', line 71

def self.populated?
  defined?(@devices) && !@devices.nil? && !@devices.empty?
end

.refreshArray<Device>

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

Returns:

  • (Array<Device>)

    The Device cache



65
66
67
68
# File 'lib/midi-communications-macos/device.rb', line 65

def self.refresh
  @devices.clear
  @devices
end

Instance Method Details

#endpointsArray<Endpoint>

Endpoints for this device

Returns:



24
25
26
27
28
29
30
31
# File 'lib/midi-communications-macos/device.rb', line 24

def endpoints
  endpoints = { source: [], destination: [] }
  endpoints.each_key do |key|
    endpoint_group = entities.map { |entity| entity.endpoints[key] }.flatten
    endpoints[key] += endpoint_group
  end
  endpoints
end

#populate_endpoint_ids(last_id) ⇒ Integer

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

Parameters:

  • last_id (Integer)

    The highest already used endpoint ID

Returns:

  • (Integer)

    The highest used endpoint ID after populating this device’s endpoints



36
37
38
39
40
# File 'lib/midi-communications-macos/device.rb', line 36

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