Skip to main content

Play and Record Sound with Python

Project description

This Python module provides bindings for the PortAudio library and a few convenience functions to play and record NumPy arrays containing audio signals.

Documentation:

http://python-sounddevice.readthedocs.io/

Source code repository and issue tracker:

http://github.com/spatialaudio/python-sounddevice/

Python Package Index:

http://pypi.python.org/pypi/sounddevice/

License:

MIT – see the file LICENSE for details.

Requirements

Python:

Of course, you’ll need Python. Any version where CFFI (see below) is supported should work. If you don’t have Python installed yet, you should get one of the distributions which already include CFFI and NumPy (and many other useful things), e.g. Anaconda or WinPython.

pip/setuptools:

Those are needed for the installation of the Python module and its dependencies. Most systems will have these installed already, but if not, you should install it with your package manager or you can download and install pip and setuptools as described on the pip installation page. If you happen to have pip but not setuptools, use this command:

python3 -m pip install setuptools --user
CFFI:

The C Foreign Function Interface for Python is used to access the C-API of the PortAudio library from within Python. It supports CPython 2.6, 2.7, 3.x; and is distributed with PyPy. If it’s not installed already, you should install it with your package manager (the package might be called python3-cffi or similar), or you can get it with:

python3 -m pip install cffi --user
PortAudio library:

The PortAudio library must be installed on your system (and CFFI must be able to find it). Again, you should use your package manager to install it (the package might be called libportaudio2 or similar). If you prefer, you can of course also download the sources and compile the library yourself. If you are using Mac OS X or Windows, the library will be installed automagically with pip (see “Installation” below).

NumPy (optional):

NumPy 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 you need NumPy, you should install it with your package manager or use a Python distribution that already includes NumPy (see above). Installing NumPy with pip requires a compiler and several additional libraries and is therefore not recommended for beginners.

Installation

Once you have installed the above-mentioned dependencies, you can use pip to download and install the latest release with a single command:

python3 -m pip install sounddevice --user

If you want to install it system-wide for all users (assuming you have the necessary rights), you can just drop the --user option.

To un-install, use:

python3 -m pip uninstall sounddevice

Usage

First, import the module:

import sounddevice as sd

Playback

Assuming you have a NumPy array named myarray holding audio data with a sampling frequency of fs (in the most cases this will be 44100 or 48000 frames per second), you can play it back with sounddevice.play():

sd.play(myarray, fs)

This function returns immediately but continues playing the audio signal in the background. You can stop playback with sounddevice.stop():

sd.stop()

If you know that you will use the same sampling frequency for a while, you can set it as default using sounddevice.default.samplerate:

sd.default.samplerate = fs

After that, you can drop the samplerate argument:

sd.play(myarray)

Recording

To record audio data from your sound device into a NumPy array, use sounddevice.rec():

duration = 10  # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2)

Again, for repeated use you can set defaults using sounddevice.default:

sd.default.samplerate = fs
sd.default.channels = 2

After that, you can drop the additional arguments:

myrecording = sd.rec(duration * fs)

This function also returns immediately but continues recording in the background. In the meantime, you can run other commands. If you want to check if the recording is finished, you should use sounddevice.wait():

sd.wait()

If the recording was already finished, this returns immediately; if not, it waits and returns as soon as the recording is finished.

Alternatively, you could have used the blocking argument in the first place:

myrecording = sd.rec(duration * fs, blocking=True)

By default, the recorded array has the data type 'float32' (see sounddevice.default.dtype), but this can be changed with the dtype argument:

myrecording = sd.rec(duration * fs, dtype='float64')

Simultaneous Playback and Recording

To play back an array and record at the same time, use sounddevice.playrec():

myrecording = sd.playrec(myarray, fs, channels=2)

The number of output channels is obtained from myarray, but the number of input channels still has to be specified.

Again, default values can be used:

sd.default.samplerate = fs
sd.default.channels = 2
myrecording = sd.playrec(myarray)

In this case the number of output channels is still taken from myarray (which may or may not have 2 channels), but the number of input channels is taken from sounddevice.default.channels.

Device Selection

In many cases, the default input/output device(s) will be the one(s) you want, but it is of course possible to choose a different device. Use sounddevice.query_devices() to get a list of supported devices. The same list can be obtained from a terminal by typing the command

python3 -m sounddevice

You can use the corresponding device ID to select a desired device by assigning to sounddevice.default.device or by passing it as device argument to sounddevice.play(), sounddevice.Stream() etc.

