PyAudiere! A high-level audio interface for Python
Current Version: 0.2
PyAudiere is a very flexible and easy to use audio library for users of the Python programming language. Available methods allow you to read soundfiles of various formats into memory and play them, or stream them if they are large. You can pass sound buffers as NumPy arrays of float32's to play (non-blocking). You can also create pure tones, square waves, or 'on-line' white or pink noise. All of these functions can be utilized concurrently. Sweet!
PyAudiere is useful for Python programmers who want to painlessly add sound to their applications. It is also just as useful in an interactive environment, particularly in science and education settings involving signal processing applications.
PyAudiere depends on the Audiere Sound Library, and like Audiere, is open source and licensed under the LGPL. PyAudiere was originally written by Chad Austin, and has been extended and is now maintained by Christopher Brown.
News
2010-07-22 • Win32 installer for Python 2.7.
2009-03-30 • Fixed error in deb package that linked to a non-existent lib. Sorry about that.
2009-03-24 • Fixed error in Windows installer that placed audiere.dll in the wrong directory. PyAudiere runs great on Python 2.6, so I added a Windows installer. Please report any bugs to c-b -at- asu.edu.
2009-03-17 • Version 0.2 released. This version fixes a memory leak in the open_array method. Other properties and methods were added as well. pyaudiere.org started.
Download
The deb package depends on libaudiere-1.9.4, which is available in both Debian and Ubuntu package repositories. The Windows installers have no dependencies (except numpy if you want to play arrays, of course). To build from source, extract into the /audiere/bindings folder in the Audiere source distribution right beside the included Python bindings folder and issue the standard 'python setup.py build' command.
- pyaudiere-0.2-py2.5-i386.deb (Debian Package)
- pyaudiere-0.2.win32-py2.5.msi (Windows Installer)
- pyaudiere-0.2.win32-py2.6.msi (For Python 2.6 on Windows)
- pyaudiere-0.2.win32-py2.7.msi (For Python 2.7 on Windows)
- pyaudiere-0.2.zip (Source Package)
- pyaudiere-0.2.tar.gz (Source Package)
PyAudiere methods and properties:
- get_devices()
- Returns a python dict containing the available audio devices Possible output devices are DirectSound or WinMM in Windows, OSS on Linux and Cygwin, and SGI AL on IRIX.
- open_device()
- Returns an object that represents an audio device. You can specify a device (from one of the devices returned by get_devices) or pass no argument for the default device.
- readonly field: __version__ :: string
- The pyAudiere version number
- readonly field: __libversion__ :: string
- The Audiere library version number
Device methods and properties:
- create_tone(frequency)
- Creates a tone with the specified frequency and returns an OutputStream object that represents it.
- create_square(frequency)
- Creates a squarewave with the specified frequency and returns an OutputStream object that represents it.
- create_white()
- Creates white noise and returns an OutputStream object that represents it.
- create_pink()
- Creates pink noise and returns an OutputStream object that represents it (Pink noise has equal energy per octave).
- open_file(filename [, streaming])
-
Opens a soundfile from the filename, and returns an OutputStream
object for it. The streaming parameter sets whether the
soundfile is loaded into memory before playing, or is streamed.
It defaults to false (no streaming).
The following file formats are supported: Ogg Vorbis, MP3, FLAC, uncompressed WAV, AIFF, MOD, S3M, XM, and IT. - open_array(buffer, fs)
- Opens a sound buffer for playback, and returns an OutputStream object for it. The buffer should be a NumPy array of Float32's with one or two columns for mono of stereo playback. The second parameter is the sampling frequency. Values outside the range +-1 will be clipped.
- readonly field: name :: string
- The device name (eg., 'directsound', 'winmm', 'oss', etc.)
OutputStream methods and properties
- play()
- Begins playback of the stream. Does nothing if stream is playing.
- pause()
- Stops playback of the stream, but does not reset.
- stop()
- Stops playback, and resets the stream to the beginning.
- reset()
- Resets the stream to the beginning.
- field: playing :: boolean
- Gets or sets whether the stream is playing. 1=playing, 0=not playing.
- field: repeating :: boolean
- Gets or sets whether the stream will repeat. If 1, when the end of the stream is hit, the stream starts playback from the beginning.
- field: volume :: floating point number
- Gets or sets the current volume of the stream. 1 is maximum, 0 is silence.
- field: pan :: floating point number
- Gets or sets the current pan of the stream. 0 is centered, -1 is all of the way to the left, and 1 is to the right.
- field: pitchshift :: floating point number
- Gets or sets the current pitch shift. 1 is no shift, 2 is an octave higher, and 0.5 is an octave lower.
- readonly field: seekable :: boolean
- If this is true, the following fields are applicable.
- readonly field: length :: integer
- The number of samples within this stream.
- field: position :: integer
- Gets or sets the current position of the play cursor in the stream.
Some examples:
import audiere # Open default audio device d = audiere.open_device() # Pink noise in left channel p = d.create_pink() p.pan = -1 p.play() # 500-Hz tone in right channel t = d.create_tone(500) t.pan = 1 t.play() # make it a 1000-Hz tone instead t.pitchshift = 2 # Stream a flac file: f = d.open_file('/path/to/file.flac',1) f.play() # Load a sound file into memory, then play it looped: l = d.open_file('/path/to/file2.wav') l.repeating = 1 l.play() #Read in a sound file, do some processing, play it out: from scikits.audiolab import wavread from scipy.signal import lfilter from scipy.signal.filter_design import butter buff,fs,enc = wavread('/path/to/file3.wav') # Low-pass filter @ 1000 Hz b,a = butter(6,1000./(fs/2)) fb = lfilter(b,a,buff[:,1]) # first channel m = d.open_array(fb,fs) m.playing = 1 def wavplay(buff, fs, pan=0): '''Plays a sound buffer with blocking, matlab-style ''' import audiere from time import sleep d = audiere.open_device() s = d.open_array(buff,fs) s.pan = pan s.play() while s.playing: sleep(.01)