Module: MIDICommunications::Input::StreamReader

Included in:
MIDICommunications::Input
Defined in:
lib/midi-communications/input/stream_reader.rb

Overview

Methods for reading MIDI messages from an input device.

Provides multiple methods for retrieving MIDI data in different formats: numeric bytes, hex strings, or raw data arrays.

Instance Method Summary collapse

Instance Method Details

#gets(*args) ⇒ Array<Hash>

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

Parameters:

  • args (Object)

    arguments passed to the underlying device

Returns:

  • (Array<Hash>)

    array of message hashes with :data and :timestamp keys



29
30
31
32
33
# File 'lib/midi-communications/input/stream_reader.rb', line 29

def gets(*args)
  @device.gets(*args)
rescue SystemExit, Interrupt
  exit
end

#gets_data(*args) ⇒ Array<Integer>

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]

Parameters:

  • args (Object)

    arguments passed to the underlying device

Returns:

  • (Array<Integer>)

    flat array of all MIDI bytes



65
66
67
68
# File 'lib/midi-communications/input/stream_reader.rb', line 65

def gets_data(*args)
  arr = gets(*args)
  arr.map { |msg| msg[:data] }.inject(:+)
end

#gets_data_s(*args) ⇒ String Also known as: gets_data_bytestr, gets_data_hex

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"

Parameters:

  • args (Object)

    arguments passed to the underlying device

Returns:

  • (String)

    concatenated hex string of all MIDI data



81
82
83
84
# File 'lib/midi-communications/input/stream_reader.rb', line 81

def gets_data_s(*args)
  arr = gets_bytestr(*args)
  arr.map { |msg| msg[:data] }.join
end

#gets_s(*args) ⇒ Array<Hash> Also known as: gets_bytestr, gets_hex

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 }]

Parameters:

  • args (Object)

    arguments passed to the underlying device

Returns:

  • (Array<Hash>)

    array of message hashes with :data (String) and :timestamp keys



46
47
48
49
50
# File 'lib/midi-communications/input/stream_reader.rb', line 46

def gets_s(*args)
  @device.gets_s(*args)
rescue SystemExit, Interrupt
  exit
end