-
Notifications
You must be signed in to change notification settings - Fork 1
/
rnnoise_loader
executable file
·228 lines (211 loc) · 6.68 KB
/
rnnoise_loader
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
#!/bin/bash
UNLOAD=0
MIC=1
SPEAKER=0
CONTROL=95
DEST="pacmd"
LADSPA_LIB=librnnoise_ladspa.so
SOURCE="$(pactl info | grep 'Default Source' | tr -d ' ' | cut -d ':' -f2)"
while getopts ':humspfil:c:tx' opt; do
case ${opt} in
h )
echo "Usage: $(basename "$0") [-u] [-m] [-s] [-p] [-c value]"
echo "Loading/Unloading of microphone suppression is enabled by default"
echo ""
echo "Options:"
echo -e " -u \tUnload modules from PulseAudio instead of (re)loading"
echo -e " -m \tDisable load/unload microphone suppression for default source"
echo -e " -s \tLoad/Unload speaker suppression for default sink"
echo -e " -t \tToggle on/off, based on mic status if both are activated"
echo -e " -c \tSet RNNoise control, in percent (default: 95)"
echo -e " -p \tOnly print PulseAudio commands, do not execute them (pipe mode)"
echo -e " -l \tProvide path to librnnoise_ladspa.so (by default the standard ladspa lib path will be used)"
echo -e " -f \tForce loading, disable checks if ladspa library is valid"
echo -e " -x \tXFCE Generic monitor mode"
echo -e " -i \tInfo mode, show current status and exit"
echo -e " -h \tShow this help and exit"
echo ""
echo "Notes:"
echo "The source for the microphone noise suppression is set to the default source"
echo "when this script is running. The sink for the speaker is set to '@DEFAULT_SINK@'."
echo "Current default source: ${SOURCE}"
exit 0
;;
u )
UNLOAD=1
;;
m )
MIC=0
;;
s )
SPEAKER=1
;;
t )
TOGGLE=1
;;
p )
DEST="cat"
;;
c )
if ! [[ $OPTARG =~ ^[0-9]+$ ]] ; then
echo "Invalid argument: -${opt} requires an integer" >&2
exit 1
fi
CONTROL="$OPTARG"
;;
l )
LADSPA_LIB="$OPTARG"
;;
f )
FORCE_LADSPA=1
;;
i )
SHOW_STATUS=1
;;
x )
XFCE_MONITOR=1
;;
\? )
echo "Invalid option: $OPTARG" >&2
exit 1
;;
: )
echo "Argument required for -${OPTARG}"
exit 1
;;
esac
done
get_active_mic (){
pactl list modules short | grep -o 'module-loopback.sink=rnnoise_mic_raw_in source=[^[:blank:]]\+' | cut -d '=' -f 3
}
is_mic_active (){
[ -n "$(get_active_mic)" ]
}
is_speaker_active (){
pactl list modules short | grep -q 'module-ladspa-sink.sink_name=rnnoise_out_ladspa'
}
unload (){
module="$1"
options="$2"
module_id=$(pactl list modules short | grep -e "${module}.${options}" | cut -f1)
if [ -n "$module_id" ]
then
echo "unload-module ${module_id}" | $DEST
fi
}
unload_mic (){
unload 'module-remap-source' 'source_name=rnnoise_denoised'
unload 'module-loopback' 'sink=rnnoise_mic_raw_in'
unload 'module-ladspa-sink' 'sink_name=rnnoise_mic_raw_in'
unload 'module-null-sink' 'sink_name=rnnoise_mic_denoised_out'
}
unload_speaker (){
unload 'module-loopback' 'source=rnnoise_out_in_sink.monitor'
unload 'module-loopback' 'source=rnnoise_out_out_sink.monitor'
unload 'module-ladspa-sink' 'sink_name=rnnoise_out_ladspa'
unload 'module-null-sink' 'sink_name=rnnoise_out_in_sink'
unload 'module-null-sink' 'sink_name=rnnoise_out_out_sink'
}
if [ -n "$TOGGLE" ]
then
if [ $MIC -eq 1 ]
then
is_mic_active && UNLOAD=1 || UNLOAD=0
elif [ $SPEAKER -eq 1 ]
then
is_speaker_active && UNLOAD=1 || UNLOAD=0
else
echo "At least one output must be specified!" >&2
exit 4
fi
fi
if [ -n "$SHOW_STATUS" ]
then
is_mic_active && echo "Loaded for input $(get_active_mic)"
is_speaker_active && echo "Loaded for default output"
exit 0
fi
if [ -n "$XFCE_MONITOR" ]
then
echo -n '<txt>'
if [ $MIC -eq 1 ]
then
is_mic_active && echo -n "M" || echo -n "m"
else
OPTS+=' -m'
fi
if [ $SPEAKER -eq 1 ]
then
is_speaker_active && echo -n "S" || echo -n "s"
OPTS+=' -s'
fi
echo '</txt>'
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo "<txtclick>${SCRIPTDIR}/$(basename "$0") -t${OPTS}</txtclick>"
is_mic_active && echo "<tool>On input: $(get_active_mic)</tool>" || echo "<tool></tool>"
exit 0
fi
if [ $UNLOAD -eq 0 ]
then
# Always force loading if ladspa helpers are not available
if [ -z $FORCE_LADSPA ] && ! command -v listplugins &> /dev/null && ! command -v analyseplugin &> /dev/null
then
FORCE_LADSPA=1
echo "Disabling LADSPA checking as analyser is not available" >&2
fi
# Load modules, check if ladspa plugin is valid first. If it is valid unload
# the plugins before loading to make sure only a single RNNoise instance per
# sink/source is present.
if [ -z $FORCE_LADSPA ]
then
if ! { { listplugins 2>> /dev/null | grep -q "$LADSPA_LIB" ;} || [ -f "$LADSPA_LIB" ]; }
then
echo "Ladspa: library '${LADSPA_LIB}' not found" >&2
exit 2
fi
if ! { analyseplugin "$LADSPA_LIB" | grep -q "noise_suppressor_mono"; }
then
echo "Ladspa: Label 'noise_suppressor_mono' not found in plugin '${LADSPA_LIB}'" >&2
exit 3
fi
fi
if [ $MIC -eq 1 ]
then
unload_mic
if [ "$DEST" == "pacmd" ]
then
echo "Loading RNNoise for source ${SOURCE}"
fi
$DEST << MODULES
load-module module-null-sink sink_name=rnnoise_mic_denoised_out rate=48000
load-module module-ladspa-sink sink_name=rnnoise_mic_raw_in sink_master=rnnoise_mic_denoised_out label=noise_suppressor_mono plugin=${LADSPA_LIB} control=${CONTROL}
load-module module-loopback sink=rnnoise_mic_raw_in source=${SOURCE} channels=1 latency_msec=1 source_dont_move=true sink_dont_move=true
load-module module-remap-source source_name=rnnoise_denoised master=rnnoise_mic_denoised_out.monitor source_properties="device.description='RNNoise Microphone'" channels=1
MODULES
fi
if [ $SPEAKER -eq 1 ]
then
unload_speaker
if [ "$DEST" == "pacmd" ]
then
echo "Loading RNNoise for sink @DEFAULT_SINK@"
fi
$DEST << MODULES
load-module module-null-sink sink_name=rnnoise_out_out_sink
load-module module-null-sink sink_name=rnnoise_out_in_sink sink_properties="device.description='RNNoise Speaker'"
load-module module-ladspa-sink sink_name=rnnoise_out_ladspa sink_master=rnnoise_out_out_sink label=noise_suppressor_mono channels=1 plugin=${LADSPA_LIB} control=${CONTROL} rate=48000
load-module module-loopback source=rnnoise_out_out_sink.monitor sink=@DEFAULT_SINK@ channels=2 latency_msec=50 source_dont_move=true sink_dont_move=true
load-module module-loopback source=rnnoise_out_in_sink.monitor sink=rnnoise_out_ladspa channels=1 latency_msec=50 source_dont_move=true sink_dont_move=true
MODULES
fi
else
# Unload modules only
if [ $MIC -eq 1 ]
then
unload_mic
fi
if [ $SPEAKER -eq 1 ]
then
unload_speaker
fi
fi