Un4Seen BASS for Python: PyBASS

Un4Seen BASS Audio Library for Python

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.

Indeed, a lot of radio play out systems use BASS as their main audio player/recorder. You’d be surprised with some of the big name (and small) broadcast software manufacturers who use BASS. Just check out their Program Files folder for the presence of BASS.dll.

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:

  1. Download the module (from a link above – I used the SourceForge version)
  2. Download the main BASS library from their website (there are versions for each platform – choose the correct one)
  3. Add the platform-specific files to the PyBASS directory.
    1. For PC, copy the bass.dll file.
    2. For Mac, copy the libbass.dylib file and rename it to libbass.so
  4. 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)
BASS_ChannelPlay(filePlayerHandle, False)

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.

Anthony Eden is a technologist. He's been developing software and websites and working in broadcast media for over 8 years now. He works full time for Hope Media, and provides contract services through Media Realm.

Follow Anthony on Twitter: @anthony_eden or Google+

But Wait... There's More!

Sign up for the email newsletter about media and technology.

Tags: Audio Playback · BASS · Python

Stay Up to Date

Every few weeks we’ll let you know about the latest in web, broadcast and media.

Worth a read…
Building a Radio Station Website-Building

Developing a Radio Station Website

Hope Media iPhone App

Recent Work: Hope Media iPhone App

Radio Automation Software

Radio Station Playout / Automation Software

Radio Studio Design and Build

Building a Radio Studio

Free Software

Free Software for Radio Stations

Popular articles…
Church Streaming

How to: Live Webcast your Church Service – Getting Started

OpenVPN Tunnel Through Proxy

OpenVPN Client through a Restrictive Firewall and Proxy

Wordpress Pros and Cons

WordPress Pros and Cons

Recently published…
Radio-Donations-Page-Design-and-Usability---header

Radio Station Website: Donation Page Design and Usability

Streaming Radio Player: Development

Radio Station Website: Radio Player Design & Development

Building a Radio Station Website-Building

Developing a Radio Station Website

Stay Up to Date

Every few weeks we’ll let you know about the latest in web, broadcast and media.