You are not logged in.

#1 2017-11-02 13:42:53

7thSon
Member
Registered: 2017-05-07
Posts: 186

[SOLVED] Script to toggle pulseaudio outputs

My speakers and headphones always play simultaneously unless I go into alsamixer and disable auto mute, and lower the volume of either speakers or headphones.
Does anyone have, or know of, a bash script that toggles between speaker and headphone outputs? I found the script below (which throws errors when I run it), but I want one that toggles without sending a parameter to the script.

#!/bin/sh

NAME=$(basename -- "$0")

command -v amixer >/dev/null 2>&1 || { echo >&2 "amixer not installed"; exit 1; }
command -v pacmd >/dev/null 2>&1 || { echo >&2 "pacmd not installed"; exit 1; }

amixer -c1 sset "Auto-Mute Mode" Disabled > /dev/null

if [ -z $1 ]; then
  echo -e "Usage:\nFront Speakers\t: $NAME 0\t\nHeadphones\t: $NAME 1\nBoth Speakers\t: $NAME 2"
  exit 1
elif [ $1 -eq 0 ] 2> /dev/null; then
  pacmd set-sink-port 1 analog-output-lineout
  amixer -c1 set Headphone 0% > /dev/null
  amixer -c1 set Front 100% > /dev/null
elif [ $1 -eq 1 ] 2> /dev/null; then
  pacmd set-sink-port 1 analog-output-headphones
  amixer -c1 set Front 0% > /dev/null
  amixer -c1 set Headphone 100% > /dev/null
elif [ $1 -eq 2 ] 2> /dev/null; then
  pacmd set-sink-port 1 analog-output-headphones
  amixer -c1 set Front 100% > /dev/null
  amixer -c1 set Headphone 100% > /dev/null
else
  echo -e "Invalid argument"
  exit 1
fi

So in short I want to be able to bind a script to a single keyboard shortcut to toggle between speaker/headphone output.

Last edited by 7thSon (2017-11-02 18:45:15)

Offline

#2 2017-11-02 13:53:00

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] Script to toggle pulseaudio outputs

What errors do you get? Have you verified that your card is card 1 as well and that the outputs to be manipulated have the same names? Without information on the state of your system no one can help you nudge you into the correct direction for a working script. What are the outputs of

aplay -l
amixer -c$index of card from aplay -l
pacmd list-cards
pacmd list-sinks

Offline

#3 2017-11-02 14:22:38

7thSon
Member
Registered: 2017-05-07
Posts: 186

Re: [SOLVED] Script to toggle pulseaudio outputs

V1del wrote:

What errors do you get? Have you verified that your card is card 1 as well and that the outputs to be manipulated have the same names? Without information on the state of your system no one can help you nudge you into the correct direction for a working script. What are the outputs of

aplay -l
amixer -c$index of card from aplay -l
pacmd list-cards
pacmd list-sinks

The output I get from the script, regardless of passing 0 or 1 is:

amixer: Unable to find simple control 'Auto-Mute Mode',0

No sink found by this name or index.
amixer: Unable to find simple control 'Headphone',0

amixer: Unable to find simple control 'Front',0

aplay -l:

