-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi.go
63 lines (55 loc) · 1.08 KB
/
midi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Package midi is a package for talking to midi devices in Go.
package midi
// Packet is a MIDI packet.
type Packet struct {
Data [3]byte
Err error
}
// DeviceType is a flag that says if a device is an input, an output, or duplex.
type DeviceType int
// Device types.
const (
DeviceInput DeviceType = iota
DeviceOutput
DeviceDuplex
)
func (t DeviceType) String() string {
switch t {
case DeviceInput:
return "INPUT"
case DeviceOutput:
return "OUTPUT"
case DeviceDuplex:
return "DUPLEX"
default:
panic("unrecognized device type")
}
}
// Note represents a MIDI note.
type Note struct {
Number int
Velocity int
}
// CC represents a MIDI control change message.
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
}
}