Class: MIDIMessage::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/midi-message/parser.rb

Overview

Simple message parsing For more advanced parsing check out nibbler

Constant Summary collapse

MESSAGE_TYPE =
[
  NoteOff,
  NoteOn,
  PolyphonicAftertouch,
  ControlChange,
  ProgramChange,
  ChannelAftertouch,
  PitchBend,
  SystemMessage
].freeze
SYSTEM_MESSAGE_TYPE =
[
  SystemExclusive,
  SystemCommon,
  SystemRealtime
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MIDIMessage

Can take either a hex string eg Parser.new(“904040”) or bytes eg Parser.new(0x90, 0x40, 0x40) or an array of bytes eg Parser.new([0x90, 0x40, 0x40])

Parameters:

  • args (Array<Fixnum>, *Fixnum, String)


39
40
41
42
43
44
45
# File 'lib/midi-message/parser.rb', line 39

def initialize(*args)
  @data = case args.first
          when Array then args.first
          when Numeric then args
          when String then TypeConversion.hex_string_to_numeric_byte_array(args.first)
          end
end

Class Method Details

.parse(*args) ⇒ MIDIMessage

Can take either a hex string eg Parser.new(“904040”) or bytes eg Parser.new(0x90, 0x40, 0x40) or an array of bytes eg Parser.new([0x90, 0x40, 0x40])

Parameters:

  • args (Array<Fixnum>, *Fixnum, String)

Returns:



29
30
31
32
# File 'lib/midi-message/parser.rb', line 29

def self.parse(*args)
  parser = new(*args)
  parser.parse
end

Instance Method Details

#parseMIDIMessage

Parse the data and return a message

Returns:



49
50
51
52
53
54
55
56
57
# File 'lib/midi-message/parser.rb', line 49

def parse
  nibbles = get_nibbles
  klass = MESSAGE_TYPE.find { |type| type::STATUS == nibbles[0] }
  if klass == SystemMessage
    build_system_message(nibbles[1])
  else
    klass.new(nibbles[1], *@data.drop(1))
  end
end