-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
250 lines (233 loc) · 5.25 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
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
package main
import (
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/manifoldco/promptui"
"gitlab.com/gomidi/midi/v2"
"gitlab.com/gomidi/midi/v2/drivers"
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv" // autoregisters driver
)
func SelectInPort() (drivers.In, error) {
inPorts := midi.GetInPorts()
if len(inPorts) == 0 {
return nil, errors.New("No input MIDI devices found")
}
if len(inPorts) == 1 {
return inPorts[0], nil
}
prompt := promptui.Select{
Label: "Input Device",
Items: inPorts,
}
idx, _, err := prompt.Run()
if err != nil {
return nil, err
}
return inPorts[idx], nil
}
func SelectOutPort() (drivers.Out, error) {
outPorts := midi.GetOutPorts()
if len(outPorts) == 0 {
return nil, errors.New("No output MIDI devices found")
}
if len(outPorts) == 1 {
return outPorts[0], nil
}
prompt := promptui.Select{
Label: "Output Device",
Items: outPorts,
}
idx, _, err := prompt.Run()
if err != nil {
return nil, err
}
return outPorts[idx], nil
}
func SelectPattern() ([]midi.Interval, error) {
prompt := promptui.Select{
Label: "Pattern",
Items: []string{
"1-3-5-8-5-3-1",
"5-4-3-2-1",
"1-3-5-3-1",
},
}
idx, _, err := prompt.Run()
if err != nil {
return nil, err
}
return [][]midi.Interval{
// 1-3-5-8-5-3-1
{
midi.Unison,
midi.MajorThird,
midi.Fifth,
midi.Octave,
midi.Fifth,
midi.MajorThird,
midi.Unison,
},
// 5-4-3-2-1
{
midi.Fifth,
midi.Fourth,
midi.MajorThird,
midi.MajorSecond,
midi.Unison,
},
// 1-3-5-3-1
{
midi.Unison,
midi.MajorThird,
midi.Fifth,
midi.MajorThird,
midi.Unison,
},
}[idx], nil
}
const channel = 0
const velocity = 80
const tempo = 100 // bpm
// HandleInputs is a state machine using a channel of incoming midi messages.
// It uses goto statements to handle state transitions. It calls playback when
// it is time to play the arpeggio, and returns when the channel is closed.
// Some people would likely scoff at the use of gotos here but it makes things really
// easy imo
func HandleInputs(messages chan midi.Message, playback func(midi.Note)) {
timeout := time.Duration(1 / (tempo / 60.0) * 2.2 /* wiggle room */ * float64(time.Second))
// TODO: determine tempo from distance between pitches?
INITIAL:
msg1, ok := <-messages
if !ok {
return // channel closed
}
var msg1Note uint8
var msg3Note uint8
var c uint8
var v uint8
if !msg1.GetNoteStart(&c, &msg1Note, &v) || c != channel {
goto INITIAL
}
FIRST_DOWN:
select {
case msg2, ok := <-messages:
if !ok {
return // channel closed
}
var msg2Note uint8
if !msg2.GetNoteEnd(&c, &msg2Note) || c != channel || msg2Note != msg1Note {
goto FIRST_DOWN // keep waiting
}
case <-time.After(timeout):
goto INITIAL
}
FIRST_UP:
select {
case msg3, ok := <-messages:
if !ok {
return // channel closed
}
if !msg3.GetNoteStart(&c, &msg3Note, &v) || c != channel {
goto FIRST_UP // keep waiting
}
case <-time.After(timeout):
goto INITIAL
}
SECOND_DOWN:
select {
case msg4, ok := <-messages:
if !ok {
return // channel closed
}
var msg4Note uint8
if !msg4.GetNoteEnd(&c, &msg4Note) || c != channel || msg4Note != msg3Note {
goto SECOND_DOWN // keep waiting
}
// sequence complete, play
playback(midi.Note(msg4Note))
goto INITIAL
case <-time.After(timeout):
goto INITIAL
}
}
func Play(out drivers.Out, baseNote midi.Note, intervals []midi.Interval) error {
delay := time.Duration(1 / (tempo / 60.0) * float64(time.Second))
err := out.Send(midi.NoteOff(channel, uint8(baseNote)))
if err != nil {
return err
}
time.Sleep(delay) // sending an off and delaying first seems to behave better
for _, offset := range intervals {
note := baseNote.Transpose(offset)
noteOn := midi.NoteOn(channel, uint8(note), velocity)
noteOff := midi.NoteOff(channel, uint8(note))
err = out.Send(noteOn)
if err != nil {
return err
}
time.Sleep(delay)
err = out.Send(noteOff)
if err != nil {
return err
}
}
return nil
}
func main() {
defer midi.CloseDriver()
in, err := SelectInPort()
if err != nil {
fmt.Printf("problem accessing MIDI in: %v\n", err)
os.Exit(1)
}
out, err := SelectOutPort()
if err != nil {
fmt.Printf("problem accessing MIDI out: %v\n", err)
os.Exit(1)
}
invervals, err := SelectPattern()
if err != nil {
fmt.Printf("problem with pattern: %v\n", err)
os.Exit(1)
}
err = in.Open()
if err != nil {
fmt.Printf("problem opening MIDI in: %v\n", err)
os.Exit(1)
}
defer in.Close()
err = out.Open()
if err != nil {
fmt.Printf("problem opening MIDI out: %v\n", err)
os.Exit(1)
}
defer out.Close()
messages := make(chan midi.Message)
defer close(messages)
stop, err := in.Listen(func(msg []byte, milliseconds int32) {
messages <- midi.Message(msg)
}, drivers.ListenConfig{})
if err != nil {
fmt.Printf("problem receiving MIDI messages: %v\n", err)
os.Exit(1)
}
defer stop()
// Main loop
go HandleInputs(messages, func(n midi.Note) {
err := Play(out, n, invervals)
if err != nil {
fmt.Printf("playback issue: %v", err)
os.Exit(1)
}
})
// wait for interrupt
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("Ready to play")
<-done // Will block here until user hits ctrl+c
fmt.Println("Closing MIDI devices")
}