forked from splace/joysticks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.go
70 lines (62 loc) · 1.49 KB
/
play.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
package main
import (
"io"
"os/exec"
"time"
"math"
"os"
"os/signal"
"log"
)
import . "github.com/splace/joysticks"
import . "github.com/splace/sounds"
func main() {
stopChan := make(chan os.Signal)
signal.Notify(stopChan, os.Interrupt)
events := Capture(
Channel{10, HID.OnLong}, // event[0] button #10 long pressed
Channel{1, HID.OnClose}, // event[1] button #1 closes
Channel{7, HID.OnOpen}, // event[2] button #7 opens
Channel{8, HID.OnOpen}, // event[3] button #8 opens
Channel{2, HID.OnRotate}, // event[4] hat #1 rotates
Channel{1, HID.OnEdge}, // event[5] hat #2 rotates
)
var volume float32 = .5
var octave =5
var note int =1
for {
select {
case <-stopChan: // wait for SIGINT
log.Println("Interrupted")
return
case <-events[0]:
return
case <-events[1]:
play(NewSound(NewTone(Period(octave,note), float64(volume)), time.Second/3))
case <-events[2]:
octave++
case <-events[3]:
octave--
case h := <-events[4]:
volume = h.(AngleEvent).Angle/6.28 + .5
case h := <-events[5]:
//f = time.Duration(100*math.Pow(2, float64(h.(HatAngleEvent).Angle)/6.28)) * time.Second / 44000
note = int(h.(AngleEvent).Angle*6 / math.Pi)+6
play(NewSound(NewTone(Period(octave,note), float64(volume)), time.Second/3))
}
}
}
func play(s Sound) {
cmd := exec.Command("aplay")
out, in := io.Pipe()
go func() {
Encode(in, 2, 44100, s)
in.Close()
}()
cmd.Stdin = out
err := cmd.Run()
if err != nil {
panic(err)
}
log.Println("Playing:",s)
}