Announcement

Collapse
No announcement yet.

PulseAudio config script to quickly change audio settings tailored to use cases

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    PulseAudio config script to quickly change audio settings tailored to use cases

    Below I paste a configuration script that helps me quickly change audio (PulseAudio) settings on my KDE/Ubuntu machine. I made it since I got tired of sifting through the complicated PulseAudio settings when trying to make my Skype audio or HDMI TV-out audio work. This script lists the four user cases I have (normal, bluetooth audio, bluetooth communication, HDMI TV-out) and the general case (enable all cards).

    Unfortunately, it is not a complete programme that you can just use: you will need to tailor it to your computer and your user case. But it might be helplful to some.

    To use it, first save it as a bash script (eg. in ~/bin/) under some smart name like audio-profiles.sh, make it executable and add to your menu. And of course, you have to edit it a lot to make it adapted to your particular computer/use case.

    I could explain more about how to do that - just ask in comments below.

    Code:
    #!/bin/bash
    
    ## variables ##########################################
    ## native audio card
    acard="alsa_card.pci-0000_00_1b.0"
    aprof_a="output:analog-stereo+input:analog-stereo"
    aprof_d="output:hdmi-stereo+input:analog-stereo"
    asink="alsa_output.pci-0000_00_1b.0.analog-stereo"
    asource="alsa_input.pci-0000_00_1b.0.analog-stereo"
    
    ## my bluetooth headphones
    mac="E0:9D:FA:0A:77:DB"
    bcard="bluez_card.E0_9D_FA_0A_77_DB" #bluetooth headset card
    bprof_m="a2dp_sink" # Music profile
    bprof_h="headset_head_unit" # Headset profile
    bsink_m="bluez_sink.E0_9D_FA_0A_77_DB.a2dp_sink"
    bsink_h="bluez_sink.E0_9D_FA_0A_77_DB.headset_head_unit"
    bsource="bluez_source.E0_9D_FA_0A_77_DB.headset_head_unit"
    ## /variables #########################################
    
    ## default kdialog selection statuses
    no1="off"
    no2="off"
    no3="off"
    no4="off"
    no5="off"
    
    ##count active audio profiles
    count=`pacmd list-cards|grep "active profile"|grep -v "<off>"|awk -F"<" '{ print $2 }' | awk -F">" '{ print $1 }'|wc -l`
    echo $count
    
    ##if more than one profiles, activate kdialog select 5
    if [ $count -gt 1 ]; then
    	no5="on"
    	#echo "test"
    else
    	## find the one active profile
    	active_profile=`pacmd list-cards|grep "active profile"|grep -v "<off>"|awk -F"<" '{ print $2 }' | awk -F">" '{ print $1 }'`
    
    	case $active_profile in
    	"$aprof_a" )
    		no1="on"
    		;;
    	"$bprof_m" )
    		no2="on"
    		;;
    	"$bprof_h" )
    		no3="on"
    		;;
    	"$aprof_d" )
    		no4="on"
    		;;
    	*)
    		kdialog --title "Could not read profile" --passivepopup "You have a non-typical configuration or \nthe script has an error" 5
    		;;
    	esac
    fi
    
    profile=`kdialog --title "Audio profiles" --radiolist "Choose your audio profile:" \
    1 "Default (music/communication)" $no1 \
    2 "Music (bluetooth)" $no2 \
    3 "Communication (bluetooth)" $no3 \
    4 "Movie via HDMI to TV" $no4 \
    5 "More than one profile" $no5`
    
    ## Terminate the script if the user chooses "Cancel" in the dialog:
    if [ $? != 0 ]; then
    	exit
    fi
    
    ## do what the user asked above
    case "$profile" in
    1 )
    	pacmd set-card-profile $acard $aprof_a
    	pacmd set-card-profile $bcard off
    	pacmd set-default-sink $asink
    	pacmd set-default-source $asource
    	;;
    2 )
    	## first check, if my bluetooth headset is connected
    	device=`hcitool con|grep $mac|wc -l`
    	if [ $device -eq 1 ]; then
    		pacmd set-card-profile $bcard $bprof_m
    		pacmd set-card-profile $acard off
    		pacmd set-default-sink $bsink_m
    	else
    		kdialog --error "Your headset is not connected to computer. \nConnect it and try again."
    	fi
    	;;
    3 )
    	##same as above
    	device=`hcitool con|grep $mac|wc -l`
    	if [ $device -eq 1 ]; then
    		pacmd set-card-profile $bcard $bprof_h
    		pacmd set-card-profile $acard off
    		pacmd set-default-sink $bsink_h
    		pacmd set-default-source $bsource
    	else
    		kdialog --error "Your headset is not connected to computer. \nConnect it and try again."
    	fi
    	;;
    4 )
    	hdmi=`xrandr|grep "HDMI-. connected"|wc -l`
    	if [ $hdmi -gt 0 ]; then
    		pacmd set-card-profile $acard $aprof_d
    		pacmd set-card-profile $bcard off
    		pacmd set-default-source $asource
    	else
    		kdialog --sorry "An external monitor is not attached \nthrough HDMI. Please attach it and\n try again."
    	fi
    	;;
    5 )
    	pacmd set-card-profile $acard $aprof_a
    	## first check, if my bluetooth headset is connected
    	device=`hcitool con|grep $mac|wc -l`
    	if [ $device -eq 1 ]; then
    	pacmd set-card-profile $bcard $bprof_m
    	else
    		kdialog --sorry "Your headset is not connected to computer. \nOnly the native audio card will be activated."
    	fi
    	pacmd set-default-sink $asink
    	pacmd set-default-source $asource
    	;;
    ## Other variants: something went wrong...
    *) 
    	kdialog --error "Something is not right, \nyou have to fix the script."
    ;;
    esac
    Last edited by dgvirtual; Dec 25, 2018, 04:03 PM. Reason: minor update to the script
Working...
X