-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (152 loc) · 3.41 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/pkg/errors"
"github.com/rakyll/portmidi"
"github.com/scgolang/midi"
)
func main() {
var (
debug bool
deviceName string
timeout time.Duration
usePortmidi bool
)
flag.BoolVar(&debug, "debug", false, "Debug mode.")
flag.StringVar(&deviceName, "d", "k-board", "MIDI device name.")
flag.BoolVar(&usePortmidi, "p", false, "Use github.com/rakyll/portmidi instead of github.com/scgolang/midi")
flag.DurationVar(&timeout, "t", 2*time.Second, "Timeout during which we expect to receive a note off.")
flag.Parse()
if usePortmidi {
die(withPortmidi(debug, deviceName, timeout))
}
packets, err := getPacketChan(deviceName)
if err != nil {
die(err)
}
var (
ctx = context.Background()
notes = map[byte]time.Time{}
tk = time.NewTicker(20 * time.Millisecond)
waiting = time.NewTicker(10 * time.Second)
)
for {
select {
case <-ctx.Done():
die(ctx.Err())
case <-waiting.C:
die(errors.New("timeout waiting for data"))
case pkts := <-packets:
waiting.Stop()
for _, pkt := range pkts {
if pkt.Err != nil {
die(pkt.Err)
}
if debug {
fmt.Printf("%#v\n", pkt.Data)
continue
}
switch pkt.Data[0] {
case 0x90:
notes[pkt.Data[1]] = time.Now() // Note On
case 0x80:
notes[pkt.Data[1]] = time.Time{} // Note Off
}
}
case <-tk.C:
notes = check(notes, timeout)
}
}
}
func check(notes map[byte]time.Time, timeout time.Duration) map[byte]time.Time {
var (
m = map[byte]time.Time{}
now = time.Now()
)
for note, t := range notes {
if t.IsZero() {
continue
}
if now.Sub(t) < timeout {
m[note] = t
continue
}
fmt.Fprintf(os.Stderr, "Missing Note Off for %d\n", note)
}
return m
}
func die(err error) {
if err == nil {
return
}
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
func getPacketChan(deviceName string) (<-chan []midi.Packet, error) {
devices, err := midi.Devices()
if err != nil {
return nil, err
}
var device *midi.Device
for _, d := range devices {
if strings.Contains(strings.ToLower(d.Name), deviceName) {
device = d
break
}
}
if device == nil {
return nil, errors.New("no device named " + deviceName + " detected")
}
device.QueueSize = 16 // Arbitrary channel buffer size.
if err := device.Open(); err != nil {
return nil, err
}
return device.Packets()
}
func withPortmidi(debug bool, deviceName string, timeout time.Duration) error {
portmidi.Initialize()
defer portmidi.Terminate()
var packets *portmidi.Stream
for i := 0; i < portmidi.CountDevices(); i++ {
info := portmidi.Info(portmidi.DeviceID(i))
if info == nil {
return errors.Errorf("device ID %d out of range", i)
}
if !strings.Contains(strings.ToLower(info.Name), deviceName) {
continue
}
if debug {
fmt.Fprintf(os.Stderr, "deviceID=%d deviceName=%s\n", i, info.Name)
}
stream, err := portmidi.NewInputStream(portmidi.DeviceID(i), 1024)
if err != nil {
return errors.Wrap(err, "creating input stream")
}
packets = stream
break
}
if packets == nil {
return errors.New("could not find device with name " + deviceName)
}
defer func() { _ = packets.Close() }() // Best effort.
var (
ctx = context.Background()
)
for {
select {
case <-ctx.Done():
return ctx.Err()
case event := <-packets.Listen():
if debug {
fmt.Printf("%#v\n", event)
continue
}
}
}
return nil
}