Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is the final product, switch_sound.sh:

#!/bin/bash

# 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 Fedora 27, Gnome 3.26.2

# SETUP
# 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 from 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, 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)
#
# Call this switch_sound.sh script from switch_sound.desktop
# Add switch_sound.desktop to Favorites so it shows up on your toolbar.

#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 phone ringer?</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

Thanks to fcomida for pointing me to pactl.

Here is the final product, switch_sound.sh:

#!/bin/bash

# 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 Fedora 27, Gnome 3.26.2

# SETUP
# 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 from 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, 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)
#
# Call this switch_sound.sh script from switch_sound.desktop
# Add switch_sound.desktop to Favorites so it shows up on your toolbar.

#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 phone ringer?</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

Thanks to fcomida for pointing me to pactl.

Here is the final product, switch_sound.sh:

#!/bin/bash

# FEATURES
################################### 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://github.com/wolfv6/switch_sound/issues
# 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 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 switch-sound to speakers.
# Tested on Linux Fedora 27, Gnome 3.26.2

# SETUP
############################### 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 from 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 toolbar.
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 f

or phone ringer?</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

Thanks to fcomida for pointing me to pactl.

Here is the final product, switch_sound.sh:

#!/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://github.com/wolfv6/switch_sound/issues
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 f

or phone ringer?</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