Class: MIDICommunicationsMacOS::Device
- Inherits:
-
Object
- Object
- MIDICommunicationsMacOS::Device
- Defined in:
- lib/midi-communications-macos/device.rb
Overview
Represents a physical or virtual MIDI device.
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 an Entity.
Devices contain entities, which in turn contain endpoints (sources and destinations).
Instance Attribute Summary collapse
-
#entities ⇒ Array<Entity>
readonly
The device's entities.
-
#id ⇒ Integer
readonly
Unique numeric ID.
- #name ⇒ Object readonly
Class Method Summary collapse
-
.all(options = {}) ⇒ Array<Device>
Returns all available MIDI devices.
-
.populated? ⇒ Boolean
Has the device list been populated?.
-
.refresh ⇒ Array<Device>
Clears the device cache.
Instance Method Summary collapse
-
#endpoints ⇒ Hash{Symbol => Array<Endpoint>}
Returns all endpoints for this device, grouped by type.
-
#initialize(id, device_pointer, include_offline: false) ⇒ Device
constructor
private
Creates a new Device wrapper.
-
#populate_endpoint_ids(last_id) ⇒ Integer
Assign all of this Device's endpoints an consecutive local id.
Constructor Details
#initialize(id, device_pointer, include_offline: false) ⇒ Device
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new Device wrapper.
36 37 38 39 40 41 |
# File 'lib/midi-communications-macos/device.rb', line 36 def initialize(id, device_pointer, include_offline: false) @id = id @resource = device_pointer @entities = [] populate(include_offline: include_offline) end |
Instance Attribute Details
#entities ⇒ Array<Entity> (readonly)
Returns the device's entities.
26 27 28 |
# File 'lib/midi-communications-macos/device.rb', line 26 def entities @entities end |
#id ⇒ Integer (readonly)
Returns unique numeric ID.
26 27 28 |
# File 'lib/midi-communications-macos/device.rb', line 26 attr_reader :entities, :id, :name |
#name ⇒ Object (readonly)
26 27 28 |
# File 'lib/midi-communications-macos/device.rb', line 26 attr_reader :entities, :id, :name |
Class Method Details
.all(options = {}) ⇒ Array<Device>
Returns all available MIDI devices.
Devices are cached by default. Use cache: false to refresh, or call
refresh to clear the cache.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/midi-communications-macos/device.rb', line 77 def self.all( = {}) use_cache = [:cache] || true include_offline = [: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?
105 106 107 |
# File 'lib/midi-communications-macos/device.rb', line 105 def self.populated? defined?(@devices) && !@devices.nil? && !@devices.empty? end |
Instance Method Details
#endpoints ⇒ Hash{Symbol => Array<Endpoint>}
Returns all endpoints for this device, grouped by type.
46 47 48 49 50 51 52 53 |
# File 'lib/midi-communications-macos/device.rb', line 46 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
58 59 60 61 62 |
# File 'lib/midi-communications-macos/device.rb', line 58 def populate_endpoint_ids(last_id) id = 0 entities.each { |entity| id += entity.populate_endpoint_ids(id + last_id) } id end |