this post was submitted on 03 Oct 2023
58 points (98.3% liked)

Linux

48207 readers
710 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

I often switch between phones and speakers, but I'm too lazy to do it through the sound preferences window. So I came up with this script*, and I'm sharing it here as others might find it useful.

You'll need to tweak it a bit to work in your machine, but once you do it you can run it from a launcher or a keyboard shortcut, it's really comfy.

Okay, here's the code:


#!/bin/bash

# You'll need to swap those four values with the ones that work in your machine.
# Check the rest of the post for further info.
mainCard="pci-0000_06_00.1"
mainProfile="hdmi-stereo-extra1"
altCard="pci-0000_00_09.2"
altProfile="analog-stereo"

# If the current default source is main, your new source is alt. Else, your new is main.
if [[ $(pactl get-default-source) == "alsa_output.$mainCard.$mainProfile.monitor" ]]
then declare -g newCard="$altCard" newProfile="$altProfile"
else declare -g newCard="$mainCard" newProfile="$mainProfile"
fi

# Tells PulseAudio to shift the card profile and default sink to the new.
pactl set-card-profile "alsa_card.${newCard}" "output:${newProfile}"
pacmd set-default-sink "alsa_output.${newCard}.${newProfile}" &> /dev/null\

# Tells PulseAudio to shift the currently running programs to use the new output.
for i in $(pacmd list-sink-inputs | grep index | awk '{print $2}')
do pacmd move-sink-input "$i" "alsa_output.${newCard}.${newProfile}" &> /dev/null
done

# Optional text notification.
if [[ $(pactl get-default-source) == "alsa_output.$mainCard.$mainProfile.monitor" ]]
then notify-send -t 500 "Main sound output on!"
elif [[ $(pactl get-default-source) == "alsa_output.$altCard.$altProfile.monitor" ]]
then notify-send -t 500 "Alt sound output on!"
else notify-send -t 2000 "Something weird happened."
fi

# Optional audio notification. It runs VLC but it's easy to adapt or remove if you want.
cvlc --play-and-exit /usr/share/sounds/freedesktop/stereo/message-new-instant.oga


Check the first four lines of code. You'll need to replace that "pci.blahblah" and "audio.stereo.whatever" junk with the ones from your machine. To know them, run pacmd list-sources | grep name: in a terminal. The output will look like this:

name: ⟨alsa_output.pci-0000_06_00.1.hdmi-stereo-extra1.monitor⟩
name: ⟨alsa_output.pci-0000_00_09.2.analog-stereo.monitor⟩

Ignore ⟨alsa_output and monitor⟩. The second-to-last chunk (e.g. hdmi-stereo-extra1) is the profile. The rest (e.g. pci-0000_06_00.1) is the card. Now replace those in the script.

*credits: this script is partially inspired on this AskUbuntu comment.

all 17 comments
sorted by: hot top controversial new old
[–] [email protected] 22 points 1 year ago (2 children)

I have a project I made that offers a GUI for this if anyone is interested: https://gitlab.com/konomikitten/pa-mv

[–] [email protected] 6 points 1 year ago

This is great! It looks more serious than my method, that smells "dirty hack" from a distance. Glad to see other people tacking the UX issue.

[–] [email protected] 1 points 1 year ago

This is very cool!

[–] [email protected] 10 points 1 year ago* (last edited 1 year ago) (1 children)

NOTE: Lemmy is hellbent on replacing the "&" in the code with "& amp;". You'll probably want to undo it before running this code.

[–] [email protected] 8 points 1 year ago (2 children)

There is mark-up that turns text blocks to a code block that doesn't change anything.

You can also try pasting it into a text editor as what you copied it from may be at fault (Microsoft word is known to do this).

[–] [email protected] 1 points 1 year ago* (last edited 1 year ago) (1 children)

I used Pluma to do this, so the issue is mostly Lemmy. (IIRC the devs did this to avoid some XSS).

I'm aware of the ``` mark-up (I didn't use it to highlight the code in pink - useful to set it apart from the comments), is there another?

(I also had to replace the angle brackets with ⟨⟩, glad that I had those due to Linguistics. Otherwise Lemmy just noms on them for breakfast.)

[–] [email protected] 2 points 1 year ago

You can use the greater-than sign to make code blocks but for some reason it then picks up your use of pound sign as the formatting for headers even though putting text into a code block should stop that.

[–] [email protected] 5 points 1 year ago (1 children)

People still use pulseaudio? Pipewire works way better for me

[–] [email protected] 1 points 1 year ago (1 children)

I should try pipewire out someday. The reason I'm using pulseaudio is basically "it's Mint default and I didn't bother changing it".

[–] [email protected] 3 points 1 year ago (1 children)

Really? I though mint moved to pipewire. I guess I'll need to go look to verify

[–] [email protected] 1 points 1 year ago (1 children)

Now I'm genuinely confused, given that I got both PipeWire and PulseAudio packages installed, by default (I didn't mess with it):

[–] [email protected] 2 points 1 year ago (1 children)

I you look to see which one is running?

[–] [email protected] 2 points 1 year ago (1 children)

ps -e | pipewire says that pipewire failed to load, and pactl info outputs /run/user/1000/pulse/native for server string. As such, I think that PulseAudio is running without PipeWire.

It's... weird, though. I wonder why Mint does this. It's a relatively new instal running Mint 21.2.

[–] [email protected] 3 points 1 year ago

That's kind of weird. Maybe you could post it on the Linux mint forms

[–] [email protected] 2 points 1 year ago (1 children)

Thanks for this post, I'm looking forward to hammering something like this out myself and this is a great jumping off point. 👍

[–] [email protected] 2 points 1 year ago

You're welcome! It ain't much but it's honest work. I also have other scripts that I don't mind sharing, but they're mostly dumb stuff, like a simple autoclick using xdotool.