Un4Seen’s BASS Audio Library is a popular, cross-platform closed-source library for playing and recording audio streams in a number of popular formats. I personally have used it previously within Microsoft .NET. Now it’s possible to use this useful library within Python, thanks to a ctypes module.
You see, BASS is a C Library. The Python interpreter is also commonly compiled for C. Using the ctypes library it’s possible to make these two things work together for good.
What can you do with BASS?
In it’s simplest form, BASS can record and play audio files in formats such as WAV, MP2, MP3, MP1, OGG and AIFF. It supports multiple streams simultaneously, to multiple sound cards (or use the mixer module to play multiple things to the one output device). It also supports many effects such as time stretching (used a lot in radio to make the hours time out correctly), via the BASS FX plug-in.
Getting Started with BASS for Python
Using the full power of BASS and it’s plugins is possible in Python thanks to the PyBASS module. It’s available on GitHub and SourceForge. It’s a ctypes module, which basically means the original C methods are exposed to Python. Here’s how to get started:
- Download the module (from a link above – I used the SourceForge version)
- Download the main BASS library from their website (there are versions for each platform – choose the correct one)
- Add the platform-specific files to the PyBASS directory.
- For PC, copy the bass.dll file.
- For Mac, copy the libbass.dylib file and rename it to libbass.so
- Run one of the sample projects, and use that as a starting point for your own project
For each of the additional modules you use, you’ll need to download the appropriate libraries from the Un4Seen BASS website and copy it into the correct location. It’s important to note that BASS is not open source, and indeed must be paid for for non-personal projects, so check your license compatibility when using this library (especially if you want to embed the compiled libraries in your project).
PyBASS Sample Code
The examples provided by PyBASS are quiet good. As a point of reference, here is a super-simple player. It contains no error-checking or the like. It will either play your audio file or it won’t.
from pybass import *
BASS_Init(-1, 44100, 0, 0, 0)
filePlayerHandle = BASS_StreamCreateFile(False, “mySampleFile.mp3”, 0, 0)
It’s really that simple!
PyBASS and BASS FX Plugin
PyBASS provides wrappers for a lot of BASS ‘extras’ (such as the Mixer module), but not the FX plugin. Thankfully, it’s fairly easy to start adding ctypes FX wrappers yourself if you are a little familiar with Python. I’ve done a little work on this already to implement time stretching. Here is an example, as a GitHub Gist.
Hopefully this has been of some help to you. Although there is not too much information out there about using BASS within a Python application, it is absolutely possible and possibly the best way to handle audio in Python. If there’s anything you would like to add, post it in the comments.