Skip to content

Instantly share code, notes, and snippets.

@rbobillot
Last active August 1, 2022 23:31
Show Gist options
  • Save rbobillot/497ad01631d96519a48ba9c5f0f3fa8a to your computer and use it in GitHub Desktop.
Save rbobillot/497ad01631d96519a48ba9c5f0f3fa8a to your computer and use it in GitHub Desktop.
# Fixes a "no sound after sleep" bug on Ubuntu (20.04 and 22.04)
#
# After sleep (on my Mi Notebook laptop, for example),
# the sound doesn't work anymore on the main audio output (but still works through the jack cable)
# Running these commands will fix it
audio_restore() {
DEVICE_ID=`lspci -D |grep Audio|awk '{print $1}'`
sudo su - -c "echo 1 > '/sys/bus/pci/devices/${DEVICE_ID}/remove'"
sleep 0.5
sudo su - -c "echo 1 > '/sys/bus/pci/rescan'"
sleep 0.5
pactl set-sink-mute `pactl get-default-sink` 1 # mutes
pactl set-sink-mute `pactl get-default-sink` 0 # unmutes
}
@rbobillot
Copy link
Author

rbobillot commented Aug 1, 2022

Alternatively, I can automate it:
sudo vim /lib/systemd/system-sleep/99_restart_sound

#!/bin/sh
  
case "$1" in
    post)
        DEVICE_ID=`lspci -D |grep Audio|awk '{print $1}'`
        echo 1 > /sys/bus/pci/devices/${DEVICE_ID}/remove
        sleep 1
        echo 1 > /sys/bus/pci/rescan
esac

sudo chmod +x /lib/systemd/system-sleep/99_restart_sound

And then, after a sleep, I manually press the mute/unmute button

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