forked from xlab/portmidi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (105 loc) · 2.84 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
package main
import (
"flag"
"log"
"time"
"github.com/rcrowley/go-metrics"
"github.com/xlab/closer"
"github.com/xlab/portmidi"
"github.com/xlab/treeprint"
)
var (
inName = flag.String("in", "", "MIDI device name to use as input.")
outName = flag.String("out", "", "MIDI device name to use as output.")
inDevID = flag.Int("in-dev", -1, "MIDI device ID to use as input.")
outDevID = flag.Int("out-dev", -1, "MIDI device ID to use as output.")
)
func init() {
flag.Parse()
log.SetFlags(log.Lshortfile)
}
func main() {
defer closer.Close()
portmidi.Initialize()
closer.Bind(func() {
portmidi.Terminate()
})
numDevices := portmidi.CountDevices()
log.Println("[INFO] total MIDI devices:", numDevices)
if numDevices < 2 {
closer.Fatalln("[ERR] midipipe cannot operate with less than 2 devices")
}
inputs := make(map[string]portmidi.DeviceID, numDevices)
outputs := make(map[string]portmidi.DeviceID, numDevices)
for i := 0; i < numDevices; i++ {
info := portmidi.GetDeviceInfo(portmidi.DeviceID(i))
if info.IsInputAvailable {
inputs[info.Name] = portmidi.DeviceID(i)
}
if info.IsOutputAvailable {
outputs[info.Name] = portmidi.DeviceID(i)
}
}
log.Println("[INFO] available inputs:", inputs)
log.Println("[INFO] available outputs:", outputs)
inDev := findCandidate(inputs, *inDevID, *inName, true)
outDev := findCandidate(outputs, *outDevID, *outName, false)
inInfo := portmidi.GetDeviceInfo(inDev)
log.Printf("[INFO] input device id=%d %s", inDev, treeprint.Repr(inInfo))
in, err := portmidi.NewInputStream(inDev, 1024, 0)
if err != nil {
closer.Fatalln("[ERR] cannot init an input stream:", err)
}
closer.Bind(func() {
in.Close()
})
outInfo := portmidi.GetDeviceInfo(outDev)
log.Printf("[INFO] output device id=%d %s", outDev, treeprint.Repr(outInfo))
out, err := portmidi.NewOutputStream(outDev, 1024, 0, 0)
if err != nil {
closer.Fatalln("[ERR] cannot init an output stream:", err)
}
closer.Bind(func() {
out.Close()
log.Println("bye!")
})
meter := metrics.NewMeter()
go func() {
t := time.NewTicker(time.Minute)
for range t.C {
snap := meter.Snapshot()
log.Println("[DBG] rate 1 minute", snap.Rate1())
log.Println("[DBG] rate 5 minute", snap.Rate5())
log.Println("[DBG] rate mean", snap.RateMean())
}
}()
go func() {
sink := out.Sink()
for ev := range in.Source() {
meter.Mark(int64(1))
sink <- ev
}
}()
closer.Hold()
}
func findCandidate(devices map[string]portmidi.DeviceID,
id int, name string, input bool) (dev portmidi.DeviceID) {
if id >= 0 {
dev = portmidi.DeviceID(id)
return
}
if len(name) > 0 {
nameID, ok := devices[name]
if ok {
dev = nameID
return
}
closer.Fatalln("[ERR] midipipe was unable to locate required device:", name)
}
if input {
dev, _ = portmidi.DefaultInputDeviceID()
return
}
dev, _ = portmidi.DefaultOutputDeviceID()
return
}