-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
83 lines (68 loc) · 1.89 KB
/
output.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
package output
import (
"errors"
"github.com/heucuva/go-qwertysynth/internal/output/device"
deviceCommon "github.com/heucuva/go-qwertysynth/internal/output/device/common"
)
type devicePriority int
// the further down the list, the higher the priority
const (
devicePriorityNone = devicePriority(iota)
devicePriorityPulseAudio
devicePriorityWinmm
devicePriorityDirectSound
)
var (
// DefaultOutputDeviceName is the default device name
DefaultOutputDeviceName = "none"
devicePriorityMap = make(map[string]devicePriority)
)
func calculateOptimalDefaultOutputDeviceName() string {
preferredPriority := devicePriority(0)
preferredName := "none"
for name := range device.Map {
if priority, ok := devicePriorityMap[name]; ok && priority > preferredPriority {
preferredName = name
preferredPriority = priority
}
}
return preferredName
}
// CreateOutputDevice creates an output device based on the provided settings
func CreateOutputDevice(settings deviceCommon.Settings) (device.Device, error) {
d, err := device.CreateOutputDevice(settings)
if err != nil {
return nil, err
}
if d == nil {
return nil, errors.New("could not create output device")
}
return d, nil
}
// Setup finalizes the output device preference system
func Setup() {
DefaultOutputDeviceName = calculateOptimalDefaultOutputDeviceName()
}
// DeviceInfo returns information about a device
type DeviceInfo struct {
Priority int
Kind deviceCommon.Kind
}
func GetOutputDevices() map[string]DeviceInfo {
m := make(map[string]DeviceInfo)
for k, v := range devicePriorityMap {
if d, ok := device.Map[k]; ok {
m[k] = DeviceInfo{
Priority: int(v),
Kind: d.Kind,
}
}
}
return m
}
func init() {
_ = devicePriorityNone // lint
devicePriorityMap["pulseaudio"] = devicePriorityPulseAudio
devicePriorityMap["winmm"] = devicePriorityWinmm
devicePriorityMap["directsound"] = devicePriorityDirectSound
}