/
profiles.go
66 lines (54 loc) · 1.33 KB
/
profiles.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
package bluez
import (
"github.com/mafik/pulseaudio"
"github.com/pkg/errors"
)
// AudioProfile stores the audio profile information.
type AudioProfile struct {
Name string
Description string
Index uint32
Active bool
}
// ListAudioProfiles lists audio profiles of a sound card.
func ListAudioProfiles(deviceAddress string) ([]AudioProfile, error) {
var profiles []AudioProfile
client, err := pulseaudio.NewClient()
if err != nil {
return nil, err
}
defer client.Close()
cards, err := client.Cards()
if err != nil {
return nil, err
}
for _, card := range cards {
if addr, ok := card.PropList["device.string"]; ok {
if addr != deviceAddress {
continue
}
for profileName, profile := range card.Profiles {
if profile.Available != 1 {
continue
}
profiles = append(profiles, AudioProfile{
Index: card.Index,
Name: profileName,
Description: profile.Description,
Active: profile.Name == card.ActiveProfile.Name,
})
}
return profiles, nil
}
}
return nil, errors.New("No profiles found")
}
// SetAudioProfile sets an audio profile for a sound card.
func (a AudioProfile) SetAudioProfile() error {
client, err := pulseaudio.NewClient()
if err != nil {
return err
}
defer client.Close()
return client.SetCardProfile(a.Index, a.Name)
}