diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..84038c6 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +test: + @go test + +.PHONY: test diff --git a/launchpad_test.go b/launchpad_test.go deleted file mode 100644 index b1c5aa7..0000000 --- a/launchpad_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package midi - -import ( - "fmt" - "testing" - "time" -) - -func TestLaunchpad(t *testing.T) { - // This test can be run if you have a MIDI device with ID "hw:1,0,0" - // It will tell you if this package is able to talk to your device. - // The messages that are sent here are specific to the Novation Launchpad Mini. - // The reason this package exists is because of issues that popped up when - // trying to use github.com/rakyll/portmidi to talk to the launchpad on Linux. - // For the launchpad MIDI reference, see https://d19ulaff0trnck.cloudfront.net/sites/default/files/novation/downloads/4080/launchpad-programmers-reference.pdf - t.SkipNow() - - devices, err := Devices() - if err != nil { - t.Fatal(err) - } - var device *Device - for _, d := range devices { - if d.Name == "Launchpad Mini" { - device = d - } - } - if device == nil { - t.Fatal("getting device Launchpad Mini") - } - if err := device.Open(); err != nil { - t.Fatal(err) - } - if _, err := device.Write([]byte{0xB0, 0x00, 0x00}); err != nil { - t.Fatal(err) - } - fmt.Println("reset buttons") - - if _, err := device.Write([]byte{0xB0, 0x00, 0x7D}); err != nil { - t.Fatal(err) - } - fmt.Println("buttons should be lit") - - time.Sleep(2 * time.Second) - - if _, err := device.Write([]byte{0xB0, 0x00, 0x00}); err != nil { - t.Fatal(err) - } - fmt.Println("reset buttons") - - // Test hangs here until you send some MIDI data! - packets, err := device.Packets() - if err != nil { - t.Fatal(err) - } - - fmt.Println("waiting for packet") - packet := <-packets - if packet.Err != nil { - t.Fatal(err) - } - fmt.Printf("packet %#v\n", packet) - - if err := device.Close(); err != nil { - t.Fatal(err) - } -} diff --git a/list_test.go b/list_test.go index e13bf10..32d3c0b 100644 --- a/list_test.go +++ b/list_test.go @@ -1,19 +1,11 @@ package midi import ( - "fmt" "testing" ) func TestDevices(t *testing.T) { - devices, err := Devices() - if err != nil { + if _, err := Devices(); err != nil { t.Fatal(err) } - for i, d := range devices { - if d == nil { - continue - } - fmt.Printf("device %d: %#v\n", i, *d) - } } diff --git a/midi.go b/midi.go index 466d36b..68b3530 100644 --- a/midi.go +++ b/midi.go @@ -28,3 +28,23 @@ type CC struct { Number int Value int } + +const ( + MessageTypeUnknown = iota + MessageTypeCC + MessageTypeNoteOff + MessageTypeNoteOn + MessageTypePolyKeyPressure +) + +// GetMessageType returns the message type for the provided packet. +func GetMessageType(p Packet) int { + switch p.Data[0] & 0xF0 { + case 0x80: + return MessageTypeNoteOff + case 0x90: + return MessageTypeNoteOn + default: + return MessageTypeUnknown + } +} diff --git a/midi_test.go b/midi_test.go new file mode 100644 index 0000000..4bf3ae7 --- /dev/null +++ b/midi_test.go @@ -0,0 +1,28 @@ +package midi + +import ( + "testing" +) + +func TestGetMessageType(t *testing.T) { + for _, tc := range []struct { + Expect int + Input Packet + Name string + }{ + { + Expect: MessageTypeNoteOn, + Input: Packet{Data: [3]uint8{0x90, 0x4f, 0x16}}, + Name: "Note On message type", + }, + { + Expect: MessageTypeNoteOff, + Input: Packet{Data: [3]uint8{0x80, 0x4f, 0x0}}, + Name: "Note Off message type", + }, + } { + if expect, got := tc.Expect, GetMessageType(tc.Input); expect != got { + t.Fatalf("%s: expected %d, got %d", tc.Name, expect, got) + } + } +}