-
Notifications
You must be signed in to change notification settings - Fork 3
/
source_output.go
120 lines (110 loc) · 3.13 KB
/
source_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
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
package pulseaudio
import (
errors2 "errors"
"io"
"math"
"strings"
)
// SourceOutput contains information about a source output in pulseaudio
type SourceOutput struct {
Index uint32
Name string
OwnerModule uint32
ClientIndex uint32
Source uint32
SampleSpec sampleSpec
ChannelMap channelMap
Cvolume cvolume
BufferUsec uint64
SourceUsec uint64
ResampleMethod string
Driver string
Muted bool
PropList map[string]string
Corked bool
HasVolume bool
VolumeWritable bool
Format formatInfo
Client *Client
}
// ReadFrom deserializes a source output packet from pulseaudio
func (s *SourceOutput) ReadFrom(r io.Reader) (int64, error) {
err := bread(r,
uint32Tag, &s.Index,
stringTag, &s.Name,
uint32Tag, &s.OwnerModule,
uint32Tag, &s.ClientIndex,
uint32Tag, &s.Source,
&s.SampleSpec,
&s.ChannelMap,
usecTag, &s.BufferUsec,
usecTag, &s.SourceUsec,
stringTag, &s.ResampleMethod,
stringTag, &s.Driver,
&s.PropList,
&s.Corked,
&s.Cvolume,
&s.Muted,
&s.HasVolume,
&s.VolumeWritable)
if err != nil {
return 0, err
}
err = bread(r, &s.Format)
return 0, nil
}
func (s SourceOutput) SetVolume(volume float32) error {
_, err := s.Client.request(commandSetSourceOutputVolume, uint32Tag, s.Index, cvolume{uint32(volume * 0xffff)})
return err
}
func (s SourceOutput) SetMute(b bool) error {
muteCmd := '0'
if b {
muteCmd = '1'
}
_, err := s.Client.request(commandSetSourceOutputMute, uint32Tag, s.Index, uint8(muteCmd))
return err
}
func (s SourceOutput) ToggleMute() error {
return s.SetMute(!s.Muted)
}
func (s SourceOutput) IsMute() bool {
return s.Muted
}
func (s SourceOutput) GetVolume() float32 {
return float32(math.Round(float64(float32(s.Cvolume[0])/0xffff)*100)) / 100
}
// SourceOutputs queries PulseAudio for a list of source outputs and returns an array
func (c *Client) SourceOutputs() ([]SourceOutput, error) {
b, err := c.request(commandGetSourceOutputInfoList)
if err != nil {
return nil, err
}
var sourceOutputs []SourceOutput
for b.Len() > 0 {
var sourceOutput SourceOutput
err = bread(b, &sourceOutput)
if err != nil {
return nil, err
}
sourceOutput.Client = c
sourceOutputs = append(sourceOutputs, sourceOutput)
}
return sourceOutputs, nil
}
func (c *Client) GetSourceOutputByName(name string) (SourceOutput, error) {
sourceOutputs, err := c.SourceOutputs()
if err != nil {
return SourceOutput{}, err
}
for _, sourceOutput := range sourceOutputs {
sourceName, ok := sourceOutput.PropList["application.name"]
if !ok {
continue
}
if strings.ToLower(sourceName) == strings.ToLower(name) {
return sourceOutput, nil
}
}
return SourceOutput{}, errors2.New("Could not get source output: " + name)
}