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 :)

Ben
  • 115
  • 1
  • 2
  • 10

4 Answers4

5

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.

jdonald
  • 2,972
  • 14
  • 39
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 ).

eftshift0
  • 800
  • 1
  • 7
  • 13
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

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

Shreyas Murali
  • 2,446
  • 1
  • 16
  • 23