-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
155 lines (114 loc) · 4.24 KB
/
main.go
File metadata and controls
155 lines (114 loc) · 4.24 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
package main
import (
"fmt"
"os"
"os/signal"
driver "gitlab.com/gomidi/rtmididrv"
hyperarp "gitlab.com/gomidi/hyperarp/lib"
"gitlab.com/gomidi/midi"
"gitlab.com/gomidi/midi/cc"
config "gitlab.com/metakeule/config"
)
var CONFIG = config.MustNew("hyperarp", VERSION, "hyper arpeggiator")
var (
inArg = CONFIG.NewInt32("in", "number of the input MIDI port (use hyperarp list to see the available MIDI ports)", config.Required, config.Shortflag('i'))
outArg = CONFIG.NewInt32("out", "number of the output MIDI port (use hyperarp list to see the available MIDI ports)", config.Required, config.Shortflag('o'))
transposeArg = CONFIG.NewInt32("transpose", "transpose (number of semitones)", config.Default(int32(0)), config.Shortflag('t'))
tempoArg = CONFIG.NewFloat32("tempo", "tempo (BPM)", config.Default(float32(120.0)), config.Shortflag('b'))
ccDirectionSwitchArg = CONFIG.NewInt32("ccdir", "controller number for the direction switch", config.Default(int32(cc.GeneralPurposeButton1Switch)))
ccTimeIntervalArg = CONFIG.NewInt32("cctiming", "controller number to set the timing interval", config.Default(int32(cc.GeneralPurposeSlider1)))
ccStyleArg = CONFIG.NewInt32("ccstyle", "controller number to select the playing style (staccato, non-legato, legato)", config.Default(int32(cc.GeneralPurposeSlider2)))
noteDirectionSwitchArg = CONFIG.NewInt32("notedir", "note (key) for the direction switch")
noteTimeIntervalArg = CONFIG.NewInt32("notetiming", "note (key) for the timing interval")
noteStyleArg = CONFIG.NewInt32("notestyle", "note (key) for the playing style (staccato, non-legato, legato)")
controlChannelArg = CONFIG.NewInt32("ctrlch", "channel for control messages (only needed if not the same as the input channel")
listCmd = CONFIG.MustCommand("list", "show the available MIDI ports").Skip("in").Skip("out").Skip("transpose").Skip("tempo").Skip("ccdir").Skip("cctiming").Skip("ccstyle").Skip("notedir").Skip("notetiming").Skip("notestyle").Skip("ctrlch")
)
func main() {
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err.Error())
os.Exit(1)
return
}
os.Exit(0)
}
func run() error {
drv, err := driver.New()
if err != nil {
return err
}
defer drv.Close()
err = CONFIG.Run()
if err != nil {
fmt.Fprint(os.Stderr, CONFIG.Usage())
listMIDIDevices(drv)
return err
}
if CONFIG.ActiveCommand() == listCmd {
listMIDIDevices(drv)
return nil
}
// make sure to close all open ports at the end
defer drv.Close()
var inPort midi.In = nil
in := inArg.Get()
inPort, err = midi.OpenIn(drv, int(in), "")
if err != nil {
return err
}
var outPort midi.Out = nil
out := outArg.Get()
outPort, err = midi.OpenOut(drv, int(out), "")
if err != nil {
return err
}
defer inPort.Close()
defer outPort.Close()
opts := []hyperarp.Option{
hyperarp.CCDirectionSwitch(uint8(ccDirectionSwitchArg.Get())),
hyperarp.CCTimeInterval(uint8(ccTimeIntervalArg.Get())),
hyperarp.CCStyle(uint8(ccStyleArg.Get())),
hyperarp.Tempo(float64(tempoArg.Get())),
}
if noteDirectionSwitchArg.IsSet() {
opts = append(opts, hyperarp.NoteDirectionSwitch(uint8(noteDirectionSwitchArg.Get())))
}
if noteTimeIntervalArg.IsSet() {
opts = append(opts, hyperarp.NoteTimeInterval(uint8(noteTimeIntervalArg.Get())))
}
if noteStyleArg.IsSet() {
opts = append(opts, hyperarp.NoteStyle(uint8(noteStyleArg.Get())))
}
if controlChannelArg.IsSet() {
opts = append(opts, hyperarp.ControlChannel(uint8(controlChannelArg.Get())))
}
tr := int8(transposeArg.Get())
if tr != 0 {
opts = append(opts, hyperarp.Transpose(tr))
}
arp := hyperarp.New(inPort, outPort, opts...)
go arp.Run()
defer arp.Close()
sigchan := make(chan os.Signal, 10)
// listen for ctrl+c
go signal.Notify(sigchan, os.Interrupt)
// interrupt has happend
<-sigchan
fmt.Println("\n--interrupted!")
return nil
}
func listMIDIDevices(d midi.Driver) {
ins, _ := d.Ins()
fmt.Print("\n--- MIDI input ports ---\n\n")
for _, port := range ins {
fmt.Printf("[%d] %#v\n", port.Number(), port.String())
}
outs, _ := d.Outs()
fmt.Print("\n--- MIDI output ports ---\n\n")
for _, port := range outs {
fmt.Printf("[%d] %#v\n", port.Number(), port.String())
}
fmt.Println("\n\n")
return
}