Installation§
First of all, you’ll need Python. There are many distributions of Python available, anyone where CFFI is supported should work.
You can get the latest sounddevice
release from PyPI (using pip
).
But first, it is recommended to create a virtual environment
(e.g. using python3 -m venv or conda create).
After activating the environment, the sounddevice
module can be installed with:
python -m pip install sounddevice
If you have already installed an old version of the module in the environment,
you can use the --upgrade
flag to get the newest release.
To un-install, use:
python -m pip uninstall sounddevice
If you want to try the very latest development version of the sounddevice
module,
have a look at the section about Contributing.
If you install the sounddevice
module with pip
on macOS or Windows,
the PortAudio library will be installed automagically.
On other platforms, you might have to install PortAudio with your package
manager (the package might be called libportaudio2
or similar).
Note
If you install PortAudio with a package manager (including conda
),
it will likely override the version installed with pip
.
The NumPy library is only needed if you want to play back and record NumPy arrays.
The classes sounddevice.RawStream
, sounddevice.RawInputStream
and
sounddevice.RawOutputStream
use plain Python buffer objects and don’t need
NumPy at all.
If needed – and not installed already – NumPy can be installed like this:
python -m pip install numpy
ASIO Support§
Installing the sounddevice
module with pip
(on Windows)
will provide two PortAudio DLLs, with and without ASIO support.
By default, the DLL without ASIO support is loaded
(because of the problems mentioned in issue #496).
To load the DLL with ASIO support, the environment variable SD_ENABLE_ASIO
has to be set before importing the sounddevice
module,
for example like this:
import os
# Set environment variable before importing sounddevice. Value is not important.
os.environ["SD_ENABLE_ASIO"] = "1"
import sounddevice as sd
print(sd.query_hostapis())
Note
This will only work if the sounddevice
module has been installed via pip
.
This will not work with the conda
package (see below).
Note
This will not work if a custom portaudio.dll
is present in the %PATH%
(as described in the following section).
Custom PortAudio Library§
If you want to use a different version of the PortAudio library
(maybe a development version or a version with different features selected),
you can rename the library to libportaudio.so
(Linux)
or libportaudio.dylib
(macOS) and move it to /usr/local/lib
.
On Linux, you might have to run sudo ldconfig
after that,
for the library to be found.
On Windows, you can rename the library to portaudio.dll
and move it to any directory in your %PATH%
.
In case of doubt you should create a fresh directory for your library
and add that to your PATH
variable.
Alternative Packages§
If you are using the conda
package manager (e.g. with miniforge),
you can install the sounddevice
module from the conda-forge
channel:
conda install -c conda-forge python-sounddevice
You can of course also use mamba
if conda
is too slow.
Note
The PortAudio package on conda-forge
doesn’t have ASIO support,
see https://github.com/conda-forge/portaudio-feedstock/issues/9.
There are also packages for several other package managers: