-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_windows.go
146 lines (119 loc) · 3.18 KB
/
audio_windows.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"
"strings"
"github.com/gen2brain/malgo"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
var audioBackends = []malgo.Backend{
malgo.BackendWasapi,
malgo.BackendDsound,
}
func getParentIdPrefixes() (map[string]string, error) {
m := make(map[string]string)
path := fmt.Sprintf(`SYSTEM\CurrentControlSet\Enum\USB\VID_%04X&PID_%04X`, vendorId, productId)
k, err := registry.OpenKey(registry.LOCAL_MACHINE, path, registry.RESOURCE_LIST)
if err != nil {
return nil, err
}
defer k.Close()
keys, err := k.ReadSubKeyNames(0)
if err != nil {
return nil, err
}
for _, key := range keys {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf(`%s\%s`, path, key), registry.READ)
if err != nil {
return nil, err
}
parentId, _, err := k.GetStringValue("ParentIdPrefix")
if err != nil {
return nil, err
}
m[key] = strings.ToUpper(parentId)
}
return m, nil
}
func resolveWindowsAudioDevices(parentIdPrefixes map[string]string, typ string) (map[string]string, error) {
m := make(map[string]string)
path := `SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\` + typ
k, err := registry.OpenKey(registry.LOCAL_MACHINE, path, registry.RESOURCE_LIST)
if err != nil {
return nil, err
}
defer k.Close()
keys, err := k.ReadSubKeyNames(0)
if err != nil {
return nil, err
}
for _, key := range keys {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf(`%s\%s\Properties`, path, key), registry.READ)
if err != nil {
return nil, err
}
s, _, err := k.GetStringValue("{b3f8fa53-0004-438e-9003-51a46e139bfc},2")
if err != nil {
return nil, err
}
for serial, prefix := range parentIdPrefixes {
if strings.Contains(s, prefix) {
m[serial] = key
}
}
}
return m, nil
}
var windowsInputDevices map[string]string
var windowsOutputDevices map[string]string
func init() {
parentIds, err := getParentIdPrefixes()
if err != nil {
panic(err)
}
windowsInputDevices, err = resolveWindowsAudioDevices(parentIds, "Capture")
if err != nil {
panic(err)
}
windowsOutputDevices, err = resolveWindowsAudioDevices(parentIds, "Render")
if err != nil {
panic(err)
}
}
func resolveAudioDeviceID(serial string, deviceType malgo.DeviceType) (audioDeviceId, error) {
var deviceId string
switch deviceType {
case malgo.Capture:
deviceId = "{0.0.1.00000000}." + windowsInputDevices[serial]
case malgo.Playback:
deviceId = "{0.0.0.00000000}." + windowsOutputDevices[serial]
}
devices, err := audioContext.Devices(deviceType)
if err != nil {
return audioDeviceId{}, err
}
switch audioBackend {
case malgo.BackendWasapi:
for _, device := range devices {
if windows.UTF16PtrToString((*uint16)(device.ID.Pointer())) == deviceId {
return audioDeviceId{
malgo: device.ID,
id: deviceId,
}, nil
}
}
case malgo.BackendDsound:
suffix := strings.Replace(deviceId[37:54], "-", "", 1)
for _, device := range devices {
if strings.HasSuffix(device.ID.String(), suffix) {
return audioDeviceId{
malgo: device.ID,
id: deviceId,
}, nil
}
}
default:
panic("invalid audio backend")
}
return audioDeviceId{}, fmt.Errorf("device not found: %s", serial)
}