3

Kind of at a loss here. Currently the below code doesn't play a wav file as it should. I added some print statements and discovered that the while loop never finishes.

I'm using a Raspberry Pi 4. I installed PyAudio using...

sudo apt-get install libasound-dev
sudo apt-get install portaudio19-dec
sudo apt-get install python3-pyaudio

The strange thing is the recording works. In addition, using aplay soundfile.wav also works.

def play(fileName):
    CHUNK = 1024

    wf = wave.open(fileName, 'rb')

    p = pyaudio.PyAudio()

    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=2,
                    rate=wf.getframerate(),
                    output=True,
                    output_device_index=0)
  
    data = wf.readframes(CHUNK)

    print("starting loop")

    while len(data) > 0: # also tried: while data != ''
        stream.write(data)
        data = wf.readframes(CHUNK)

    print("finished loop")

    stream.stop_stream()
    stream.close()

    p.terminate()
        
play("soundfile.wav")

The code above results in the following messages.

Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.pcm.dsnoop.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.pcm.dsnoop.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5036:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM bluealsa
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM input
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 924
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock

starting loop

If anyone has any insight into what might be the issue I would appreciate it.

ptan9o
  • 174
  • 9
  • Did you ever get this sorted out? – Paul Wieland Mar 09 '21 at 19:09
  • 1
    No. For this project, I gave up on using PyAudio and just used ALSA. What's strange is that I performed this exact same example on the Raspberry Pi 3 and it worked fine. Perhaps it has something to do with the Pi 4. – ptan9o Mar 10 '21 at 20:32
  • Im in a similar boat, but the issue is that the project I'm trying to get working was written on pyaudio. I haven't been able to figure out why it won't work with the pi4. – Paul Wieland Mar 10 '21 at 20:47
  • I can relate to experiencing much frustration over this issue. It sounds like this won't be a solution for you, but the hack I used to simply play and audio file was `os.system("aplay myFile.wav")` – ptan9o Mar 10 '21 at 20:54

0 Answers0