-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_controller.go
179 lines (145 loc) · 3.6 KB
/
midi_controller.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
package main
import (
"fmt"
"github.com/mozillazg/go-unidecode"
"gitlab.com/gomidi/midi/mid"
"log"
"strings"
)
type MidiController struct {
driver mid.Driver
name string
in mid.In
out mid.Out
writer *mid.Writer
reader *mid.Reader
}
func NewMidiController(driver mid.Driver, name string) *MidiController {
return &MidiController{
driver: driver,
name: name,
}
}
const (
CcFader uint8 = 70
CcLedRing uint8 = 80
CcLedMeter uint8 = 90
NoteEncoder uint8 = 0
NoteTime uint8 = 1
NotePrevious uint8 = 20
NoteNext uint8 = 21
NoteStop uint8 = 22
NotePlay uint8 = 23
NoteBankLeft uint8 = 25
NoteBankRight uint8 = 26
NoteFader uint8 = 110
ColorBlack uint8 = 0
ColorRed uint8 = 1
ColorGreen uint8 = 2
ColorYellow uint8 = 3
ColorBlue uint8 = 4
ColorMagenta uint8 = 5
ColorCyan uint8 = 6
ColorWhite uint8 = 7
InvertNone uint8 = 0
InvertTop uint8 = 1
InvertBottom uint8 = 2
InvertBoth uint8 = 3
)
func (c *MidiController) OpenOut() error {
outs, err := c.driver.Outs()
if err != nil {
return err
}
for _, out := range outs {
if strings.HasPrefix(out.String(), c.name) {
log.Printf("opening out port with name %s\n", out.String())
c.out = out
err := c.out.Open()
if err != nil {
return err
}
c.writer = mid.ConnectOut(c.out)
c.writer.ConsolidateNotes(false)
return nil
}
}
return fmt.Errorf("no midi output found starting with name %s", c.name)
}
func (c *MidiController) OpenIn() error {
ins, err := c.driver.Ins()
if err != nil {
return err
}
for _, in := range ins {
if strings.HasPrefix(in.String(), c.name) {
log.Printf("opening in port with name %s\n", in.String())
c.in = in
err := c.in.Open()
if err != nil {
return err
}
c.reader = mid.NewReader(mid.NoLogger())
return mid.ConnectIn(c.in, c.reader)
}
}
return fmt.Errorf("no midi input found starting with name %s", c.name)
}
func (c *MidiController) Close() error {
if c.in != nil {
c.in.Close()
}
if c.out != nil {
c.out.Close()
}
return nil
}
func (c *MidiController) Reset() error {
for n := uint8(1); n <= 35; n++ {
err := c.writer.NoteOn(n, 0)
if err != nil {
return err
}
}
c.writer.ControlChange(CcFader, 0)
c.writer.ControlChange(CcLedRing, 64)
c.writer.ControlChange(CcLedMeter, 0)
c.writer.SysEx(c.CreateSegmentDisplayData(EmptySegmentDisplayData()))
c.writer.SysEx(c.CreateLcdDisplayData("", ColorBlack, InvertNone))
return nil
}
func (c *MidiController) CreateLcdDisplayData(characters string, color uint8, invert uint8) []byte {
data := make([]byte, 14)
copy(data, unidecode.Unidecode(characters))
colorCode := color | (invert << 4)
return append([]byte{0x00, 0x20, 0x32, 0x41, 0x4c, 0x00, colorCode}, data...)
}
func (c *MidiController) CreateSegmentDisplayData(data SegmentDisplayData) []byte {
return append([]byte{0x00, 0x20, 0x32, 0x41, 0x37}, append(data.text, data.dots...)...)
}
type SegmentDisplayData struct {
text []byte
dots []byte
}
func EmptySegmentDisplayData() SegmentDisplayData {
return SegmentDisplayData{
text: make([]byte, 12),
dots: make([]byte, 2),
}
}
func NewSegmentDisplayData(text string) SegmentDisplayData {
textBytes := make([]byte, 12)
copy(textBytes, unidecode.Unidecode(text))
return SegmentDisplayData{
text: lcd7bitRender(textBytes),
dots: lcd7bitRenderDots(textBytes),
}
}
func NewSegmentDisplayDataTime(text string) SegmentDisplayData {
textBytes := make([]byte, 12)
copy(textBytes, unidecode.Unidecode(text))
return SegmentDisplayData{
text: lcd7bitRender(textBytes),
dots: []byte{0x50, 0x00},
}
}