forked from Magisk-Modules-Alt-Repo/audio-misc-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customize-functions.sh
383 lines (336 loc) · 14.2 KB
/
customize-functions.sh
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
#!/system/bin/sh
# Get the active audio policy configuration fille from the audioserever
function getActivePolicyFile()
{
dumpsys media.audio_policy | awk '
/^ Config source: / {
print $3
}'
}
function stopDRC()
{
# stopDRC has two args specifying a main audio policy configuration XML file (eg. audio_policy_configuration.xml) and its dummy one to be overridden
if [ $# -eq 2 -a -r "$1" -a -w "$2" ]; then
# Copy and override an original audio_policy_configuration.xml to its dummy file
cp -f "$1" "$2"
# Change audio_policy_configuration.xml file to remove DRC
sed -i 's/speaker_drc_enabled[:space:]*=[:space:]*"true"/speaker_drc_enabled="false"/' "$2"
fi
}
# Get the actual audio configuration XML file name, even for Xiaomi, OnePlus, etc. stock devices
# that may overlay another file on the dummy mount point file
function getActualConfigXML()
{
if [ $# -eq 1 ]; then
local dir=${1%/*}
local fname=${1##*/}
local sname=${fname%.*}
if [ -r "${dir}/${sname}_sec.xml" ]; then
echo "${dir}/${sname}_sec.xml"
elif [ -e "${dir}_qssi" -a -r "${dir}_qssi/${fname}" ]; then
# OnePlus stock pattern
echo "${dir}_qssi/${fname}"
elif [ "${dir##*/}" = "sku_`getprop ro.board.platform`" -a -r "${dir%/*}/${fname}" ]; then
# OnePlus stock pattern2
echo "${dir%/*}/${fname}"
elif [ -r "${dir}/audio/${fname}" ]; then
# Xiaomi stock pattern
echo "${dir}/audio/${fname}"
elif [ -r "${dir}/${sname}_base.xml" ]; then
echo "${dir}/${sname}_base.xml"
else
echo "$1"
fi
fi
}
function toHexString()
{
if [ $# -ge 1 ]; then
local tmp
tmp=`echo -n "$1" | xxd -p | tr -d ' \n'`
if [ $# -eq 2 ]; then
if [ ${#tmp} -ge $2 ]; then
echo -n ${tmp:0:$2}
else
local strt=`expr ${#tmp} + 1`
echo -n "$tmp"
for i in `seq $strt $2` ; do
echo -n "0"
done
fi
else
echo -n "$tmp"
fi
else
return 1
fi
}
# Patch libalsautils.so to map a property string to another
# arg1: original libalsautils.so file; arg2: patched libalsautils.so file;
function patchMapProperty()
{
local orig_prop='ro.audio.usb.period_us'
local new_prop='vendor.audio.usb.perio'
if [ $# -eq 2 -a -r "$1" ]; then
local pat1=`toHexString "$orig_prop"`
local pat2=`toHexString "$new_prop" ${#pat1}`
xxd -p <"$1" | tr -d ' \n' | sed -e "s/$pat1/$pat2/" \
| awk 'BEGIN {
foldWidth=60
getline buf
len=length(buf)
for (i=1; i <= len; i+=foldWidth) {
if (i + foldWidth - 1 <= len)
print substr(buf, i, foldWidth)
else
print substr(buf, i, len)
}
exit
}' \
| xxd -r -p >"$2"
return $?
else
return 1
fi
}
function makeLibraries()
{
local MAGISKPATH="$(magisk --path)"
local d lname
for d in "lib" "lib64"; do
for lname in "libalsautils.so" "libalsautilsv2.so" "audio_usb_aoc.so"; do
if [ -r "${MAGISKPATH}/.magisk/mirror/vendor/${d}/${lname}" ]; then
mkdir -p "${MODPATH}/system/vendor/${d}"
patchMapProperty "${MAGISKPATH}/.magisk/mirror/vendor/${d}/${lname}" "${MODPATH}/system/vendor/${d}/${lname}"
chmod 644 "${MODPATH}/system/vendor/${d}/${lname}"
chcon u:object_r:vendor_file:s0 "${MODPATH}/system/vendor/${d}/${lname}"
chown root:root "${MODPATH}/system/vendor/${d}/${lname}"
chmod -R a+rX "${MODPATH}/system/vendor/${d}"
if [ -z "${REPLACE}" ]; then
REPLACE="/system/vendor/${d}/${lname}"
else
REPLACE="${REPLACE} /system/vendor/${d}/${lname}"
fi
fi
done
done
}
# Replace system property values for old Androids and some low performance SoC's
function loosenedMessage()
{
local freq="96kHz"
if [ $# -gt 0 ]; then
freq="$1"
fi
ui_print ""
ui_print "****************************************************************"
ui_print " Loosened the USB jitter level for more than $freq USB outputs! "
ui_print " (\"USB Samplerate Unlocker\" was detected) "
ui_print "****************************************************************"
ui_print ""
}
function replaceSystemProps_Old()
{
if [ -e "${MODPATH%/*/*}/modules/usb-samplerate-unlocker" -o -e "${MODPATH%/*/*}/modules_update/usb-samplerate-unlocker" ]; then
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3875/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=3875/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3875/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=3875/' \
"$MODPATH/system.prop-workaround"
loosenedMessage
else
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop-workaround"
fi
sed -i \
-e 's/ro\.audio\.resampler\.psd\.enable_at_samplerate=.*$/ro\.audio\.resampler\.psd\.enable_at_samplerate=48000/' \
-e 's/ro\.audio\.resampler\.psd\.stopband=.*$/ro\.audio\.resampler\.psd\.stopband=167/' \
-e 's/ro\.audio\.resampler\.psd\.halflength=.*$/ro\.audio\.resampler\.psd\.halflength=368/' \
-e 's/ro\.audio\.resampler\.psd\.tbwcheat=.*$/ro\.audio\.resampler\.psd\.tbwcheat=106/' \
"$MODPATH/system.prop"
sed -i \
-e 's/ro\.audio\.resampler\.psd\.halflength=.*$/ro\.audio\.resampler\.psd\.halflength=320/' \
"$MODPATH/system.prop-workaround"
}
function replaceSystemProps_S4()
{
if [ -e "${MODPATH%/*/*}/modules/usb-samplerate-unlocker" -o -e "${MODPATH%/*/*}/modules_update/usb-samplerate-unlocker" ]; then
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=5000/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=5000/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=5000/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=5000/' \
"$MODPATH/system.prop-workaround"
loosenedMessage
else
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3875/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=3875/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3875/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=3875/' \
"$MODPATH/system.prop-workaround"
fi
sed -i \
-e 's/ro\.audio\.resampler\.psd\.stopband=.*$/ro\.audio\.resampler\.psd\.stopband=194/' \
-e 's/ro\.audio\.resampler\.psd\.halflength=.*$/ro\.audio\.resampler\.psd\.halflength=520/' \
-e 's/ro\.audio\.resampler\.psd\.tbwcheat=.*$/ro\.audio\.resampler\.psd\.cutoff_percent=100/' \
"$MODPATH/system.prop"
sed -i \
-e 's/ro\.audio\.resampler\.psd\.halflength=.*$/ro\.audio\.resampler\.psd\.halflength=320/' \
"$MODPATH/system.prop-workaround"
}
function replaceSystemProps_kona()
{
if [ ! "`getprop ro.vendor.build.version.release_or_codename`" -ge "12" -a \
\( -e "${MODPATH%/*/*}/modules/usb-samplerate-unlocker" -o -e "${MODPATH%/*/*}/modules_update/usb-samplerate-unlocker" \) ]; then
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=20375/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=20375/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=20375/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=20375/' \
"$MODPATH/system.prop-workaround"
loosenedMessage 192kHz
else
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop-workaround"
fi
}
function replaceSystemProps_SDM845()
{
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2750/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2750/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2750/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2750/' \
"$MODPATH/system.prop-workaround"
}
function replaceSystemProps_SDM()
{
# Do nothing even if "usb-samplerate-unlocker" exists
:
}
function replaceSystemProps_MTK_Dimensity()
{
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop-workaround"
}
function replaceSystemProps_Tensor()
{
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=2625/' \
"$MODPATH/system.prop-workaround"
}
function replaceSystemProps_Others()
{
if [ -e "${MODPATH%/*/*}/modules/usb-samplerate-unlocker" -o -e "${MODPATH%/*/*}/modules_update/usb-samplerate-unlocker" ]; then
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3250/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=3250/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3250/' \
-e 's/vendor\.audio\.usb\.out\.period_us=.*$/vendor\.audio\.usb\.out\.period_us=3250/' \
"$MODPATH/system.prop-workaround"
loosenedMessage
fi
}
function stopSpatializer()
{
# stopSpatializer has two args specifying an audio policy configuration XML file (eg. bluetooth_audio_policy_configuration_7_0.xml)
# and its dummy one to be overridden
if [ $# -eq 2 -a -r "$1" -a -w "$2" ]; then
# Copy and override an original audio_policy_configuration.xml to its dummy file
cp -f "$1" "$2"
# Change an audio_policy_configuration.xml file to remove Spatializer
sed -i 's/flags[:space:]*=[:space:]*"AUDIO_OUTPUT_FLAG_SPATIALIZER"//' "$2"
fi
}
function deSpatializeAudioPolicyConfig()
{
if [ $# -ne 1 -o -z "$1" -o ! -r "$1" ]; then
return 1
fi
local MAGISKPATH="$(magisk --path)"
local configXML="$1"
# Don't use "$MAGISKPATH/.magisk/mirror/system${configXML}" instead of "$MAGISKPATH/.magisk/mirror${configXML}".
# In some cases, the former may link to overlaied "${configXML}" by Magisk itself (not original mirrored "${configXML}").
local mirrorConfigXML="$MAGISKPATH/.magisk/mirror${configXML}"
if [ -n "$configXML" -a -r "$mirrorConfigXML" ]; then
grep -e "flags[[:space:]]*=[[:space:]]*\"AUDIO_OUTPUT_FLAG_SPATIALIZER\"" "$mirrorConfigXML" >"/dev/null" 2>&1
if [ "$?" -eq 0 ]; then
local modConfigXML="$MODPATH/system${configXML}"
mkdir -p "${modConfigXML%/*}"
touch "$modConfigXML"
stopSpatializer "$mirrorConfigXML" "$modConfigXML"
chmod 644 "$modConfigXML"
chcon u:object_r:vendor_configs_file:s0 "$modConfigXML"
chown root:root "$modConfigXML"
chmod -R a+rX "${modConfigXML%/*}"
if [ -z "$REPLACE" ]; then
REPLACE="/system${configXML}"
else
REPLACE="$REPLACE /system${configXML}"
fi
fi
fi
}
function disablePrivApps()
{
if [ $# -ne 1 -o -z "$1" ]; then
return 1
fi
local MAGISKPATH="$(magisk --path)"
local dir mdir
local PrivApps="$1"
for dir in $PrivApps; do
if [ -d "${MAGISKPATH}/.magisk/mirror${dir}" ]; then
case "${dir}" in
/system/* )
dir="${dir#/system}"
;;
esac
mdir="${MODPATH}/system${dir}"
mkdir -p "$mdir"
chmod a+rx "$mdir"
if [ -z "$REPLACE" ]; then
REPLACE="/system${dir}"
else
REPLACE="${REPLACE} /system${dir}"
fi
fi
done
}