-
Notifications
You must be signed in to change notification settings - Fork 3
/
source.go
158 lines (147 loc) · 3.96 KB
/
source.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
147
148
149
150
151
152
153
154
155
156
157
158
package pulseaudio
import (
errors2 "errors"
"io"
"math"
)
//Source contains information about a source in pulseaudio, e.g. a microphone
type Source struct {
Index uint32
Name string
Description string
SampleSpec sampleSpec
ChannelMap channelMap
ModuleIndex uint32
Cvolume cvolume
Muted bool
MonitorSourceIndex uint32
MonitorSourceName string
Latency uint64
Driver string
Flags uint32
PropList map[string]string
RequestedLatency uint64
BaseVolume uint32
SinkState uint32
NVolumeSteps uint32
CardIndex uint32
Ports []sinkPort
ActivePortName string
Formats []formatInfo
Client *Client
}
//ReadFrom deserialized a PA source packet
func (s *Source) ReadFrom(r io.Reader) (int64, error) {
var portCount uint32
err := bread(r,
uint32Tag, &s.Index,
stringTag, &s.Name,
stringTag, &s.Description,
&s.SampleSpec,
&s.ChannelMap,
uint32Tag, &s.ModuleIndex,
&s.Cvolume,
&s.Muted,
uint32Tag, &s.MonitorSourceIndex,
stringTag, &s.MonitorSourceName,
usecTag, &s.Latency,
stringTag, &s.Driver,
uint32Tag, &s.Flags,
&s.PropList,
usecTag, &s.RequestedLatency,
volumeTag, &s.BaseVolume,
uint32Tag, &s.SinkState,
uint32Tag, &s.NVolumeSteps,
uint32Tag, &s.CardIndex,
uint32Tag, &portCount)
if err != nil {
return 0, err
}
s.Ports = make([]sinkPort, portCount)
for i := uint32(0); i < portCount; i++ {
err = bread(r, &s.Ports[i])
if err != nil {
return 0, err
}
}
if portCount == 0 {
err = bread(r, stringNullTag)
if err != nil {
return 0, err
}
} else {
err = bread(r, stringTag, &s.ActivePortName)
if err != nil {
return 0, err
}
}
var formatCount uint8
err = bread(r,
uint8Tag, &formatCount)
if err != nil {
return 0, err
}
s.Formats = make([]formatInfo, formatCount)
for i := uint8(0); i < formatCount; i++ {
err = bread(r, &s.Formats[i])
if err != nil {
return 0, err
}
}
return 0, nil
}
func (s Source) SetVolume(volume float32) error {
_, err := s.Client.request(commandSetSourceVolume, uint32Tag, uint32(0xffffffff), stringTag, []byte(s.Name), byte(0), cvolume{uint32(volume * 0xffff)})
return err
}
func (s Source) SetMute(b bool) error {
muteCmd := '0'
if b {
muteCmd = '1'
}
_, err := s.Client.request(commandSetSourceMute, uint32Tag, uint32(0xffffffff), stringTag, []byte(s.Name), byte(0), uint8(muteCmd))
return err
}
func (s Source) ToggleMute() error {
return s.SetMute(!s.Muted)
}
func (s Source) IsMute() bool {
return s.Muted
}
func (s Source) GetVolume() float32 {
return float32(math.Round(float64(float32(s.Cvolume[0])/0xffff) * 100)) / 100
}
// Sources queries pulseaudio for a list of all it's sources and returns an array of them
func (c *Client) Sources() ([]Source, error) {
b, err := c.request(commandGetSourceInfoList)
if err != nil {
return nil, err
}
var sources []Source
for b.Len() > 0 {
var source Source
err = bread(b, &source)
if err != nil {
return nil, err
}
source.Client = c
sources = append(sources, source)
}
return sources, nil
}
func (c *Client) GetDefaultSource() (Source, error) {
s, err := c.ServerInfo()
if err != nil {
return Source{}, err
}
sources, err := c.Sources()
if err != nil {
return Source{}, err
}
for _, source := range sources{
if source.Name == s.DefaultSource {
return source, nil
}
}
return Source{}, errors2.New("Could not get default sink")
}