-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
181 lines (136 loc) · 3.79 KB
/
session.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
package deej
import (
"fmt"
"strings"
"github.com/go-ole/go-ole"
"github.com/mitchellh/go-ps"
"github.com/moutend/go-wca"
"github.com/bupher/deejtemp/util"
"go.uber.org/zap"
)
// Session represents a single addressable audio session
type Session interface {
GetVolume() float32
SetVolume(v float32) error
// TODO: future mute support
// GetMute() bool
// SetMute(m bool) error
Key() string
Release()
}
type wcaSession struct {
pid uint32
processName string
system bool
logger *zap.SugaredLogger
control *wca.IAudioSessionControl2
volume *wca.ISimpleAudioVolume
eventCtx *ole.GUID
}
type masterSession struct {
logger *zap.SugaredLogger
volume *wca.IAudioEndpointVolume
eventCtx *ole.GUID
}
func newWCASession(
logger *zap.SugaredLogger,
control *wca.IAudioSessionControl2,
volume *wca.ISimpleAudioVolume,
pid uint32,
eventCtx *ole.GUID,
) (*wcaSession, error) {
s := &wcaSession{
control: control,
volume: volume,
pid: pid,
eventCtx: eventCtx,
}
// special treatment for system sounds session
if pid == 0 {
s.system = true
} else {
// find our session's process name
process, err := ps.FindProcess(int(pid))
if err != nil {
logger.Warnw("Failed to find process name by ID", "pid", pid, "error", err)
defer s.Release()
return nil, fmt.Errorf("find process name by pid: %w", err)
}
s.processName = process.Executable()
}
// use a self-identifying session name e.g. deej.sessions.chrome
s.logger = logger.Named(strings.TrimSuffix(s.Key(), ".exe"))
s.logger.Debugw("Created audio session instance", "session", s)
return s, nil
}
func newMasterSession(
logger *zap.SugaredLogger,
volume *wca.IAudioEndpointVolume,
eventCtx *ole.GUID,
) (*masterSession, error) {
s := &masterSession{
logger: logger.Named(masterSessionName),
volume: volume,
eventCtx: eventCtx,
}
s.logger.Debugw("Created audio session instance", "session", s)
return s, nil
}
func (s *wcaSession) GetVolume() float32 {
var level float32
if err := s.volume.GetMasterVolume(&level); err != nil {
s.logger.Warnw("Failed to get session volume", "error", err)
}
return util.NormalizeScalar(level)
}
func (s *wcaSession) SetVolume(v float32) error {
if err := s.volume.SetMasterVolume(v, s.eventCtx); 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 *wcaSession) Release() {
s.logger.Debug("Releasing audio session")
s.volume.Release()
s.control.Release()
}
func (s *wcaSession) Key() string {
if s.system {
return systemSessionName
}
return strings.ToLower(s.processName)
}
func (s *wcaSession) String() string {
sessionDesc := fmt.Sprintf("%s (pid %d)", s.processName, s.pid)
if s.system {
sessionDesc = "system sounds"
}
return fmt.Sprintf("<session: %s, vol: %.2f>", sessionDesc, s.GetVolume())
}
func (s *masterSession) GetVolume() float32 {
var level float32
if err := s.volume.GetMasterVolumeLevelScalar(&level); err != nil {
s.logger.Warnw("Failed to get session volume", "error", err)
}
return util.NormalizeScalar(level)
}
func (s *masterSession) SetVolume(v float32) error {
if err := s.volume.SetMasterVolumeLevelScalar(v, s.eventCtx); 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 *masterSession) Release() {
s.logger.Debug("Releasing audio session")
s.volume.Release()
}
func (s *masterSession) Key() string {
return masterSessionName
}
func (s *masterSession) String() string {
return fmt.Sprintf("<session: %s, vol: %.2f>", masterSessionName, s.GetVolume())
}