bluetooth audio profile with PulseAudio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh -e | |
# argument can be: | |
# - list | |
# - next (default) | |
# - any text matching a profile name or description | |
arg=${1:-next} | |
infos=$(pactl list cards | sed -n '/bluez/,/^Card/p') | |
if [ -z "$infos" ] ; then | |
echo 'no bluetooth audio device' >&2 | |
exit 1 | |
fi | |
get_info () | |
{ | |
echo "$infos" | | |
sed -rn "s,[[:space:]]$1(:| =) ,,p" | | |
tr -d '"' | |
} | |
get_profiles() | |
{ | |
echo "$infos" | | |
sed -n '/Profiles:/,/Active Profile:/p' | | |
sed -n '/available: yes/p' | | |
sed 's,[[:space:]]*\(.*\) (.*,\1,' | |
} | |
card=$(get_info 'Name') | |
bus=$(get_info 'device.bus') | |
form=$(get_info 'device.form_factor') | |
name=$(get_info 'device.description') | |
profile=$(get_info 'Active Profile') | |
printf "[%s %s %s] %s" $bus $form $name $profile | |
profiles=$(get_profiles) | |
if [ "$arg" = 'list' ] ; then | |
echo | |
echo "$profiles" | sed 's,^,\t- ,' | |
exit 0 | |
fi | |
if [ "$arg" = 'next' ] ; then # round-robin | |
profiles_count=$(echo "$profiles" | wc -l) | |
profile_index=$(echo "$profiles" | sed -n "/$profile:/=") | |
next_profile_index=$(($profile_index % $profiles_count + 1)) | |
new_profile=$(echo "$profiles" | sed -n "${next_profile_index}s,: ,\n,p") | |
else # free-form match | |
new_profile=$(echo "$profiles" | sed -n "/$arg/Ip" | head -n1 | sed 's,: ,\n,') | |
if [ -z "$new_profile" ] ; then | |
echo | |
echo 'no profile match' >&2 | |
exit 1 | |
fi | |
fi | |
echo " -> $new_profile" | |
pactl set-card-profile $card $(echo "$new_profile" | head -n1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment