You are not logged in.

#1 2016-06-14 12:29:22

theailer
Member
Registered: 2016-01-06
Posts: 19

[SOLVED]Getting systemd to play audio at boot

Hi.
I've been really trying to get systemd to play a sound after boot but before login to tty.
What my findings boil down to is that my systemd service file runs as root but root is not allowed to play sounds through pulseaudio.
Anyone have any idea how to do this ?
I could settle for playing a sound on lid open instead.

Right now my service file looks like this and resides as a user service file.

[Unit]
Description=Boot Sound
Requires=pulseaudio.socket

[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/usr/bin/paplay /var/local/biosbeep.wav

[Install]
WantedBy=multi-user.target

Last edited by theailer (2016-06-16 06:42:11)

Offline

#2 2016-06-14 12:42:11

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,409

Re: [SOLVED]Getting systemd to play audio at boot

For the user service case, this thread provides a good rundown of what is necessary https://bbs.archlinux.org/viewtopic.php?id=207465

The gist here is, if you have a user service you want to depend on default.target instead of multi-user.target

For the bootup case I'd probably be easier to just use plain ALSA with aplay on the specific device, since it is going to be a short sound it should free the card before pulseaudio takes access over (which is when you've logged in)

Offline

#3 2016-06-14 13:36:02

theailer
Member
Registered: 2016-01-06
Posts: 19

Re: [SOLVED]Getting systemd to play audio at boot

Thanks for the suggestion, now atleast I can see that it tries to start on boot with journalctl --user.
Also changed to use aplay instead of paplay as seen below.
But it stops on an error instead, I'm missing something here.
When logged in I can start the service and make it play the sound.

Service file:

