Skip to content
Advertisement

Can’t hear sound when audio is being played in Pyglet (Raspberry Pi 4B, Raspbian)

I am running the latest version of Raspbian on the Raspberry Pi with Python 2.7 and 3.7 installed. For a project, I need to implement some spatial/binaural audio file playback capabilities in Python, and Pyglet (version 1.5.6) (https://pypi.org/project/pyglet/, https://github.com/pyglet/pyglet) seems to be the simplest option with least dependencies. However, I am not being able to hear audio output through the headphones. Here’s what I did:

  1. Installed pyglet dependencies: sudo apt-get install ffmpeg libopenal1 libopenal-dev python3-pil.imagetk python3-pil python3-matplotlib python3-scipy gstreamer1.0-alsa gstreamer1.0-python3-plugin-loader (From previous programs, the system also had cmake, python3-opencv libopencv-dev libusb-1.0-0-dev, libboost-program-options-dev and python3-numpy installed)
  2. I then installed pyglet as per given in the guide: sudo pip3 install --upgrade pyglet. The installation proceeded without any errors.
  3. I know that by default often, Raspberry Pi spits audio out through the HDMI port instead of A/V port. So I went to raspi-config using sudo raspi-config, went to Advanced Options -> Audio. Selected the proper option (in my case 1 Headphones). I checked if audio was playing fine through my headphones by playing couple of youtube videos and audio files on the Raspberry Pi itself.
  4. I then checked if pyglet was working by importing pyglet with import pyglet in a python shell, it worked fine without any warnings or errors.
  5. I launched the example program provided here: https://github.com/pyglet/pyglet/tree/master/examples/soundspace. To do so, I downloaded the repo https://github.com/pyglet/pyglet and moved to the proper directory (via cd command) and wrote: sudo python3 soundspace.py. For a brief overview, the program contains 4 instruments that can be dragged across the room with a mouse. In doing so, the audio that you hear from each instrument changes depending on position and orientation of those instruments as well as your virtual position inside the mini game. The program executes without any error being shown in the terminal, but I don’t hear any sound. When I try to move the objects, the program freezes but no error is shown in the terminal.
  6. Since I was least bothered about the complex program, I went ahead to create my own simple program that will play a wav file (sourced from the Pyglet’s developer repo itself before anyone says the wav file is incompatible https://github.com/pyglet/pyglet/tree/master/examples/soundspace), reference from: https://pyglet.readthedocs.io/en/latest/programming_guide/media.html . Here’s the code snippet:
import pyglet
pyglet.options['search_local_libs'] = True
pyglet.options['audio'] = ('openal')
source = pyglet.media.load('Bass.wav', streaming=False)
source.play()

According to the documentation, if I am not wrong, this is supposed to play the wav file being fed to the source. The program seems to run without any errors shown in the terminal. But I do not hear any audio output from the program.

  1. I also messed around a bit with pyglet.media.Player() but same observation persits: no error in terminal but no audio as well.

Any suggestions on how to fix the issue? Note that I would prefer to use Pyglet as this is the only spatial/binaural/positional audio Python package that has managed to work almost well without breaking my OS, so it would be best if alternative suggestions be a bit inline with this package and not entirely something different.

Advertisement

Answer

https://github.com/NicklasTegner/PyAL Since I was out of time. I tried experimenting with other OpenAL Python libraries and this one works fine.

Edit: The actual problem was not with Pyglet but with PulseAudio, which clashes with ALSA. I solved the problem by connecting an external USB audio card and audio was working properly (with HRTFs and all).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement