bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
689 stars 70 forks source link

Cannot load soundcard as root #71

Closed mpariente closed 4 years ago

mpariente commented 4 years ago

Hi Bastibe,

In some cases, I'd like to use sudo python to run my code (if I were to use keyboard for example.) however my code freezes at the import.

Here is the state when I press Ctrl + C

----> 1 import soundcard as sc

/usr/local/lib/python3.6/dist-packages/soundcard/__init__.py in <module>
      2 
      3 if sys.platform == 'linux':
----> 4     from soundcard.pulseaudio import *
      5 
      6     # also load main classes if building documentation:

/usr/local/lib/python3.6/dist-packages/soundcard/pulseaudio.py in <module>
    204     _pa_stream_set_read_callback = _pa.pa_stream_set_read_callback
    205 
--> 206 _pulse = _PulseAudio()
    207 atexit.register(_pulse._shutdown)
    208 

/usr/local/lib/python3.6/dist-packages/soundcard/pulseaudio.py in __init__(self)
     69 
     70         while self._pa_context_get_state(self.context) != _pa.PA_CONTEXT_READY:
---> 71             time.sleep(0.001)
     72 
     73     def _shutdown(self):

KeyboardInterrupt: 

Any idea why this would be? Thanks

bastibe commented 4 years ago

It seems that pulseaudio is not configured for your root account. Does the behavior change if you first open a root session with su, and then call python?

In general, however, it might be better to give your user access rights to the restricted resource you are using instead of just executing your program as root.

mpariente commented 4 years ago

Thanks for your answer.

It seems that pulseaudio is not configured for your root account. Does the behavior change if you first open a root session with su, and then call python?

It it the same in that case.

In general, however, it might be better to give your user access rights to the restricted resource you are using instead of just executing your program as root.

I also tried something like that but keyboard doesn't work in this case either. sudo su username -c "command" also works nice to have sudo rights and executing as a user. In this case, soundcard would load but keyboard would still disagree.

How can I configure pulseaudio for the root account then?

bastibe commented 4 years ago

How can I configure pulseaudio for the root account then?

I have no idea, to be honest. As far as I know, Pulse is a local server that your app connects to on a socket. Perhaps you merely need to start the pulseaudio server as root? Or perhaps you need to make the pulseaudio socket available to root via some environment variable?

mpariente commented 4 years ago

Thanks, I'll let you know if I find anything !

brunovollmer commented 4 years ago

Hey guys,

I have a similar problem. But I'm running the programm without root or anything. Any idea what could be wrong?

Output ps aux | grep pulseaudio: bruno 1944 0.5 0.1 2072620 19824 ? S<sl 14:13 0:05 /usr/bin/pulseaudio --daemonize=no --log-target=journal bruno 5493 0.0 0.0 9376 2600 pts/1 S+ 14:30 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox pulseaudio

Output pulseaudio --check; echo $?: 1