Skip to content

Instantly share code, notes, and snippets.

@othmar52
Last active April 14, 2023 10:02
Show Gist options
  • Save othmar52/945febe4a6d4cfa63d632b3bf6048fbc to your computer and use it in GitHub Desktop.
Save othmar52/945febe4a6d4cfa63d632b3bf6048fbc to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
usage_and_exit () {
echo "Checks if passed arguments are valid audio-playback devices"
echo ""
echo -e "\033[1;37;40mUsage:\033[0m plughwinfo.sh [<parameters>|<playback-devices>]"
echo " -l (--list) Display all available playback devices"
echo " -h (--help) Display this message"
echo " <playback-devices> Returns error if one of the passed arguments is not a valid playback-device"
echo ""
echo -e "\033[1;37;40mRequirements:\033[0m"
echo " alsa (www.alsa-project.org)"
echo ""
exit 0
}
list_playback_devices () {
for card_id in $(grep "^\<[0-9]\?\|^ [0-9]" /proc/asound/cards | awk '{ print $1 }');
do
card_name=$(cat /proc/asound/card$card_id/id);
for device_id in $(grep "^device:" /proc/asound/card$card_id/pcm0c/info 2> /dev/null | awk '{ print $2 }');
do
for subdevice_av in $(grep "^subdevices_avail:" /proc/asound/card$card_id/pcm0c/info 2> /dev/null | awk '{ print $2 }');
do
for subdevice_id in $( seq 0 $(expr $subdevice_av - 1))
do
echo plughw:$card_name,$device_id,$subdevice_id;
done
done
done
done
}
match_devices () {
DEVICES=$@
for i in $DEVICES; do
if [ $(echo $PLAYBACK_DEVICES | grep $i | wc -l) -eq "0" ]; then
exit 1
fi
done
exit 0
}
PLAYBACK_DEVICES=`list_playback_devices`
if [ $# = 0 ]; then
echo "$PLAYBACK_DEVICES"
exit 0
fi
while [ $# -gt 0 ]; do
case $1 in
-h | --help | h)
usage_and_exit
;;
-l | --list)
echo "$PLAYBACK_DEVICES"
exit 0
;;
*)
match_devices "$@"
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment