forked from noisetorch/NoiseTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.go
203 lines (173 loc) · 6 KB
/
module.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
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/lawl/pulseaudio"
)
const (
loaded = iota
unloaded
inconsistent
)
// the ugly and (partially) repeated strings are unforunately difficult to avoid, as it's what pulse audio expects
func updateNoiseSupressorLoaded(c *pulseaudio.Client, b *int) {
upd, err := c.Updates()
if err != nil {
fmt.Printf("Error listening for updates: %v\n", err)
}
for {
*b = supressorState(c)
if !c.Connected() {
break
}
<-upd
}
}
func supressorState(c *pulseaudio.Client) int {
//perform some checks to see if it looks like the noise supressor is loaded
_, nullsink, err := findModule(c, "module-null-sink", "sink_name=nui_mic_denoised_out")
if err != nil {
log.Printf("Couldn't fetch module list to check for module-null-sink: %v\n", err)
}
_, ladspasink, err := findModule(c, "module-ladspa-sink", "sink_name=nui_mic_raw_in sink_master=nui_mic_denoised_out")
if err != nil {
log.Printf("Couldn't fetch module list to check for module-ladspa-sink: %v\n", err)
}
_, loopback, err := findModule(c, "module-loopback", "sink=nui_mic_raw_in")
if err != nil {
log.Printf("Couldn't fetch module list to check for module-loopback: %v\n", err)
}
_, remap, err := findModule(c, "module-remap-source", "master=nui_mic_denoised_out.monitor source_name=nui_mic_remap")
if err != nil {
log.Printf("Couldn't fetch module list to check for module-remap-source: %v\n", err)
}
if nullsink && ladspasink && loopback && remap {
return loaded
}
if nullsink || ladspasink || loopback || remap {
return inconsistent
}
return unloaded
}
func loadSupressor(ctx *ntcontext, inp input) error {
c := ctx.paClient
log.Printf("Querying pulse rlimit\n")
pid, err := getPulsePid()
if err != nil {
return err
}
lim, err := getRlimit(pid)
if err != nil {
return err
}
log.Printf("Rlimit: %+v. Trying to remove.\n", lim)
if hasCapSysResource(getCurrentCaps()) {
log.Printf("Have capabilities\n")
removeRlimit(pid)
} else {
log.Printf("Capabilities missing, removing via pkexec\n")
removeRlimitAsRoot(pid)
}
defer setRlimit(pid, &lim) // lowering RLIMIT doesn't require root
newLim, err := getRlimit(pid)
if err != nil {
return err
}
log.Printf("Rlimit: %+v\n", newLim)
log.Printf("Loading supressor\n")
idx, err := c.LoadModule("module-null-sink", "sink_name=nui_mic_denoised_out rate=48000")
if err != nil {
return err
}
log.Printf("Loaded null sink as idx: %d\n", idx)
time.Sleep(time.Millisecond * 500) // pulseaudio gets SIGKILL'd because of RLIMITS if we send these too fast
idx, err = c.LoadModule("module-ladspa-sink",
fmt.Sprintf("sink_name=nui_mic_raw_in sink_master=nui_mic_denoised_out "+
"label=noise_suppressor_mono plugin=%s control=%d", ctx.librnnoise, ctx.config.Threshold))
if err != nil {
return err
}
log.Printf("Loaded ladspa sink as idx: %d\n", idx)
time.Sleep(time.Millisecond * 1000) // pulseaudio gets SIGKILL'd because of RLIMITS if we send these too fast
idx, err = c.LoadModule("module-loopback",
fmt.Sprintf("source=%s sink=nui_mic_raw_in channels=1 latency_msec=1 source_dont_move=true sink_dont_move=true", inp.ID))
if err != nil {
return err
}
log.Printf("Loaded loopback as idx: %d\n", idx)
time.Sleep(time.Millisecond * 500) // pulseaudio gets SIGKILL'd because of RLIMITS if we send these too fast
idx, err = c.LoadModule("module-remap-source", `master=nui_mic_denoised_out.monitor `+
`source_name=nui_mic_remap source_properties="device.description='NoiseTorch Microphone'"`)
if err != nil {
return err
}
log.Printf("Loaded ladspa sink as idx: %d\n", idx)
return nil
}
func unloadSupressor(c *pulseaudio.Client) error {
log.Printf("Unloading pulseaudio modules\n")
// we ignore errors here on purpose, since NT didn't use to do this for unloading anyways
// and we don't want to prompt for root again
// so this only suceeds with CAP_SYS_RESOURCE, which we want to make the new default anyways.
if pid, err := getPulsePid(); err == nil {
if lim, err := getRlimit(pid); err == nil {
removeRlimit(pid)
defer setRlimit(pid, &lim)
}
}
log.Printf("Searching for null-sink\n")
m, found, err := findModule(c, "module-null-sink", "sink_name=nui_mic_denoised_out")
if err != nil {
return err
}
if found {
log.Printf("Found null-sink at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
time.Sleep(time.Millisecond * 500) // pulseaudio gets SIGKILL'd because of RLIMITS if we send these too fast
log.Printf("Searching for ladspa-sink\n")
m, found, err = findModule(c, "module-ladspa-sink", "sink_name=nui_mic_raw_in sink_master=nui_mic_denoised_out")
if err != nil {
return err
}
if found {
log.Printf("Found ladspa-sink at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
time.Sleep(time.Millisecond * 500) // pulseaudio gets SIGKILL'd because of RLIMITS if we send these too fast
log.Printf("Searching for loopback\n")
m, found, err = findModule(c, "module-loopback", "sink=nui_mic_raw_in")
if err != nil {
return err
}
if found {
log.Printf("Found loopback at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
time.Sleep(time.Millisecond * 500) // pulseaudio gets SIGKILL'd because of RLIMITS if we send these too fast
log.Printf("Searching for remap-source\n")
m, found, err = findModule(c, "module-remap-source", "master=nui_mic_denoised_out.monitor source_name=nui_mic_remap")
if err != nil {
return err
}
if found {
log.Printf("Found remap source at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
return nil
}
// Finds a module by exactly matching the module name, and checking if the second string is a substring of the argument
func findModule(c *pulseaudio.Client, name string, argMatch string) (module pulseaudio.Module, found bool, err error) {
lst, err := c.ModuleList()
if err != nil {
return pulseaudio.Module{}, false, err
}
for _, m := range lst {
if m.Name == name && strings.Contains(m.Argument, argMatch) {
return m, true, nil
}
}
return pulseaudio.Module{}, false, nil
}