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.rtfd.org/

Code:

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

Python Package Index:

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

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:

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 2.0 beta2 or later. You should install it with your package manager (if it’s not installed already), or you can get it with:

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. 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 is not recommended.

Installation

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

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:

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():

>>> myrecording2 = 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
>>> myrecording2 = 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.print_devices() to get a list of supported devices. 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.

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)
    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)
    outdata[:] = indata

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

Blocking Read/Write Streams

Coming soon!

More Examples

Have a look in the examples/ directory.

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.2.0.tar.gz (30.6 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.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-win_amd64.whl (168.5 kB view details)

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

sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-win32.whl (165.7 kB view details)

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

sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-macosx_10_6_x86_64.whl (69.4 kB view details)

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

sounddevice-0.2.0-py2.py3-none-any.whl (28.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for sounddevice-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2628183e154538021a53d627adc58346282524a4bcf116504ba37fa01a3a3ddf
MD5 80339a170e5d687923d995ccfbae379d
BLAKE2b-256 386161eca14cb5806b8d77bdcf084badafa5b8c32a5ad96836d26cf798e7587a

See more details on using hashes here.

File details

Details for the file sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-win_amd64.whl.

File metadata

File hashes

Hashes for sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-win_amd64.whl
Algorithm Hash digest
SHA256 524459f0b96607570cb47e5c94721f376c8a1fabeedae00acaad6c3b4a6a99aa
MD5 47abc8bb5c6256568aaea2830d919815
BLAKE2b-256 7db1ad9e3e4171bed1211d01440bb796ad8c9219a4d0613f28cf51a1e6af927e

See more details on using hashes here.

File details

Details for the file sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-win32.whl.

File metadata

File hashes

Hashes for sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-win32.whl
Algorithm Hash digest
SHA256 4600da6e8bc9a04149daf453832a63156caaf324a91516c949326019d5bc31cb
MD5 b7c6559aa56fccb9fb198b7a5f4267c4
BLAKE2b-256 c439af96024659b4f0dfb319757827716aac528746334369533aad1ef959fe55

See more details on using hashes here.

File details

Details for the file sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-macosx_10_6_x86_64.whl.

File metadata

File hashes

Hashes for sounddevice-0.2.0-py2.py3.cp26.cp27.cp32.cp33.cp34.cp35.pp27.pp32-none-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 4ff38d64238680d71ef2e78fec330737d769175c602ce29141ac6ae207517e2e
MD5 9fe10573f312410c8f816c054bac9682
BLAKE2b-256 4a2179fa4bc2c68516ab0f1925929cd90f3181bc7475484c0491a99083561c18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sounddevice-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7546dc3074ce8123910336031ffd9db76646ee0d66fecdb46cb952a1f216d7dc
MD5 ea116a14c3e19543d3b21ab146f8279d
BLAKE2b-256 2b5bcd030a460be1353102aec137dadb4a994a897bbe2722a06b79f565c5616b

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