Class: MusaLCEServer::MIDIDevices
- Inherits:
-
Object
- Object
- MusaLCEServer::MIDIDevices
- Includes:
- Enumerable
- Defined in:
- lib/midi-devices.rb
Overview
Manages available MIDI output devices.
Provides enumeration and lookup of MIDI devices, automatically synchronizing with the system's available devices.
Instance Method Summary collapse
-
#[](name) ⇒ MIDIDevice?
Retrieves a device by exact name.
-
#each {|MIDIDevice| ... } ⇒ Enumerator
Iterates over all MIDI devices.
-
#find(name) ⇒ MIDIDevice?
Finds a device by name suffix.
-
#initialize(sequencer) ⇒ MIDIDevices
constructor
Creates a new MIDI devices manager.
-
#sync ⇒ void
Synchronizes the device list with system MIDI devices.
Constructor Details
#initialize(sequencer) ⇒ MIDIDevices
Creates a new MIDI devices manager.
22 23 24 25 26 27 |
# File 'lib/midi-devices.rb', line 22 def initialize(sequencer) @sequencer = sequencer @low_level_devices = {} sync end |
Instance Method Details
#[](name) ⇒ MIDIDevice?
Retrieves a device by exact name.
65 66 67 |
# File 'lib/midi-devices.rb', line 65 def [](name) @low_level_devices[name] end |
#each {|MIDIDevice| ... } ⇒ Enumerator
Iterates over all MIDI devices.
87 88 89 90 91 92 93 |
# File 'lib/midi-devices.rb', line 87 def each(&block) if block_given? @low_level_devices.values.each(&block) else @low_level_devices.values.each end end |
#find(name) ⇒ MIDIDevice?
Finds a device by name suffix.
Useful when device names include prefixes that vary by system.
78 79 80 81 |
# File 'lib/midi-devices.rb', line 78 def find(name) full_name = @low_level_devices.keys.find { |_| _.end_with?(name) } @low_level_devices[full_name] end |
#sync ⇒ void
This method returns an undefined value.
Synchronizes the device list with system MIDI devices.
Adds newly connected devices and removes disconnected ones.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/midi-devices.rb', line 34 def sync # Without this, MIDICommunications answers from the list it built the # first time it was asked, and nothing plugged in since would ever be # seen -- which made the sentence above false. MIDICommunications::Loader.refresh missing = @low_level_devices.keys MIDICommunications::Output.all.each do |low_level_device| name = low_level_device.name # Outside the guard below, deliberately: seeing a device is what marks # it as still connected, whether or not it is one we already had. missing.delete name next if @low_level_devices.key?(name) @low_level_devices[name] = MIDIDevice.new(@sequencer, low_level_device) end # remove disconnected devices # missing.each do |name| @low_level_devices.delete name end end |