Skip to main content

GIL-less Portaudio Streams for Python

Project description

https://badge.fury.io/py/pastream.svg https://travis-ci.org/tgarc/pastream.svg?branch=master https://ci.appveyor.com/api/projects/status/wk52r5jy9ri7dsi9/branch/master?svg=true

GIL-less Portaudio Streams for Python

pastream builds on top of portaudio and the excellent sounddevice python bindings to provide some more advanced functionality right out of the box. Note that in addition to the pastream library, pastream includes a command line application for playing and recording audio files.

Features

GIL-less Audio Callbacks
Having the portaudio callback implemented in C means audio interrupts can be serviced quickly and reliably without ever needing to acquire the Python Global Interpreter Lock (GIL). This is crucial when working with libraries like Pillow which may greedily grab and hold the GIL subsequently causing audio overruns/underruns.
Input Stream iterators

Efficiently retrieve live audio capture data through an iterable. As simple as:

import pastream as ps
for chunk in ps.chunks():
    process(chunk)

See pastream.chunks and pastream.InputStream.chunks method.

Reader/Writer Threads
pastream simplifies the process of implementing stream reader and writer threads to manipulate and/or generate data in the background while leaving the main thread free for higher level management tasks.

Dependencies

cffi

sounddevice (depends on PortAudio)

soundfile (depends on libsndfile)

(Optional) numpy

Installation

For linux platforms a recent version of the PortAudio and libsndfile C libraries are required. (For Windows and OSX, the sounddevice and soundfile packages include prebuilt versions for you). You can either install the latest available from your package manager (e.g. apt-get install libportaudio2 libsndfile for debian/raspbian) or install the latest stable build from the package website (Recommended); see links in Dependencies.

pastream is now available on PyPI. Installation is as easy as:

$ pip install pastream

Building From Source

To compile from source under unix platforms, libffi is required. (For Windows, this is already included with cffi). libffi is available through most package managers (e.g., yum install libffi-devel, apt-get install libffi-dev, brew install libffi). More information on installing libffi is available here.

If doing a fresh checkout:

$ git clone --recursive http://github.com/tgarc/pastream

If you already have a checkout:

$ git submodule update --init

Then do a pip install from your working copy:

$ pip install <path/to/checkout>

Building Documentation

Documentation for pastream can be easily generated in a wide variety of formats using Sphinx. Just follow the steps below.

Checkout the repository:

$ git clone --recursive http://github.com/tgarc/pastream

Then use the included makefile/make.bat to generate documentation. (Here we output to the html format):

$ cd pastream/docs
$ make html

Examples

Record 1000 frames to file, then play it back:

import pastream as ps

# Use *with* statements to auto-close the stream
with ps.SoundFileInputStream('recording.wav') as stream:
    stream.frames = 1000
    stream.start()
    stream.wait() # Block until recording is done

with ps.SoundFileOutputStream('recording.wav') as stream:
    stream.frames = 1000
    stream.start()
    stream.wait()

Grab (real) frequency transformed live audio stream with 50% overlap:

import pastream as ps, numpy as np

chunksize = 1024
window = np.hanning(chunksize)
for x_l in ps.chunks(chunksize, overlap=chunksize//2, channels=1):
    X_l = np.fft.rfft(x_l * window)

See also the included examples under /examples.

Command Line Application

Once installed, the pastream application should be callable from your command line. If you’re familiar with sox you’ll notice that some of the command line syntax is quite similar. Here are a few examples to help get you started.

Display the help file:

$ pastream -h

List available audio devices:

$ pastream -l

Simultaneous play and record from the default audio device:

$ pastream input.wav output.wav

Pipe input from sox using the AU format:

$ sox -n -t au - synth sine 440 | pastream - output.wav

Play a RAW file:

$ pastream -c1 -r48k -e=pcm_16 -o output.raw

Record 10 seconds of audio at 48kHz:

$ pastream null output.wav -r48k -n=480k

Project details


Download files

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

Files for pastream, version 0.0.8
Filename, size File type Python version Upload date Hashes
Filename, size pastream-0.0.8-cp27-cp27m-macosx_10_6_intel.whl (34.4 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp27-cp27m-manylinux1_i686.whl (43.9 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp27-cp27m-manylinux1_x86_64.whl (45.7 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp27-cp27mu-manylinux1_i686.whl (43.9 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp27-cp27mu-manylinux1_x86_64.whl (45.7 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp27-cp27m-win32.whl (27.4 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp27-cp27m-win_amd64.whl (28.1 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp33-cp33m-manylinux1_i686.whl (40.6 kB) File type Wheel Python version cp33 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp33-cp33m-manylinux1_x86_64.whl (42.3 kB) File type Wheel Python version cp33 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp33-cp33m-win32.whl (27.4 kB) File type Wheel Python version cp33 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp33-cp33m-win_amd64.whl (28.0 kB) File type Wheel Python version cp33 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp34-cp34m-macosx_10_6_intel.whl (34.5 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp34-cp34m-manylinux1_i686.whl (40.7 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp34-cp34m-manylinux1_x86_64.whl (42.4 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp34-cp34m-win32.whl (27.4 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp34-cp34m-win_amd64.whl (28.0 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp35-cp35m-macosx_10_6_intel.whl (34.5 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp35-cp35m-manylinux1_i686.whl (40.7 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp35-cp35m-manylinux1_x86_64.whl (42.4 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp35-cp35m-win32.whl (28.9 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp35-cp35m-win_amd64.whl (30.4 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp36-cp36m-macosx_10_6_intel.whl (34.5 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp36-cp36m-manylinux1_i686.whl (40.7 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp36-cp36m-manylinux1_x86_64.whl (42.4 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp36-cp36m-win32.whl (28.9 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pastream-0.0.8-cp36-cp36m-win_amd64.whl (30.4 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size pastream-0.0.8.tar.gz (47.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page