reset sound after suspend to memory (deep sleep)
#5
Information 
Today I got a bit fed up by the sound not working after deep sleep, so until the driver itself is capable of handling sleep I hacked together a workaround that seems to work well enough. I haven't packaged it yet or anything, but I wanted to share with community what I got so far.

Right now I have two pieces. First is a script:
Code:
$ cat /usr/local/sbin/sndreset
#!/bin/bash
chmod 000 /dev/snd/pcmC0D0*
message_timeout=0
while lsof /dev/snd/pcmC0D0* | grep -q /dev/snd/pcmC0D0
do
if (( message_timeout == 0 ))
then
message_timeout=11
for line in "$(ps -eo pid,user:32,args | grep "dbus-daemon.*--session" | grep -v grep | xargs)"
do
pid="$(echo "$line" | cut -d' ' -f 1)"
user="$(echo "$line" | cut -d' ' -f 2)"
bus_addr="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//')"
DBUS_SESSION_BUS_ADDRESS="$bus_addr" sudo -u $user -E /usr/bin/notify-send -u normal -t 10000 -a 'Audio recovery' 'Unable to restore audio' 'A process is attempting audio playback, it needs to be stopped before audio can be restored.'
done
fi
let message_timeout--
sleep 1
done
tee /sys/bus/i2c/drivers/es8316/{un,}bind <<< 1-0011 > /dev/null
sleep 1
/usr/sbin/alsactl -E HOME=/run/alsa -E XDG_RUNTIME_DIR=/run/alsa/runtime restore

Second is a systemd service unit:
Code:
$ cat /etc/systemd/system/pinebookpro-sleep.service
[Unit]
Descript=Recover audio after sleep
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

[Service]
ExecStart=/usr/local/sbin/sndreset

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

Make sure the script is executable and the service unit is enabled.

How does it work:
Service unit plugs itself to run the script after suspend is done. I think I should be able to remove `hibernate.target` from it, but I decided to keep it around just in case. The meat is in the script itself, and here is what it does:
1. it removes all permissions from the /dev/snd/pcm* files, thus making sure no new process will be able to open them.
2. it enters a loop where for as long as the above files are still open it will send a message to all active sessions hinting at what to do to restore sound playback. Said message will be active for 10 seconds, and it is sent every 11 seconds. Loop iterates every second.
3. once whatever holds the above files open is stopped the loop will end. If one is using PulseAudio it should close those files too once no application is trying to play any sound. Since the files are set to permissions 000 no new process, including PulseAudio, should be able to open them again.
4. the device is re-initialized
5. finally, after a bit of a delay, restore asound.state to raise the volume in ALSA mixer - without this the sound volume may be stuck too low.

Note that this script doesn't need PulseAudio to be stopped in any way, it only needs that nothing tries to play any sound.

Finally about asound.state - I used one from Manjaro as a basis, but I: a) edited it to set both instances of 'Headphone Playback Volume' and both instances of 'Headphone Mixer Volume' to max value (3 and 11) - four places to change total; and b) set immutable flag on the file once it's copied to /var/lib/alsa/asound.state - otherwise I had it reset to values less than max volume. Something I still haven't figured out yet - how to call `alsactl restore` automatically at the right time on boot, because if it's called too early - the volume will not be restored properly, and sound will be stuck being very quiet. So far the best I was able to do is call it using systemd timer at `OnBootSec=1min`.
This message was created with 100% recycled electrons
  Reply


Messages In This Thread
RE: reset sound after suspend to memory (deep sleep) - by moonwalkers - 03-08-2021, 11:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sleep and external display - Are there any options? chris88233 7 1,736 09-04-2023, 09:03 PM
Last Post: wdt
  PineBook Pro seems to go to deep sleep, but doesn't wake up pogo 11 5,285 08-31-2023, 04:20 PM
Last Post: TRS-80
  Rkvdec memory leak? Abhinav 2 841 06-02-2023, 08:55 AM
Last Post: Abhinav
  Sound on Armbian Bram 1 901 04-01-2023, 03:16 PM
Last Post: TRS-80
  Suspend/Resume Broken with Bootloader on SPI Flash xp19375 2 1,169 03-24-2023, 04:25 PM
Last Post: srs5694
  With the help of a friend, I installed a beautiful deep os distribution, but I won't wangyukunshan 0 605 03-03-2023, 10:56 PM
Last Post: wangyukunshan
  Hibernation and Speaker Sound Issues, First Month of PBP kebab 7 2,990 11-02-2022, 02:33 PM
Last Post: kebab
  Resume from suspend not working after flashing Tow-Boot to SPI xp19375 3 1,978 10-31-2022, 10:14 AM
Last Post: wdt
  Loud distorted speaker sound when connecting to power supply myself600 2 1,203 09-21-2022, 12:11 PM
Last Post: myself600
  How to make manjaro sleep or hibernate? KDE? regretfulpineuser 0 988 06-20-2022, 12:56 AM
Last Post: regretfulpineuser

Forum Jump:


Users browsing this thread: 1 Guest(s)