Skip to content

Instantly share code, notes, and snippets.

@wolfv6
Last active September 10, 2023 17:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfv6/df6deb2ac7667d16d621d6da79ef99e0 to your computer and use it in GitHub Desktop.
Save wolfv6/df6deb2ac7667d16d621d6da79ef99e0 to your computer and use it in GitHub Desktop.
switch_sound.sh is a script that switches between speaker and headphone with one mouse click.
#!/bin/bash
################################### license ##################################
# switch_sound.sh is a script that switches between speaker and headphone with one mouse click.
# Written in 2018 by Wolfram Volpi, contact at https://gist.github.com/wolfv6/df6deb2ac7667d16d621d6da79ef99e0
# To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
# This software is distributed without any warranty.
# You should have received a copy of the CC0 Public Domain Dedication along with this software.
# If not, see http://creativecommons.org/publicdomain/zero/1.0/.
################################## Features ##################################
# One-click switch-sound to headphone, useful when PC receives an incoming phone call.
# Large pop-up reminds user to switch to speakers, to hear phone ring on next incoming call.
# One-click switch-sound to speakers.
# Tested on Linux Fedora 27, Gnome 3.26.2
############################### Setup on Gnome ###############################
# The speakers and headphones are always plugged in.
#
# https://www.systutorials.com/docs/linux/man/1-pactl/
# PulseAudio clients can send audio to "sinks" and receive audio from "sources".
# So sinks are outputs (audio goes there), sources are inputs (audio comes from there).
# pactl commands are by Tanu Kaskinen, explained by Tanu himself on
# https://lists.freedesktop.org/archives/pulseaudio-discuss/2018-March/029623.html
#
# Edit this file's pactl commands to match your sinks
# For example, on my system, Sink #0 is speakers, and Sink #1 is headphone:
# $ pactl list sinks
# Sink #0
# Name: alsa_output.usb-1130_USB_AUDIO-00.iec958-stereo
# Ports:
# iec958-stereo-output: Digital Output (S/PDIF) (priority: 0)
# Sink #1
# Name: alsa_output.pci-0000_00_1b.0.analog-stereo
# Ports:
# analog-output-headphones: Headphones (priority: 9000, available)
#
# Create a desktop file named switch_sound.desktop
# Call this switch_sound.sh script from switch_sound.desktop
# Add switch_sound.desktop to Favorites so it shows up on your taskbar.
#switch to the internal sound card
pactl set-card-profile alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo
pactl set-card-profile alsa_card.usb-1130_USB_AUDIO-00 off
#open large pop-up
zenity --warning --title="Headphone is on" --text="<span size='xx-large'>Switch to speakers? (for next phone ring)</span>" --width=1000 --height=500
#when user closes pop-up, switch to the USB sound card
pactl set-card-profile alsa_card.usb-1130_USB_AUDIO-00 output:iec958-stereo
pactl set-card-profile alsa_card.pci-0000_00_1b.0 input:analog-stereo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment