Other Switch audio device script

I just knocked up a little bash script to switch audio devices because i use i3wm instead of Desktop like gnome

I have 3 audio devices, the built in speakers, headphones and a usb dac

So i created a script to easily switch audio outputs called switch-audio,
which lets you see the current audio device and switch between built in speakers, headphones and usb audio device

The set up.

1 - Create a bin directory in your home directory

Bash:
mkdir -p ~/bin

2 - Edit your ~/.bashrc and add the home bin directory to your bash path

Bash:
# home bin
if [ -d "$HOME/bin" ]; then
   PATH="$HOME/bin:$PATH"
fi

3 - Add the switch-audio script to your home bin directory

Bash:
#!/usr/local/bin/bash

# check to see if script was run as root
if [[ $UID -ne 0 ]]; then
  printf "%s\n" "$(basename "$0") must be run as root using sudo"
  exit 1
fi

# Create the prompt
PS3="select an audio device: "
options=("speakers" "headphones" "usb" "current" "quit")
OLD_IFS=${IFS}; #ifs space seperator
IFS=$'\t\n' #change ifs seperator from spaces to new line

# select and case statement
select opt in "${options[@]}"; do
case $opt in
  speakers)
        sysctl hw.snd.default_unit=0
        break;;
  headphones)
        sysctl hw.snd.default_unit=1
        break;;
  usb)
        sysctl hw.snd.default_unit=2
        break;;
  current)
        cat /dev/sndstat
        break;;
  quit)
    echo "Quitting"
    IFS=${OLD_IFS}
    break;;
  *)    printf "%s\n" "Usage: $(basename "$0") [ speakers | headphones | usb | current | quit ]";;
esac
done

4 - Make the script executable

Bash:
chmod u+x ~/bin/switch-audio

5 - Source your ~/.bashrc to pick up the switch-audio script

Bash:
source ~/.bashrc

6 - Run the switch-audio script with sudo

sudo switch-audio

This displays the following menu which lets you switch audio devices,
and see the currently used audio device or quit the script

1) speakers
2) headphones
3) usb
4) current
5) quit
select an audio device:

The script creates a prompt called 'select an audio device'

At the prompt just type the number next to the audio device you want to switch to,
or type 4 to view the current audio device or 5 to quit
 
I also made a simple one for my XFCE scenario. I have an icon on the panel calling it:

C-like:
current=$(grep default /dev/sndstat | cut -c4-4);

if [ $current == 0 ]; then
    sysctl hw.snd.default_unit=1;
else
    sysctl hw.snd.default_unit=0;
fi

#restart mixer on the panel so it reads the new device:
kill `ps -aux | grep -i mixer | awk '{print $2}'`

#notify after the switch happened:
verbose=$(grep default /dev/sndstat)
notify-send "Activate: $verbose" --icon=dialog-information;
 
Last edited:
It may be possible to automate the OP needs.
snd_hda(4) describes how to put headphone and speakers into the same unit and provide headphone jack sensing to mute speaker output. No script needed.

As far as the usb device, devd(8) may be able to detect the usb device and run

Code:
dev.hdaa.%d.reconfig
                       Setting this to a non-zero value    makes
                       driver to destroy existing pcm devices
                       and process new pins configuration set
                       via dev.hdaa.%d.nid%d_config.

and restore the headphone/speaker unit on usb disconnect.
 
What plain English descriptions would you suggest for the devices in my case?

Code:
grahamperrin@momh167-gjp4-8570p:~ % cat /dev/sndstat
Installed devices:
pcm0: <ATI R6xx (HDMI)> (play)
pcm1: <IDT 92HD81B1X (Analog 2.0+HP/2.0)> (play/rec)
pcm2: <IDT 92HD81B1X (Analog)> (play/rec) default
pcm3: <USB audio> (play/rec)
No devices installed from userspace.
grahamperrin@momh167-gjp4-8570p:~ %

I mean, I'm confused by the two IDT lines, much of the jargon is meaningless to me.

I see two jack sockets on the side of this notebook (HP EliteBook 8570p).

One of the two has a microphone icon alone, so I sort of expect the device line for this socket to say (rec), not (play/rec) … does that make sense? Obviously I'm confused :)
 
Back
Top