smart-mirror icon indicating copy to clipboard operation
smart-mirror copied to clipboard

Pulseaudio crashes constantly

Open geman220 opened this issue 10 years ago • 12 comments

As far as I can tell PulseAudio is crashing causing audio playback to fail on the mirror. Opening Pauvcontrol and muting then unmuting the output sometimes will enable playback for a short period (5-10 seconds). You can keep muting / unmuting to resume playback for short bursts of time. The same behavior seems to happen just by turning the volume up/down. It's almost as if changing any setting "wakes up" pulse. Because of this I am fairly certain the issue exists within PulseAudio. Unfortunately I am unable to get my microphone to work with the mirror if pulseaudio is not running.

How to replicate:

  1. pulseaudio --kill to make sure pulseaudio isn't running
  2. npm start dev
  3. Open developer console
  4. Say "Smart Mirror" (The mirror does recognize it's wake up voice command)
  5. A new line comes up in console with an error saying error: "audio-capture"
  6. In the developer console type annyang.simulate("show me how to tie a bow tie");
  7. YouTube will load and the video will play without freezing / audio issues
  8. Close out the mirror

Next:

  1. pulseaudio --start
  2. npm start dev
  3. Say "Smart Mirror show me how to tie a bow tie"
  4. The voice command should work and YouTube should load the first frame
  5. However, the video either won't play or the audio will cut out and the video will freeze after 10-20 seconds or so. It won't complete. This also applies to Soundcloud and can be tested via the same methods.

Running pulseaudio --log-level=debug I noticed the following pop up whenever the audio drops out:

D: [alsa-sink-bcm2835 ALSA] protocol-native.c: Requesting rewind due to end of underrun.
D: [alsa-sink-bcm2835 ALSA] alsa-sink.c: Requested to rewind 656 bytes.
D: [alsa-sink-bcm2835 ALSA] alsa-sink.c: Limited to 656 bytes.
D: [alsa-sink-bcm2835 ALSA] alsa-sink.c: before: 164
D: [alsa-sink-bcm2835 ALSA] alsa-sink.c: after: 164
D: [alsa-sink-bcm2835 ALSA] alsa-sink.c: Rewound 656 bytes.
D: [alsa-sink-bcm2835 ALSA] sink.c: Processing rewind...
D: [alsa-sink-bcm2835 ALSA] sink-input.c: Have to rewind 656 bytes on render memblockq.
D: [alsa-sink-bcm2835 ALSA] sink-input.c: Have to rewind 656 bytes on render memblockq.
D: [alsa-sink-bcm2835 ALSA] source.c: Processing rewind...

Update 1:

After more fiddling, I am not sure what combination of things has made the biggest impact but here is where I'm at.

  1. I am on the dev branch and I ran /scripts/pi-install.sh (no noticable increase in performance)
  2. I did a slight overclock on my pi (no noticable increase in performance)
  3. I did everything outlined in this post (performance increased see notes below)
  4. I did other random changes to daemon.conf and default.pa but I won't know them for sure until I compare my current implementation to defaults.

I am seeing far less of the errors from pulseaudio in regard to D: [alsa-sink-bcm2835 ALSA] sink.c: Processing rewind... Sometimes I can play a whole YouTube or SoundCloud, it's hit or miss. In general I am getting longer play times before a crash. Once it does crash it will not recover until I close the mirror and re-launch. I am getting the "rainbow square"in the top right corner of the mirror however. I am using the original 2.5A wall wart but I may buy a new one just to be sure.

Update 2:

I disabled and removed bluetooth from my pi. I'm sure it wasn't using much power, but it was using SOME power so it's gone. I've made more completely arbitrary changes in my daemon.conf, (specifically increasing default-fragments = and default-fragment-size-msec = and I'm now up to 15 minutes (and counting) of continual playback in Soundcloud. My advice is to play around with those values until you get good playback. Like I said I've also made more changes but I'll have to check them all to give a more detailed explanation. Looks like the issue is 100% related to Pulseaudio, but more specifically it's resource usage. If it's possible, my suggestion would be to move away from Pulseaudio.

Update 3:

Wanted to update the changes I made to the best of my memory.

  • sudo nano /etc/pulse/default.pa : I changed load-module module-udev-detect to load-module module-udev-detect tsched=0
  • sudo nano /etc/pulse/daemon.conf : I un-commented and changed the following:
high-priority = yes
nice-level = -11
realtime-scheduling = yes
realtime-priority = 9
exit-idle-time = -1
resample-method = trivial
rlimit-nice = 31
rlimit-rtprio = 9
default-sample-format = s16le
default-sample-rate = 44100
default-sample-channels = 2
default-fragments = 10
default-fragment-size-msec = 7
  • sudo nano /etc/security/limits.conf I added the following lines to the end:
pi - nice -11
pi - rtprio 9

I believe that's all the changes I made, but it was a long process of trial and error so I can't guarantee those were the only changes I made, but hopefully it helps get someone at least 90% of the way there.

geman220 avatar Jun 25 '16 22:06 geman220

Thank you @geman220 for this super well documented issue! Seems to share the same root cause as #287. When streaming recorded audio and simultaneously playing, PulseAudio (more specifically the ALSA sync) hangs on a rewind.

I'll be looking at two potential solutions here: fixing the PulseAudio issue, and removing PulseAudio as a dependency for the smart mirror.

evancohen avatar Jun 26 '16 16:06 evancohen

No problem, happy to help however I can. Hopefully if someone else has this issue they can at least try this workaround. I appreciate your help!

geman220 avatar Jun 26 '16 19:06 geman220

After some investigation I have found that it is actually fairly straightforward to get this working without PulseAudio (which should solve this issue).

First, you'll want to determine your playback and recording devices:

$ aplay -l
 **** List of PLAYBACK Hardware Devices ****
 card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
   Subdevices: 8/8
   Subdevice #0: subdevice #0
   Subdevice #1: subdevice #1
   Subdevice #2: subdevice #2
   Subdevice #3: subdevice #3
   Subdevice #4: subdevice #4
   Subdevice #5: subdevice #5
   Subdevice #6: subdevice #6
   Subdevice #7: subdevice #7
 card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
   Subdevices: 1/1
   Subdevice #0: subdevice #0

Here the playback device is card 0, device 0, or hw:0,0 (hw:0,1 is HDMI audio out).

Then you'll want to determine the recording device:

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Camera [Vimicro USB2.0 UVC Camera], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Here the recording device is card 1, device 0, or hw1:0.

UPDATE 1: I've modified this configuration file to work for every microphone I could test with, it should be general enough to work for everyone.

And finally you'll want to use these to fill in your ~/.asoundrc file:

#asym fun start here. we define one pcm device called "pluged"
pcm.pluged {
    type plug
    #this is your output device
    slave.pcm "hw:0,1"
}

#one called "dsnooped" for capturing
pcm.dsnooped {
    ipc_key 1027
    type dsnoop
    #this is your input device
    slave.pcm "hw:1,0"
}

#and this is the real magic
pcm.asymed {
    type asym
    playback.pcm "pluged"
    capture.pcm "dsnooped"
}

#a quick plug plugin for above device to do the converting magic
pcm.pasymed {
    type plug
    slave.pcm "asymed"
}

#a ctl device to keep xmms happy
ctl.pasymed {
    type hw
    card 0
}

#for aoss:
pcm.dsp0 {
    type plug
    slave.pcm "asymed"
}

ctl.mixer0 {
    type hw
    card 0
}

pcm.!default {
    type plug
    slave.pcm "asymed"
}

Reboot!

What is this madness? dsnoop allows several applications to record from the same device simultaneously. This is essentially what what PulseAudio was doing for us before, only it's a huge dependency and was crashing. Luckily dsnoop comes as a part of ALSA on recent distributions of the Raspbian so this is an easy fix.

Caveats

  • Hopefully none, anymore 😄

tl;dr Audio on Linux is notoriously complex

evancohen avatar Jul 02 '16 03:07 evancohen

Still doesn't work for me.

7h30n3 avatar Jul 03 '16 18:07 7h30n3

@7h30n3 not a lot to go on there... Can you please include the make and model of your webcam, the output of arecord -L, and any terminal errors you get after activating the keyword?

evancohen avatar Jul 03 '16 18:07 evancohen

My keyword is not recognizer :( i tried with annyang.service() in console and microphone start to work. But any answer with keyword

Keopss avatar Jul 07 '16 11:07 Keopss

After following the "remove pulseaudio" instructions, no voice is detected at all for me. The first thing I noticed is the my aplay -l looked slightly off: pi@smartmirror:~ $ aplay -l **** List of PLAYBACK Hardware Devices **** card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA] Subdevices: 7/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI] Subdevices: 1/1 Subdevice #0: subdevice #0 ` Only 7/8...weird but w/e. Prior to reboot the arecord -l looked similar with 0/1 as opposed to 1/1. After reboot it shows 1/1.

pi@smartmirror:~ $ arecord -l **** List of CAPTURE Hardware Devices **** card 1: Webcam [FULL HD 1080P Webcam], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0

First thing I attempted was: # git pull # npm update # npm install # sudo reboot

No change.

I checked npm start dev and saw no recognition of voice. 3 errors related to traffic which I haven't implemented but nothing else.

I checked audio via browser and OMG it is horrendous. I will try to troubleshoot this evening because I am running out of time this A.M.....

GobleSt avatar Jul 08 '16 10:07 GobleSt

@evancohen I finally found an easy solution for voice recognition (Hotword Detection always worked) and I'm still using PulseAudio. I just removed the "Volume Control (ALSA)" from the taskbar as described here:

https://www.raspberrypi.org/blog/another-update-raspbian/

The use of PulseAudio has one other effect that may cause issues for a small number of users – specifically, if you are already using PulseAudio for anything other than interfacing with Bluetooth devices. This plugin will automatically stop the PulseAudio service whenever a standard ALSA device is selected. If you are using PulseAudio for your own purposes, it would be best to remove the volumealsa plugin from the taskbar completely to avoid this – just right-click anywhere on the taskbar, choose ‘Add/Remove Panel Items’, and remove the “Volume Control (ALSA)” item from the list.

Maybe you can verify that.

7h30n3 avatar Jul 09 '16 18:07 7h30n3

I reinstalled pulseaudio etc and removed the volume control as per above....commands are responsive again but playback (soundcloud, youtube) still has issues. I haven't been able to work on this because working longer hours lately....will try to devote sometime soon.

GobleSt avatar Jul 09 '16 22:07 GobleSt

I have done this steps:

1º - sudo apt-get install python-pyaudio python3-pyaudio sox 2º - Configured ~/.asoundrc 3º - Setup Audio Sound Output sudo modprobe snd_bcm2835 sudo amixer cset numid=3 1

4º If i follow steps of "Update 3" i get sound some seconds and later silence a any sound is played

Keyword works perfectly. but no sound :(

Keopss avatar Jul 12 '16 09:07 Keopss

Hey all, so I had this same problem and my setup is as follows:

  • RP3
  • Insignia USB Mic
  • Rasbian Jessie

Symptom - USB MIC does not work, tried all troubleshooting steps in guide and in bugs but no luck

I found a post on the RP site that had similar symptoms so I changed the following and it seemed to fix all of my issues

"where there is a file called aliases.conf in /lib/modprobe.d which contains the line options snd-usb-audio index=-2 and overrides the etc/modprobe.d/ files, so you need to change that one.

Comment out with a # the line “options snd-usb-audio index=-2”"

In /usr/share/alsa/alsa.conf I un-commented “load card-specific configuration files (on request)”

I'm not a programmer or dev at all, but your mirror inspired me to code. Heres a link and credit to the person who resolved this issue for me. Thanks all!

https://www.raspberrypi.org/forums/viewtopic.php?p=314611

franklinam1 avatar Jul 22 '16 22:07 franklinam1

Closing this out because this is no longer an issue :)

evancohen avatar Sep 22 '16 19:09 evancohen