2022-10-26 16:01:28 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
# Copyright (C) 2020-present Fewtarius
|
|
|
|
|
|
|
|
. /etc/profile
|
|
|
|
|
2022-11-12 20:39:58 +00:00
|
|
|
ES_SETTINGS="/storage/.config/emulationstation/es_settings.cfg"
|
2023-01-26 22:51:43 +00:00
|
|
|
SETTINGS_CACHE="/storage/.cache/audio"
|
2022-11-12 20:39:58 +00:00
|
|
|
|
2022-11-23 05:35:54 +00:00
|
|
|
function save_state()
|
|
|
|
{
|
2023-01-26 22:51:43 +00:00
|
|
|
if [ ! -d "${SETTINGS_CACHE}" ]
|
|
|
|
then
|
|
|
|
mkdir -p ${SETTINGS_CACHE}
|
|
|
|
fi
|
|
|
|
get_audio_device >${SETTINGS_CACHE}/active_device
|
|
|
|
get_es_path >${SETTINGS_CACHE}/active_path
|
|
|
|
cp -f /storage/.config/asound.conf ${SETTINGS_CACHE}/asound.conf
|
|
|
|
cp -f /storage/.config/asound.state ${SETTINGS_CACHE}/asound.state
|
2022-11-23 05:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function restore_state()
|
|
|
|
{
|
2023-01-26 22:51:43 +00:00
|
|
|
if [ -e "${SETTINGS_CACHE}/asound.conf" ]
|
|
|
|
then
|
|
|
|
cp -f ${SETTINGS_CACHE}/asound.conf /storage/.config/
|
|
|
|
cp -f ${SETTINGS_CACHE}/asound.state /storage/.config/
|
|
|
|
set-audio esset "$(cat ${SETTINGS_CACHE}/active_path)"
|
|
|
|
set-audio set "$(cat ${SETTINGS_CACHE}/active_device)"
|
|
|
|
rm -f ${SETTINGS_CACHE}/{asound.conf,asound.state,active_path,active_device}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_audio_device() {
|
|
|
|
MYAUDIODEVICE=$(get_setting system.audiodevice)
|
|
|
|
if [ ! -z "${MYAUDIODEVICE}" ]
|
|
|
|
then
|
|
|
|
echo ${MYAUDIODEVICE}
|
|
|
|
else
|
|
|
|
echo "DEFAULT (SYSTEM PROVIDED)"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_es_path() {
|
|
|
|
AUDIODEVICE=$(grep AudioDevice ${ES_SETTINGS} | sed -e 's#^.*="##g' -e 's#"\ .*$##g')
|
|
|
|
if [ -z "${AUDIODEVICE}" ]
|
2022-11-23 05:35:54 +00:00
|
|
|
then
|
2023-01-26 22:51:43 +00:00
|
|
|
echo "DEFAULT (SYSTEM PROVIDED)"
|
|
|
|
else
|
|
|
|
echo "${AUDIODEVICE}"
|
2022-11-23 05:35:54 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-11-23 23:21:00 +00:00
|
|
|
# Check if an audio device string corresponds to a bluetooth device
|
|
|
|
function is_bluetooth() {
|
|
|
|
if [[ "$1" =~ ^Device.* ]]
|
|
|
|
then
|
|
|
|
true
|
|
|
|
return
|
|
|
|
else
|
|
|
|
false
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-11-12 20:39:58 +00:00
|
|
|
function list_audio_controls() {
|
|
|
|
IFS=""
|
2022-11-16 12:02:30 +00:00
|
|
|
ACTIVE_DEVICE=$(get_audio_device)
|
2022-11-23 23:21:00 +00:00
|
|
|
if is_bluetooth "${ACTIVE_DEVICE}"
|
2022-11-16 12:02:30 +00:00
|
|
|
then
|
2023-01-26 22:51:43 +00:00
|
|
|
CONTROLS=$(amixer -D bluealsa controls | awk 'BEGIN {FS="'\''"} {print $2}')
|
2022-11-16 12:02:30 +00:00
|
|
|
else
|
2023-01-26 22:51:43 +00:00
|
|
|
CONTROLS=$(amixer scontrols | awk 'BEGIN {FS="'\''"} {print $2}')
|
2022-11-16 12:02:30 +00:00
|
|
|
fi
|
2022-11-12 20:39:58 +00:00
|
|
|
echo "DEFAULT (SYSTEM PROVIDED)"
|
|
|
|
echo "CUSTOM (UNMANAGED)"
|
|
|
|
for CONTROL in "${CONTROLS[@]}"
|
|
|
|
do
|
|
|
|
echo ${CONTROL} | awk '{print $1}' | grep -v -E 'Mic|Extension|Capture|Differential|Left|Right' | uniq
|
|
|
|
done
|
|
|
|
echo "--------"
|
|
|
|
for CONTROL in "${CONTROLS[@]}"
|
|
|
|
do
|
|
|
|
echo ${CONTROL}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:01:28 +00:00
|
|
|
function list_audio_devices() {
|
|
|
|
echo "DEFAULT (SYSTEM PROVIDED)"
|
|
|
|
echo "DEFAULT HDMI"
|
2023-01-26 22:51:43 +00:00
|
|
|
echo "BLUETOOTH DEVICE"
|
2022-10-26 16:01:28 +00:00
|
|
|
echo "CUSTOM (UNMANAGED)"
|
2022-11-12 20:39:58 +00:00
|
|
|
echo "--------"
|
2022-11-16 11:11:31 +00:00
|
|
|
BTACTIVE=$(systemctl is-active bluetooth)
|
|
|
|
if [ "${BTACTIVE}" == "active" ]
|
|
|
|
then
|
2022-11-19 22:37:57 +00:00
|
|
|
BTDEVICES=$(bluetoothctl devices Paired)
|
2022-11-16 11:11:31 +00:00
|
|
|
while read -r BTDEV
|
|
|
|
do
|
|
|
|
echo "${BTDEV}"
|
|
|
|
done <<< "${BTDEVICES}"
|
|
|
|
fi
|
2022-10-26 16:01:28 +00:00
|
|
|
for SDEVICE in $(find /proc/asound/card*/pcm*/info)
|
|
|
|
do
|
|
|
|
TYPE=$(awk '/^stream:/ {print $2}' ${SDEVICE})
|
|
|
|
if [[ "${TYPE}" =~ PLAYBACK ]]
|
|
|
|
then
|
|
|
|
CARD=$(awk '/^card:/ {print $2}' ${SDEVICE})
|
|
|
|
DEVICE=$(awk '/^device:/ {print $2}' ${SDEVICE})
|
|
|
|
NAME=$(awk '/^name:/ {print $2}' ${SDEVICE})
|
|
|
|
echo "${NAME} (${CARD}:${DEVICE})"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_audio_device() {
|
2022-11-24 21:58:40 +00:00
|
|
|
SELECTION="$1"
|
2022-11-23 23:21:00 +00:00
|
|
|
# When switching from a non-bluetooth to a bluetooth device,
|
|
|
|
# store the last configuration in order to restore it on reboot.
|
|
|
|
if is_bluetooth "${SELECTION}"
|
|
|
|
then
|
|
|
|
ACTIVE_DEVICE=$(get_audio_device)
|
|
|
|
if ! is_bluetooth "${ACTIVE_DEVICE}"
|
|
|
|
then
|
|
|
|
save_state
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-10-26 16:01:28 +00:00
|
|
|
set_setting system.audiodevice "${SELECTION}"
|
|
|
|
if [ "${SELECTION}" == "DEFAULT (SYSTEM PROVIDED)" ]
|
|
|
|
then
|
2022-12-30 13:01:15 +00:00
|
|
|
CARD="0"
|
|
|
|
HWDEV="hw:${CARD},0"
|
2022-10-26 16:01:28 +00:00
|
|
|
elif [ "${SELECTION}" == "DEFAULT HDMI" ]
|
|
|
|
then
|
|
|
|
CARD="0"
|
|
|
|
HWDEV="hdmi"
|
2023-01-26 22:51:43 +00:00
|
|
|
elif [ "${SELECTION}" == "BLUETOOTH DEVICE" ]
|
|
|
|
then
|
|
|
|
HWDEV="bluealsa"
|
2022-10-26 16:01:28 +00:00
|
|
|
elif [ "${SELECTION}" == "CUSTOM (UNMANAGED)" ]
|
|
|
|
then
|
|
|
|
exit 0
|
2022-11-23 23:21:00 +00:00
|
|
|
elif is_bluetooth "${SELECTION}"
|
2022-11-13 05:37:12 +00:00
|
|
|
then
|
|
|
|
MAC=$(echo "${SELECTION}" | awk '/^Device/ {print $2}')
|
2022-11-15 02:41:18 +00:00
|
|
|
# Reconnect device in case it auto-connected.
|
2022-11-17 03:57:16 +00:00
|
|
|
# This doesn't seem necessary anymore, re-activate in case of issues.
|
|
|
|
# bluetoothctl disconnect ${MAC}
|
2022-11-15 02:41:18 +00:00
|
|
|
if bluetoothctl connect ${MAC}
|
2022-11-23 05:35:54 +00:00
|
|
|
then
|
2023-01-26 22:51:43 +00:00
|
|
|
HWDEV="bluealsa"
|
2022-11-15 02:41:18 +00:00
|
|
|
fi
|
2022-11-13 05:37:12 +00:00
|
|
|
exit 0
|
2022-10-26 16:01:28 +00:00
|
|
|
else
|
2022-11-12 20:39:58 +00:00
|
|
|
if [ "${SELECTION}" == "--------" ]
|
|
|
|
then
|
|
|
|
exit 0
|
|
|
|
fi
|
2022-10-26 16:01:28 +00:00
|
|
|
for SDEVICE in $(find /proc/asound/card*/pcm*/info)
|
|
|
|
do
|
|
|
|
TYPE=$(awk '/^stream:/ {print $2}' ${SDEVICE})
|
|
|
|
if [[ "${TYPE}" =~ PLAYBACK ]]
|
|
|
|
then
|
|
|
|
CARD=$(awk '/^card:/ {print $2}' ${SDEVICE})
|
|
|
|
DEVICE=$(awk '/^device:/ {print $2}' ${SDEVICE})
|
|
|
|
NAME=$(awk '/^name:/ {print $2}' ${SDEVICE})
|
2022-12-20 12:18:00 +00:00
|
|
|
if [ "${SELECTION}" == "${NAME} (${CARD}:${DEVICE})" ]
|
2022-10-26 16:01:28 +00:00
|
|
|
then
|
|
|
|
HWDEV="hw:${CARD},${DEVICE}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2022-12-20 12:18:00 +00:00
|
|
|
if [[ "${HWDEV}" =~ ^hw ]]
|
2022-12-20 12:03:05 +00:00
|
|
|
then
|
|
|
|
cat <<EOF >/storage/.config/asound.conf
|
2022-10-26 16:01:28 +00:00
|
|
|
ctl.!default {
|
|
|
|
type hw
|
|
|
|
card ${CARD}
|
|
|
|
}
|
2022-12-20 01:03:59 +00:00
|
|
|
|
|
|
|
pcm.!default {
|
|
|
|
type plug
|
2023-01-05 13:35:36 +00:00
|
|
|
slave.pcm "softvol"
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm.softvol {
|
|
|
|
type softvol
|
|
|
|
slave.pcm "dmixer"
|
|
|
|
control {
|
|
|
|
name "Pre-Amp"
|
|
|
|
card ${CARD}
|
|
|
|
}
|
|
|
|
min_dB -5.0
|
|
|
|
max_dB 20.0
|
|
|
|
resolution 6
|
2022-12-20 01:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pcm.dmixer {
|
|
|
|
type dmix
|
|
|
|
ipc_key 1024
|
|
|
|
slave {
|
|
|
|
pcm "${HWDEV}"
|
|
|
|
period_time 0
|
2023-01-05 13:35:36 +00:00
|
|
|
period_size 4096
|
|
|
|
buffer_size 131072
|
2023-01-01 20:19:02 +00:00
|
|
|
rate 176400
|
2022-12-20 01:03:59 +00:00
|
|
|
}
|
|
|
|
bindings {
|
|
|
|
0 0
|
|
|
|
1 1
|
|
|
|
}
|
|
|
|
}
|
2023-01-26 22:51:43 +00:00
|
|
|
EOF
|
|
|
|
elif [[ "${HWDEV}" =~ bluealsa ]]
|
|
|
|
then
|
|
|
|
cat <<EOF >/storage/.config/asound.conf
|
|
|
|
ctl.!default {
|
|
|
|
type hw
|
|
|
|
card 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm.!default {
|
|
|
|
type plug
|
|
|
|
slave.pcm "softvol"
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm.softvol {
|
|
|
|
type softvol
|
|
|
|
slave.pcm "dmixer"
|
|
|
|
control {
|
|
|
|
name "Pre-Amp"
|
|
|
|
card 0
|
|
|
|
}
|
|
|
|
min_dB -5.0
|
|
|
|
max_dB 20.0
|
|
|
|
resolution 6
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm.dmixer {
|
|
|
|
type asym
|
|
|
|
capture.pcm cards.pcm.default
|
|
|
|
playback.pcm bluealsa
|
|
|
|
hint.description "Bluetooth Audio Device"
|
|
|
|
}
|
2022-12-20 12:03:05 +00:00
|
|
|
EOF
|
|
|
|
else
|
|
|
|
cat <<EOF >/storage/.config/asound.conf
|
|
|
|
pcm.!default {
|
2022-12-20 12:18:00 +00:00
|
|
|
type plug
|
|
|
|
slave {
|
2023-01-05 13:35:36 +00:00
|
|
|
pcm "softvol"
|
2022-12-20 12:18:00 +00:00
|
|
|
}
|
2022-12-20 12:03:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:35:36 +00:00
|
|
|
pcm.softvol {
|
|
|
|
type softvol
|
|
|
|
slave.pcm "dmixer"
|
|
|
|
control {
|
|
|
|
name "Pre-Amp"
|
|
|
|
card ${CARD}
|
|
|
|
}
|
|
|
|
min_dB -5.0
|
|
|
|
max_dB 20.0
|
|
|
|
resolution 6
|
|
|
|
}
|
|
|
|
|
2022-12-20 12:18:00 +00:00
|
|
|
ctl.!default {
|
|
|
|
type hw
|
|
|
|
card ${CARD}
|
2022-12-20 12:03:05 +00:00
|
|
|
}
|
2022-10-26 16:01:28 +00:00
|
|
|
EOF
|
2022-12-20 12:18:00 +00:00
|
|
|
fi
|
2023-01-26 22:51:43 +00:00
|
|
|
set-audio esset $(set-audio firstcontrol)
|
2022-10-26 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 20:39:58 +00:00
|
|
|
function set_es_path() {
|
|
|
|
AUDIODEVICE=${1}
|
|
|
|
if [ "${AUDIODEVICE}" == "CUSTOM (UNMANAGED)" ] || \
|
|
|
|
[ "${AUDIODEVICE}" == "--------" ]
|
|
|
|
then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
AUDIOTEST=$(grep "AudioDevice" ${ES_SETTINGS} 2>/dev/null)
|
2022-11-13 21:28:43 +00:00
|
|
|
sed -i '/^.*<string name="AudioDevice".*$/d' ${ES_SETTINGS}
|
2022-11-12 20:39:58 +00:00
|
|
|
if [ -e "/storage/.config/profile.d/99-mixer" ]
|
|
|
|
then
|
|
|
|
rm "/storage/.config/profile.d/99-mixer"
|
|
|
|
fi
|
|
|
|
if [ ! "${AUDIODEVICE}" = "DEFAULT (SYSTEM PROVIDED)" ]
|
|
|
|
then
|
|
|
|
sed -i '/^.*AudioCard.*$/a \\t<string name="AudioDevice" value="'"${AUDIODEVICE}"'" \/>' ${ES_SETTINGS}
|
|
|
|
echo "DEVICE_AUDIO_MIXER=\"${AUDIODEVICE}\"" >/storage/.config/profile.d/99-mixer
|
2022-11-13 18:24:50 +00:00
|
|
|
if [ "${DEVICE_VOLUMECTL}" = true ]
|
|
|
|
then
|
|
|
|
systemctl restart volume
|
|
|
|
fi
|
2022-11-12 20:39:58 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-01-26 22:51:43 +00:00
|
|
|
function assumed_control() {
|
|
|
|
ACTIVE_DEVICE=$(get_audio_device)
|
|
|
|
if is_bluetooth "${ACTIVE_DEVICE}"
|
2022-11-12 20:39:58 +00:00
|
|
|
then
|
2023-01-26 22:51:43 +00:00
|
|
|
ACONTROLS=$(amixer -D bluealsa controls | awk 'BEGIN {FS="'\''"} {print $2}')
|
2022-11-12 20:39:58 +00:00
|
|
|
else
|
2023-01-26 22:51:43 +00:00
|
|
|
ACONTROLS=$(amixer scontrols | awk 'BEGIN {FS="'\''"} {print $2}')
|
2022-11-12 20:39:58 +00:00
|
|
|
fi
|
2023-01-26 22:51:43 +00:00
|
|
|
echo ${ACONTROLS} | awk '{print $1}'
|
2022-11-12 20:39:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:01:28 +00:00
|
|
|
case $1 in
|
2022-11-12 20:39:58 +00:00
|
|
|
controls)
|
|
|
|
list_audio_controls
|
|
|
|
;;
|
2022-10-26 16:01:28 +00:00
|
|
|
list)
|
|
|
|
list_audio_devices
|
|
|
|
;;
|
|
|
|
set)
|
2022-11-12 20:39:58 +00:00
|
|
|
set_audio_device "$2"
|
|
|
|
;;
|
|
|
|
esset)
|
|
|
|
set_es_path "$2"
|
2022-10-26 16:01:28 +00:00
|
|
|
;;
|
|
|
|
get)
|
2022-11-12 20:39:58 +00:00
|
|
|
get_audio_device
|
|
|
|
;;
|
2023-01-26 22:51:43 +00:00
|
|
|
firstcontrol)
|
|
|
|
assumed_control
|
|
|
|
;;
|
2022-11-12 20:39:58 +00:00
|
|
|
esget)
|
|
|
|
get_es_path
|
2022-10-26 16:01:28 +00:00
|
|
|
;;
|
2022-11-23 05:35:54 +00:00
|
|
|
save)
|
|
|
|
save_state
|
|
|
|
;;
|
|
|
|
restore)
|
|
|
|
restore_state
|
|
|
|
;;
|
2022-10-26 16:01:28 +00:00
|
|
|
esac
|