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



55
56
57
# File 'lib/midi-devices.rb', line 55

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



77
78
79
80
81
82
83
# File 'lib/midi-devices.rb', line 77

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



68
69
70
71
# File 'lib/midi-devices.rb', line 68

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
# File 'lib/midi-devices.rb', line 34

def sync
  names = @low_level_devices.keys

  MIDICommunications::Output.all.each do |low_level_device|
    next if @low_level_devices.key?(low_level_device.name)

    @low_level_devices[low_level_device.name] = MIDIDevice.new(@sequencer, low_level_device)
    names.delete low_level_device.name
  end

  # remove disconnected devices
  #
  names.each do |name|
    @low_level_devices.delete name
  end
end