/
devices.go
77 lines (68 loc) · 1.63 KB
/
devices.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
package main
import (
"fmt"
"strings"
"github.com/rakyll/portmidi"
)
func deviceID(idint int) portmidi.DeviceId {
return portmidi.DeviceId(idint)
}
type devices struct {
info map[portmidi.DeviceId]*portmidi.DeviceInfo
}
func newDevices() (devs devices) {
portmidi.Initialize()
devs = devices{
info: map[portmidi.DeviceId]*portmidi.DeviceInfo{},
}
for id := 0; id < portmidi.CountDevices(); id++ {
devID := deviceID(id)
devs.info[devID] = portmidi.GetDeviceInfo(devID)
}
return
}
func (d devices) Input(name string) (id portmidi.DeviceId, err error) {
var device *portmidi.DeviceInfo
for id, device = range d.info {
if device.Name == name && device.IsInputAvailable {
return
}
}
// Try a prefix match if we didn't get an exact match
for id, device = range d.info {
if strings.HasPrefix(device.Name, name) && device.IsInputAvailable {
return
}
}
err = fmt.Errorf("no input found for: %s", name)
return
}
func (d devices) Output(name string) (id portmidi.DeviceId, err error) {
var device *portmidi.DeviceInfo
for id, device = range d.info {
if device.Name == name && device.IsOutputAvailable {
return
}
}
// Try a prefix match if we didn't get an exact match
for id, device = range d.info {
if strings.HasPrefix(device.Name, name) && device.IsOutputAvailable {
return
}
}
err = fmt.Errorf("no output found for: %s", name)
return
}
func (d devices) DeviceNames() (names []string) {
namemap := map[string]struct{}{}
for _, device := range d.info {
namemap[device.Name] = struct{}{}
}
names = make([]string, len(namemap))
i := 0
for name, _ := range namemap {
names[i] = name
i++
}
return
}