python-sounddevice icon indicating copy to clipboard operation
python-sounddevice copied to clipboard

What is necessary (systemd target) to be able to run a program using sounddevice as fast as possible after Linux boot?

Open josephernest opened this issue 3 years ago • 4 comments
trafficstars

For an embedded computer, I'd like to start a program using sound (Python + sounddevice) as fast as possible after Linux boot.

I tested various systemd configs, the best is:

[Unit]
Description=Test
DefaultDependencies=false       # don't wait for anything, start as soon as possible :-)

[Service]
Type=simple
ExecStart=/usr/bin/python /root/foo.py
WorkingDirectory=/root/

but it sometimes fails (see below).

What other systemd option do you know allows to start a program using Python + sounddevice as fast as possible after boot?

Before=local-fs.target seems ok, but I think we can do better (accord to my tests with the systemd-analyze plot command).

Has anyone tested to optimize this? @mgeier maybe?

Dec 10 22:51:27 foo foo.sh[116]:   File "/usr/local/lib/python2.7/dist-packages/sounddevice.py", line 778, in __init__
Dec 10 22:51:27 foo foo.sh[116]:     extra_settings, samplerate)
Dec 10 22:51:27 foo foo.sh[116]:   File "/usr/local/lib/python2.7/dist-packages/sounddevice.py", line 2571, in _get_stream_parameters
Dec 10 22:51:27 foo foo.sh[116]:     info = query_devices(device)
Dec 10 22:51:27 foo foo.sh[116]:   File "/usr/local/lib/python2.7/dist-packages/sounddevice.py", line 569, in query_devices
Dec 10 22:51:27 foo foo.sh[116]:     raise PortAudioError('Error querying device {0}'.format(device))
Dec 10 22:51:27 foo foo.sh[116]: sounddevice.PortAudioError: Error querying device 0
Dec 10 22:51:27 foo systemd[1]: foo.service: Main process exited, code=exited, status=1/FAILURE
Dec 10 22:51:27 foo systemd[1]: foo.service: Failed with result 'exit-code'.

josephernest avatar Dec 14 '21 18:12 josephernest

Sorry, I have no idea.

The error message seems to originate from a call to the PortAudio function Pa_GetDeviceInfo(0) returning a NULL pointer, you can probably ask the PortAudio people (http://portaudio.com/contacts.html)?

mgeier avatar Dec 22 '21 11:12 mgeier

I'm running into the same issue. Tried running this as a user service, and still no dice. Any luck since December, @josephernest?

alexissantos avatar Mar 23 '22 20:03 alexissantos

This has been a while but has anyone been able to solve this?

alex-laycalvert avatar Jun 06 '23 11:06 alex-laycalvert

Actually got it working on my Raspberry Pi after finding this post.

I have After=sound.target in my systemd service file and had to specify a sound card in /etc/asound.conf which did not exist yet. After adding:

defaults.pcm.card 3

To /etc/asound.conf since I needed device 3 from cat /proc/asound/cards. After testing it by rebooting on a Raspberry Pi it works as expected now.

alex-laycalvert avatar Jun 06 '23 12:06 alex-laycalvert