Class: MIDICommunications::Output

Inherits:
Object
  • Object
show all
Extended by:
Device::ClassMethods
Includes:
Device::InstanceMethods
Defined in:
lib/midi-communications/output.rb

Overview

A MIDI output device for sending MIDI messages.

Output devices send MIDI data to external instruments, software synthesizers, or other MIDI destinations. Use the class methods to discover and select available output devices.

Examples:

List available outputs

MIDICommunications::Output.list

Send a note to the first output

output = MIDICommunications::Output.first
output.puts(0x90, 60, 100)  # Note On, middle C, velocity 100
sleep(0.5)
output.puts(0x80, 60, 0)    # Note Off

Send messages as hex strings

output = MIDICommunications::Output.first
output.puts_s("904060")     # Note On
output.puts_s("804060")     # Note Off

Interactive selection

output = MIDICommunications::Output.gets

Find by name

output = MIDICommunications::Output.find_by_name("IAC Driver Bus 1")
output.open

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directionSymbol (readonly) Originally defined in module Device::InstanceMethods

Returns the device direction (:input or :output).

Returns:

  • (Symbol)

    the device direction (:input or :output)

#display_nameString (readonly) Originally defined in module Device::InstanceMethods

Returns the device display name.

Returns:

  • (String)

    the device display name

#enabledBoolean (readonly) Originally defined in module Device::InstanceMethods

Returns whether the device is currently open.

Returns:

  • (Boolean)

    whether the device is currently open

#idInteger (readonly) Originally defined in module Device::InstanceMethods

Returns the device ID.

Returns:

  • (Integer)

    the device ID

#manufacturerString (readonly) Originally defined in module Device::InstanceMethods

Returns the device manufacturer name.

Returns:

  • (String)

    the device manufacturer name

#modelString (readonly) Originally defined in module Device::InstanceMethods

Returns the device model name.

Returns:

  • (String)

    the device model name

#nameString (readonly) Originally defined in module Device::InstanceMethods

Returns the device name.

Returns:

  • (String)

    the device name

Class Method Details

.allArray<Output>

Returns all available MIDI output devices.

Examples:

outputs = MIDICommunications::Output.all
outputs.each { |o| puts o.name }

Returns:

  • (Array<Output>)

    array of output devices



43
44
45
# File 'lib/midi-communications/output.rb', line 43

def self.all
  Loader.devices(direction: :output)
end

.at(index) ⇒ Input, Output Also known as: [] Originally defined in module Device::ClassMethods

Returns the device at the given index without opening it.

Examples:

device = MIDICommunications::Output.at(0)
device.open if device

Parameters:

  • index (Integer)

    device index

Returns:

.each {|device| ... } ⇒ Enumerator Originally defined in module Device::ClassMethods

Iterates over all devices of this type.

Examples:

MIDICommunications::Output.each { |o| puts o.name }

Yields:

  • (device)

    each device

Yield Parameters:

Returns:

  • (Enumerator)

    if no block given

.find_by_name(name) ⇒ Input, ... Originally defined in module Device::ClassMethods

Finds a device by its name.

Examples:

output = MIDICommunications::Output.find_by_name("IAC Driver Bus 1")

Parameters:

  • name (String, Symbol)

    the device name to search for

Returns:

  • (Input, Output, nil)

    the matching device or nil if not found

.first {|device| ... } ⇒ Input, Output Originally defined in module Device::ClassMethods

Selects and opens the first available device.

Examples:

output = MIDICommunications::Output.first

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Returns:

.gets {|device| ... } ⇒ Input, Output Originally defined in module Device::ClassMethods

Interactive console prompt for device selection.

Displays available devices and waits for user input. When a valid selection is received, the device is opened and returned.

Examples:

output = MIDICommunications::Output.gets
# Select a MIDI output...
# 0) IAC Driver Bus 1
# > 0

Yields:

  • (device)

    optional block to execute with the opened device

Yield Parameters:

Returns:

.last {|device| ... } ⇒ Input, Output Originally defined in module Device::ClassMethods

Selects and opens the last available device.

Examples:

output = MIDICommunications::Output.last

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Returns:

.listArray<String> Originally defined in module Device::ClassMethods

Prints the ID and name of each device to the console.

Examples:

MIDICommunications::Output.list
# 0) IAC Driver Bus 1
# 1) USB MIDI Device

Returns:

  • (Array<String>)

    array of formatted device names

