spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
982 stars 145 forks source link

Issue on sample rate in Ubuntu 20.04, no audio #353

Open luisarandas opened 2 years ago

luisarandas commented 2 years ago

Hello all. I am trying to listen to some numpy arrays generated from a neural net, and even following the steps of the recommended installation I always get the same error. Also, I noticed a problem when using the conda environment where I installed the libraries, is that I cannot see the "output" or "default" ALSA choice in soundcard.query_devices().

Currently it prints this error:

audio shape (31744,)
<  0 HDA NVidia: HDMI 0 (hw:0,3), ALSA (0 in, 8 out)
   1 HDA NVidia: HDMI 1 (hw:0,7), ALSA (0 in, 2 out)
   2 HDA NVidia: HDMI 2 (hw:0,8), ALSA (0 in, 8 out)
   3 HDA NVidia: HDMI 3 (hw:0,9), ALSA (0 in, 8 out)
   4 HDA NVidia: HDMI 4 (hw:0,10), ALSA (0 in, 8 out)
   5 HDA NVidia: HDMI 5 (hw:0,11), ALSA (0 in, 8 out)
   6 HDA NVidia: HDMI 6 (hw:0,12), ALSA (0 in, 8 out)
>  7 FCA1616: USB Audio (hw:1,0), ALSA (12 in, 0 out)
   8 HD-Audio Generic: ALC892 Analog (hw:2,0), ALSA (2 in, 2 out)
   9 HD-Audio Generic: ALC892 Alt Analog (hw:2,2), ALSA (2 in, 0 out)
  10 hdmi, ALSA (0 in, 8 out)
  11 input, ALSA (128 in, 0 out)
(1246976, 'PortAudio V19.7.0-devel, revision 147dd722548358763a8b649b3e4b41dfffbcfbb6')
Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2050
Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2724
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2845

<traceback>

File "/home/luis/anaconda3/envs/tensorflowpy37/lib/python3.7/site-packages/sounddevice.py", line 2738, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening OutputStream: Invalid sample rate [PaErrorCode -9997]

I've tried everything, from changing the sample rate, to completely reinstall this. still I'm not sure what is happening here. The interesting thing is that when I deactivate the environment I get more options in the devices array:

  10 hdmi, ALSA (0 in, 8 out)
  11 pulse, ALSA (32 in, 32 out)
  12 input, ALSA (128 in, 0 out)
  13 default, ALSA (32 in, 32 out)

But I can't actually test this since I'm not able to install the libraries I have in the conda env in the $user. Any idea on how to solve this? Thanks in advance.

mgeier commented 2 years ago

https://github.com/spatialaudio/python-sounddevice/issues/244#issuecomment-715386074

luisarandas commented 2 years ago

@mgeier I already went through that issue. I can't even do the check_input_settings() command:

Expression 'ret' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1818
Traceback (most recent call last):
  File "debug.py", line 33, in <module>
    a = sd.check_input_settings()
  File "/home/luis/anaconda3/envs/tensorflowpy37/lib/python3.7/site-packages/sounddevice.py", line 677, in check_input_settings
    _check(_lib.Pa_IsFormatSupported(parameters, _ffi.NULL, samplerate))
  File "/home/luis/anaconda3/envs/tensorflowpy37/lib/python3.7/site-packages/sounddevice.py", line 2736, in _check
    raise PortAudioError(errormsg, err, hosterror_info)
sounddevice.PortAudioError: Unanticipated host error [PaErrorCode -9999]: 'Cannot allocate memory' [ALSA error -12]

The big problem I see in this is that I can do this outside the conda environment. Including hearing sound with the same sample rate (44.1k). Any idea what this might be?

HaHeho commented 2 years ago

The big problem I see in this is that I can do this outside the conda environment. Including hearing sound with the same sample rate (44.1k). Any idea what this might be?

Can you recreate this in a minimal conda environment and post the conda list --export? So you or someone else can try on a different system.

luisarandas commented 2 years ago

Ok here it goes. The toy environment that is able to access default, ALSA

