distribution/packages/jelos/sources/scripts/set-audio

100 lines
2.1 KiB
Bash
Executable file

#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2020-present Fewtarius
. /etc/profile
function list_audio_devices() {
echo "DEFAULT (SYSTEM PROVIDED)"
echo "DEFAULT HDMI"
echo "CUSTOM (UNMANAGED)"
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() {
SELECTION="$1"
set_setting system.audiodevice "${SELECTION}"
if [ "${SELECTION}" == "DEFAULT (SYSTEM PROVIDED)" ]
then
if [ "$(get_setting system.rg353v)" = "1" ]
then
cp /usr/config/asound.conf.RG353V /storage/.config
exit 0
else
if [ -e "/usr/config/asound.conf" ]
then
cp /usr/config/asound.conf /storage/.config
exit 0
else
CARD="0"
HWDEV="hw:${CARD},0"
fi
fi
elif [ "${SELECTION}" == "DEFAULT HDMI" ]
then
CARD="0"
HWDEV="hdmi"
elif [ "${SELECTION}" == "CUSTOM (UNMANAGED)" ]
then
exit 0
else
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})
if [ "${SELECTION}" == "${NAME} (${CARD}:${DEVICE})" ]
then
HWDEV="hw:${CARD},${DEVICE}"
fi
fi
done
fi
cat <<EOF >/storage/.config/asound.conf
pcm.!default {
type plug
slave {
pcm "${HWDEV}"
}
}
ctl.!default {
type hw
card ${CARD}
}
EOF
}
function get_audio_device() {
MYAUDIODEVICE=$(get_setting system.audiodevice)
if [ ! -z "${MYAUDIODEVICE}" ]
then
echo ${MYAUDIODEVICE}
else
echo "DEFAULT (SYSTEM PROVIDED)"
fi
}
case $1 in
list)
list_audio_devices
;;
set)
set_audio_device "$2"
;;
get)
get_audio_device
;;
esac