Audio Switch one-click

One-click audio output selection :loud_sound::headphones:

I have two audio outputs: speakers and headphones. Both are always connected, and I want to switch between them quickly. Here’s a simple and elegant solution using a generic monitor widget for a panel, without installing any additional components.

A button-indicator will appear on the panel that toggles devices when clicked and displays the currently selected device.
audio_switch.png

The image in the middle shows this button.

The solution isn’t directly related to the Qubes OS, just to Xfce.

Add a generic_monitor widget to a panel. For now, just add it.

In Dom0, create a file, for example:

/home/user/bin/audio_switch.pl
#!/usr/bin/perl
#
# !!! Replace the numbers 54, 55, 20
#
use v5.40;

my %audio_icon = (
        54 => {
                icon => 'audio-headphones',
                next => 55,
        },
        55 => {
                icon => 'audio-speakers',
                next => 54,
        },
);
my $genmon = 20;

my $audio = join '|', keys %audio_icon;
my($sink) = `wpctl status` =~ /\*\s*($audio)\./;
my $cmd = sprintf "sh -c 'wpctl set-default %d && xfce4-panel --plugin-event=genmon-%d:refresh:bool:true'",
        $audio_icon{$sink}{next}, $genmon;

printf "<icon>%s</icon><iconclick>%s</iconclick><tool>%s</tool>",
        $audio_icon{$sink}{icon},
        $cmd,
        $audio_icon{$sink}{icon},
        ;

Replace the numbers 54, 55, 20 with your own.
How do you find them?

54 and 55 are the identifiers for your audio devices. You can view them in the console by running wpctl status under the “Audio - Sinks:” section.
20 is the widget name of the particular genmon instance, like “genmon-20”. To get this name, go to the panel properties screen and on the Items tab, hover your mouse over the genmon plugin to get it’s internal name.

You’ll also need to come up with names for the cute icons using the system’s default labels, such as audio-headphones and audio-speakers. It’s a creative process :grinning:

Run chmod +x /home/user/bin/audio_switch.pl

Open the generic_monitor’s properties and enter /home/user/bin/audio_switch.pl in the “Command” field.
Set the “Period (s)” to a fairly long value, such as 300.
Click Save and enjoy your new button-indicator. :star_struck:

1 Like

Interesting. How did you figure out it is a Xfce vs QubesOS issue? The reason I ask is I used the community guides to create an audio cube. It works great, but …

Now I have 2 volume widgets. One responds to the volume/mute buttons on my laptop but has no impact on the actual volume. (makes sense, because that widget is tied to dom0 via the keyboard but dom0 is no longer the audio-vm) The new widget, pasystray, which is tied to the new audio-vm and changes the volume but is not connected to the volume/mute keyboard keys.

I could probably make it work using a policy to pass the keypresses to the audio-vm, then tweak pasystray to accept the inputs. But, still have 2 volume widgets. Feels almost done …

I keep thinking, “I should figure out how to make this work and add to the community guide” But, life keeps getting in the way. Looking for hints …

In dom0:

sudo mv /etc/xdg/autostart/pasystray.desktop /home/user/pasystray.desktop.bak

The redundant icon will disappear after a reboot.

As far as raising/lowering/muting goes, I can give you my i3 shortcuts and you can probably adapt them to Xfce pretty easily:

bindsym XF86AudioRaiseVolume exec --no-startup-id qrexec-client -t -e -d sys-audio -- DEFAULT:'pactl set-sink-volume @DEFAULT_SINK@ +5%'
bindsym XF86AudioLowerVolume exec --no-startup-id qrexec-client -t -e -d sys-audio -- DEFAULT:'pactl set-sink-volume @DEFAULT_SINK@ -5%'
bindsym XF86AudioMute exec --no-startup-id qrexec-client -t -e -d sys-audio -- DEFAULT:'pactl set-sink-mute @DEFAULT_SINK@ toggle'
bindsym XF86AudioMicMute exec --no-startup-id qrexec-client -t -e -d sys-audio -- DEFAULT:'pactl set-source-mute @DEFAULT_SOURCE@ toggle'

This requires the pactl command to be available in sys-audio.

NOTE: With these shortcuts, it is strongly recommended (by me) to use qrexec-client instead of qvm-run, for performance reasons. Just putting this out there for the future, the above are already optimised to use qrexec directly.

1 Like

This is my solution for audio in Dom0, but the button-indicator can be adapted for other purposes as well.