-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_pong.go
142 lines (117 loc) · 2.98 KB
/
ping_pong.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"time"
"github.com/rakyll/portmidi"
"github.com/wcharczuk/go-chart"
)
func main() {
portmidi.Initialize()
inPtr := flag.Uint("in", 0, "In Device ID")
outPtr := flag.Uint("out", 0, "Out Device ID")
graphTitlePtr := flag.String("graph-title", "", "Graph Title")
graphFileNamePtr := flag.String("graph-filename", "results/output.png", "Graph Filename")
listPtr := flag.Bool("list", false, "List Devices")
flag.Parse()
if *listPtr {
listDevices()
return
}
times, durations := pingPong(portmidi.DeviceID(*inPtr),
portmidi.DeviceID(*outPtr))
saveGraph(times, durations, *graphTitlePtr, *graphFileNamePtr)
}
func listDevices() {
for id := 0; id < portmidi.CountDevices(); id++ {
inf := portmidi.Info(portmidi.DeviceID(id))
fmt.Printf("ID: %d\tName: %s\tInput: %t\tOutput: %t\n",
id,
inf.Name,
inf.IsInputAvailable,
inf.IsOutputAvailable)
}
}
func pingPong(inID portmidi.DeviceID,
outID portmidi.DeviceID) (times []float64, durations []float64) {
fmt.Printf("In: %v\n", portmidi.Info(inID).Name)
fmt.Printf("Out: %v\n", portmidi.Info(outID).Name)
in, err := portmidi.NewInputStream(inID, 1024)
exitOnError(err)
defer in.Close()
out, err := portmidi.NewOutputStream(outID, 1024, 0)
exitOnError(err)
defer out.Close()
globalStartTime := time.Now()
for time.Now().Sub(globalStartTime) < time.Second*30 {
startTime := time.Now()
pingSysEx := []byte{0xF0, 0x00, 0x22, 0x77, 0x01, 0xF7}
err = out.WriteSysExBytes(portmidi.Time(), pingSysEx)
exitOnError(err)
timestamp := time.Now().Sub(globalStartTime)
fmt.Printf("%v: Ping? ", timestamp)
waitForEvent(in)
event, err := in.ReadSysExBytes(6)
exitOnError(err)
pongSysEx := []byte{0xF0, 0x00, 0x22, 0x77, 0x02, 0xF7, 0x00, 0x00}
res := bytes.Compare(event, pongSysEx)
if res == 0 {
rtt := time.Now().Sub(startTime)
fmt.Printf("Pong! (%v)\n", rtt)
times = append(times, float64(timestamp.Seconds()))
durations = append(durations, float64(rtt.Nanoseconds())/1000000.0)
} else {
fmt.Printf("Mismatch! %02x\n", event)
}
time.Sleep(time.Millisecond * 20)
}
return times, durations
}
func saveGraph(
times []float64,
durations []float64,
graphTitle string,
graphFileName string) {
graph := chart.Chart{
Title: graphTitle,
XAxis: chart.XAxis{
ValueFormatter: func(v interface{}) string {
if vf, isFloat := v.(float64); isFloat {
return fmt.Sprintf("%0.2f", vf)
}
return ""
},
Name: "Time (seconds)",
},
YAxis: chart.YAxis{
Name: "Round-trip time (ms)",
},
Series: []chart.Series{
chart.ContinuousSeries{
XValues: times,
YValues: durations,
},
},
}
f, _ := os.Create(graphFileName)
defer f.Close()
err := graph.Render(chart.PNG, f)
exitOnError(err)
}
func waitForEvent(stream *portmidi.Stream) {
for {
ready, err := stream.Poll()
exitOnError(err)
if ready {
break
}
time.Sleep(time.Microsecond)
}
}
func exitOnError(err error) {
if err != nil {
panic(err.Error())
}
}