.use(index) {|device| ... } ⇒ Input, Output Also known as: open Originally defined in module Device::ClassMethods

Selects and opens the device at the given index.

Examples:

output = MIDICommunications::Output.use(0)

Parameters:

  • index (Integer, Symbol)

    device index or :first/:last

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Returns:

Instance Method Details

#close(*args) ⇒ Boolean Originally defined in module Device::InstanceMethods

Closes the device.

Examples:

output.close

Parameters:

  • args (Object)

    arguments passed to the underlying device

Returns:

  • (Boolean)

    true if the device was closed, false if already closed

#closed?Boolean Originally defined in module Device::InstanceMethods

Returns true if the device is closed (not enabled).

Returns:

  • (Boolean)

    true if device is closed

#enabled?Boolean Originally defined in module Device::InstanceMethods

Returns alias for #enabled.

Returns:

#initialize(device) ⇒ Object Originally defined in module Device::InstanceMethods

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.

Creates a new device wrapper.

Parameters:

  • device (Object)

    platform-specific device object

#open(*args) {|device| ... } ⇒ Input, Output Originally defined in module Device::InstanceMethods

Opens the device for use.

When a block is given, the device is automatically closed when the block exits. Otherwise, the device is closed at program exit.

Examples:

Open and close automatically with block

output.open do |o|
  o.puts(0x90, 60, 100)
end  # device closed here

Open manually (closed at program exit)

output.open
output.puts(0x90, 60, 100)

Parameters:

  • args (Object)

    arguments passed to the underlying device

Yields:

  • (device)

    optional block to execute with the open device

Yield Parameters:

Returns:

#puts(*messages) ⇒ Array<Integer>, Array<String>

Sends a MIDI message to the output.

Accepts multiple message formats for flexibility:

  • Numeric bytes: output.puts(0x90, 0x40, 0x40)
  • Array of numeric bytes: output.puts([0x90, 0x40, 0x40])
  • Hex string: output.puts("904040")
  • Array of strings: output.puts(["904040", "804040"])
  • Objects with to_bytes method: output.puts(midi_event)

Examples:

Send Note On as bytes

output.puts(0x90, 60, 100)

Send Note On as array

output.puts([0x90, 60, 100])

Send Note On as hex string

output.puts("903C64")

Parameters:

  • messages (Array<Integer>, Array<String>, Integer, String)

    MIDI messages in any supported format

Returns:

  • (Array<Integer>, Array<String>)

    the messages sent



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/midi-communications/output.rb', line 67

def puts(*messages)
  message = messages.first
  case message
  when Array then messages.each { |array| puts(*array.flatten) }
  when Integer then puts_bytes(*messages)
  when String then puts_s(*messages)
  else
    if message.respond_to?(:to_bytes)
      puts_bytes(*message.to_bytes.flatten)
    elsif message.respond_to?(:to_a)
      puts_bytes(*message.to_a.flatten)
    end
  end
end

#puts_bytes(*messages) ⇒ Integer+

Sends a MIDI message as numeric bytes.

This is a lower-level method that does not perform type checking. Use #puts for automatic format detection.

Examples:

output.puts_bytes(0x90, 0x40, 0x40)  # Note On, note 64, velocity 64

Parameters:

  • messages (Integer)

    numeric byte values (e.g., 0x90, 0x40, 0x40)

Returns:

  • (Integer, Array<Integer>)

    the message bytes sent



110
111
112
113
# File 'lib/midi-communications/output.rb', line 110

def puts_bytes(*messages)
  @device.puts_bytes(*messages)
  messages.count < 2 ? messages[0] : messages
end

#puts_s(*messages) ⇒ String+ Also known as: puts_bytestr, puts_hex

Sends a MIDI message as a hex string.

This is a lower-level method that does not perform type checking. Use #puts for automatic format detection.

Examples:

output.puts_s("904060")  # Note On
output.puts_s("804060")  # Note Off

Parameters:

  • messages (String)

    one or more hex strings (e.g., "904040")

Returns:

  • (String, Array<String>)

    the message(s) sent



93
94
95
96
# File 'lib/midi-communications/output.rb', line 93

def puts_s(*messages)
  @device.puts_s(*messages)
  messages.count < 2 ? messages[0] : messages
end

#typeSymbol Originally defined in module Device::InstanceMethods

Returns alias for #direction.

Returns: