I'm trying to run an audio visualization python script on my raspberry pi over SSH. The script uses pyaudio for the microphone input, which I understand uses pulseaudio under the hood. The script is also using an Adafruit library to control some NeoPixel LEDs, which must be run as root.
I've learned that by default pulseaudio doesn't run as root, but must run as a user. I verified this by creating a simple python script (throughput.py) to list the pyaudio devices. When I run without sudo I see 3 devices, including pulse:
pi@raspberrypi:~/audio-reactive-led-strip/python $ python3 throughput.py
Input Device id 1 - Antlion USB adapter: Audio (hw:1,0)
Input Device id 2 - pulse
Input Device id 6 - default
However, when I run it as sudo, I only see a single device and pulse is missing:
pi@raspberrypi:~/audio-reactive-led-strip/python $ sudo python3 throughput.py
Input Device id 1 - Antlion USB adapter: Audio (hw:1,0)
Although it's apparently not recommended, I've modified /etc/systemd/system/pulseaudio.service
following this post in order to run the pulseaudio service as system service. (It's not clear to me if this is the same as running it as root.):
[Unit]
Description=Pulseaudio sound server
After=avahi-daemon.service network.target
[Service]
ExecStart=/usr/bin/pulseaudio --system --disallow-exit --disallow-module-loading
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
With this in place, all 3 devices are listed when running as sudo (including pulse) and I'm able to run my visualization script locally on the pi.
However, if I SSH into the pi and run the exact same command (as sudo), it doesn't find the pulseaudio device - same as it was doing locally before I modified pulseaudio.service
. My guess is there's slightly different permissions for sudo locally vs over SSH, but I'm pretty green at linux and haven't had much luck finding anything to suggest that's true or how to work around it.
I don't know if it's relevant, but I'm using a terminal inside VS Code Remote-SSH for the SSH parts.
UPDATE 1
Based on an answer below, I tried using Putty with X11 forwarding. However, I get the following error when running start-pulseaudio-x11
:
pi@raspberrypi:~/audio-reactive-led-strip/python $ start-pulseaudio-x11
PuTTY X11 proxy: unable to connect to forwarded X server: Network error: Connection refused
xcb_connection_has_error() returned true
Failure: Module initialization failed
PuTTY X11 proxy: Unsupported authorisation protocol
xcb_connection_has_error() returned true
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused
Anyone know how to resolve this?
UPDATE 2
After copying the file .Xauthority
from /home/pi
to /root
see this post, I was able to successfully see all 3 adapters when I run sudo python3 throughput.py
from Putty with X11 forwarding enabled and Xming running!
However, it seems that this .Xauthority
file is only good for one SSH session. I think I can copy it each time using sudo
from Putty, but I haven't tried that yet. I'll have to read up on how the .Xauthority
file works to try to understand what's going on and would definitely appreciate any insight people have!
pi
and not asroot
. I'll try the changes you suggested nextUser=root
topulseaudio.service
,pulseaudio
is still running aspi
. What's interesting is that this isn't a problem when I run my python script locally.