-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
306 lines (278 loc) · 7.97 KB
/
connection.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
MIT License
Copyright (c) 2020 Star Brilliant
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"bufio"
"fmt"
"io"
"net"
"sort"
"strings"
"sync"
"time"
"github.com/m13253/midimark"
"golang.org/x/crypto/ssh"
)
const DefaultTimeout = 1 * time.Minute
type connection struct {
AppConf *config
ConnConf *connConfig
KnownHosts ssh.HostKeyCallback
Songs []song
DebugChanMessage chan<- debugEventMessage
DebugChanNote chan<- debugEventNote
OnConnected *sync.WaitGroup
StartTime <-chan time.Time
}
type note struct {
SongID int
Event midimark.Event
MTrk *midimark.MTrk
SongStart time.Duration
}
func (c *connection) Start() error {
notes := c.loadNotes()
port := c.ConnConf.Port
if port == "" {
port = "22"
}
addr := net.JoinHostPort(c.ConnConf.Host, port)
sshConf := &ssh.ClientConfig{
User: c.ConnConf.Username,
Auth: []ssh.AuthMethod{
ssh.Password(c.ConnConf.Password),
},
HostKeyCallback: c.KnownHosts,
BannerCallback: func(message string) error {
sc := bufio.NewScanner(strings.NewReader(message))
for sc.Scan() {
c.DebugChanMessage <- debugEventMessage{
Hostname: c.ConnConf.Name,
Message: sc.Text(),
}
}
return nil
},
Timeout: DefaultTimeout,
}
c.DebugChanMessage <- debugEventMessage{
Hostname: c.ConnConf.Name,
Message: fmt.Sprintf("Connecting to %s", addr),
}
sshClient, err := ssh.Dial("tcp", addr, sshConf)
if err != nil {
c.OnConnected.Done()
<-c.StartTime
return err
}
defer sshClient.Close()
sshSession, err := sshClient.NewSession()
if err != nil {
c.OnConnected.Done()
<-c.StartTime
return err
}
defer sshSession.Close()
var stdoutFinished sync.WaitGroup
stdoutFinished.Add(1)
stdout := c.pipeToStdout(&stdoutFinished)
defer stdoutFinished.Wait()
defer stdout.Close()
sshSession.Stdout = stdout
sshSession.Stderr = stdout
var stdin *io.PipeWriter
sshSession.Stdin, stdin = io.Pipe()
err = sshSession.Shell()
if err != nil {
stdin.Close()
c.OnConnected.Done()
<-c.StartTime
return err
}
defer sshSession.Wait()
defer stdin.Close()
c.DebugChanMessage <- debugEventMessage{
Hostname: c.ConnConf.Name,
Message: fmt.Sprintf("Connected to %s", addr),
}
c.OnConnected.Done()
startTime, ok := <-c.StartTime
if !ok {
panic("internal error: start time is invalid")
}
currentRPN := uint16(0x0000)
currentData := uint16(0xffff)
RPN := [3]uint16{
0x00: 0x0100, // Pitch wheel range: (value>>7)+(value&0x7f)/100 semitones
0x01: 0x2000, // Fine tuning: (value-0x2000)/8192 semitones
0x02: 0x2000, // Coarse tuning: (value>>7)-0x40 semitones
}
pitchWheel := [16]int16{0}
for _, note := range notes {
songAbsTick := note.Event.Common().AbsTick
songAbsTime := note.MTrk.ConvertAbsTickToDuration(songAbsTick)
startAbsTime := note.SongStart + songAbsTime
durationToSleep := startAbsTime - time.Now().Sub(startTime)
time.Sleep(durationToSleep)
switch event := note.Event.(type) {
case *midimark.EventNoteOn:
var frequency float64
if event.Channel != 10 {
pitchWheelRange := float64(RPN[0]>>7) + float64(RPN[0]&0x7f)/100
pitchWheelValue := float64(pitchWheel[event.Channel-1]) * float64(pitchWheelRange) / 8192
fineTuning := (float64(RPN[1]) - 0x2000) / 8192
coarseTuning := float64(RPN[2]>>7) - 0x40
pitch := float64(event.Key) + pitchWheelValue + fineTuning + coarseTuning
frequency = midiNoteToHertz(pitch)
} else {
frequency = 20
}
var length time.Duration
if event.RelatedNoteOff != nil {
songAbsTickOff := event.RelatedNoteOff.AbsTick
songAbsTimeOff := note.MTrk.ConvertAbsTickToDuration(songAbsTickOff)
length = songAbsTimeOff - songAbsTime
} else {
length = 1 * time.Second
}
if length <= 0 {
continue
}
var lengthMilli int64
if event.Channel != 10 {
lengthMilli = int64((length + 999999*time.Nanosecond) / time.Millisecond)
} else {
lengthMilli = 1
}
_, err := fmt.Fprintf(stdin, ":beep as-value frequency=%.0f length=%dms;\n", frequency, lengthMilli)
if err != nil {
return err
}
select {
case c.DebugChanNote <- debugEventNote{
Hostname: c.ConnConf.Name,
Frequency: frequency,
LengthMilli: lengthMilli,
}:
default:
}
case *midimark.EventPitchWheelChange:
pitchWheel[event.Channel-1] = event.Pitch
case *midimark.EventControlChange:
switch event.Control {
case 0x06: // Data entry MSB
currentData = uint16(event.Value)<<7 | (currentData & 0x407f)
if (currentData&0xc000) == 0 && currentRPN <= 2 {
RPN[currentRPN] = currentData
}
case 0x26: // Data entry LSB
currentData = (currentData & 0xbf80) | uint16(event.Value)
if (currentData&0xc000) == 0 && currentRPN <= 2 {
RPN[currentRPN] = currentData
}
case 0x60: // Data +1
if currentRPN <= 2 {
RPN[currentRPN] = (RPN[currentRPN] + 1) & 0x3fff
}
case 0x61: // Data -1
if currentRPN <= 2 {
RPN[currentRPN] = (RPN[currentRPN] - 1) & 0x3fff
}
case 0x64: // RPN LSB
currentRPN = (currentRPN & 0xbf80) | uint16(event.Value)
case 0x65: // RPN MSB
currentRPN = uint16(event.Value)<<7 | (currentRPN & 0x407f)
}
}
}
c.DebugChanMessage <- debugEventMessage{
Hostname: c.ConnConf.Name,
Message: "Closing connection",
}
return nil
}
func (c *connection) loadNotes() []note {
songStart := time.Duration(0)
var notes []note
for songID, song := range c.Songs {
for trackID, mtrk := range song.Sequence.Tracks {
if _, ok := c.ConnConf.Tracks.Map[uint16(trackID)]; !ok {
if !c.ConnConf.Tracks.OtherTracks {
continue
}
if _, ok := c.AppConf.TracksDefined[uint16(trackID)]; ok {
continue
}
}
for _, event := range mtrk.Events {
switch event := event.(type) {
case *midimark.EventNoteOn, *midimark.EventPitchWheelChange:
case *midimark.EventControlChange:
switch event.Control {
case 0x06: // Data entry MSB
case 0x26: // Data entry LSB
case 0x60: // Data +1
case 0x61: // Data -1
case 0x64: // RPN LSB
case 0x65: // RPN MSB
default:
continue
}
default:
continue
}
notes = append(notes, note{
SongID: songID,
Event: event,
MTrk: mtrk,
SongStart: songStart,
})
}
}
songStart += song.Duration
}
sort.Slice(notes, func(i, j int) bool {
if notes[i].SongID < notes[j].SongID {
return true
}
if notes[i].SongID == notes[j].SongID {
commonI, commonJ := notes[i].Event.Common(), notes[j].Event.Common()
return commonI.AbsTick < commonJ.AbsTick ||
(commonI.AbsTick == commonJ.AbsTick && commonI.FilePosition == commonJ.FilePosition)
}
return false
})
return notes
}
func (c *connection) pipeToStdout(wg *sync.WaitGroup) *io.PipeWriter {
r, w := io.Pipe()
go func(r *io.PipeReader, name string, wg *sync.WaitGroup) {
sc := bufio.NewScanner(r)
for sc.Scan() {
c.DebugChanMessage <- debugEventMessage{
Hostname: c.ConnConf.Name,
Message: sc.Text(),
}
}
r.Close()
wg.Done()
}(r, c.ConnConf.Name, wg)
return w
}