/
gotar-hero.go
146 lines (134 loc) · 3.13 KB
/
gotar-hero.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
package main
import (
"fmt"
"flag"
"github.com/gvalkov/golang-evdev"
"github.com/rakyll/portmidi"
)
const (
butGreen = 293
butRed = 289
butYellow = 288
butBlue = 290
butOrange = 291
butTilt = 292
butStrumbar = 1
butStart = 297
butSelect = 296
)
var (
butMap map[uint16]uint32 = map [uint16]uint32 {
butGreen : 0,
butRed : 1,
butYellow : 2,
butBlue : 3,
butOrange : 4,
}
playing int64 = -1
octave int64 = 0
hold bool = false
butState int64 = 0
baseNote int64
vel int64
midiChan int64
)
func ReadGuitar(c chan *evdev.InputEvent, errs chan error, dev string){
guit, err := evdev.Open(dev)
if err != nil{
errs <- err
return
}
for {
ev, err := guit.ReadOne()
if err != nil{
errs <- err
return
}
if ev.Type == evdev.EV_KEY || ev.Type == evdev.EV_ABS {
c <- ev
}
}
}
func SetNote(shift uint32, value int32) {
if value != 0 {
butState |= 1 << shift
} else {
butState &= ^(1 << shift)
}
}
func NoteOn(stream *portmidi.Stream, note int64, velocity int64){
playing = note
stream.WriteShort(0x90|(midiChan-1), note, velocity)
}
func NoteOff(stream *portmidi.Stream, note int64, velocity int64){
stream.WriteShort(0x80|(midiChan-1), note, velocity)
playing = -1
}
func SwapNote(stream *portmidi.Stream, newNote int64, velocity int64){
oldNote := playing
NoteOff(stream, oldNote, velocity)
NoteOn(stream, newNote, velocity)
playing = newNote
}
func AllNotesOff(stream *portmidi.Stream){
stream.WriteShort(0xB0|(midiChan-1), 0x7B, 0x00)
playing = -1
}
func main() {
dev := flag.String("d", "/dev/input/by-id/usb-0810_Twin_USB_Joystick-event-joystick", "The GH controller event device")
flag.Int64Var(&baseNote, "b", 48, "The base midi note with no button pressed")
flag.Int64Var(&vel, "v", 100, "Midi note velocity")
flag.Int64Var(&midiChan, "c", 1, "Midi channel")
flag.Parse()
portmidi.Initialize()
out, err := portmidi.NewOutputStream(portmidi.GetDefaultOutputDeviceId(), 32, 0)
if err != nil{
fmt.Println(err)
return
}
c := make(chan *evdev.InputEvent)
e := make(chan error)
go ReadGuitar(c, e, *dev)
for {
select{
case but := <- c:
switch but.Code {
case butGreen, butRed, butYellow, butBlue, butOrange:
SetNote(butMap[but.Code], but.Value)
if playing != -1 {
SwapNote(out, baseNote + butState + octave, vel)
}
case butStrumbar:
if but.Value == 255 || but.Value == 0 {
NoteOn(out, baseNote + butState + octave, vel)
} else if !hold {
NoteOff(out, playing, vel)
}
case butSelect:
if but.Value != 0 {
hold = !hold
if (!hold){
NoteOff(out, playing, vel)
}
}
case butTilt:
if but.Value == 1 {
octave += 12
} else {
octave -= 12
}
if playing != -1 {
SwapNote(out, baseNote + butState + octave, vel)
}
case butStart:
AllNotesOff(out)
}
case err := <- e:
fmt.Println(err)
close(c)
close(e)
return
default:
}
}
}