-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
84 lines (73 loc) · 1.69 KB
/
log.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
package main
import (
"fmt"
"github.com/fossegrim/midimap/lang/matcher"
"gitlab.com/gomidi/midi"
"gitlab.com/gomidi/midi/reader"
)
// logCommandModifier corresponds to the log command modifier. args corresponds
// to the list of arguments listed on the command line after the log command
// modifier.
//
// For documentation about the log command modifier itself, consult midimap(1).
func logCommandModifier(args []string) error {
// Parse args
var m matcher.Matcher
var receivedMatcher bool
switch len(args) {
case 2:
var err error
m, err = matcher.Parse(args[1])
if err != nil {
return err
}
case 1:
default:
return errUsage
}
portNumber, err := parsePortNumber(args[0])
if err != nil {
return err
}
drv, err := newDriver()
if err != nil {
return err
}
defer drv.Close()
ins, err := drv.Ins()
if err != nil {
return err
}
in, err := getInByPortNumber(ins, portNumber)
if err != nil {
return err
}
err = in.Open()
if err != nil {
return err
}
defer in.Close()
rd := reader.New(
reader.NoLogger(),
reader.Each(func(pos *reader.Position, msg midi.Message) {
if !receivedMatcher || matcherMatchesMessage(m, msg) {
fmt.Println("---===---")
fmt.Printf("%s\n", msg)
fmt.Printf("status: %d\n", msg.Raw()[0])
fmt.Printf("data1: %d\n", msg.Raw()[1])
fmt.Printf("data2: %d\n", msg.Raw()[2])
}
}),
)
// I don't understand how and why this snippet works, but it does.
// I asked for an explanation in https://github.com/vipul-sharma20/midi-macro/issues/1.
// TODO: Read chapter 8 in the go programming language, which hopefully explains this.
exit := make(chan string)
go rd.ListenTo(in)
for {
select {
case <-exit:
return nil
}
}
}