**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC898 Analog [ALC898 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 1: ALC898 Digital [ALC898 Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia_1 [HDA NVidia], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia_1 [HDA NVidia], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia_1 [HDA NVidia], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia_1 [HDA NVidia], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: NVidia [HDA NVidia], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: NVidia [HDA NVidia], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: NVidia [HDA NVidia], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: NVidia [HDA NVidia], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

amixer -c0

Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 87
  Mono: Playback 87 [100%] [0.00dB] [on]
Simple mixer control 'Headphone',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 87
  Mono:
  Front Left: Playback 87 [100%] [0.00dB] [on]
  Front Right: Playback 87 [100%] [0.00dB] [on]
Simple mixer control 'PCM',0
  Capabilities: pvolume
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 255
  Mono:
  Front Left: Playback 255 [100%] [0.00dB]
  Front Right: Playback 255 [100%] [0.00dB]
Simple mixer control 'Front',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 87
  Mono:
  Front Left: Playback 87 [100%] [0.00dB] [on]
  Front Right: Playback 87 [100%] [0.00dB] [on]
Simple mixer control 'Front Mic',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 31
  Mono:
  Front Left: Playback 0 [0%] [-34.50dB] [off]
  Front Right: Playback 0 [0%] [-34.50dB] [off]
Simple mixer control 'Front Mic Boost',0
  Capabilities: volume
  Playback channels: Front Left - Front Right
  Capture channels: Front Left - Front Right
  Limits: 0 - 3
  Front Left: 2 [67%] [20.00dB]
  Front Right: 2 [67%] [20.00dB]
Simple mixer control 'Surround',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 87
  Mono:
  Front Left: Playback 0 [0%] [-65.25dB] [off]
  Front Right: Playback 0 [0%] [-65.25dB] [off]
Simple mixer control 'Center',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 87
  Mono: Playback 0 [0%] [-65.25dB] [off]
Simple mixer control 'LFE',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 87
  Mono: Playback 0 [0%] [-65.25dB] [off]
Simple mixer control 'Line',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 31
  Mono:
  Front Left: Playback 0 [0%] [-34.50dB] [off]
  Front Right: Playback 0 [0%] [-34.50dB] [off]
Simple mixer control 'Line Boost',0
  Capabilities: volume
  Playback channels: Front Left - Front Right
  Capture channels: Front Left - Front Right
  Limits: 0 - 3
  Front Left: 0 [0%] [0.00dB]
  Front Right: 0 [0%] [0.00dB]
Simple mixer control 'IEC958',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [off]
Simple mixer control 'IEC958 Default PCM',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
Simple mixer control 'IEC958',1
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
Simple mixer control 'IEC958',16
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]
Simple mixer control 'Beep',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 31
  Mono:
  Front Left: Playback 0 [0%] [-34.50dB] [off]
  Front Right: Playback 0 [0%] [-34.50dB] [off]
Simple mixer control 'Capture',0
  Capabilities: cvolume cswitch
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 63
  Front Left: Capture 63 [100%] [30.00dB] [off]
  Front Right: Capture 63 [100%] [30.00dB] [off]
Simple mixer control 'Capture',1
  Capabilities: cvolume cswitch
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 63
  Front Left: Capture 0 [0%] [-17.25dB] [off]
  Front Right: Capture 0 [0%] [-17.25dB] [off]
Simple mixer control 'Capture',2
  Capabilities: cvolume cswitch
  Capture channels: Front Left - Front Right
  Limits: Capture 0 - 63
  Front Left: Capture 0 [0%] [-17.25dB] [off]
  Front Right: Capture 0 [0%] [-17.25dB] [off]
Simple mixer control 'Auto-Mute Mode',0
  Capabilities: enum
  Items: 'Disabled' 'Enabled'
  Item0: 'Disabled'
Simple mixer control 'Input Source',0
  Capabilities: cenum
  Items: 'Front Mic' 'Rear Mic' 'Line'
  Item0: 'Front Mic'
Simple mixer control 'Input Source',1
  Capabilities: cenum
  Items: 'Front Mic' 'Rear Mic' 'Line'
  Item0: 'Front Mic'
Simple mixer control 'Input Source',2
  Capabilities: cenum
  Items: 'Front Mic' 'Rear Mic' 'Line'
  Item0: 'Front Mic'
Simple mixer control 'Loopback Mixing',0
  Capabilities: enum
  Items: 'Disabled' 'Enabled'
  Item0: 'Disabled'
Simple mixer control 'Rear Mic',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 31
  Mono:
  Front Left: Playback 0 [0%] [-34.50dB] [off]
  Front Right: Playback 0 [0%] [-34.50dB] [off]
Simple mixer control 'Rear Mic Boost',0
  Capabilities: volume
  Playback channels: Front Left - Front Right
  Capture channels: Front Left - Front Right
  Limits: 0 - 3
  Front Left: 0 [0%] [0.00dB]
  Front Right: 0 [0%] [0.00dB]

pacmd list-cards

3 card(s) available.
    index: 0
	name: <alsa_card.pci-0000_01_00.1>
	driver: <module-alsa-card.c>
	owner module: 6
	properties:
		alsa.card = "1"
		alsa.card_name = "HDA NVidia"
		alsa.long_card_name = "HDA NVidia at 0xf7080000 irq 17"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:01:00.1"
		sysfs.path = "/devices/pci0000:00/0000:00:01.0/0000:01:00.1/sound/card1"
		device.bus = "pci"
		device.vendor.id = "10de"
		device.vendor.name = "NVIDIA Corporation"
		device.product.id = "10f0"
		device.product.name = "GP104 High Definition Audio Controller"
		device.string = "1"
		device.description = "GP104 High Definition Audio Controller"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	profiles:
		output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400, available: no)
		output:hdmi-surround: Digital Surround 5.1 (HDMI) Output (priority 300, available: no)
		output:hdmi-surround71: Digital Surround 7.1 (HDMI) Output (priority 300, available: no)
		output:hdmi-stereo-extra1: Digital Stereo (HDMI 2) Output (priority 5200, available: no)
		output:hdmi-surround-extra1: Digital Surround 5.1 (HDMI 2) Output (priority 100, available: no)
		output:hdmi-surround71-extra1: Digital Surround 7.1 (HDMI 2) Output (priority 100, available: no)
		output:hdmi-stereo-extra2: Digital Stereo (HDMI 3) Output (priority 5200, available: no)
		output:hdmi-surround-extra2: Digital Surround 5.1 (HDMI 3) Output (priority 100, available: no)
		output:hdmi-surround71-extra2: Digital Surround 7.1 (HDMI 3) Output (priority 100, available: no)
		output:hdmi-stereo-extra3: Digital Stereo (HDMI 4) Output (priority 5200, available: no)
		output:hdmi-surround-extra3: Digital Surround 5.1 (HDMI 4) Output (priority 100, available: no)
		output:hdmi-surround71-extra3: Digital Surround 7.1 (HDMI 4) Output (priority 100, available: no)
		off: Off (priority 0, available: unknown)
	active profile: <off>
	ports:
		hdmi-output-0: HDMI / DisplayPort (priority 5900, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-1: HDMI / DisplayPort 2 (priority 5800, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-2: HDMI / DisplayPort 3 (priority 5700, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-3: HDMI / DisplayPort 4 (priority 5600, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
    index: 1
	name: <alsa_card.pci-0000_02_00.1>
	driver: <module-alsa-card.c>
	owner module: 7
	properties:
		alsa.card = "2"
		alsa.card_name = "HDA NVidia"
		alsa.long_card_name = "HDA NVidia at 0xf5080000 irq 18"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:02:00.1"
		sysfs.path = "/devices/pci0000:00/0000:00:01.1/0000:02:00.1/sound/card2"
		device.bus = "pci"
		device.vendor.id = "10de"
		device.vendor.name = "NVIDIA Corporation"
		device.product.id = "0e0a"
		device.product.name = "GK104 HDMI Audio Controller"
		device.string = "2"
		device.description = "GK104 HDMI Audio Controller"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	profiles:
		output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400, available: no)
		output:hdmi-surround: Digital Surround 5.1 (HDMI) Output (priority 300, available: no)
		output:hdmi-surround71: Digital Surround 7.1 (HDMI) Output (priority 300, available: no)
		output:hdmi-stereo-extra1: Digital Stereo (HDMI 2) Output (priority 5200, available: no)
		output:hdmi-surround-extra1: Digital Surround 5.1 (HDMI 2) Output (priority 100, available: no)
		output:hdmi-surround71-extra1: Digital Surround 7.1 (HDMI 2) Output (priority 100, available: no)
		output:hdmi-stereo-extra2: Digital Stereo (HDMI 3) Output (priority 5200, available: no)
		output:hdmi-surround-extra2: Digital Surround 5.1 (HDMI 3) Output (priority 100, available: no)
		output:hdmi-surround71-extra2: Digital Surround 7.1 (HDMI 3) Output (priority 100, available: no)
		output:hdmi-stereo-extra3: Digital Stereo (HDMI 4) Output (priority 5200, available: no)
		output:hdmi-surround-extra3: Digital Surround 5.1 (HDMI 4) Output (priority 100, available: no)
		output:hdmi-surround71-extra3: Digital Surround 7.1 (HDMI 4) Output (priority 100, available: no)
		off: Off (priority 0, available: unknown)
	active profile: <off>
	ports:
		hdmi-output-0: HDMI / DisplayPort (priority 5900, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-1: HDMI / DisplayPort 2 (priority 5800, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-2: HDMI / DisplayPort 3 (priority 5700, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-3: HDMI / DisplayPort 4 (priority 5600, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
    index: 2
	name: <alsa_card.pci-0000_00_1b.0>
	driver: <module-alsa-card.c>
	owner module: 8
	properties:
		alsa.card = "0"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0xf7b10000 irq 36"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:00:1b.0"
		sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card0"
		device.bus = "pci"
		device.vendor.id = "8086"
		device.vendor.name = "Intel Corporation"
		device.product.id = "1e20"
		device.product.name = "7 Series/C216 Chipset Family High Definition Audio Controller (Z77 Extreme4 motherboard)"
		device.form_factor = "internal"
		device.string = "0"
		device.description = "Built-in Audio"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	profiles:
		input:analog-stereo: Analog Stereo Input (priority 60, available: unknown)
		output:analog-stereo: Analog Stereo Output (priority 6000, available: unknown)
		output:analog-stereo+input:analog-stereo: Analog Stereo Duplex (priority 6060, available: unknown)
		output:analog-surround-21: Analog Surround 2.1 Output (priority 800, available: no)
		output:analog-surround-21+input:analog-stereo: Analog Surround 2.1 Output + Analog Stereo Input (priority 860, available: unknown)
		output:analog-surround-40: Analog Surround 4.0 Output (priority 700, available: no)
		output:analog-surround-40+input:analog-stereo: Analog Surround 4.0 Output + Analog Stereo Input (priority 760, available: unknown)
		output:analog-surround-41: Analog Surround 4.1 Output (priority 800, available: no)
		output:analog-surround-41+input:analog-stereo: Analog Surround 4.1 Output + Analog Stereo Input (priority 860, available: unknown)
		output:analog-surround-50: Analog Surround 5.0 Output (priority 700, available: no)
		output:analog-surround-50+input:analog-stereo: Analog Surround 5.0 Output + Analog Stereo Input (priority 760, available: unknown)
		output:analog-surround-51: Analog Surround 5.1 Output (priority 800, available: no)
		output:analog-surround-51+input:analog-stereo: Analog Surround 5.1 Output + Analog Stereo Input (priority 860, available: unknown)
		output:iec958-stereo: Digital Stereo (IEC958) Output (priority 5500, available: unknown)
		output:iec958-stereo+input:analog-stereo: Digital Stereo (IEC958) Output + Analog Stereo Input (priority 5560, available: unknown)
		output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400, available: no)
		output:hdmi-stereo+input:analog-stereo: Digital Stereo (HDMI) Output + Analog Stereo Input (priority 5460, available: unknown)
		output:hdmi-surround: Digital Surround 5.1 (HDMI) Output (priority 300, available: no)
		output:hdmi-surround+input:analog-stereo: Digital Surround 5.1 (HDMI) Output + Analog Stereo Input (priority 360, available: unknown)
		output:hdmi-surround71: Digital Surround 7.1 (HDMI) Output (priority 300, available: no)
		output:hdmi-surround71+input:analog-stereo: Digital Surround 7.1 (HDMI) Output + Analog Stereo Input (priority 360, available: unknown)
		output:hdmi-stereo-extra1: Digital Stereo (HDMI 2) Output (priority 5200, available: no)
		output:hdmi-stereo-extra1+input:analog-stereo: Digital Stereo (HDMI 2) Output + Analog Stereo Input (priority 5260, available: unknown)
		output:hdmi-surround-extra1: Digital Surround 5.1 (HDMI 2) Output (priority 100, available: no)
		output:hdmi-surround-extra1+input:analog-stereo: Digital Surround 5.1 (HDMI 2) Output + Analog Stereo Input (priority 160, available: unknown)
		output:hdmi-surround71-extra1: Digital Surround 7.1 (HDMI 2) Output (priority 100, available: no)
		output:hdmi-surround71-extra1+input:analog-stereo: Digital Surround 7.1 (HDMI 2) Output + Analog Stereo Input (priority 160, available: unknown)
		off: Off (priority 0, available: unknown)
	active profile: <output:analog-stereo>
	sinks:
		alsa_output.pci-0000_00_1b.0.analog-stereo/#0: Built-in Audio Analog Stereo
	sources:
		alsa_output.pci-0000_00_1b.0.analog-stereo.monitor/#0: Monitor of Built-in Audio Analog Stereo
	ports:
		analog-input-front-mic: Front Microphone (priority 8500, latency offset 0 usec, available: yes)
			properties:
				device.icon_name = "audio-input-microphone"
		analog-input-rear-mic: Rear Microphone (priority 8200, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "audio-input-microphone"
		analog-input-linein: Line In (priority 8100, latency offset 0 usec, available: no)
			properties:
				
		analog-output-lineout: Line Out (priority 9900, latency offset 0 usec, available: no)
			properties:
				
		analog-output-headphones: Headphones (priority 9000, latency offset 0 usec, available: yes)
			properties:
				device.icon_name = "audio-headphones"
		iec958-stereo-output: Digital Output (S/PDIF) (priority 0, latency offset 0 usec, available: unknown)
			properties:
				
		hdmi-output-0: HDMI / DisplayPort (priority 5900, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"
		hdmi-output-1: HDMI / DisplayPort 2 (priority 5800, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "video-display"

pacmd list-sinks

1 sink(s) available.
  * index: 0
	name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
	driver: <module-alsa-card.c>
	flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
	state: SUSPENDED
	suspend cause: IDLE 
	priority: 9039
	volume: front-left: 65536 / 100% / 0,00 dB,   front-right: 65536 / 100% / 0,00 dB
	        balance 0,00
	base volume: 65536 / 100% / 0,00 dB
	volume steps: 65537
	muted: no
	current latency: 0,00 ms
	max request: 0 KiB
	max rewind: 0 KiB
	monitor source: 0
	sample spec: s16le 2ch 44100Hz
	channel map: front-left,front-right
	             Stereo
	used by: 0
	linked by: 0
	configured latency: 0,00 ms; range is 0,50 .. 2000,00 ms
	card: 2 <alsa_card.pci-0000_00_1b.0>
	module: 8
	properties:
		alsa.resolution_bits = "16"
		device.api = "alsa"
		device.class = "sound"
		alsa.class = "generic"
		alsa.subclass = "generic-mix"
		alsa.name = "ALC898 Analog"
		alsa.id = "ALC898 Analog"
		alsa.subdevice = "0"
		alsa.subdevice_name = "subdevice #0"
		alsa.device = "0"
		alsa.card = "0"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0xf7b10000 irq 36"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:00:1b.0"
		sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card0"
		device.bus = "pci"
		device.vendor.id = "8086"
		device.vendor.name = "Intel Corporation"
		device.product.id = "1e20"
		device.product.name = "7 Series/C216 Chipset Family High Definition Audio Controller (Z77 Extreme4 motherboard)"
		device.form_factor = "internal"
		device.string = "front:0"
		device.buffering.buffer_size = "352800"
		device.buffering.fragment_size = "176400"
		device.access_mode = "mmap+timer"
		device.profile.name = "analog-stereo"
		device.profile.description = "Analog Stereo"
		device.description = "Built-in Audio Analog Stereo"
		alsa.mixer_name = "Realtek ALC898"
		alsa.components = "HDA:10ec0899,18491898,00100003 HDA:80862806,80860101,00100000"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	ports:
		analog-output-lineout: Line Out (priority 9900, latency offset 0 usec, available: no)
			properties:
				
		analog-output-headphones: Headphones (priority 9000, latency offset 0 usec, available: yes)
			properties:
				device.icon_name = "audio-headphones"
	active port: <analog-output-headphones>

That was a lot of output yikes
Anyway, I changed the script I found to target c0 instead, and that works for switching the outputs by running "sh speakers.sh 0" and "sh speakers.sh 1".
Now I would like to modify it so that I only need to run "sh speakers.sh" and the scripts toggles automatically between the outputs.
I guess that information is available somewhere in the output I just pasted into this post, so if I can grab that reliably and use it as variables to determine which toggle to do.

Thanks for your help so far smile

Offline

#4 2017-11-02 18:43:53

7thSon
Member
Registered: 2017-05-07
Posts: 186

Re: [SOLVED] Script to toggle pulseaudio outputs

Here is the modified script that toggles the audio output between speakers and headphones. If both outputs are initially on, it defaults to headphones.

#!/bin/sh

command -v amixer >/dev/null 2>&1 || { echo >&2 "amixer not installed"; exit 1; }
command -v pacmd >/dev/null 2>&1 || { echo >&2 "pacmd not installed"; exit 1; }

amixer -c0 sset "Auto-Mute Mode" Disabled > /dev/null

speakersVol=$(amixer -c0 | grep "Simple mixer control 'Front'" -A 5 | sed -n 6p | cut -d " " -f6)
hphonesVol=$(amixer -c0 | grep "Simple mixer control 'Headphone'" -A 5 | sed -n 6p | cut -d " " -f6)

if [ $speakersVol -gt 0 ]; then
  speakersOn=true;
else
  speakersOn=false;
fi

if [ $hphonesVol -gt 0 ]; then
  hphonesOn=true;
else
  hphonesOn=false;
fi

#if both speakers and headphones are on, default to headphones
if [ $speakersOn == true ] && [ $hphonesOn == true ] 2> /dev/null; then
  pacmd set-sink-port 0 analog-output-headphones
  amixer -c0 set Front 0% > /dev/null
  amixer -c0 set Headphone 100% > /dev/null
  notify-send "Audio output set to headphones" -t 2000
  exit 0
#if speakers are on, switch to headphones
elif [ $speakersOn == true ] 2> /dev/null; then
  pacmd set-sink-port 0 analog-output-headphones
  amixer -c0 set Front 0% > /dev/null
  amixer -c0 set Headphone 100% > /dev/null
  notify-send "Audio output set to headphones" -t 2000
  exit 0
#if headphones are on, switch to speakers
elif [ $speakersOn == false ] 2> /dev/null; then
  pacmd set-sink-port 0 analog-output-lineout
  amixer -c0 set Headphone 0% > /dev/null
  amixer -c0 set Front 100% > /dev/null
  notify-send "Audio output set to speakers" -t 2000
  exit 0
fi

Thanks for the help on getting started with this smile

Last edited by 7thSon (2017-11-02 19:16:28)

Offline

#5 2017-11-09 20:44:01

7thSon
Member
Registered: 2017-05-07
Posts: 186

Re: [SOLVED] Script to toggle pulseaudio outputs

I have an additional need of support here - I'm using the script above and I set it to run at startup as well in order to default my audio output to the headphones on boot, but that doesn't seem to work.
I thought that the section in the code that checks if both outputs are on would have sorted that out, but it seems like it doesn't.

What could I do to make that functionality work? I guess I could make a separate script that runs on boot and simply sets the output to headphones, but it would be preferable to keep everything inside this one file.

Offline

#6 2017-11-09 21:52:33

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] Script to toggle pulseaudio outputs

Define "at startup". If you make it a systemd service, the file will run long before pulseaudio is even started. Either use autostart facilities of your DE/WM so that the script is run after your session and pulseaudio are started, or create a Systemd User service that depends on the startup of the pulseaudio.service which would have a similar effect (or add the corresponding pacmd command (without the pacmd) to your /etc/pulse/default.pa so that pulseaudio will run the corresponding command during startup, however you lose the amixer context info in that case).

Offline

#7 2017-11-09 22:43:21

7thSon
Member
Registered: 2017-05-07
Posts: 186

Re: [SOLVED] Script to toggle pulseaudio outputs

V1del wrote:

Define "at startup". If you make it a systemd service, the file will run long before pulseaudio is even started. Either use autostart facilities of your DE/WM so that the script is run after your session and pulseaudio are started, or create a Systemd User service that depends on the startup of the pulseaudio.service which would have a similar effect (or add the corresponding pacmd command (without the pacmd) to your /etc/pulse/default.pa so that pulseaudio will run the corresponding command during startup, however you lose the amixer context info in that case).

I put it in the XFCE "Session and Startup" manager, but I will try to make a service of it and set it up to wait for pulseaudio as you suggested.
However, now that I tried to find the pulseaudio service I couldn't find it by "systemctl list-units --full | grep pulse", is it called something else, or am I looking in the wrong place?

Offline

#8 2017-11-09 23:04:17

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,425

Re: [SOLVED] Script to toggle pulseaudio outputs

Yes, read the article I linked system services and user services aren't running in the same context.

Offline

#9 2017-11-14 15:56:50

7thSon
Member
Registered: 2017-05-07
Posts: 186

Re: [SOLVED] Script to toggle pulseaudio outputs

V1del wrote:

Yes, read the article I linked system services and user services aren't running in the same context.

Fantastic, thanks for the help. I created a user service in ~/.config/systemd/user and enabled it, and now the audio output script works as expected smile

Offline

Board footer

Powered by FluxBB