Skip to content

Commit b35e2e9

Browse files
committed
add GetMessageType
1 parent b95127c commit b35e2e9

File tree

5 files changed

+53
-76
lines changed

5 files changed

+53
-76
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test:
2+
@go test
3+
4+
.PHONY: test

launchpad_test.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

list_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
package midi
22

33
import (
4-
"fmt"
54
"testing"
65
)
76

87
func TestDevices(t *testing.T) {
9-
devices, err := Devices()
10-
if err != nil {
8+
if _, err := Devices(); err != nil {
119
t.Fatal(err)
1210
}
13-
for i, d := range devices {
14-
if d == nil {
15-
continue
16-
}
17-
fmt.Printf("device %d: %#v\n", i, *d)
18-
}
1911
}

midi.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,23 @@ type CC struct {
2828
Number int
2929
Value int
3030
}
31+
32+
const (
33+
MessageTypeUnknown = iota
34+
MessageTypeCC
35+
MessageTypeNoteOff
36+
MessageTypeNoteOn
37+
MessageTypePolyKeyPressure
38+
)
39+
40+
// GetMessageType returns the message type for the provided packet.
41+
func GetMessageType(p Packet) int {
42+
switch p.Data[0] & 0xF0 {
43+
case 0x80:
44+
return MessageTypeNoteOff
45+
case 0x90:
46+
return MessageTypeNoteOn
47+
default:
48+
return MessageTypeUnknown
49+
}
50+
}

midi_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package midi
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetMessageType(t *testing.T) {
8+
for _, tc := range []struct {
9+
Expect int
10+
Input Packet
11+
Name string
12+
}{
13+
{
14+
Expect: MessageTypeNoteOn,
15+
Input: Packet{Data: [3]uint8{0x90, 0x4f, 0x16}},
16+
Name: "Note On message type",
17+
},
18+
{
19+
Expect: MessageTypeNoteOff,
20+
Input: Packet{Data: [3]uint8{0x80, 0x4f, 0x0}},
21+
Name: "Note Off message type",
22+
},
23+
} {
24+
if expect, got := tc.Expect, GetMessageType(tc.Input); expect != got {
25+
t.Fatalf("%s: expected %d, got %d", tc.Name, expect, got)
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)