Class: CoreMIDI::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/coremidi.rb

Class Method Summary collapse

Class Method Details

.parse(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/coremidi.rb', line 15

def self.parse(data)
  spec = {
    0x80 => Events::NoteOff,
    0x90 => lambda {|data| (data[Events::NoteOn.members.index("velocity")] == 0) ? Events::NoteOff : Events::NoteOn },
    0xA0 => Events::KeyPressure,
    0xC0 => Events::ProgramChange,
    0xD0 => Events::ChannelPressure
  }
  
  klass = spec.detect {|code, _|
    data[0] & 0xF0 == code # First byte is the type code
  }

  return Events::Unknown.new(data) if klass.nil?

  klass = klass.last
  klass = klass.call(data) if klass.respond_to?(:call) # Resolve any lambdas into a class

  klass.new(
    data[0] & 0x0F, # Second byte contains the channel
    *data[1..-1]
  )
end