==========
== 0x2f ==
==========

Quickly switching default audio inputs/outputs with bemenu and Pulse Audio

I’ve been using the graphical pavucontrol program for switching inputs. I wanted to switch to something leaner.

I came up with the below script. It depends on:

  • pactl
  • jq - for parsing pactl JSON output
  • bemenu - you could probably use dmenu too

The rest should be included with your system.

Use the script like changeaudio.sh sink (audio output) or changeaudio.sh source (audio input)

#!/bin/bash
# Simple audio output/input switcher based on bemenu

DEVICE=$1

if [[ ! $DEVICE =~ (sink|source) ]]; then
    echo "Pass as argument either 'sink' (to change audio output) or 'source' (to change audio input)."
    exit 1
fi

pactl --format=json list $DEVICE\s | \
jq -r '.[] | "\(.index) \(.description)"' | \
bemenu -l -1 | \
awk '{print $1}' | \
xargs pactl set-default-$DEVICE

It’s cool that pactl added support for JSON output. Without it, the only option left would be to use the “indented blocks of data” default output format. I’ve seen people write Pyton parsers for it.