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

So my situation is that, in largely standard Ubuntu 16.04 I have suddenly the symptom that ALSA recognizes and can use my Nvidia card's HDMI output, whereas pulse thinks the HDMI output is unplugged.

The trick then is to force pulse to use the ALSA “sink” that represents your HDMI output.

This starts with finding what alsa calls your HDMI “sinks”:

aplay -l

If you have multiple, see which one is the right one by using

speaker-test -c 2 -r 48000 -D hw:[card_number],[device_number]

Then force pulse to see it and use it by default

pacmd load-module module-alsa-sink device=hw:[card_number],[device_number]
pacmd set-default-sink alsa_output.hw_[card_number]_[device_number]

Those last two command can be put into /etc/pulse/default.pa but deciding where in that file is a bit of a challenge, and, more importantly, (if you have a setup like mine) your card number can change so may not be correct next time you boot.

So I left /etc/pulse/default.pa alone and wrote this script that runs as a startup script configured from the Startup Applications tool, and is also available as a Unity button.

Note that the grep in the script will need modifying to your own HDMI card setup. When you run aplay by hand, take note of the name that ALSA gives the card and device that works, and use that in the grep command on line 18.

@gustamo
Copy link

gustamo commented Jul 23, 2020

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

@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