Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Restructured source codes to be sharable with hifi-maximizer-mod at my another repository.
  • Loading branch information
yzyhk904 authored Nov 14, 2022
1 parent 4cd630b commit 2114654
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 166 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ This module,
<li>adjusts a USB transfer period of the USB HAL driver (not the recently common hardware offloading USB (tunneling) driver) for directly reducing the jitter of a PLL in a DAC (even in an asynchronous mode); Use <a href="https://github.com/yzyhk904/USB_SampleRate_Changer">"USB_SampleRate_Changer"</a> to switch from the usual hardware offloading USB (tunneling) driver to the USB HAL one,</li>
<li>sets a higher bitrate limit of bluetooth codec SBC (dual channel mode) for EDR 2Mbps entry class earphones (not for EDR 3Mbps performance ones, but including AV amplifiers and BT speakers),</li>
<li>sets an audio scheduling tunable "vendor.audio.adm.buffering.ms" "2" to reduce jitter on all audio outputs,</li>
<li>nullifys volume listener libraries in "soundfx" folders for disabling slight compression (maybe a peak limiter only on Qcomm devices); I recommend using <a href="https://github.com/Magisk-Modules-Alt-Repo/drc-remover">"DRC remover"</a> additionally for disabling much larger compression (DRC) if on Qcomm devices</li>
<li>nullifys volume listener libraries in "soundfx" folders for disabling slight compression (maybe a peak limiter only on Qcomm devices); I recommend using <a href="https://github.com/Magisk-Modules-Alt-Repo/drc-remover">"DRC remover"</a> additionally for disabling much larger compression (DRC) if on Qcomm devices</li>
</ol><br/>
for improving audio quality effectively in a simple manner.
<br/>
<br/>
<br/>

* This module has been tested on LineageOS and ArrowOS ROM's, and phh GSI's (Android 10 & 11 & 12, Qualcomm & MediaTek SoC, and Arm32 & Arm64 combinations).
* This module has been tested on LineageOS and ArrowOS ROM's, and phh GSI's (Android 10 ~ 13, Qualcomm & MediaTek SoC, and Arm32 & Arm64 combinations).

* Note 1: This module raises the resampling quality from AOSP standard one (stop band attenuation 90dB & cut off 100% of the Nyquist frequency & half filter length 32) to a very mastering quality (179dB & 99% & 408 for Android 12 and later devices and 160db & 91% & 480 for Android 9 & 10 & 11 ones (except low performance ones), but 167dB & 106% & 368 for low performance Android 12 and later devices, 160dB & 91% & 320 for low performance Android 9 & 10 & 11 ones; because earlier than Android 12 has a bug relating to aliasing processing around the Nyquist frequency). However, this cannot raise the quality for Android 8.1 and earlier ones. Please keep in mind that those attenuation values are used for a resampler design as a targeted ones and may not be accomplished in the AOSP implementation.

Expand All @@ -39,8 +39,8 @@ This module,
| 167 | 368 | | 106 | Low Performance A12 |
| 179 | 408 | | 99 | High Performance A12 |
| External examples: | - | - | - | - |
| 100 | 29 | | 109 | AK4493 (Sharp Roll-Off for x N over-sampling) |
| 120 | 35 | | 110 | ESS 9038PRO (Sharp Roll-Off for x N over-sampling) |
| 100 | 29 | | 109 | AK4493 (Sharp Roll-Off for N-fold over-sampling) |
| 120 | 35 | | 110 | ESS 9038PRO (Sharp Roll-Off for N-fold over-sampling) |
| 98 | 130 | 98.5 | | MacOS Leopard (guess) |
| 160 | 240 | | 100 | iZotope, No-Alias (guess) |
| 98 | 64 | | 100 | SoX HQ linear phase (guess) |
Expand Down
254 changes: 254 additions & 0 deletions customize-functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
#!/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
}

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"; 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}"
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/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3875/' \
"$MODPATH/system.prop-workaround"

loosenedMessage

else
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2625/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=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/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=5000/' \
"$MODPATH/system.prop-workaround"

loosenedMessage

else
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3875/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=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/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=20375/' \
"$MODPATH/system.prop-workaround"

loosenedMessage 192kHz

else
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2500/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=2500/' \
"$MODPATH/system.prop-workaround"

fi
}

function replaceSystemProps_SDM()
{
# Do nothing even if "usb-samplerate-unlocker" exists
:
}

function replaceSystemProps_MTK_Dimensity()
{
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 '$avendor.audio.usb.out.period_us=3250\nvendor.audio.usb.out.period_count=2' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3250/' \
-e '$avendor.audio.usb.out.period_us=3250\nvendor.audio.usb.out.period_count=2' \
"$MODPATH/system.prop-workaround"

loosenedMessage

else
sed -i \
-e '$avendor.audio.usb.out.period_us=2500\nvendor.audio.usb.out.period_count=2' \
"$MODPATH/system.prop"
sed -i \
-e '$avendor.audio.usb.out.period_us=2500\nvendor.audio.usb.out.period_count=2' \
"$MODPATH/system.prop-workaround"

fi

}

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/' \
"$MODPATH/system.prop"
sed -i \
-e 's/vendor\.audio\.usb\.perio=.*$/vendor\.audio\.usb\.perio=3250/' \
"$MODPATH/system.prop-workaround"

loosenedMessage

fi

}
Loading

0 comments on commit 2114654

Please sign in to comment.