3

I am running a pygame window on a raspberry pi. The raspberry pi is connected to an lcd through gpio (not hdmi). My pygame script does not contain any mention of audio, only some basic rectangle drawing and text drawing.

However when I run it, it outputs this error:

ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory

I have never heard of the ALSA lib so this error message confuses me. I have checked to see whether i have ALSA lib (sudo apt-get install alsa-utils ) and it said alsa-utils is already the newest version (1.1.3-1) so thats not the problem.

I don't care that sound doesn't work on pygame. The application works perfectly just this annoying 6 line error message shows. I just want to get rid of the messages as it fills up my console everytime i run it.

Thanks :)

7
  • Is the user who runs the pygame script in the group "audio"? You can check with groups [USERNAME].
    – Fabian
    Commented May 11, 2018 at 22:05
  • @Fabian yes, so I have removed 'pi' from group and rebooting, hasn't changed anything
    – Ben
    Commented May 12, 2018 at 13:50
  • Well, in case you run the script by a different user then I'd suggest to add this user to group "audio" :-) No need to remove "pi" from this group.
    – Fabian
    Commented May 12, 2018 at 13:58
  • Could you prefix your command with SDL_AUDIODRIVER=dsp and see what happens?
    – jdonald
    Commented May 12, 2018 at 17:38
  • 1
    Good to hear... just wanted to make sure I was on the right track. You could embed this into your Python program via import os then os.environ["SDL_AUDIODRIVER"] = "dsp" prior to import pygame. If this works as well I can write up a detailed answer with an explanation, and hopefully receive a bounty.
    – jdonald
    Commented May 13, 2018 at 22:58

4 Answers 4

5
+50

Here's a writeup for the hack we came up with in the comment stream:

Most fortunately, we're able to take advantage of the limited scope of this request:

I don't care that sound doesn't work on pygame. The application works perfectly just this annoying 6 line error message shows. I just want to get rid of the messages

We don't know why exactly ALSA is spouting these errors, but all we need to do is avoid ALSA. Pygame is built on top of SDL, so we can use all the same video and audio controls.

The SDL default sound driver is alsa but the Python program can select something else by starting with:

import os
os.environ['SDL_AUDIODRIVER'] = 'dsp'

Note I suggested putting both lines at the very top even prior to import pygame to be on the safe side. If desired to make the code prettier: I'm not sure if the os.environ statement can be moved down lower just prior to pygame.init(), but you can try and find out.

dsp refers to the Linux Open Sound System (OSS) where sound is sent via I/O writes to /dev/dsp. As there's no immediate desire to play sound we could have tested any setting other than alsa. dsp was just the most vanilla one that came to mind.

In the future if you wish to support sound in your Pygames via PulseAudio or one of the other drivers--and via the headphone jack or any other hardware--this understanding of SDL could come in handy.

4
  • Thanks, alsa seems a little broken here so avoiding it is probably the best option
    – Ben
    Commented May 14, 2018 at 18:52
  • @jdonald: your solution did not work at my case, but instead I tried: os.environ['SDL_AUDIODRIVER'] = 'alsa'and this worked. I am so confused. I am using Raspbian Stretch. Commented Jun 14, 2019 at 11:56
  • That's strange. I can't explain that considering ALSA is already the default, but good to see this answer eventually came in handy for someone other than the OP.
    – jdonald
    Commented Jul 20, 2019 at 22:17
  • Idea: Perhaps you got here due to a search error messages and you're not on a Pi, but instead, are in some container environment where you don't even have /dev/dsp. In that case, try file - which the libsdl FAQ says writes audio to a file. That ought to work on an audio-less system. (Can someone verify this?)
    – davidbak
    Commented Oct 6, 2022 at 2:18
1

Well... even if your script has no mention of any sound stuff, pygame could very well include sound-related stuff inside and that's why you are seeing those messages. If you want to avoid those messages from showing up on the terminal, you can just redirect output so that you don't get it at all: python my-script.py &> /dev/null and voila! No output on the terminal. Could also try with 2> instead of &> if you want to only avoid error messages or even play a little bit with pipes if you want to avoid ALSA messages and see everything else ( blahblah 2>&1 | grep -v ALSA ).

6
  • But this will suppress also all other messages, warnings and errors on the console.
    – Ingo
    Commented May 11, 2018 at 20:40
  • Then try the last one I said ( 2>&1 | grep -v ALSA ), that will get rid of messages that say ALSA.
    – eftshift0
    Commented May 11, 2018 at 20:41
  • Ah... right, have overseen that.
    – Ingo
    Commented May 11, 2018 at 20:47
  • You could add 2>> alsa_error.log as another option.
    – Fabian
    Commented May 11, 2018 at 20:47
  • Yeah that's one fix, however typing that in is going to take a long time and remembering that statement so i can't accept it as a viable answer
    – Ben
    Commented May 12, 2018 at 13:51
0

Approach 1: identify and possibly fix the issue

Clarifying some of the below questions could help narrow down the issue:

  • Running raspbian or some other OS ? (possible there might be something driver specific in there)

  • Are there any related errors in dmesg ?

  • Do you have a monitor w/o speakers connected in addition to the LCD on the GPIO ? According to this, audio might (sometimes) get disabled when connected to HDMI monitor. If so that might explain the error. There are a couple of solutions suggested there (I am not including them here as they too long to paste here)

  • Similar error message here, one person successfully fixed it using sudo apt-get update sudo apt-get install alsa-utils sudo modprobe snd_bcm2835 sudo apt-get install avahi-utils sudo reboot


Approach 2: avoid the problem

According to this initializing pygame like so import pygame from pygame.locals import * ... pygame.init()

tries to initialize all modules, which would include audio that fails here. Apparently it is not required to do so and you could initialize only the modules you are using and skip audio to avoid the problem.

pygame.font.init()

The list of available modules is here

HTH

1
  • none of the parts from approach one or two worked sorry ;(
    – Ben
    Commented May 14, 2018 at 16:14
0

So I just found a workaround if you want sound to work as well

Instead of:

import os
os.environ['SDL_AUDIODRIVER'] = 'dsp'

Just do:

import os

Not sure at all why this works, but it did for me. No more "ALSA" errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.