Skip to main content

Play and Record Sound with Python

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

If you are using Windows, you can alternatively install one of the packages provided at http://www.lfd.uci.edu/~gohlke/pythonlibs/#sounddevice. The PortAudio library is also included in the package and you can get the rest of the dependencies on the same page.

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.5  # seconds
myrecording = sd.rec(int(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.5  # seconds

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

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

Same thing with sounddevice.RawStream:

import sounddevice as sd
duration = 5.5  # seconds

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

with sd.RawStream(channels=2, dtype='int24', callback=callback):
    sd.sleep(int(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).

Metadata

Release files for sounddevice 0.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sounddevice 0.3.7
File Size Uploaded
sounddevice-0.3.7.tar.gz 46.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for sounddevice 0.3.7
File
sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win_amd64.whl CPython 2.7, Python 2, CPython 2.6, CPython 3.2, CPython 3.3, CPython 3.4, PyPy 3.5, CPython 3.6, PyPy 3.3, PyPy 3.6, Python 3, CPython 3.5, PyPy 3.2, PyPy 2.7, PyPy 3.4 none Windows x86-64 Details
sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win32.whl Python 3, CPython 3.5, PyPy 2.7, PyPy 3.4, CPython 2.7, PyPy 3.3, CPython 3.4, PyPy 3.6, Python 2, CPython 2.6, CPython 3.2, CPython 3.3, PyPy 3.5, CPython 3.6, PyPy 3.2 none Windows x86-32 Details
sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-macosx_10_6_x86_64.whl CPython 3.4, Python 3, CPython 3.5, PyPy 3.2, PyPy 3.3, PyPy 2.7, PyPy 3.6, PyPy 3.4, CPython 2.7, PyPy 3.5, CPython 2.6, CPython 3.6, Python 2, CPython 3.3, CPython 3.2 none macOS 10.6+ x86-64 Details
sounddevice-0.3.7-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 492.5 kB

Release files / sounddevice-0.3.7.tar.gz

Download URL sounddevice-0.3.7.tar.gz
Size 46.0 kB
Tags Source
SHA-256 checksum
How to use checksums
962d968280ebf734539b432edf7ddba775a87e718af3ef9e96173093dd25d0b1
BLAKE2b-256 checksum
How to use checksums
e84938d359b343e917381fbc0e0055c83c285448f6826c6292416a500c195eca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win_amd64.whl

Download URL sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win_amd64.whl
Size 169.0 kB
Tags CPython 2.6 CPython 2.7 CPython 3.2 CPython 3.3 CPython 3.4 CPython 3.5 CPython 3.6 PyPy 2.7 PyPy 3.2 PyPy 3.3 PyPy 3.4 PyPy 3.5 PyPy 3.6 Python 2 Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
bf128e00401a42b18e5770fdc7180617d3295aa58fe59db9b492c9421422e97c
BLAKE2b-256 checksum
How to use checksums
efcd6cab2d98a1bbba0c8b1d0a0ce4659291d452278e2078e3d02ac432105db8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win32.whl

Download URL sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-win32.whl
Size 164.9 kB
Tags CPython 2.6 CPython 2.7 CPython 3.2 CPython 3.3 CPython 3.4 CPython 3.5 CPython 3.6 PyPy 2.7 PyPy 3.2 PyPy 3.3 PyPy 3.4 PyPy 3.5 PyPy 3.6 Python 2 Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
65f4a0e721d8d1f0052f6503bd759485c87c2dc9b77ed12693d6b6ca2ba7655d
BLAKE2b-256 checksum
How to use checksums
138a3e973ae45b54959cef2190e2feaf8e1e0710732bdce4f2ec1d68cd7808e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-macosx_10_6_x86_64.whl

Download URL sounddevice-0.3.7-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.cp36.pp27.pp32.pp33.pp34.pp35.pp36-none-macosx_10_6_x86_64.whl
Size 79.6 kB
Tags CPython 2.6 CPython 2.7 CPython 3.2 CPython 3.3 CPython 3.4 CPython 3.5 CPython 3.6 PyPy 2.7 PyPy 3.2 PyPy 3.3 PyPy 3.4 PyPy 3.5 PyPy 3.6 Python 2 Python 3 macOS 10.6+ x86-64
SHA-256 checksum
How to use checksums
b65ac0d458d6fca2c50736a99fab12c0112c0d8d7237b3e840d526f6f007b19c
BLAKE2b-256 checksum
How to use checksums
885f14ed2ee85aa74a1a49b78b545d66375bc613d41fe2c32a5be1aaf2a0e914
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sounddevice-0.3.7-py2.py3-none-any.whl

Download URL sounddevice-0.3.7-py2.py3-none-any.whl
Size 33.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a8feebdc38de48b149fe43e8f49d34a1907bb5f35f5630ed044ed5d81d6b0537
BLAKE2b-256 checksum
How to use checksums
983b4cfd36b2f91cd4836433d71a00ed0aa13a1ec980efc021cc4074146b78d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.5.6

6 release files

0.5.5

6 release files

0.5.4

6 release files

0.5.3

5 release files

0.5.2

5 release files

0.5.1

5 release files

0.5.0

5 release files

0.4.7

5 release files

0.4.6

5 release files

0.4.5

5 release files

0.4.4

5 release files

0.4.3

5 release files

0.4.2

5 release files

0.4.1

5 release files

0.4.0

5 release files

0.3.15

5 release files

0.3.14

5 release files

0.3.13

5 release files

0.3.10

5 release files

0.3.9

5 release files

0.3.8

5 release files

This release

0.3.7 This release

5 release files

0.3.6

5 release files

0.3.5

5 release files

0.3.4

5 release files

0.3.3

5 release files

0.3.2

5 release files

0.3.1

5 release files

0.3.0

5 release files

0.2.2

5 release files

0.2.1

5 release files

0.2.0

5 release files

0.1.0

2 release files

0.0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page