Created
May 1, 2018 10:53
-
-
Save mrbuk/e94ed342503b45417729af65fcdfce23 to your computer and use it in GitHub Desktop.
Set the default sink and source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DEFAULT_CARD=alsa_card.pci-0000_01_00.1 | |
DEFAULT_SOURCE=alsa_input.usb-046d_HD_Pro_Webcam_C920_B6E53ECF-02.analog-stereo | |
SOUND_FILE=ChID-BLITS-EBU-Narration441-16b.wav | |
function stop_sound_system() { | |
echo "Stopping pulseaudion"; killall pulseaudio | |
echo "Waiting 10s ..."; sleep 10 | |
} | |
function set_default_sink() { | |
# extract current output | |
local active_output | |
local profile | |
local sink | |
active_output=$(pactl list | grep hdmi-output \ | |
| grep -v 'not available' \ | |
| grep 'available' \ | |
| egrep -o 'hdmi-output-[^:]+' \ | |
| uniq) | |
# sink looks like | |
# alsa_output.pci-0000_01_00.1.hdmi-stereo | |
# which means it can be build based on the CARD value | |
if [[ "$active_output" = "hdmi-output-0" ]]; then | |
profile="output:hdmi-stereo" | |
sink="${DEFAULT_CARD/card/output}.hdmi-stereo" | |
elif [[ "$active_output" = "hdmi-output-1" ]]; then | |
profile="output:hdmi-stereo-extra1" | |
sink="${DEFAULT_CARD/card/output}.hdmi-stereo-extra1" | |
else | |
echo "Unknown active output: $active_output" | |
exit 1 | |
fi | |
pactl set-sink-mute "${sink}" 0 | |
pactl set-sink-volume "${sink}" 50% | |
pactl set-card-profile "${DEFAULT_CARD}" "${profile}" | |
pactl set-default-sink "${sink}" | |
pactl set-default-source "${DEFAULT_SOURCE}" | |
} | |
set_default_sink | |
play "$SOUND_FILE" > /dev/null 2>&1 & | |
while true; do | |
read -p "Do you hear a sound? (y/n) " yn | |
case $yn in | |
[Yy]* ) kill $!; echo "All fine."; exit 0;; | |
[Nn]* ) kill $!; stop_sound_system; set_default_sink; break;; | |
* ) echo "Please answer y or n." ;; | |
esac | |
done | |
while true; do | |
read -p "Checking sound again. Do you hear a sound? (y/n) " yn | |
case $yn in | |
[Yy]* ) kill $!; echo "All fine."; exit 0;; | |
[Nn]* ) kill $!; echo "Please troubleshoot problem."; exit 0;; | |
* ) echo "Please answer y or n." ;; | |
esac | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment