/
sink.go
256 lines (217 loc) · 5.83 KB
/
sink.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package pulse
// #cgo CFLAGS: -Wno-error=implicit-function-declaration
// #include "conn.h"
// #cgo pkg-config: libpulse
import "C"
import (
"encoding/json"
"fmt"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/typeutil"
)
type SinkState int
const (
SinkStateInvalid SinkState = C.PA_SINK_INVALID_STATE
SinkStateRunning = C.PA_SINK_RUNNING
SinkStateIdle = C.PA_SINK_IDLE
SinkStateSuspended = C.PA_SINK_SUSPENDED
)
func (self SinkState) String() string {
switch self {
case SinkStateRunning:
return `RUNNING`
case SinkStateIdle:
return `IDLE`
case SinkStateSuspended:
return `SUSPENDED`
default:
return `INVALID`
}
}
// A Sink represents a logical audio output destination with its own volume control.
//
type Sink struct {
CardIndex int
Channels int
CurrentVolumeStep int
Description string
DriverName string
Index int
ModuleIndex int
MonitorSourceIndex int
MonitorSourceName string
Muted bool
Name string
NumFormats int
NumPorts int
NumVolumeSteps int
Properties map[string]interface{}
State SinkState
VolumeFactor float64
conn *Conn
}
func (self *Sink) MarshalJSON() ([]byte, error) {
type Alias Sink
return json.Marshal(&struct {
StateValue string
*Alias
}{
StateValue: self.State.String(),
Alias: (*Alias)(self),
})
}
// Populate this sink's fields with data in a string-interface{} map.
//
func (self *Sink) Initialize(properties map[string]interface{}) error {
self.Properties, _ = maputil.DiffuseMap(properties, `.`)
if err := populateStruct(self.Properties, self); err == nil {
self.loadSinkStateFromProperties()
} else {
return err
}
return nil
}
func (self *Sink) P(key string) typeutil.Variant {
return maputil.M(self.Properties).Get(key)
}
func (self *Sink) loadSinkStateFromProperties() {
state := SinkStateInvalid
if v := self.P(`_state`); !v.IsNil() {
switch int(v.Int()) {
case int(C.PA_SINK_RUNNING):
state = SinkStateRunning
case int(C.PA_SINK_IDLE):
state = SinkStateIdle
case int(C.PA_SINK_SUSPENDED):
state = SinkStateSuspended
}
delete(self.Properties, `_state`)
}
self.State = state
}
// Synchronize this sink's data with the PulseAudio daemon.
//
func (self *Sink) Refresh() error {
operation := NewOperation(self.conn)
defer operation.Destroy()
operation.paOper = C.pa_context_get_sink_info_by_index(
self.conn.context,
C.uint32_t(self.Index),
(C.pa_sink_info_cb_t)(C.pulse_get_sink_info_by_index_callback),
operation.Userdata(),
)
// wait for the operation to finish and handle success and error cases
return operation.WaitSuccess(func(op *Operation) error {
if l := len(op.Payloads); l == 1 {
payload := operation.Payloads[0]
if err := self.Initialize(payload.Properties); err != nil {
return err
}
} else {
return fmt.Errorf("Invalid sink response: expected 1 payload, got %d", l)
}
return nil
})
}
// Set the volume of all channels of this sink to a factor of the maximum
// volume (0.0 <= v <= 1.0). Factors greater than 1.0 will be accepted, but
// clipping or distortion may occur beyond that value.
//
func (self *Sink) SetVolume(factor float64) error {
if self.Channels > 0 {
operation := NewOperation(self.conn)
defer operation.Destroy()
newVolume := &C.pa_cvolume{}
// new volume is the (maximum number of normal volume steps * factor)
newVolume = C.pa_cvolume_init(newVolume)
newVolumeT := C.pa_volume_t(C.uint32_t(uint(float64(self.NumVolumeSteps) * factor)))
// prepare newVolume for its journey into PulseAudio
C.pa_cvolume_set(newVolume, C.uint(self.Channels), newVolumeT)
// make the call
operation.paOper = C.pa_context_set_sink_volume_by_index(
self.conn.context,
C.uint32_t(self.Index),
newVolume,
(C.pa_context_success_cb_t)(C.pulse_generic_success_callback),
operation.Userdata(),
)
// wait for the result, refresh, return any errors
if err := operation.Wait(); err == nil {
return self.Refresh()
} else {
return err
}
} else {
return fmt.Errorf("Cannot set volume on sink %d, no channels defined", self.Index)
}
}
// Add the given factor to the current sink volume
//
func (self *Sink) IncreaseVolume(factor float64) error {
if err := self.Refresh(); err == nil {
newFactor := (self.VolumeFactor + factor)
return self.SetVolume(newFactor)
} else {
return err
}
}
// Remove the given factor from the current sink volume, or
// set to a minimum of 0.0.
//
func (self *Sink) DecreaseVolume(factor float64) error {
if err := self.Refresh(); err == nil {
newFactor := (self.VolumeFactor - factor)
if newFactor < 0.0 {
return self.SetVolume(0.0)
} else {
return self.SetVolume(newFactor)
}
} else {
return err
}
}
// Explicitly set the muted or unmuted state of the sink.
//
func (self *Sink) SetMute(mute bool) error {
operation := NewOperation(self.conn)
defer operation.Destroy()
var muting C.int
if mute {
muting = C.int(1)
} else {
muting = C.int(0)
}
operation.paOper = C.pa_context_set_sink_mute_by_index(
self.conn.context,
C.uint32_t(self.Index),
muting,
(C.pa_context_success_cb_t)(C.pulse_generic_success_callback),
operation.Userdata(),
)
// wait for the result, refresh, return any errors
if err := operation.Wait(); err == nil {
return self.Refresh()
} else {
return err
}
}
// Explicitly mute the sink.
//
func (self *Sink) Mute() error {
return self.SetMute(true)
}
// Explicitly unmute the sink.
//
func (self *Sink) Unmute() error {
return self.SetMute(false)
}
// Mute or unmute the sink, depending on whether it is currently
// unmuted or muted (respectively).
//
func (self *Sink) ToggleMute() error {
if err := self.Refresh(); err == nil {
return self.SetMute(!self.Muted)
} else {
return err
}
}