# platform: linux-64
_libgcc_mutex=0.1=conda_forge
_openmp_mutex=4.5=1_gnu
ca-certificates=2021.5.30=ha878542_0
certifi=2021.5.30=py37h89c1867_0
cffi=1.14.6=pypi_0
ld_impl_linux-64=2.36.1=hea4e1c9_1
libffi=3.3=h58526e2_2
libgcc-ng=9.3.0=h2828fa1_19
libgomp=9.3.0=h2828fa1_19
libstdcxx-ng=9.3.0=h6de172a_19
ncurses=6.2=h58526e2_4
numpy=1.21.0=pypi_0
openssl=1.1.1k=h7f98852_0
pip=21.1.3=pyhd8ed1ab_0
portaudio=19.6.0=h14c3975_4
pyaudio=0.2.11=py37h14c3975_2
pycparser=2.20=pypi_0
python=3.7.10=hffdb5ce_100_cpython
python_abi=3.7=2_cp37m
readline=8.1=h46c0cb4_0
setuptools=49.6.0=py37h89c1867_3
sounddevice=0.4.1=pypi_0
sqlite=3.36.0=h9cd32fc_0
tk=8.6.10=h21135ba_1
wheel=0.36.2=pyhd3deb0d_0
xz=5.2.5=h516909a_1
zlib=1.2.11=h516909a_1010

And the environment that I need with an older tf version that doesnt access the default, ALSA therefore doesn't play audio.

# platform: linux-64
_libgcc_mutex=0.1=conda_forge
_openmp_mutex=4.5=1_gnu
_tflow_select=2.3.0=mkl
absl-py=0.13.0=pyhd8ed1ab_0
alsa-lib=1.2.3=h516909a_0
astor=0.8.1=pyh9f0ad1d_0
attrs=21.2.0=pypi_0
audioread=2.1.9=pypi_0
bimpy=0.1.1=pypi_0
c-ares=1.17.1=h7f98852_1
ca-certificates=2021.5.30=ha878542_0
cached-property=1.5.2=hd8ed1ab_1
cached_property=1.5.2=pyha770c72_1
certifi=2021.5.30=py37h89c1867_0
cffi=1.14.6=pypi_0
cycler=0.10.0=pypi_0
decorator=5.0.9=pypi_0
gast=0.2.2=py_0
google-pasta=0.2.0=pyh8c360ce_0
grpcio=1.38.1=py37hb27c1af_0
h5py=3.3.0=nompi_py37ha3df211_100
hdf5=1.10.6=nompi_h6a2412b_1114
importlib-metadata=4.6.1=py37h89c1867_0
iniconfig=1.1.1=pypi_0
joblib=1.0.1=pypi_0
keras-applications=1.0.8=py_1
keras-preprocessing=1.1.2=pyhd8ed1ab_0
kiwisolver=1.3.1=pypi_0
krb5=1.19.1=hcc1bbae_0
ld_impl_linux-64=2.36.1=hea4e1c9_1
libblas=3.9.0=9_openblas
libcblas=3.9.0=9_openblas
libcurl=7.77.0=h2574ce0_0
libedit=3.1.20191231=he28a2e2_2
libev=4.33=h516909a_1
libffi=3.3=h58526e2_2
libgcc-ng=9.3.0=h2828fa1_19
libgfortran-ng=9.3.0=hff62375_19
libgfortran5=9.3.0=hff62375_19
libgomp=9.3.0=h2828fa1_19
liblapack=3.9.0=9_openblas
libnghttp2=1.43.0=h812cca2_0
libopenblas=0.3.15=pthreads_h8fe5266_1
libprotobuf=3.17.2=h780b84a_0
librosa=0.7.1=pypi_0
libssh2=1.9.0=ha56f1ee_6
libstdcxx-ng=9.3.0=h6de172a_19
llvmlite=0.31.0=pypi_0
markdown=3.3.4=pyhd8ed1ab_0
matplotlib=3.1.1=pypi_0
ncurses=6.2=h58526e2_4
numba=0.48.0=pypi_0
numpy=1.21.0=py37h038b26d_0
openssl=1.1.1k=h7f98852_0
opt_einsum=3.3.0=pyhd8ed1ab_1
packaging=21.0=pypi_0
pip=21.1.3=pyhd8ed1ab_0
pip-autoremove=0.9.1=pypi_0
pluggy=0.13.1=pypi_0
protobuf=3.17.2=py37hcd2ae1e_0
py=1.10.0=pypi_0
pycparser=2.20=pypi_0
pyparsing=2.4.7=pypi_0
python=3.7.10=h12debd9_4
python-dateutil=2.8.1=pypi_0
python_abi=3.7=2_cp37m
readline=8.1=h46c0cb4_0
resampy=0.2.2=pypi_0
scikit-learn=0.21.3=pypi_0
scipy=1.3.1=pypi_0
setuptools=57.1.0=pypi_0
six=1.16.0=pyh6c4a22f_0
sounddevice=0.4.1=pypi_0
sqlite=3.36.0=h9cd32fc_0
tensorboard=1.15.0=py37_0
tensorflow=1.15.0=mkl_py37h28c19af_0
tensorflow-base=1.15.0=mkl_py37he1670d9_0
tensorflow-estimator=1.15.1=pyh2649769_0
termcolor=1.1.0=py_2
threadpoolctl=2.1.0=pypi_0
tk=8.6.10=h21135ba_1
toml=0.10.2=pypi_0
tqdm=4.61.2=pypi_0
typing_extensions=3.10.0.0=pyha770c72_0
werkzeug=0.16.1=py_0
wheel=0.36.2=pyhd3deb0d_0
wrapt=1.12.1=py37h5e8e339_3
xz=5.2.5=h516909a_1
zipp=3.5.0=pyhd8ed1ab_0
zlib=1.2.11=h516909a_1010
HaHeho commented 2 years ago

