0

I2S Amp Breakout - ALSA snd_pcm_recover underrun occurred er
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

I2S Amp Breakout - ALSA snd_pcm_recover underrun occurred er

by subaru_sti on Tue Feb 09, 2021 1:51 pm

Hi there.

I've purchased these two products:
Adafruit I2S 3W Class D Amplifier Breakout - MAX98357A (https://www.adafruit.com/product/3006)
Speaker - 3" Diameter - 4 Ohm 3 Watt (https://www.adafruit.com/product/1314)

I have both a Raspberry Pi 4 Model B (4 GB) and a Raspberry Pi 3 Model B. The amp breakout and speaker are connected to the Pi 4. On the Pi 3, I'm just using headphones with the headphone jack. Both are running Raspberry Pi OS (32-bit) Lite, the August 2020 version.

I'm using pygame to play audio in my python program. Here is a sample program:

Code: Select all | TOGGLE FULL SIZE
from pygame import mixer

mixer.init()
mixer.music.set_volume(0.95)

audio_file = "audio/speech/cf970157f76e4e58cedfb7211b2c1314.ogg"
   
try:
    while True:
        # Play the audio
        mixer.music.load(audio_file)
        mixer.music.play()

        input("Press Enter to play the audio again . . .")
except (KeyboardInterrupt):
    pass
except Exception as e:
    print (e)

# Clean up
mixer.quit()

When I run this on the Pi 4 with the I2S amp breakout and speaker, I get the following error:

ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred

The error is random. Sometimes it happens right away, sometimes it happens after a few tries and then again later. The sound also seems quieter than I would expect. I've adjusted the volume in alsamixer to 80 but it still seems quiet. On the Pi 3, with the same code but using headphones, I never get the error. I've done a lot of searching and it seems like the error means the code is not sending samples fast enough so the hardware has run out of samples before more have been written.

To setup the I2S amp breakout, I've followed the instructions here, https://learn.adafruit.com/adafruit-max ... y-pi-usage but I've done the manual setup under "Detailed Install".

I've tried updating the buffer_size and rate in the asound.conf file. I've tried setting the same values when initializing the pygame mixer, example: "mixer.init(frequency=24000, channels=2, buffer=8192)" but that doesn't seem to help.

I tried adding something like this to "/home/pi/.asoundrc" which made the speaker much louder but then there was a bunch of popping and eventually my Pi crashed:

pcm.!default {
type asym
capture.pcm "mic"
playback.pcm "speaker"
}
pcm.mic {
type plug
slave {
pcm "hw:1,0"
}
}
pcm.speaker {
type plug
slave {
pcm "hw:0,0"
}
}

Any suggestions?

I don't quite understand what asound.conf all does. Could it be some incompatibility with that and the new Raspberry Pi OS? The instructions at https://learn.adafruit.com/adafruit-max ... y-pi-usage seem like they are from a few years ago so perhaps they are outdated?

Appreciate your help with this!

subaru_sti
 
Posts: 31
Joined: Sun Jul 21, 2013 12:47 pm

Re: I2S Amp Breakout - ALSA snd_pcm_recover underrun occurre

by mikeysklar on Wed Feb 10, 2021 6:17 pm

Pi OS has really overhauled it's audio infrastructure since December. I would actually recommend getting off the August release and getting the January + updates installed and starting from there. A lot fo the poppy audio kernel stuff has been resolved. Our FAQ kind of mentions the audio quality audios you pointed out, but I think the update will resolve those.

https://learn.adafruit.com/adafruit-i2s ... -audio-faq

https://www.raspberrypi.org/blog/new-ra ... mber-2020/

mikeysklar
 
Posts: 7777
Joined: Mon Aug 01, 2016 8:10 pm

Re: I2S Amp Breakout - ALSA snd_pcm_recover underrun occurre

by subaru_sti on Fri Feb 12, 2021 7:48 pm

I will give that a try this weekend and report back. Thank you!

subaru_sti
 
Posts: 31
Joined: Sun Jul 21, 2013 12:47 pm

Re: I2S Amp Breakout - ALSA snd_pcm_recover underrun occurre

by subaru_sti on Tue Mar 02, 2021 12:14 am

Hello. Sorry for the delay - I finally got the new January 11th 2021 Raspberry Pi OS Lite installed.

I followed the instructions again here (https://learn.adafruit.com/adafruit-max ... y-pi-usage) under Detailed Install. On the new OS, I only had to "Create asound.conf file" and "Add Device Tree Overlay". After a reboot, it didn't work. Running "alsamixer" displayed the error "cannot open mixer: No such file or directory". Running "speaker-test -c2" produced this:

Playback device is default
Stream parameters are 48000Hz, S16_LE, 2 channels
Using 16 octaves of pink noise
Playback open error: -2,No such file or directory


I found out that "/home/pi/.asoundrc" contains the following automatically now:

Code: Select all | TOGGLE FULL SIZE
pcm.!default {
  type asym
  playback.pcm {
    type plug
    slave.pcm "output"
  }
  capture.pcm {
    type plug
    slave.pcm "input"
  }
}

pcm.output {
  type hw
  card 1
}

ctl.!default {
  type hw
  card 1
}

Deleting what's in that file makes the sound work. Should I be doing that or changing something in that file?

Unfortunately, running the same python code from my original post still produces this error:

ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred

Any ideas? Thanks!

subaru_sti
 
Posts: 31
Joined: Sun Jul 21, 2013 12:47 pm

Re: I2S Amp Breakout - ALSA snd_pcm_recover underrun occurre

by mikeysklar on Tue Mar 02, 2021 5:00 pm

Go ahead and remove the .asoundrc file.

The pcm.c error looks to be resolved by adjusting a buffer size.

/etc/pulse/daemon.conf:


Code: Select all | TOGGLE FULL SIZE
default-fragments = 5
default-fragment-size-msec = 2


https://bbs.archlinux.org/viewtopic.php?id=185736

mikeysklar
 
Posts: 7777
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.