/
source.go
240 lines (201 loc) · 5.88 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
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
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 SourceState int
const (
SourceStateInvalid SourceState = C.PA_SOURCE_INVALID_STATE
SourceStateRunning = C.PA_SOURCE_RUNNING
SourceStateIdle = C.PA_SOURCE_IDLE
SourceStateSuspended = C.PA_SOURCE_SUSPENDED
)
func (self SourceState) String() string {
switch self {
case SourceStateRunning:
return `RUNNING`
case SourceStateIdle:
return `IDLE`
case SourceStateSuspended:
return `SUSPENDED`
default:
return `INVALID`
}
}
// A Source represents a logical audio input source
//
type Source struct {
BaseVolumeStep int
CardIndex int
Channels int
CurrentVolumeStep int
Description string
DriverName string
Index int
ModuleIndex int
MonitorOfSinkIndex int
MonitorOfSinkName string
Muted bool
Name string
NumFormats int
NumPorts int
NumVolumeSteps int
State SourceState
VolumeFactor float64
Properties map[string]interface{}
conn *Conn
}
func (self *Source) MarshalJSON() ([]byte, error) {
type Alias Source
return json.Marshal(&struct {
StateValue string
*Alias
}{
StateValue: self.State.String(),
Alias: (*Alias)(self),
})
}
// Populate this source's fields with data in a string-interface{} map.
//
func (self *Source) Initialize(properties map[string]interface{}) error {
self.Properties, _ = maputil.DiffuseMap(properties, `.`)
if err := populateStruct(self.Properties, self); err == nil {
self.loadSourceStateFromProperties()
} else {
return err
}
return nil
}
func (self *Source) P(key string) typeutil.Variant {
return maputil.M(self.Properties).Get(key)
}
func (self *Source) loadSourceStateFromProperties() {
state := SourceStateInvalid
if v := self.P(`_state`); !v.IsNil() {
switch int(v.Int()) {
case int(C.PA_SOURCE_RUNNING):
state = SourceStateRunning
case int(C.PA_SOURCE_IDLE):
state = SourceStateIdle
case int(C.PA_SOURCE_SUSPENDED):
state = SourceStateSuspended
}
delete(self.Properties, `_state`)
}
self.State = state
}
// Synchronize this source's data with the PulseAudio daemon.
//
func (self *Source) Refresh() error {
operation := NewOperation(self.conn)
defer operation.Destroy()
operation.paOper = C.pa_context_get_source_info_by_index(self.conn.context, C.uint32_t(self.Index), (C.pa_source_info_cb_t)(C.pulse_get_source_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 source response: expected 1 payload, got %d", l)
}
return nil
})
}
// Set the volume of all channels of this source 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 *Source) 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_source_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 source %d, no channels defined", self.Index)
}
}
// Add the given factor to the current source volume
//
func (self *Source) 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 source volume, or
// set to a minimum of 0.0.
//
func (self *Source) 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 source.
//
func (self *Source) 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_source_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 source.
//
func (self *Source) Mute() error {
return self.SetMute(true)
}
// Explicitly unmute the source.
//
func (self *Source) Unmute() error {
return self.SetMute(false)
}
// Mute or unmute the source, depending on whether it is currently
// unmuted or muted (respectively).
//
func (self *Source) ToggleMute() error {
if err := self.Refresh(); err == nil {
return self.SetMute(!self.Muted)
} else {
return err
}
}