-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeatstep.go
96 lines (87 loc) · 2.01 KB
/
beatstep.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
package beatstep
import (
"errors"
"github.com/rakyll/portmidi"
)
// BeatStep represents an instance of BeatStep device.
type BeatStep struct {
input *portmidi.Stream
output *portmidi.Stream
}
const deviceName = "Arturia BeatStep MIDI 1"
func discover() (input, output portmidi.DeviceID, err error) {
inputDevice, outputDevice := -1, -1
for device := 0; device < portmidi.CountDevices(); device++ {
info := portmidi.Info(portmidi.DeviceID(device))
if info.Name == deviceName {
if info.IsInputAvailable {
inputDevice = device
}
if info.IsOutputAvailable {
outputDevice = device
}
}
}
if inputDevice == -1 || outputDevice == -1 {
err = errors.New("beatstep: no beatstep connected")
return
}
return portmidi.DeviceID(inputDevice), portmidi.DeviceID(outputDevice), nil
}
// Open initializes new BeatStep instance.
func Open() (*BeatStep, error) {
inputDevice, outputDevice, err := discover()
if err != nil {
return nil, err
}
inputStream, err := portmidi.NewInputStream(inputDevice, 1024)
if err != nil {
return nil, err
}
outputStream, err := portmidi.NewOutputStream(outputDevice, 1024, 0)
if err != nil {
inputStream.Close()
return nil, err
}
bs := &BeatStep{
input: inputStream,
output: outputStream,
}
return bs, nil
}
func (bs *BeatStep) read() (states []State, err error) {
events, err := bs.input.Read(64)
if err != nil {
return nil, err
}
for _, event := range events {
state := decode(event)
states = append(states, state)
}
return states, nil
}
// Listen reads all the States from the device and writes
// them in to the channel it returns.
// Listen returns immidiately.
func (bs *BeatStep) Listen() <-chan State {
ch := make(chan State)
go func(ch chan<- State) {
for {
states, err := bs.read()
if err != nil {
close(ch)
return
}
for _, state := range states {
ch <- state
}
}
}(ch)
return ch
}
// Close closes internal resourses of BeatStep.
func (bs *BeatStep) Close() error {
bs.input.Close()
bs.output.Close()
return nil
}