Class: MIDICommunicationsMacOS::Device
- Inherits:
-
Object
- Object
- MIDICommunicationsMacOS::Device
- 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.
Instance Attribute Summary collapse
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.all(options = {}) ⇒ Array<Device>
All cached devices.
-
.populated? ⇒ Boolean
Has the device list been populated?.
-
.refresh ⇒ Array<Device>
Refresh the Device cache.
Instance Method Summary collapse
-
#endpoints ⇒ Array<Endpoint>
Endpoints for this device.
-
#initialize(id, device_pointer, include_offline: false) ⇒ Device
constructor
A new instance of Device.
-
#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
Returns a new instance of Device.
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
#entities ⇒ Object (readonly)
Returns the value of attribute entities.
8 9 10 |
# File 'lib/midi-communications-macos/device.rb', line 8 def entities @entities end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/midi-communications-macos/device.rb', line 8 def id @id end |
#name ⇒ Object (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
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( = {}) 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?
71 72 73 |
# File 'lib/midi-communications-macos/device.rb', line 71 def self.populated? defined?(@devices) && !@devices.nil? && !@devices.empty? end |
.refresh ⇒ Array<Device>
Refresh the Device cache. This is needed if, for example a USB MIDI device is plugged in while the program is running
65 66 67 68 |
# File 'lib/midi-communications-macos/device.rb', line 65 def self.refresh @devices.clear @devices end |
Instance Method Details
#endpoints ⇒ Array<Endpoint>
Endpoints for this device
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
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 |