forked from noisetorch/NoiseTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
452 lines (394 loc) · 13.5 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"fmt"
"log"
"strings"
"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(ctx *ntcontext) {
c := ctx.paClient
upd, err := c.Updates()
if err != nil {
fmt.Printf("Error listening for updates: %v\n", err)
}
for {
ctx.noiseSupressorState, ctx.virtualDeviceInUse = supressorState(ctx)
if !c.Connected() {
break
}
<-upd
}
}
func supressorState(ctx *ntcontext) (int, bool) {
//perform some checks to see if it looks like the noise supressor is loaded
c := ctx.paClient
var inpLoaded, outLoaded, inputInc, outputInc bool
var virtualDeviceInUse bool = false
if ctx.config.FilterInput {
if ctx.serverInfo.servertype == servertype_pipewire {
module, ladspasource, err := findModule(c, "module-ladspa-source", "source_name='NoiseTorch Microphone")
if err != nil {
log.Printf("Couldn't fetch module list to check for module-ladspa-source: %v\n", err)
}
virtualDeviceInUse = virtualDeviceInUse || (module.NUsed != 0)
inpLoaded = ladspasource
inputInc = false
} else {
_, 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)
}
module, 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)
}
virtualDeviceInUse = virtualDeviceInUse || (module.NUsed != 0)
if nullsink && ladspasink && loopback && remap {
inpLoaded = true
} else if nullsink || ladspasink || loopback || remap {
inputInc = true
}
}
} else {
inpLoaded = true
}
if ctx.config.FilterOutput {
if ctx.serverInfo.servertype == servertype_pipewire {
module, ladspasink, err := findModule(c, "module-ladspa-sink", "sink_name='NoiseTorch Headphones'")
if err != nil {
log.Printf("Couldn't fetch module list to check for module-ladspa-sink: %v\n", err)
}
virtualDeviceInUse = virtualDeviceInUse || (module.NUsed != 0)
outLoaded = ladspasink
outputInc = false
} else {
_, out, err := findModule(c, "module-null-sink", "sink_name=nui_out_out_sink")
if err != nil {
log.Printf("Couldn't fetch module list to check for output module-ladspa-sink: %v\n", err)
}
_, lad, err := findModule(c, "module-ladspa-sink", "sink_name=nui_out_ladspa")
if err != nil {
log.Printf("Couldn't fetch module list to check for output module-ladspa-sink: %v\n", err)
}
_, loop, err := findModule(c, "module-loopback", "source=nui_out_out_sink.monitor")
if err != nil {
log.Printf("Couldn't fetch module list to check for output module-ladspa-sink: %v\n", err)
}
module, outin, err := findModule(c, "module-null-sink", "sink_name=nui_out_in_sink")
if err != nil {
log.Printf("Couldn't fetch module list to check for output module-ladspa-sink: %v\n", err)
}
virtualDeviceInUse = virtualDeviceInUse || (module.NUsed != 0)
_, loop2, err := findModule(c, "module-loopback", "source=nui_out_in_sink.monitor")
if err != nil {
log.Printf("Couldn't fetch module list to check for output module-ladspa-sink: %v\n", err)
}
outLoaded = out && lad && loop && outin && loop2
outputInc = out || lad || loop || outin || loop2
}
} else {
outLoaded = true
}
if (inpLoaded || !ctx.config.FilterInput) && (outLoaded || !ctx.config.FilterOutput) && !inputInc {
return loaded, virtualDeviceInUse
}
if (inpLoaded && ctx.config.FilterInput) || (outLoaded && ctx.config.FilterOutput) || inputInc || outputInc {
return inconsistent, virtualDeviceInUse
}
return unloaded, virtualDeviceInUse
}
func loadSupressor(ctx *ntcontext, inp *device, out *device) error {
if ctx.serverInfo.servertype == servertype_pulse {
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)
removeRlimit(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)
}
if inp.checked {
var err error
if ctx.serverInfo.servertype == servertype_pipewire {
err = loadPipeWireInput(ctx, inp)
} else {
err = loadPulseInput(ctx, inp)
}
if err != nil {
log.Printf("Error loading input: %v\n", err)
return err
}
}
if out.checked {
var err error
if ctx.serverInfo.servertype == servertype_pipewire {
err = loadPipeWireOutput(ctx, out)
} else {
err = loadPulseOutput(ctx, out)
}
if err != nil {
log.Printf("Error loading output: %v\n", err)
return err
}
}
return nil
}
func loadModule(ctx *ntcontext, module, args string) (uint32, error) {
idx, err := ctx.paClient.LoadModule(module, args)
//14 = module initialisation failed
if paErr, ok := err.(*pulseaudio.Error); ok && paErr.Code == 14 {
resetUI(ctx)
ctx.views.Push(makeErrorView(ctx, fmt.Sprintf("Could not load module '%s'. This is likely a problem with your system or distribution.", module)))
}
return idx, err
}
func loadPipeWireInput(ctx *ntcontext, inp *device) error {
log.Printf("Loading supressor for pipewire\n")
idx, err := loadModule(ctx, "module-ladspa-source",
fmt.Sprintf("source_name='NoiseTorch Microphone for %s' master=%s "+
"rate=48000 channels=1 "+
"label=noisetorch plugin=%s control=%d", inp.Name, inp.ID, ctx.librnnoise, ctx.config.Threshold))
if err != nil {
return err
}
log.Printf("Loaded ladspa source as idx: %d\n", idx)
return nil
}
func loadPipeWireOutput(ctx *ntcontext, out *device) error {
log.Printf("Loading supressor for pipewire\n")
idx, err := loadModule(ctx, "module-ladspa-sink",
fmt.Sprintf("sink_name='NoiseTorch Headphones' master=%s "+
"rate=48000 channels=1 "+
"label=noisetorch plugin=%s control=%d", out.ID, ctx.librnnoise, ctx.config.Threshold))
if err != nil {
return err
}
log.Printf("Loaded ladspa source as idx: %d\n", idx)
return nil
}
func loadPulseInput(ctx *ntcontext, inp *device) error {
log.Printf("Loading supressor for pulse\n")
idx, err := loadModule(ctx, "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)
idx, err = loadModule(ctx, "module-ladspa-sink",
fmt.Sprintf("sink_name=nui_mic_raw_in sink_master=nui_mic_denoised_out "+
"label=noisetorch plugin=%s control=%d", ctx.librnnoise, ctx.config.Threshold))
if err != nil {
return err
}
log.Printf("Loaded ladspa sink as idx: %d\n", idx)
if inp.dynamicLatency {
idx, err = loadModule(ctx, "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)
} else {
idx, err = loadModule(ctx, "module-loopback",
fmt.Sprintf("source=%s sink=nui_mic_raw_in channels=1 latency_msec=50 source_dont_move=true sink_dont_move=true adjust_time=1", inp.ID))
if err != nil {
return err
}
log.Printf("Loaded fixed latency loopback as idx: %d\n", idx)
}
idx, err = loadModule(ctx, "module-remap-source", fmt.Sprintf(`master=nui_mic_denoised_out.monitor `+
`source_name=nui_mic_remap source_properties="device.description='NoiseTorch Microphone for %s'"`, inp.Name))
if err != nil {
return err
}
log.Printf("Loaded remap source as idx: %d\n", idx)
return nil
}
func loadPulseOutput(ctx *ntcontext, out *device) error {
_, err := loadModule(ctx, "module-null-sink", `sink_name=nui_out_out_sink`)
if err != nil {
return err
}
_, err = loadModule(ctx, "module-null-sink", `sink_name=nui_out_in_sink sink_properties="device.description='NoiseTorch Headphones'"`)
if err != nil {
return err
}
_, err = loadModule(ctx, "module-ladspa-sink", fmt.Sprintf(`sink_name=nui_out_ladspa sink_master=nui_out_out_sink `+
`label=noisetorch channels=1 plugin=%s control=%d rate=%d`,
ctx.librnnoise, ctx.config.Threshold, 48000))
if err != nil {
return err
}
_, err = loadModule(ctx, "module-loopback",
fmt.Sprintf("source=nui_out_out_sink.monitor sink=%s channels=2 latency_msec=50 source_dont_move=true sink_dont_move=true", out.ID))
if err != nil {
return err
}
_, err = loadModule(ctx, "module-loopback",
fmt.Sprintf("source=nui_out_in_sink.monitor sink=nui_out_ladspa channels=1 latency_msec=50 source_dont_move=true sink_dont_move=true"))
if err != nil {
return err
}
return nil
}
func unloadSupressor(ctx *ntcontext) error {
if ctx.serverInfo.servertype == servertype_pipewire {
return unloadSupressorPipeWire(ctx)
} else {
return unloadSupressorPulse(ctx)
}
}
func unloadSupressorPipeWire(ctx *ntcontext) error {
log.Printf("Unloading modules for pipewire\n")
log.Printf("Searching for module-ladspa-source\n")
c := ctx.paClient
m, found, err := findModule(c, "module-ladspa-source", "source_name='NoiseTorch Microphone")
if err != nil {
return err
}
if found {
log.Printf("Found module-ladspa-source at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
log.Printf("Searching for module-ladspa-sink\n")
m, found, err = findModule(c, "module-ladspa-sink", "sink_name='NoiseTorch Headphones'")
if err != nil {
return err
}
if found {
log.Printf("Found module-ladspa-sink at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
return nil
}
func unloadSupressorPulse(ctx *ntcontext) error {
log.Printf("Unloading modules for pulseaudio\n")
if pid, err := getPulsePid(); err == nil {
if lim, err := getRlimit(pid); err == nil {
log.Printf("Trying to remove rlimit. Limit is: %+v\n", lim)
removeRlimit(pid)
newLim, _ := getRlimit(pid)
log.Printf("Rlimit: %+v\n", newLim)
defer setRlimit(pid, &lim)
}
}
log.Printf("Searching for null-sink\n")
c := ctx.paClient
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)
}
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)
}
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)
}
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)
}
log.Printf("Searching for output module-null-sink\n")
m, found, err = findModule(c, "module-null-sink", "sink_name=nui_out_out_sink")
if err != nil {
return err
}
if found {
log.Printf("Found output null sink at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
log.Printf("Searching for output module-null-sink\n")
m, found, err = findModule(c, "module-null-sink", "sink_name=nui_out_in_sink")
if err != nil {
return err
}
if found {
log.Printf("Found output null sink at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
log.Printf("Searching for output module-ladspa-sink\n")
m, found, err = findModule(c, "module-ladspa-sink", "sink_name=nui_out_ladspa")
if err != nil {
return err
}
if found {
log.Printf("Found output ladspa sink at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
log.Printf("Searching for output module-loopback\n")
m, found, err = findModule(c, "module-loopback", "source=nui_out_out_sink.monitor")
if err != nil {
return err
}
if found {
log.Printf("Found output loopback at id [%d], sending unload command\n", m.Index)
c.UnloadModule(m.Index)
}
log.Printf("Searching for output module-loopback\n")
m, found, err = findModule(c, "module-loopback", "source=nui_out_in_sink.monitor")
if err != nil {
return err
}
if found {
log.Printf("Found output loopback 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
}