Instead of the numerical device ID, you can also use a space-separated list of case-insensitive substrings of the device name (and the host API name, if needed). See sounddevice.default.device for details.

import sounddevice as sd
sd.default.samplerate = 44100
sd.default.device = 'digital output'
sd.play(myarray)

Callback Streams

Callback “wire” with sounddevice.Stream:

import sounddevice as sd
duration = 5  # seconds

def callback(indata, outdata, frames, time, status):
    if status:
        print(status, flush=True)
    outdata[:] = indata

with sd.Stream(channels=2, callback=callback):
    sd.sleep(duration * 1000)

Same thing with sounddevice.RawStream:

import sounddevice as sd
duration = 5  # seconds

def callback(indata, outdata, frames, time, status):
    if status:
        print(status, flush=True)
    outdata[:] = indata

with sd.RawStream(channels=2, dtype='int24', callback=callback):
    sd.sleep(duration * 1000)

Blocking Read/Write Streams

Instead of using a callback function, you can also use the blocking methods sounddevice.Stream.read() and sounddevice.Stream.write() (and of course the corresponding methods in sounddevice.InputStream, sounddevice.OutputStream, sounddevice.RawStream, sounddevice.RawInputStream and sounddevice.RawOutputStream).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sounddevice-0.3.6.tar.gz (44.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win_amd64.whl (168.1 kB view details)

Uploaded CPython 2.6CPython 2.7CPython 3.2CPython 3.3CPython 3.4CPython 3.5CPython 3.6PyPyPython 2Python 3Windows x86-64

sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win32.whl (164.0 kB view details)

Uploaded CPython 2.6CPython 2.7CPython 3.2CPython 3.3CPython 3.4CPython 3.5CPython 3.6PyPyPython 2Python 3Windows x86

sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-macosx_10_6_x86_64.whl (78.7 kB view details)

Uploaded CPython 2.6CPython 2.7CPython 3.2CPython 3.3CPython 3.4CPython 3.5CPython 3.6PyPyPython 2Python 3macOS 10.6+ x86-64

sounddevice-0.3.6-py2.py3-none-any.whl (32.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sounddevice-0.3.6.tar.gz.

File metadata

  • Download URL: sounddevice-0.3.6.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sounddevice-0.3.6.tar.gz
Algorithm Hash digest
SHA256 4ef39be2d13069fbad8c69ac259e018d96ce55c23b529a7e0be9bd9a76e2e8da
MD5 0f606b84ee1c5344036d19b4ff29823c
BLAKE2b-256 9e60ee498d3a7cedca6d409db6da16a588fec93d278b70162d1ab4c6dcdeff5b

See more details on using hashes here.

File details

Details for the file sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win_amd64.whl.

File metadata

File hashes

Hashes for sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 b454ccfa89a373ad01a9b8a3a3c330d03e4e4abb8dcd5946b07aca1ad5ab4fc8
MD5 3fc6d6adc9b34eb2aa76a58cef7d08a0
BLAKE2b-256 645ef9e74b3fa8fd034a2e6693275e516abe8b36b86190a06e8817a9af8cad6a

See more details on using hashes here.

File details

Details for the file sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win32.whl.

File metadata

File hashes

Hashes for sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win32.whl
Algorithm Hash digest
SHA256 dd7f94e425bf5b17bb71d67fc89f7396c688be97cd7d8e68baa63ef8a1082e88
MD5 17b9e339adc2732c31522ee4ac51f08a
BLAKE2b-256 d69b00d34e2283347f00fe1b7ef20c44d26712d93c02768e5ef6ca11823abcc6

See more details on using hashes here.

File details

Details for the file sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-macosx_10_6_x86_64.whl.

File metadata

File hashes

Hashes for sounddevice-0.3.6-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 1edf001d46b8d11fbeec371fc2bb92ffc504fc5f158acf59e2968697f69d1045
MD5 34094ae5dfeb2906114aca17903b17a2
BLAKE2b-256 7ae04b02c8232ef69a7cf03b031bbcb96a04eff31be2cce78807ddb49966a1e6

See more details on using hashes here.

File details

Details for the file sounddevice-0.3.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sounddevice-0.3.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 51867f04a612145326d42706e5c4d24f14585a45f31a4634b7fb60c935afe2f4
MD5 912908ab2aa8a7de192dc677b8c25df5
BLAKE2b-256 e19d22dca34822c78e01d17d9c189f0bc59b3836ea12ab65a5ec09d05551f0d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page