Class: MIDICommunications::Input

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

Overview

A MIDI input device for receiving MIDI messages.

Input devices receive MIDI data from external controllers, instruments, or other MIDI sources. Use the class methods to discover and select available input devices.

Examples:

List available inputs

MIDICommunications::Input.list

Open the first input and read messages

input = MIDICommunications::Input.first
messages = input.gets
# => [{ data: [144, 60, 100], timestamp: 1024 }]

Interactive selection

input = MIDICommunications::Input.gets

Find by name

input = MIDICommunications::Input.find_by_name("USB MIDI Device")
input.open

See Also:

Defined Under Namespace

Modules: StreamReader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

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

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

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

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

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

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

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

Class Method Details

.allArray<Input>

Returns all available MIDI input devices.

Examples:

inputs = MIDICommunications::Input.all
inputs.each { |i| puts i.name }


41
42
43
# File 'lib/midi-communications/input.rb', line 41

def self.all
  Loader.devices(direction: :input)
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

.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:

.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")

.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:

.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:

.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:

.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

.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)

Yields:

  • (device)

    optional block to execute with the device

Yield Parameters:

Instance Method Details

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

Closes the device.

Examples:

output.close

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

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

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

#gets(*args) ⇒ Array<Hash> Originally defined in module StreamReader

Reads MIDI messages from the input buffer.

Returns all messages received since the last read. Each message includes the MIDI data and a timestamp.

Examples:

messages = input.gets
# => [{ data: [144, 60, 100], timestamp: 1024 },
#     { data: [128, 60, 100], timestamp: 1100 }]

Process messages in a loop

loop do
  messages = input.gets
  messages.each { |m| puts m[:data].inspect }
  sleep(0.01)
end

#gets_data(*args) ⇒ Array<Integer> Originally defined in module StreamReader

Reads MIDI data as a flat array of bytes.

Returns all message data concatenated into a single array, without timestamps.

Examples:

data = input.gets_data
# => [144, 60, 100, 128, 60, 100, 144, 40, 120]

#gets_data_s(*args) ⇒ String Also known as: gets_data_bytestr, gets_data_hex Originally defined in module StreamReader

Reads MIDI data as a concatenated hex string.

Returns all message data concatenated into a single hex string, without timestamps.

Examples:

data = input.gets_data_s
# => "90406080406090447F"

#gets_s(*args) ⇒ Array<Hash> Also known as: gets_bytestr, gets_hex Originally defined in module StreamReader

Reads MIDI messages as hex strings.

Similar to #gets but returns data as hex strings instead of byte arrays.

Examples:

messages = input.gets_s
# => [{ data: "904060", timestamp: 904 },
#     { data: "804060", timestamp: 1150 }]

#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.

#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)

Yields:

  • (device)

    optional block to execute with the open device

Yield Parameters:

#typeSymbol Originally defined in module Device::InstanceMethods