Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit f5ceb71

Browse files
committed
Parse key pressure events
1 parent 70b842e commit f5ceb71

File tree

3 files changed

+5
-0
lines changed

3 files changed

+5
-0
lines changed

lib/coremidi.rb

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def self.parse(data)
1616
klass = {
1717
Constants::NOTE_ON => lambda {|data| (data[Events::NoteOn.members.index("velocity")] == 0) ? Events::NoteOff : Events::NoteOn },
1818
Constants::NOTE_OFF => Events::NoteOff,
19+
Constants::KEY_PRESSURE => Events::KeyPressure,
1920
Constants::PROGRAM_CHANGE => Events::ProgramChange
2021
}.detect {|constant, klass|
2122
data[0] & Constants::TYPE == constant
@@ -33,6 +34,7 @@ def self.parse(data)
3334
module Events
3435
class NoteOn < Struct.new(:channel, :pitch, :velocity); end;
3536
class NoteOff < Struct.new(:channel, :pitch, :velocity); end;
37+
class KeyPressure < Struct.new(:channel, :pitch, :pressure); end;
3638
class ProgramChange < Struct.new(:channel, :preset); end;
3739
class Unknown < Struct.new(:data); end;
3840
end

lib/coremidi/constants.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Constants
66

77
NOTE_OFF = 0x80
88
NOTE_ON = 0x90
9+
KEY_PRESSURE = 0xA0
910
PROGRAM_CHANGE = 0xC0
1011
end
1112
end

spec/parsing_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def self.it_parses(data, expected)
2525
it_parses([0x81, 0x3C, 0x40], CoreMIDI::Events::NoteOff.new(0x01, 0x3C, 0x40)) # Channel 1, Middle C, half velocity
2626
it_parses([0xC0, 0x01], CoreMIDI::Events::ProgramChange.new(0x00, 0x01)) # Channel 0, Preset #1
2727
it_parses([0xC1, 0x02], CoreMIDI::Events::ProgramChange.new(0x01, 0x02)) # Channel 1, Preset #2
28+
it_parses([0xA0, 0x3C, 0x64], CoreMIDI::Events::KeyPressure.new(0x00, 0x3C, 0x64)) # Channel 0, Middle C, half pressure
29+
it_parses([0xA1, 0x3C, 0xFF], CoreMIDI::Events::KeyPressure.new(0x01, 0x3C, 0xFF)) # Channel 1, Middle C, full pressure
2830

2931
# This is technically a NoteOn event, but convention uses it most often in place of a note off event (setting velocity to 0)
3032
it_parses([0x90, 0x3C, 0x00], CoreMIDI::Events::NoteOff.new(0x00, 0x3C, 0x00)) # Channel 0, Middle C, no velocity

0 commit comments

Comments
 (0)