Class: MIDICommunications::Loader Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Populates MIDI devices from the platform adapter.

The device list is built once and kept, because enumerating is not free — on macOS it walks every device, entity and endpoint Core MIDI knows about — and most programs ask for it far more often than the machine's hardware changes. Loader.refresh is how a program that outlives a hardware change asks for the list again.

What refreshing does not do

A device that is still present comes back as the object it came back as before, rather than as a new wrapper around the same port. That matters because callers hold on to these: Musa::Clock::InputMidiClock keeps an Input for the length of a piece, Musa::MIDIVoices keeps an Output, and Device::InstanceMethods#open registers an at_exit on the instance. Replacing the list wholesale would leave those objects open, still due to be closed at exit, and no longer in Input.all — a device the program is actively using that the program can no longer find.

"Still present" means the same id reporting the same name.

Class Method Summary collapse

Class Method Details

.devices(direction: nil, refresh: false) ⇒ Array<Input>, Array<Output>

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.

Returns all MIDI devices, optionally filtered by direction.

Enumerates on the first call, and then answers from what it found.

Parameters:

  • direction (Symbol, nil) (defaults to: nil)

    :input or :output, or nil for both

  • refresh (Boolean) (defaults to: false)

    ask the platform adapter again first

Returns:



48
49
50
51
52
# File 'lib/midi-communications/loader.rb', line 48

def devices(direction: nil, refresh: false)
  populate if refresh || @devices.nil?

  direction.nil? ? @devices.values.flatten : @devices[direction]
end

.refreshArray<Input, Output>

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.

Asks the platform adapter what devices exist now.

Call this when devices may have been plugged in or unplugged since the program started. Devices that are still there keep their identity; see the note on the class.

Examples:

MIDICommunications::Loader.refresh
MIDICommunications::Output.all   # now includes what was just plugged in

Returns:

  • (Array<Input, Output>)

    every device, after re-enumerating



65
66
67
# File 'lib/midi-communications/loader.rb', line 65

def refresh
  devices(refresh: true)
end

.use(loader) ⇒ Module

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.

Sets the platform-specific loader to use.

Any devices already enumerated are discarded: they came from a different platform adapter and mean nothing to this one.

Parameters:

  • loader (Module)

    a loader answering inputs and outputs, and optionally refresh; see PhysicalLayer

Returns:

  • (Module)

    the loader



34
35
36
37
38
39
# File 'lib/midi-communications/loader.rb', line 34

def use(loader)
  @loader = loader
  @devices = nil

  loader
end