Skip to content

Commit

Permalink
add GetMessageType
Browse files Browse the repository at this point in the history
  • Loading branch information
briansorahan committed Sep 17, 2017
1 parent b95127c commit b35e2e9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 76 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
@go test

.PHONY: test
67 changes: 0 additions & 67 deletions launchpad_test.go

This file was deleted.

10 changes: 1 addition & 9 deletions list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
20 changes: 20 additions & 0 deletions midi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
28 changes: 28 additions & 0 deletions midi_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit b35e2e9

Please sign in to comment.