I wasn't able to test with that yet. But notable additions of the smaller environment that works are portaudio and pyaudio.

pyaudio was just an alternative to sounddevice for testing?

I wonder why portaudio is not in the second environment tho. Isn't that a dependency that should come with sounddevice?

luisarandas commented 2 years ago

I installed conda install portaudio and conda install pyaudio just for the sake of trying and still doesn't connect to the default out.

If portaudio is a dependency of sounddevice it doesn't get installed. But still the smaller environment has it, a print:

libportaudio.so.2
(1246976, 'PortAudio V19.7.0-devel, revision 147dd722548358763a8b649b3e4b41dfffbcfbb6')
HaHeho commented 2 years ago

The entire package is independent of platform and python version.

conda search python-sounddevice=0.4.1 --info
Loading channels: done
python-sounddevice 0.4.1 pyh9f0ad1d_0
-------------------------------------
file name   : python-sounddevice-0.4.1-pyh9f0ad1d_0.tar.bz2
name        : python-sounddevice
version     : 0.4.1
build       : pyh9f0ad1d_0
build number: 0
size        : 30 KB
license     : MIT
subdir      : noarch
url         : https://conda.anaconda.org/conda-forge/noarch/python-sounddevice-0.4.1-pyh9f0ad1d_0.tar.bz2
md5         : 5f20a8e0e2982e23bfcc9dcbcbab6ba1
timestamp   : 2020-09-27 15:45:59 UTC
dependencies:
  - cffi
  - numpy
  - portaudio
  - python

So that seems odd, like something is wrong with your environment. Maybe try to recreate it from scratch?

I can recommend to use a setup file for the environment, to easily redo it in one line, e.g. environment.yml (just to show the syntax, the actual packages there don't apply to you).

luisarandas commented 2 years ago

@HaHeho thank you. I was able to solve this by just making a new environment and installing everything from scratch, which is strange. If you want I can share the environment.yml :+1:

BTW, a new problem appeared on my machine. I am just able to sd.play(np.array, sr) with blocking=True method. If I don't have that, it just sends clicks (sort of granular) alongside the ALSA lib pcm.c:8526:(snd_pcm_recover) underrun occurred print in the terminal. Any idea of what this might be?

HaHeho commented 2 years ago

@HaHeho thank you. I was able to solve this by just making a new environment and installing everything from scratch, which is strange. If you want I can share the environment.yml 👍

Great. Sometimes environments can get a bit scrambled up when changing a lot of packages, I suppose (using conda and pip inside is always a bit sketchy, since pip does not know what is going on outside). That's why it's good to setting up the environment with all packages simultaneously (like from a file), so conda can figure out all cross-dependencies.

BTW, a new problem appeared on my machine. I am just able to sd.play(np.array, sr) with blocking=True method. If I don't have that, it just sends clicks (sort of granular) alongside the ALSA lib pcm.c:8526:(snd_pcm_recover) underrun occurred print in the terminal. Any idea of what this might be?

Take a look in this thread and see if it already helps to solve the issue with sd.play() .

luisarandas commented 2 years ago

Solved it! :+1: The sd.wait() function did it, now just need it not to stop the GUI forever, maybe go multithread. Thank you!