Most GUI kits use same backend PulseAudio. Use pactl
to write control script.
NAME pactl - Control a running PulseAudio sound server DESCRIPTION pactl can be used to issue control commands to the PulseAudio sound server. pactl only exposes a subset of the available operations. For the full set use the pacmd(1).
Source: man pactl
To check available output sinks
$ pactl list short sinks 0 alsa_output.pci-0000_01_00.1.hdmi-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED 1 alsa_output.pci-0000_00_1b.0.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
To check available input sources
$ pactl list short sources 0 alsa_output.pci-0000_01_00.1.hdmi-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED 1 alsa_output.pci-0000_00_1b.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED 2 alsa_input.pci-0000_00_1b.0.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
To check default
$ pactl info Server String: /run/user/1000/pulse/native Library Protocol Version: 30 Server Protocol Version: 30 Is Local: yes Client Index: 2 Tile Size: 65472 User Name: user Host Name: userpc Server Name: pulseaudio Server Version: 6.0 Default Sample Specification: s16le 2ch 44100Hz Default Channel Map: front-left,front-right Default Sink: alsa_output.pci-0000_00_1b.0.analog-stereo Default Source: alsa_input.pci-0000_00_1b.0.analog-stereo
To set default
pactl set-default-source id-or-name pactl set-default-sink id-or-name
Example:
pactl set-default-source alsa_input.pci-0000_00_1b.0.analog-stereo pactl set-default-sink alsa_output.pci-0000_00_1b.0.analog-stereo
or:
pactl set-default-source 2 pactl set-default-sink 1
Notes:
This will affect only new streams, you have to move the current running streams, so use:
pacmd move-sink-input <stream-id> <sink-id/name>
Nice example to copy from: Switching to HDMI Audio when HDMI is plugged into a laptop (14.04)
Some cards has multiple switchable ports, May be the one you want is not the default.
Check for available ports:
$pactl list sinks Sink #1 State: RUNNING Name: alsa_output.pci-0000_00_1b.0.analog-stereo Description: Built-in Audio Analog Stereo Driver: module-alsa-card.c ... Ports: analog-output-speaker: Speakers (priority: 10000, not available) analog-output-headphones: Headphones (priority: 9000, available) Active Port: analog-output-headphones Formats: pcm ...
To set it:
pactl set-sink-port <sink-id/name> <port-name>
Example:
pactl set-sink-port 1 analog-output-headphones
To change the Master Channel
Following this tutorial, you can set the master channel using qdbus
as follows:
Find the Master Mixer for each of your devices with the following command. First manually set the Master Channel to the one you want to check, then run the command:
qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl
Using the result of that command and the audio sink you wish you control, use the following syntax to change your master channel:
qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.setCurrentMaster "[insert Mixer]" "[insert sink]" 2>&1 > /dev/null
In the end, the script to switch all current audio to a new channel, set it as the default, and set it as the master channel looks like the following:
#!/bin/bash pactl set-default-sink alsa_output.usb-Logitech_Logitech_G930_Headset-00-Headset.analog-stereo pactl set-default-source alsa_input.usb-Logitech_Logitech_G930_Headset-00-Headset.analog-mono INPUTS=($(pacmd list-sink-inputs | grep index | awk '')) for i in $; do pacmd move-sink-input $i alsa_output.usb-Logitech_Logitech_G930_Headset-00-Headset.analog-stereo &> /dev/null; done qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.setCurrentMaster "PulseAudio::Playback_Devices=:1" "alsa_output.usb-Logitech_Logitech_G930_Headset-00-Headset.analog-stereo" 2>&1 > /dev/null