Skip to content

Instantly share code, notes, and snippets.

@bassmanitram
Last active July 25, 2020 07:55
Show Gist options
  • Save bassmanitram/495fd35b76083f0c4a79777b8ab470fd to your computer and use it in GitHub Desktop.
Save bassmanitram/495fd35b76083f0c4a79777b8ab470fd to your computer and use it in GitHub Desktop.
Workaround script to help solve Ubuntu 16.04 HDMI sound issues when ALSA can use the HDMI sink, but Pulse thinks it's unplugged
#!/bin/bash
{
#
# Get rid of pulse and its cache (so this script can be run multiple times)
#
rm ~/.config/pulse/*
pulseaudio -k
#
# Wait till pulse sorts itself out
#
sleep 2
#
# Find the HDMI "cards" - you will likely have to modify this grep since it is atuned to my
# NVidia graphics card's active HDMI audio output device
#
card=$(aplay -l | grep "NVidia.*HDMI 1" -m 1)
#
# Log the card
#
echo $card
if [ "${card}" ]; then
#
# We have a card, so now we parse the aplay output line
#
# Effectively, it you split it into an array by "," and ":" then:
#
# the card number is the last "word" of the first item in the array
# the device number is the last "word" of the third item in the array
#
IFS=',:' read -r -a details <<< "${card}"
card_number="${details[0]##* }"
dev_number="${details[2]##* }"
#
# After that, just force-add the device's associated ALSA sink to Pulse
#
pacmd load-module module-alsa-sink device=hw:${card_number},${dev_number}
#
# And force Pulse to use that ALSA sink as the default output
#
pacmd set-default-sink alsa_output.hw_${card_number}_${dev_number}
else
#
# Can't find your card - fix the grep on line 18?
#
echo "No HDMI sound target found"
exit 1
fi
#
# And we put all output into a log file in the home directory
#
} >> ~/fix_sound.log 2>&1
@bassmanitram
Copy link
Author

Finally! After days digging the web for a solution, yours is the only one that really worked. Thanks a lot!

Finally, I helped someone - in the words of my wife "That almost NEVER happens"!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment