[SOLVED] Rpi Zero + Speaker pHat + python = freeze and bugs

February 25, 2024, 18:00

bozarre

Hello ! I'm making a project with the Raspberry Pi Zero and the Pimoroni Speaker pHat. I added a push button and i want the speaker to play a sound randomly in a folder when the button is pressed. I'm doing the logic in python and everything works but I have a weird bug when playing the audio. In the command-line aplay ./redfr/Bonjour.wav works perfectly fine. But in python if I do os.system("aplay ./redfr/Bonjour.wav &") it displays Using device: speaker-phat and freeze for many seconds before playing the sound, and it freezes again for few seconds. (I tried without the & and same). So I tried using playsound('./redfr/Bonjour.wav') but it gives me an error i don´t understand (I'll post it after because it's big) I tried using pygame:
pygame.init()
soundfile = pygame.mixer.Sound('./redfr/Bonjour.wav')
soundfile.play()
This way it takes a very long time for the script to start and then I get this
pygame 2.5.2 (SDL 2.0.14, Python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Using device: speaker-phat
and it plays the sound, and if I press the button it plays the sound instantly. great right ? well not really because right after the sound played the program crashed saying Segmentation fault :/ Does anyone has an idea of what's wrong here ?

bozarre

Here's the full button.py script:
python
#import time
import RPi.GPIO as GPIO
#import playsound
import os
import random 
#import subprocess
import pygame

# button stuff
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
buttonPin = 3
GPIO.setup(buttonPin, GPIO.IN, GPIO.PUD_UP)

#listng sounds in folder
path="./redfr/"
files=os.listdir(path)

#using os.system
os.system("aplay ./redfr/Bonjour.wav &")

#using playsound
playsound('./redfr/Bonjour.wav')

#using pygame
pygame.init()
soundfile = pygame.mixer.Sound('./redfr/Bonjour.wav')
soundfile.play()

def button_callback(channel):
    print("Button was pushed!")
    randomfile = random.choice(files)
    file = path + randomfile

    #using os.system
    os.system('aplay ' + file + ' &')

    #using playsound
    playsound(file)

    #using pygame
    soundfile = pygame.mixer.Sound(file)
    soundfile.play()

GPIO.add_event_detect(buttonPin,GPIO.FALLING,callback=button_callback) # Setup event on button rising edge

message = input("Press enter to quit\n\n") # Run until someone presses enter

GPIO.cleanup() # Clean up

bozarre

And this is the error I get with playsound
playsound is relying on another python subprocess. Please use pip install pygobject if you want playsound to run more efficiently.
Traceback (most recent call last):
  File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 261, in <module>
    playsound(argv[1])
  File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 162, in _playsoundNix
    import gi
  File "/home/bozarre/.local/lib/python3.9/site-packages/gi/__init__.py", line 40, in <module>
    from . import _gi
ImportError: libgirepository-1.0.so.1: cannot open shared object file: No such file or directory
Traceback (most recent call last):
  File "/home/bozarre/button.py", line 23, in <module>
    playsound('./redfr/Bonjour.wav')
  File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 254, in <lambda>
    playsound = lambda sound, block = True: _playsoundAnotherPython('/usr/bin/python3', sound, block, macOS = False)
  File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 229, in _playsoundAnotherPython
    t.join()
  File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 218, in join
    raise self.exc
  File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 211, in run
    self.ret = self._target(self._args, *self._kwargs)
  File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 226, in <lambda>
    t = PropogatingThread(target = lambda: check_call([otherPython, playsoundPath, _handlePathOSX(sound) if macOS else sound]))
  File "/usr/lib/python3.9/subprocess.py", line 373, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/python3', '/home/bozarre/.local/lib/python3.9/site-packages/playsound.py', './redfr/Bonjour.wav']' returned non-zero exit status 1.

bozarre

Ah and important detail, once os.system is used to play the sound and causes the delay then i also have the delay with the command aplay ./redfr/Bonjour.wav. So it seems the sound device/driver becomes altered, only a reboot puts it back in order. It's very odd

bozarre

Ok i found the issue, my button was on the wrong pin (SCL) causing issues

bozarre

I moved it to another pin and the issue is gone