-
Notifications
You must be signed in to change notification settings - Fork 0
/
plasma.go
308 lines (281 loc) · 8 KB
/
plasma.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
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
package main
import (
"flag"
"fmt"
"image"
"image/color"
"math"
"math/rand"
"strconv"
"time"
_ "net/http/pprof"
midiwriter "gitlab.com/gomidi/midi/writer"
driver "gitlab.com/gomidi/rtmididrv"
//driver "gitlab.com/gomidi/midi/testdrv"
"gocv.io/x/gocv"
)
type Plasmer struct {
Midi midiwriter.ChannelWriter
Dmx midiwriter.ChannelWriter
Cam *gocv.VideoCapture
NotesOn map[uint8]midiNote
NotesIn chan map[uint8]midiNote
}
type midiNote struct {
Note, Velo uint8
Timer *time.Timer
}
func newPlasmer(max int, cam *gocv.VideoCapture) *Plasmer {
return &Plasmer{
Cam: cam,
NotesOn: make(map[uint8]midiNote, max),
NotesIn: make(chan map[uint8]midiNote, max),
}
}
func findCentroid(img gocv.Mat, ctr []image.Point) image.Point {
mat := gocv.NewMatWithSize(img.Rows(), img.Cols(), gocv.MatTypeCV8U)
pv := gocv.NewPointsVectorFromPoints([][]image.Point{ctr})
gocv.FillPoly(&mat, pv, color.RGBA{R: 255, G: 255, B: 255, A: 255})
m := gocv.Moments(mat, false) // binaryImage = false
cx := int(m["m10"] / m["m00"])
cy := int(m["m01"] / m["m00"])
return image.Point{X: cx, Y: cy}
}
/* opt 1) straight vectors x,y adapted to midi 0-127 range */
func vectorTouchToMidi(p image.Point) midiNote {
n := uint8((p.X - 20) * 125 / 600)
v := uint8((p.Y - 20) * 125 / 440)
fmt.Printf("vectorTouchToMidi: (%dx,%dy) -> (note %d, velo %d)\n", p.X, p.Y, n, v)
return midiNote{Note: n, Velo: v}
}
/* opt 2) angle and distance from center */
func angleDistToMidi(p image.Point) midiNote {
b := math.Abs(float64(p.X) - 320.0) // length of a
a := math.Abs(float64(p.Y) - 240.0) // length of b
/* angle */
var ang float64
if p.Y > 240 {
ang = math.Atan(b/a) * 180 / math.Pi
} else {
ang = (math.Atan(a/b) * 180 / math.Pi) + 90
}
/* dist */
dist := math.Sqrt(math.Pow(b, 2) + math.Pow(a, 2))
/* note, velo */
n := uint8(ang / 180 * 127)
v := uint8(dist / 240 * 127)
fmt.Printf("angleDistToMidi: (%dx,%dy) -> (ang %f, dist %f) -> (note %d, velo %d)\n", p.X, p.Y, ang, dist, n, v)
return midiNote{Note: n, Velo: v}
}
func notesPressed(img gocv.Mat, pts [][]image.Point) map[uint8]midiNote {
nts := make(map[uint8]midiNote, 10)
for _, ctrd := range pts {
c := findCentroid(img, ctrd)
n := angleDistToMidi(c)
nts[n.Note] = n
}
return nts
}
// remove smaller contours within an X-sized area
func filterContours(pv gocv.PointsVector) [][]image.Point {
minArea := 40.0
var filtered [][]image.Point
for _, c := range pv.ToPoints() {
pv := gocv.NewPointVectorFromPoints(c)
area := gocv.ContourArea(pv)
if area > minArea {
filtered = append(filtered, c)
}
}
return filtered
}
/* incoming notes channel */
func (p *Plasmer) handlePressedNotes() {
for nts := range p.NotesIn {
for i, note := range nts {
timer := time.NewTimer(time.Second * 3)
if active, ok := p.NotesOn[i]; ok {
active.Timer = timer
} else {
note.Timer = timer
p.NotesOn[i] = note
if p.Midi != nil {
midiwriter.NoteOn(p.Midi, note.Note, note.Velo)
}
if p.Dmx != nil {
c := math.Floor(float64(i)/100*3) + 1 // get a dmx channel 1-4
v := note.Velo * 2
fmt.Printf("DMX channel %f, velo %d\n", c, v)
midiwriter.NoteOn(p.Dmx, uint8(c), v)
}
fmt.Printf("pressing note %d, velo %d\n", note.Note, note.Velo)
}
}
}
}
func (p *Plasmer) expireNotes() {
for {
for i, note := range p.NotesOn {
select {
case <-note.Timer.C:
fmt.Printf("releasing note %d\n", i)
if p.Midi != nil {
midiwriter.NoteOff(p.Midi, i)
}
delete(p.NotesOn, i)
}
}
}
}
func (p *Plasmer) readCam() {
window1 := gocv.NewWindow("plasma points detector 1")
window2 := gocv.NewWindow("plasma points detector 2")
window3 := gocv.NewWindow("plasma points detector 3")
defer window1.Close()
defer window2.Close()
defer window3.Close()
img := gocv.NewMat()
img2 := gocv.NewMat()
hueImg := gocv.NewMat()
mask := gocv.NewMat()
defer img.Close()
defer img2.Close()
defer hueImg.Close()
defer mask.Close()
green := color.RGBA{0, 255, 0, 0}
for {
time.Sleep(time.Millisecond * 1) // delay to allow camera to sync
if ok := p.Cam.Read(&img); !ok {
fmt.Printf("Device closed: %#v\n", p.Cam)
return
}
if img.Empty() {
continue
}
//img.CopyTo(&img2)
gocv.Flip(img, &img2, 1) // flip horizontally
gocv.CvtColor(img2, &hueImg, gocv.ColorBGRToHSV) // convert to hue
gocv.Circle(&img2, image.Pt(310, 270), 70, color.RGBA{0, 0, 255, 0}, -1) // exclude center
gocv.Circle(&hueImg, image.Pt(310, 270), 70, color.RGBA{0, 0, 255, 0}, -1) // exclude center
// HUE-SATURATION-VUE spectrum: https://i.stack.imgur.com/gyuw4.png
// extract the pinkish red hue range to mask Mat
//gocv.InRangeWithScalar(hueImg, gocv.NewScalar(150.0, 100.0, 250.0, 0.0), gocv.NewScalar(170.0, 255.0, 255.0, 0.0), &mask)
//gocv.InRangeWithScalar(hueImg, gocv.NewScalar(143.0, 50.0, 255.0, 0.0), gocv.NewScalar(144.0, 255.0, 255.0, 0.0), &mask)
gocv.InRangeWithScalar(hueImg, gocv.NewScalar(165.0, 115.0, 115.0, 0.0), gocv.NewScalar(170.0, 255.0, 255.0, 0.0), &mask)
ctrs := gocv.FindContours(mask, gocv.RetrievalExternal, gocv.ChainApproxSimple)
fCtrs := filterContours(ctrs)
for _, ctr := range ctrs.ToPoints() {
gocv.Circle(&img2, (ctr[0]), 4, green, 2)
}
n := notesPressed(img2, fCtrs)
p.NotesIn <- n
window1.IMShow(img2)
window2.IMShow(mask)
window3.IMShow(hueImg)
if window1.WaitKey(1) == 27 {
break
}
}
}
func main() {
max := flag.Int("max", 8, "max no of simultaneous notes")
camDev := flag.String("cam", "", "address of webcam, http or id int")
midDev := flag.String("mid", "", "Midi device ID")
dmxDev := flag.String("dmx", "", "DMX device ID")
test := flag.Bool("test", false, "run test mode")
flag.Parse()
/* webcam setup */
// device or url to mjpeg stream
// go run plasma.go -cam 0 / http://root:[email protected]/mjpg/1/video.mjpg
var webcam *gocv.VideoCapture
if *camDev != "" {
if i, err := strconv.Atoi(*camDev); err == nil {
webcam, err = gocv.OpenVideoCapture(i)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", err)
return
}
} else {
webcam, err = gocv.OpenVideoCapture(*camDev)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", err)
return
}
}
}
fmt.Println(webcam)
defer webcam.Close()
p := newPlasmer(*max, webcam)
if *midDev != "" {
i, _ := strconv.Atoi(*midDev)
drv, err := driver.New()
if err != nil {
panic(err)
}
defer drv.Close()
outs, err := drv.Outs()
fmt.Printf("%v", outs)
if err != nil {
fmt.Printf("MIDI ERROR: %v", err)
}
out := outs[i]
if err := out.Open(); err != nil {
fmt.Printf("MIDI OPEN ERROR: %v", err)
}
defer out.Close()
wr := midiwriter.New(out)
wr.SetChannel(0)
p.Midi = wr
}
if *dmxDev != "" {
i, _ := strconv.Atoi(*dmxDev)
drv, err := driver.New()
if err != nil {
panic(err)
}
defer drv.Close()
outs, err := drv.Outs()
//fmt.Printf("%v", outs)
if err != nil {
panic(err)
}
out := outs[i]
if err := out.Open(); err != nil {
panic(err)
}
defer out.Close()
wr := midiwriter.New(outs[i])
wr.SetChannel(1)
p.Dmx = wr
}
go p.handlePressedNotes()
go p.expireNotes()
if *test {
window := gocv.NewWindow("plasma points detector")
img := gocv.NewMatWithSize(640, 480, gocv.MatTypeCV8U)
defer img.Close()
gocv.Circle(&img, image.Pt(330, 280), 220, color.RGBA{0, 0, 255, 0}, -1) // test
for i := 0; i < 12; i++ {
var pts []image.Point
var ctrs [][]image.Point
x := rand.Intn(600) + 20
y := rand.Intn(420) + 20
pt := image.Point{X: x, Y: y}
pts = append(pts, pt)
ctrs = append(ctrs, pts)
fmt.Printf("Random touch: (%dx, %dy)\n", x, y)
gocv.Circle(&img, image.Pt(x, y), 10, color.RGBA{0, 0, 0, 0}, -1) // test
window.IMShow(img)
n := notesPressed(img, ctrs)
p.NotesIn <- n
time.Sleep(1000 * 1000 * 500 * time.Nanosecond)
if window.WaitKey(1) == 27 {
break
}
}
time.Sleep(5 * time.Second) // for allowing channels to finish reading
}
if *camDev != "" {
p.readCam()
}
}