[Unit]
Description=Boot Sound
Requires=
[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/usr/bin/aplay -D plughw:1,0 /var/local/biosbeep.wav

[Install]
WantedBy=default.target

journalctl --user:

Jun 14 15:31:21 chromebook aplay[1165]: ALSA lib pcm_hw.c:1701:(_snd_pcm_hw_open) Invalid value for card
Jun 14 15:31:21 chromebook aplay[1165]: aplay: main:786: audio open error: No such file or directory
Jun 14 15:31:21 chromebook systemd[1129]: startupsound.service: Main process exited, code=exited, status=1/FAILURE
Jun 14 15:31:21 chromebook systemd[1129]: Failed to start Boot Sound.

Offline

#4 2016-06-14 18:45:07

beroal
Member
From: Ukraine
Registered: 2009-06-07
Posts: 325
Website

Re: [SOLVED]Getting systemd to play audio at boot

theailer wrote:

But it stops on an error instead, I'm missing something here.

A problem I encountered is that a sound card does not exist when a service is started. You can check this by inserting the following shell script into your service.

ls -l /dev/snd >> /tmp/sound.log
sleep 5
ls -l /dev/snd >> /tmp/sound.log

You may want to add the following text to your service.

[Unit]
After=sound.target

Another problem I encountered is that "sound.target" is reached when any sound card appears. I have 2 sound cards, and I want to play on Card 0, and "sound.target" is reached when Card 1 exists and Card 0 does not.

Last edited by beroal (2016-06-14 20:26:17)


we are not condemned to write ugly code

Offline

#5 2016-06-14 22:19:15

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,409

Re: [SOLVED]Getting systemd to play audio at boot

You should define a stable card index order using an appropriate modprobe config to add to the above observation

Offline

#6 2016-06-15 06:57:59

theailer
Member
Registered: 2016-01-06
Posts: 19

Re: [SOLVED]Getting systemd to play audio at boot

I did define a modprobe config index according to V1del's post but even though I have After=sound.target in the [unit] section it tries to start before sound.target?

Offline

#7 2016-06-15 09:57:18

beroal
Member
From: Ukraine
Registered: 2009-06-07
Posts: 325
Website

Re: [SOLVED]Getting systemd to play audio at boot

If you define an order of loading of sound card modules, you add unnecessary delays. Instead, you can make a service that depends on a device, because a device can be a unit. Search for the device unit in the output of "systemctl".

theailer wrote:

I did define a modprobe config index according to V1del's post but even though I have After=sound.target in the [unit] section it tries to start before sound.target?

You can check if sound card device files are present with my script above. In my case, only Card 1 is present even if I add

[Unit]
After=sys-devices-pci0000:00-0000:00:05.0-sound-card0.device

It looks like a bug in SystemD. I found that

systemctl status sound.target

delays until all sound cards are present. I did not research further.


we are not condemned to write ugly code

Offline

#8 2016-06-15 16:17:48

theailer
Member
Registered: 2016-01-06
Posts: 19

Re: [SOLVED]Getting systemd to play audio at boot

Thanks for all the help.
Even after adding

After=sys-devices-platform-80860F28:00-byt\x2dmax98090-sound-card0.device

it still tries to run it before the sound card is loaded.
Running it with a sleep timer works,so that will do for the moment.
This is the output from your script:

total 0
crw-rw---- 1 root audio 116,  1 Jun 15 17:59 seq
crw-rw---- 1 root audio 116, 33 Jun 15 17:59 timer
total 0
drwxr-xr-x 2 root root       80 Jun 15 17:59 by-path
crw-rw---- 1 root audio 116,  2 Jun 15 17:59 controlC0
crw-rw---- 1 root audio 116,  5 Jun 15 17:59 controlC1
crw-rw---- 1 root audio 116,  8 Jun 15 17:59 hwC1D2
crw-rw---- 1 root audio 116,  4 Jun 15 17:59 pcmC0D0c
crw-rw---- 1 root audio 116,  3 Jun 15 17:59 pcmC0D0p
crw-rw---- 1 root audio 116,  6 Jun 15 17:59 pcmC1D3p
crw-rw---- 1 root audio 116,  7 Jun 15 17:59 pcmC1D7p
crw-rw---- 1 root audio 116,  1 Jun 15 17:59 seq
crw-rw---- 1 root audio 116, 33 Jun 15 17:59 timer

Making the suspend and resume service files for playing sound on lid open and close was much easier than trying to play a sound at boot smile

beroal wrote:

If you define an order of loading of sound card modules, you add unnecessary delays. Instead, you can make a service that depends on a device, because a device can be a unit. Search for the device unit in the output of "systemctl".

theailer wrote:

I did define a modprobe config index according to V1del's post but even though I have After=sound.target in the [unit] section it tries to start before sound.target?

You can check if sound card device files are present with my script above. In my case, only Card 1 is present even if I add

[Unit]
After=sys-devices-pci0000:00-0000:00:05.0-sound-card0.device

It looks like a bug in SystemD. I found that

systemctl status sound.target

delays until all sound cards are present. I did not research further.

Offline

#9 2016-06-15 21:03:42

beroal
Member
From: Ukraine
Registered: 2009-06-07
Posts: 325
Website

Re: [SOLVED]Getting systemd to play audio at boot

theailer wrote:

This is the output from your script:

total 0
crw-rw---- 1 root audio 116,  1 Jun 15 17:59 seq
crw-rw---- 1 root audio 116, 33 Jun 15 17:59 timer
total 0
drwxr-xr-x 2 root root       80 Jun 15 17:59 by-path
crw-rw---- 1 root audio 116,  2 Jun 15 17:59 controlC0
crw-rw---- 1 root audio 116,  5 Jun 15 17:59 controlC1
crw-rw---- 1 root audio 116,  8 Jun 15 17:59 hwC1D2
crw-rw---- 1 root audio 116,  4 Jun 15 17:59 pcmC0D0c
crw-rw---- 1 root audio 116,  3 Jun 15 17:59 pcmC0D0p
crw-rw---- 1 root audio 116,  6 Jun 15 17:59 pcmC1D3p
crw-rw---- 1 root audio 116,  7 Jun 15 17:59 pcmC1D7p
crw-rw---- 1 root audio 116,  1 Jun 15 17:59 seq
crw-rw---- 1 root audio 116, 33 Jun 15 17:59 timer

So there is no sound card device at all when the service starts.

theailer wrote:

Running it with a sleep timer works,so that will do for the moment.

I'm glad for you. :-) Another method that seems to work is

[Unit]
Requires=$D
After=$D

where $D is the sound card device unit.


we are not condemned to write ugly code

Offline

#10 2016-06-16 06:41:42

theailer
Member
Registered: 2016-01-06
Posts: 19

Re: [SOLVED]Getting systemd to play audio at boot

Awesome. This did it !

beroal wrote:
[Unit]
Requires=$D
After=$D

where $D is the sound card device unit.

Offline

#11 2021-10-03 23:12:08

Goldman
Member
Registered: 2021-01-29
Posts: 15

Re: [SOLVED]Getting systemd to play audio at boot

I have similar problem. My systemd service run smplayer and it output sound through device 0: ALC892 Analog but normally on desktop it is HDMI. How can I set systemd config to play sound through HDMI?

[Unit]
Description=bot
After=syslog.target network.target sound.target
[Service]
Environment=DISPLAY=:0
Type=simple
User=myname
Group=users
ExecStart=/opt/bot/bot.sh
[Install]
WantedBy=default.target

$ aplay -l

**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC892 Analog [ALC892 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 1: ALC892 Digital [ALC892 Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 10: HDMI 4 [HDMI 4]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Offline

#12 2021-10-04 05:31:57

beroal
Member
From: Ukraine
Registered: 2009-06-07
Posts: 325
Website

Re: [SOLVED]Getting systemd to play audio at boot

Goldman, I suggest that you start a new thread.


we are not condemned to write ugly code

Offline

#13 2021-10-04 09:48:14

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,409

Re: [SOLVED]Getting systemd to play audio at boot

Closing this old solved topic.

Offline

Board footer

Powered by FluxBB