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.

Blocks until at least one message has arrived, and then returns every message that accumulated. It never returns an empty array.

This is part of the contract every platform adapter implements, not an accident of one of them — see PhysicalLayer. A reader loop therefore needs no delay of its own, and adding one only delays the messages that are already waiting.

Examples:

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

Read messages as they arrive

loop do
  input.gets.each { |message| puts message[:data].inspect }
end

Parameters:

  • args (Object)

    arguments passed to the underlying device

Returns:

  • (Array<Hash>)

    message hashes with :data and :timestamp keys

See Also:



34
35
36
37
38
# File 'lib/midi-communications/input/stream_reader.rb', line 34

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.

Blocks as #gets does, and 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



71
72
73
74
# File 'lib/midi-communications/input/stream_reader.rb', line 71

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



87
88
89
90
# File 'lib/midi-communications/input/stream_reader.rb', line 87

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.

Blocks exactly as #gets does, and returns the same messages with :data as a hex string instead of an array of bytes.

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



52
53
54
55
56
# File 'lib/midi-communications/input/stream_reader.rb', line 52

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