Repository navigation
Expand file tree
/
Copy pathcontroller.go
More file actions
executable file
·615 lines (502 loc) · 13.2 KB
/
Copy pathcontroller.go
File metadata and controls
executable file
·615 lines (502 loc) · 13.2 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package xtouchmini
/// X-Touch Mini
/// Handles communication with the MIDI interface.
/// X-Touch Mini are in MC mode and use the default configuration
///
/// Pt1 Pt2 Pt3 Pt4 Pt5 Pt6 Pt7 Pt8 |
/// P1 P2 P3 P4 P5 P6 P7 P8 |
/// |
/// Btn01 Btn02 Btn03 Btn04 Btn05 Btn06 Btn07 Btn08 | LayA
/// |
/// Btn09 Btn10 Btn11 Btn12 Btn13 Btn14 Btn15 Btn16 | LayB
///
/// Fader
///
import (
"errors"
"fmt"
"strings"
"time"
"gitlab.com/gomidi/midi/v2"
"gitlab.com/gomidi/midi/v2/drivers"
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
)
const (
XTouchName = "X-TOUCH MINI"
MIDI_TIMEOUT = time.Second * 5
)
// LEDtype (led state)
type LEDtype byte
const (
LEDoff LEDtype = 0
LEDblk LEDtype = 1
LEDon LEDtype = 127
)
func (l LEDtype) String() string {
switch l {
case LEDoff:
return "off"
case LEDblk:
return "blk"
case LEDon:
return "on"
}
return "?"
}
// LEDtype (led state)
type LayerLED byte
const (
LayerA_LED LayerLED = iota
LayerB_LED
)
func (l LayerLED) String() string {
if l < LayerA_LED || l > LayerB_LED {
return "?"
}
return [...]string{"Layer A LED", "Layer B LED"}[l]
}
// RxType (recieve state from external device)
type RxType byte
const (
RxVoid RxType = iota // Void (unkown type)
Pot // Pots 1..8 (P1..P8)
Push // Push pot knob (Pt1..Pt8)
Button // Button (Btn01..Btn16)
Layer // Layer (LayA, LayB)
Fader // Main Fader (Fader)
)
func (r RxType) String() string {
if r < RxVoid || r > Fader {
return "?"
}
return [...]string{"Void", "Pot", "Push", "Button", "Layer", "Fader"}[r]
}
// RxMsg message received from external device
type RxMsg struct {
Type RxType
No int
Val int
}
func (m RxMsg) String() string {
return fmt.Sprintf("%-8s %6d %6d", m.Type, m.No, m.Val)
}
// RingMode
type RingMode byte
const (
RingSingle RingMode = iota
RingTrim
RingFan
RingSpread
)
func (r RingMode) String() string {
if r < RingSingle || r > RingSpread {
return "?"
}
return [...]string{"Single", "Trim", "Fan", "Spread"}[r]
}
// XTouchMini object
type XTouchMini struct {
midiOut drivers.Out
midiIn drivers.In
stopMidi func()
onError func(error)
mcMidiChannel byte
globalMidiChannel byte
listener func(msg RxMsg, timestampms int32)
sysExData chan []byte
}
type Config func(*XTouchMini)
// SetMidiChannel sets the MIDI channel for the XTouchMini.
//
// The channel must be in the range 0..15.
// The default channel is 0.
func SetMidiChannel(channel byte) Config {
return func(xtouch *XTouchMini) {
xtouch.mcMidiChannel = channel
}
}
// onError sets the error handling function for the midi driver of the XTouchMini.
//
// The onError function will be called if an error occurs when reading or writing to the midi port.
// The function should not block, as it will be called in a separate goroutine.
func OnError(onError func(error)) Config {
return func(xtouch *XTouchMini) {
xtouch.onError = onError
}
}
// throwError calls the error handling function if it is set.
func (xtouch *XTouchMini) throwError(err error) {
if xtouch.onError != nil {
xtouch.onError(err)
}
}
// Listener is a function that is called when a message is received from the device.
type Listener func(msg RxMsg, timestampms int32)
// NewXTouchMini creates a new XTouchMini object.
//
// The listener function will be called when a message is received from the device.
// The onError function will be called if an error occurs when reading or writing to the midi port.
func NewXTouchMini(listener Listener, conf ...Config) *XTouchMini {
xtouch := XTouchMini{
listener: listener,
sysExData: make(chan []byte),
mcMidiChannel: 0,
}
// get from conf
for _, c := range conf {
c(&xtouch)
}
return &xtouch
}
// GlobalMidiChannel returns the global midi channel for the XTouchMini
func (xtouch *XTouchMini) GlobalMidiChannel() byte {
return xtouch.globalMidiChannel
}
// McMidiChannel returns the midi channel for MC mode of the XTouchMini
func (xtouch *XTouchMini) McMidiChannel() byte {
return xtouch.mcMidiChannel
}
// MidiOut returns the midi out for the XTouchMini
func (xtouch *XTouchMini) MidiOut() drivers.Out {
return xtouch.midiOut
}
// MidiIn returns the midi in for the XTouchMini
func (xtouch *XTouchMini) MidiIn() drivers.In {
return xtouch.midiIn
}
// isConnect returns true if the XTouchMini is connected, wihout any initialtion or source request
func (xtouch *XTouchMini) isConnect() bool {
found := false
for i := 0; i < len(midi.GetOutPorts()); i++ {
if strings.Contains(midi.GetOutPorts()[i].String(), XTouchName) {
found = true
break
}
}
return found
}
// IsInit returns true if the XTouchMini is connected and initialized
func (xtouch *XTouchMini) IsInit() bool {
return xtouch.isConnect() && xtouch.stopMidi != nil
}
// Init XTouchMini connect to midi , sets the global midi channel, switch to MC mode and start listening
func (xtouch *XTouchMini) Init() (err error) {
// close old connection and establish new
xtouch.Close()
err = xtouch.connectToMidi()
if err == nil {
xtouch.stopMidi, err = midi.ListenTo(xtouch.midiIn,
func(msg midi.Message, timestampms int32) {
xtouch.midiListener(msg, timestampms)
}, midi.UseSysEx(), midi.HandleError(xtouch.throwError),
)
}
if err != nil {
return err
}
// get device info and set global midi channel
bts, err := xtouch.deviceInfoBytes()
if err != nil {
return err
}
xtouch.globalMidiChannel = bts[1]
err = xtouch.modeSwitch(1)
if err != nil {
return err
}
return err
}
// Close MIDI connection
func (xtouch *XTouchMini) Close() {
isConnect := xtouch.isConnect()
if xtouch.stopMidi != nil {
xtouch.stopMidi()
xtouch.stopMidi = nil
}
if xtouch.midiIn != nil {
if isConnect {
_ = xtouch.midiIn.Close()
}
xtouch.midiIn = nil
}
if xtouch.midiOut != nil {
if isConnect {
_ = xtouch.midiOut.Close()
}
xtouch.midiOut = nil
}
}
// if not connected, connect
func (xtouch *XTouchMini) connectToMidi() (err error) {
// check if xtouch is already init, you must call Close() before establish a new connection
if xtouch.IsInit() || xtouch.midiIn != nil || xtouch.midiOut != nil {
return ErrMidiAlreadyInit
}
// establish connection
if xtouch.midiOut == nil {
xtouch.midiOut, err = midi.FindOutPort(XTouchName)
if err == nil {
xtouch.midiIn, err = midi.FindInPort(XTouchName)
}
if err != nil {
xtouch.Close()
return err
}
}
return nil
}
// Reset XTouch, Layer LEDs off, Button LEDs off, Ring LEDs off
func (xtouch *XTouchMini) Reset() (err error) {
// check connection
if !xtouch.IsInit() {
return ErrNoMidi
}
// Layer LEDs off
err = xtouch.SetLayerLED(LayerA_LED, LEDoff)
if err != nil {
return err
}
err = xtouch.SetLayerLED(LayerB_LED, LEDoff)
if err != nil {
return err
}
// Button LEDs off
for no := 1; no <= 16; no++ {
err = xtouch.SetBtnLED(no, LEDoff)
if err != nil {
return err
}
}
// Ring LEDs off
for no := 1; no <= 8; no++ {
err = xtouch.SetRingLED(no, RingSingle, 0)
if err != nil {
return err
}
}
return err
}
// convert Xtouch number to RxType + index
func (xtouch *XTouchMini) msg(no byte) (rxtype RxType, ix byte) {
switch {
case no >= 16 && no <= 23: //Pots 1..8
ix = no - 15
rxtype = Pot
case no >= 32 && no <= 39: //Pots 1..8
ix = no - 31
rxtype = Push
case no >= 84 && no <= 85: //Layer
ix = no - 83
rxtype = Layer
// Button LEDs (89, 90, 40, 41, 42, 43, 44, 45, 87, 88, 91, 92, 86, 93, 94, 95)
case no >= 89 && no <= 90: //Button 1..2
ix = no - 88
rxtype = Button
case no >= 40 && no <= 45: //Button 3..8
ix = no - 37
rxtype = Button
case no >= 87 && no <= 88: //ButtonLED 9..10
ix = no - 78
rxtype = Button
case no >= 91 && no <= 92: //ButtonLED 11..12
ix = no - 80
rxtype = Button
case no == 86: //ButtonLED 13
ix = no - 73
rxtype = Button
case no >= 93 && no <= 95: //ButtonLED 14..16
ix = no - 79
rxtype = Button
case no == 255: //Fader
ix = 0
rxtype = Fader
default:
rxtype = RxVoid
ix = 0
}
return rxtype, ix
}
// Set Button LED
func (xtouch *XTouchMini) SetBtnLED(no int, val LEDtype) (err error) {
// MIDI message addresses
//Buttons 1-8,9-16: MIDI Note
midiButtons := [16]byte{89, 90, 40, 41, 42, 43, 44, 45, 87, 88, 91, 92, 86, 93, 94, 95}
// transmit msg to device
// check val
if val > 127 {
return ErrInvalidData
}
if no > 16 {
return ErrBtnRange
}
err = xtouch.send(midi.NoteOn(xtouch.mcMidiChannel, midiButtons[no-1], byte(val)))
if err != nil {
return err
}
return err
}
// Set Ring LED
func (xtouch *XTouchMini) SetRingLED(no int, mode RingMode, val byte) (err error) {
// MIDI message addresses
midiPot := [8]byte{48, 49, 50, 51, 52, 53, 54, 55}
// check no
if no < 1 || no > 8 {
return ErrTurnRange
}
// check val
if (val > 11) || ((mode == RingSpread) && (val > 6)) {
return ErrInvalidData
}
// transmit msg to device
// 0 = off, 1-11 = single, 17-27 = trim, 33-43 = fan, 49-54 = spread
if val > 0 {
switch mode {
case RingSingle:
val += 0
case RingTrim:
val += 16
case RingFan:
val += 32
case RingSpread:
val += 48
}
}
err = xtouch.send(midi.ControlChange(xtouch.mcMidiChannel, midiPot[no-1], val))
if err != nil {
return err
}
return err
}
// Set Layer LED
func (xtouch *XTouchMini) SetLayerLED(t LayerLED, val LEDtype) (err error) {
// MIDI message addresses
// Layer A/B : MIDI Note
midiLayer := [2]byte{84, 85}
if t > 1 {
return ErrLayerRange
}
// transmit msg to device
return xtouch.send(midi.NoteOn(xtouch.mcMidiChannel, midiLayer[t], byte(val)))
}
func (xtouch *XTouchMini) midiListener(msg midi.Message, timestamp int32) {
var channel byte
var crtltype RxType
var no byte
var midiKey byte
var midiVal byte
var pitchKey int16
var pitchVal uint16
var bt []byte
// get channel
msg.GetChannel(&channel)
if msg.GetNoteOn(nil, &midiKey, &midiVal) {
crtltype, no = xtouch.msg(midiKey)
if crtltype == RxVoid {
xtouch.throwError(errors.New("NoteOn error: " + msg.String()))
}
// slog.Debug(fmt.Sprintf("NoteOn %v %v", midiKey, midiVal))
} else if msg.GetNoteOff(nil, &midiKey, &midiVal) {
crtltype, no = xtouch.msg(midiKey)
if crtltype == RxVoid {
xtouch.throwError(errors.New("NoteOff error: " + msg.String()))
}
// slog.Debug(fmt.Sprintf("NoteOff %v %v", midiKey, midiVal))
} else if msg.GetControlChange(nil, &midiKey, &midiVal) {
crtltype, no = xtouch.msg(midiKey)
if crtltype == RxVoid {
xtouch.throwError(errors.New("ControlChange error: " + msg.String()))
}
// slog.Debug(fmt.Sprintf("Controllchange %v %v", midiKey, midiVal))
} else if msg.GetSysEx(&bt) {
// slog.Debug(fmt.Sprintf("SysEx: %v ", bt))
go func() { xtouch.sysExData <- bt }()
return
} else if msg.GetPitchBend(&channel, &pitchKey, &pitchVal) {
crtltype = Fader
if crtltype == RxVoid {
xtouch.throwError(errors.New("pichbend error: " + msg.String()))
}
// slog.Debug(fmt.Sprintf("Pichbend %v %v", pitchKey, pitchVal))
} else {
// Msg-Typ wird nicht unterstützt
xtouch.throwError(errors.New("unsupported " + msg.String()))
return
}
if xtouch.listener != nil {
if crtltype == Fader {
xtouch.listener(RxMsg{crtltype, int(pitchKey), int(pitchVal)}, timestamp)
return
} else {
xtouch.listener(RxMsg{crtltype, int(no), int(midiVal)}, timestamp)
return
}
}
xtouch.throwError(errors.New("No listener found " + msg.String()))
}
// MidiChannel returns the midi channelfor MC mode of the XTouchMini
func (xtouch *XTouchMini) MidiChannel() byte {
return xtouch.mcMidiChannel
}
// send a midi message
func (xtouch *XTouchMini) send(msg midi.Message) (err error) {
if xtouch.midiOut == nil {
return ErrNoMidi
}
if !xtouch.midiOut.IsOpen() {
err = xtouch.midiOut.Open()
}
if err == nil {
err = xtouch.midiOut.Send(msg)
}
return err
}
// modeSwitch (0=Standard mode, 1=MC mode)
func (xtouch *XTouchMini) modeSwitch(mode byte) (err error) {
msg := midi.ControlChange(xtouch.globalMidiChannel, 127, mode)
return xtouch.send(msg)
}
// Device info as bytes: ??, Chanal Mode, ??, Version, Version, Layer, ??
func (xtouch *XTouchMini) deviceInfoBytes() (bts []byte, err error) {
// SysEx request info
return xtouch.sysex(0x51, 0, 0, 0, 0, 0, 0, 0, 0)
}
// Device info values as string: ??, Channel, Mode, ??, Version, Version, Layer, ??
func (xtouch *XTouchMini) DeviceInfoString() (str string, err error) {
bts, err := xtouch.deviceInfoBytes()
return strings.Trim(fmt.Sprint(bts), "[]"), err
}
// Device info values as string: ??, Channel, Mode, ??, Version, Version, Layer, ??
func (xtouch *XTouchMini) DeviceInfoText() (str string, err error) {
bts, err := xtouch.deviceInfoBytes()
if err != nil {
return "", err
}
if len(bts) < 7 {
err = ErrInfoFormat
return "", err
}
str = fmt.Sprintf("channel=%v, mode=%v, version=%v.%v, layer=%v", bts[1], bts[2], bts[4], bts[5], bts[6])
return str, err
}
// send sysex to device
func (xtouch *XTouchMini) sysex(send ...byte) (bts []byte, err error) {
// SysEx request
msg := []byte{0xF0, 0x40, 0x41, 0x42}
msg = append(msg, send...)
msg = append(msg, 0xF7)
err = xtouch.send(msg)
if err != nil {
return nil, err
}
select {
case bts := <-xtouch.sysExData:
if len(bts) < 7 {
err = ErrSysEx
return nil, err
}
slc := bts[4:]
return slc, nil
case <-time.After(MIDI_TIMEOUT):
return nil, ErrMidiTimeout
}
}