bastibe / python-soundfile

SoundFile is an audio library based on libsndfile, CFFI, and NumPy
BSD 3-Clause "New" or "Revised" License
676 stars 105 forks source link

numpy is a dependency, but this is not declared #406

Open mcclure opened 9 months ago

mcclure commented 9 months ago

python-soundfile requires numpy to run. If numpy is not installed in pip, in my testing python-soundfile will immediately crash on any function invocation with

  File "C:\Users\Andi\work\f\analogue-core-template-amaranth\src\fpga\amaranth_core\my_amaranth_core\toplevel.py", line
451, in bench
    outfile.write(frames)
  File "C:\Users\Andi\work\f\analogue-core-template-amaranth\src\fpga\amaranth_core\.venv\Lib\site-packages\soundfile.py
", line 1017, in write
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

I understand python-soundfile is usually used with numpy, however, the documentation claims it can be used without it.

My expected behavior is that the library should declare all dependencies in setup.py (soundfile does do this for cffi already) so that pip will install them automatically when installing python-soundfile.

Repro

I have a repro but it is not very convenient. You can check out https://github.com/mcclure/analogue-core-template-amaranth and check out revision cd7c72b. You will want to install "pdm" with pip, and then edit src/fpga/amaranth_core/pyproject.toml to include "soundfile" instead of "pysoundfile" and remove the dependency on numpy, then you can run with (cd src/fpga/amaranth_core && python.exe -m pdm install && python.exe -m pdm capture_wav).

The part that uses python-soundfile is here.

Note I only tested this installing numpy with pdm, but my understanding is that uses pip.

bastibe commented 9 months ago

Soundfile can indeed be used without numpy, if you use the buffer_* functions.

These were perhaps a bad idea, as almost all people no doubt use soundfile with numpy. But backwards compatibility is a thing, so that's how it is.

mcclure commented 9 months ago

Let me try again, I think I have a better repro.

Say I create a brand new fresh venv, and then I install python-soundfile. I then run this ultra-simple script:

import soundfile
soundfile.write("test.wav", [[0,0]], 48000)

This fails with "ModuleNotFoundError: No module named 'numpy'". (So does read().)

Looking in setup.py, I find that there is a "numpy" feature in extras_require, and that it does create a numpy dependency. But you only get this if you (?) install with pip install soundfile[numpy], and this is not documented either in the toplevel documentation or the documentation for the functions (such as read and write) which require numpy. (It is mentioned in the changelog that read and write require numpy but buffer_read and buffer_write do not, but this is not a very clear place for it.)

So my "expected behavior" would be one of the following changes:

bastibe commented 9 months ago

I agree. Let's require numpy for the next version of soundfile. The entire point of the library is to expose a numpy interface to libsndfile.