Can't init sound card with Systemd unit on a RPI & Adafruit voice bonnet?

Hi,

I am trying to launch a python systemd service that uses ALSA sound when the Raspberry Pi boots.

Here is the Talkie.service file in /etc/systemd/system:

Description=bondz-client
# Requires=sys-devices-platform-soc-soc:sound-sound-card1-controlC1.device

[Service]
User=user
Group=user
Type=simple
ExecStart=/usr/bin/python3 /home/romaing/Documents/Talkie_basic_sound_test.py
WorkingDirectory=/home/romaing/Documents/

StandardOutput=append:/var/log/bondz.log
StandardError=append:/var/log/bondz.log

[Install]
WantedBy=sound.target

If I start the service manually with systemctl it works fine but upon boot I get this error in the logs (and systemd stops) Here’s the log file content :

2024-01-14 16:30:23,363 |Player| INFO:Device count =  0... 
2024-01-14 16:30:23,364 |Player| INFO:Playing file tests/myrecording.wav... 
2024-01-14 16:30:23,403 |Player| ERROR:Error playing tests/myrecording.wav 
Traceback (most recent call last):
  File "/home/romaing/Documents/Talkie_basic_sound_test.py", line 71, in playWaveFile
    playStream = audio.open(
  File "/usr/local/lib/python3.9/dist-packages/pyaudio/__init__.py", line 639, in open
    stream = PyAudio.Stream(self, *args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/pyaudio/__init__.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9996] Invalid output device (no default output device)

Here’s a simplified python code that loops a sound :

from Player import Player
import io
import wave
import logging
import traceback
import pyaudio

player = Player()
logger = logging.getLogger("Player")
audio = pyaudio.PyAudio()
logging.basicConfig(
    format="%(asctime)s |%(name)s| %(levelname)s:%(message)s", level=logging.INFO
)



def play():
    playWaveFile("tests/myrecording.wav", play)


def playWaveFile(filepath, callback):
    # info = audio.get_default_output_device_info()
    logger.info(f"Device count =  {audio.get_device_count()}... ")
    logger.info(f"Playing file {filepath}... ")
    try:
        wave_file = wave.open(filepath, "rb")

        playStream = audio.open(
            format=audio.get_format_from_width(wave_file.getsampwidth()),
            channels=wave_file.getnchannels(),
            rate=wave_file.getframerate(),
            output=True,
        )
        data = wave_file.readframes(1024)
        while data:
            playStream.write(data)
            data = wave_file.readframes(1024)
        # Cleanup
        playStream.stop_stream()
        playStream.close()
        logger.info(f"Playing {filepath} done.")
        callback()
    except Exception as e:
        logger.error(f"Error playing {filepath} ")
        traceback.print_exc()
        # self._statusManager.set_app_status(Status.ERROR)


play()

I guess the sound card isn’t loaded when the service start but I thought sound.target would solve it, which it does not… Being a beginner on this topic, any help would be greatly appreciated.

I am running on Raspbian 11 (bullseye).

Here is the output of aplay -l :

**** List of PLAYBACK Hardware Devices ****
card 0: vc4hdmi [vc4-hdmi], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 [bcm2835-i2s-wm8960-hifi wm8960-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Thank a lot for your help !

Hi there,
I don’t know if this may help but , a similar issue has been solved HERE
But from my view Is the sound hat showing up in the Log as the correct device?, seems
running it in headless mode (no graphical desktop) has been tough to get going but with perseverance you can check this too.

HTH
GL :slight_smile: PJ

Thanks a lot for your @PJ_Glasso.

My main concern is that if I am logged to the Rpi with SSH it works perfectly, but when I reboot there is the sound error, like if an ssh login would change something in the sound config that would allow the unit to run properly… I’ll check the first link in detail and get back to you.

:pray:

1 Like

Try playing a sound file or using a command like aplay to test the sound output directly from the command line. This can help you determine if the issue is with the Systemd unit or with the sound card itself. Furthermore, Do you knoe? capcut template collage to help your videos achieve viral status and effortlessly craft a captivating video.

aplay works.

Just to let you know, I tried to solve with the previous advice but it didn’t work. I did a clean reinstall with the latest os from scratch and now it works. I probably changed some things I shouldn’t have while trying to solve the problem, or maybe updating software solved the problem… We’ll never know, but I can live with it! :smiley:

Thank you for your help though