Class: MusaLCEServer::MIDIDevices

Inherits:
Object
  • Object
show all
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.

Examples:

Iterating over devices

midi_devices.each { |device| puts device.name }

Finding a device by name suffix

device = midi_devices.find('IAC Driver Bus 1')

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(sequencer) ⇒ MIDIDevices

Creates a new MIDI devices manager.

Parameters:

  • sequencer (Musa::Sequencer::Sequencer)

    the sequencer for MIDI voice management

Since:

  • 0.1.0



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.

Parameters:

  • name (String)

    the exact device name

Returns:

  • (MIDIDevice, nil)

    the device or nil if not found

Since:

  • 0.1.0



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.

Yields:

Returns:

  • (Enumerator)

    if no block given

Since:

  • 0.1.0



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.

Examples:

device = midi_devices.find('Bus 1')  # Matches 'IAC Driver Bus 1'

Parameters:

  • name (String)

    the name suffix to match

Returns:

  • (MIDIDevice, nil)

    the first matching device or nil

Since:

  • 0.1.0



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

#syncvoid

This method returns an undefined value.

Synchronizes the device list with system MIDI devices.

Adds newly connected devices and removes disconnected ones.

Since:

  • 0.1.0



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