forked from omriharel/deej
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession_linux.go
224 lines (168 loc) · 4.48 KB
/
session_linux.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
package deej
import (
"errors"
"fmt"
"go.uber.org/zap"
"github.com/jfreymuth/pulse/proto"
)
// normal PulseAudio volume (100%)
const maxVolume = 0x10000
var errNoSuchProcess = errors.New("No such process")
type paSession struct {
baseSession
processName string
client *proto.Client
sinkInputIndex uint32
sinkInputChannels byte
}
type masterSession struct {
baseSession
client *proto.Client
streamIndex uint32
streamChannels byte
isOutput bool
}
func newPASession(
logger *zap.SugaredLogger,
client *proto.Client,
sinkInputIndex uint32,
sinkInputChannels byte,
processName string,
) *paSession {
s := &paSession{
client: client,
sinkInputIndex: sinkInputIndex,
sinkInputChannels: sinkInputChannels,
}
s.processName = processName
s.name = processName
s.humanReadableDesc = processName
// use a self-identifying session name e.g. deej.sessions.chrome
s.logger = logger.Named(s.Key())
s.logger.Debugw(sessionCreationLogMessage, "session", s)
return s
}
func newMasterSession(
logger *zap.SugaredLogger,
client *proto.Client,
streamIndex uint32,
streamChannels byte,
isOutput bool,
) *masterSession {
s := &masterSession{
client: client,
streamIndex: streamIndex,
streamChannels: streamChannels,
isOutput: isOutput,
}
var key string
if isOutput {
key = masterSessionName
} else {
key = inputSessionName
}
s.logger = logger.Named(key)
s.master = true
s.name = key
s.humanReadableDesc = key
s.logger.Debugw(sessionCreationLogMessage, "session", s)
return s
}
func (s *paSession) GetVolume() float32 {
request := proto.GetSinkInputInfo{
SinkInputIndex: s.sinkInputIndex,
}
reply := proto.GetSinkInputInfoReply{}
if err := s.client.Request(&request, &reply); err != nil {
s.logger.Warnw("Failed to get session volume", "error", err)
}
level := parseChannelVolumes(reply.ChannelVolumes)
return level
}
func (s *paSession) SetVolume(v float32) error {
volumes := createChannelVolumes(s.sinkInputChannels, v)
request := proto.SetSinkInputVolume{
SinkInputIndex: s.sinkInputIndex,
ChannelVolumes: volumes,
}
if err := s.client.Request(&request, nil); err != nil {
s.logger.Warnw("Failed to set session volume", "error", err)
return fmt.Errorf("adjust session volume: %w", err)
}
s.logger.Debugw("Adjusting session volume", "to", fmt.Sprintf("%.2f", v))
return nil
}
func (s *paSession) Release() {
s.logger.Debug("Releasing audio session")
}
func (s *paSession) String() string {
return fmt.Sprintf(sessionStringFormat, s.humanReadableDesc, s.GetVolume())
}
func (s *masterSession) GetVolume() float32 {
var level float32
if s.isOutput {
request := proto.GetSinkInfo{
SinkIndex: s.streamIndex,
}
reply := proto.GetSinkInfoReply{}
if err := s.client.Request(&request, &reply); err != nil {
s.logger.Warnw("Failed to get session volume", "error", err)
return 0
}
level = parseChannelVolumes(reply.ChannelVolumes)
} else {
request := proto.GetSourceInfo{
SourceIndex: s.streamIndex,
}
reply := proto.GetSourceInfoReply{}
if err := s.client.Request(&request, &reply); err != nil {
s.logger.Warnw("Failed to get session volume", "error", err)
return 0
}
level = parseChannelVolumes(reply.ChannelVolumes)
}
return level
}
func (s *masterSession) SetVolume(v float32) error {
var request proto.RequestArgs
volumes := createChannelVolumes(s.streamChannels, v)
if s.isOutput {
request = &proto.SetSinkVolume{
SinkIndex: s.streamIndex,
ChannelVolumes: volumes,
}
} else {
request = &proto.SetSourceVolume{
SourceIndex: s.streamIndex,
ChannelVolumes: volumes,
}
}
if err := s.client.Request(request, nil); err != nil {
s.logger.Warnw("Failed to set session volume",
"error", err,
"volume", v)
return fmt.Errorf("adjust session volume: %w", err)
}
s.logger.Debugw("Adjusting session volume", "to", fmt.Sprintf("%.2f", v))
return nil
}
func (s *masterSession) Release() {
s.logger.Debug("Releasing audio session")
}
func (s *masterSession) String() string {
return fmt.Sprintf(sessionStringFormat, s.humanReadableDesc, s.GetVolume())
}
func createChannelVolumes(channels byte, volume float32) []uint32 {
volumes := make([]uint32, channels)
for i := range volumes {
volumes[i] = uint32(volume * maxVolume)
}
return volumes
}
func parseChannelVolumes(volumes []uint32) float32 {
var level uint32
for _, volume := range volumes {
level += volume
}
return float32(level) / float32(len(volumes)) / float32(maxVolume)
}