Installing PySoundFile on Ubuntu 14.04

In this post I will go over installing PySoundFile using pip and virtualenvwrapper. PySoundFile is a utility for reading and writing sound files of various types, like wav, flac and ogg using the abilities of the libsndfile library. Noticeably absent is mp3 due to the license issue– use a tool like (the wonderful) sox to convert mp3’s to a supported file type. See

for more details about PySoundFile.

2015, June 16– I have new post on installing PySoundFile for use with Python 3.4– PySoundFile and Python 3.4 on Ubuntu 14.04, check it out. There are also some updates to package, that affect the Python 2.7.6 install done in this post, so I’ll show how to upgrade below, if you need to do that. –End edit.

Using pip to install Python packages comes with some difficulties– we have to worry about all the dependencies. Of course, the benefit is that we can install current package versions and software that is not in the Ubuntu repositories, like PySoundFile. So, the dependencies for PySoundFile are

We start by making sure that all of the Python development requirements are installed using apt-get :

$ sudo apt-get install build-essential python-dev

Next, make sure that we have gfortran:

$ sudo apt-get install gfortran

And, linear algebra libraries:

$ sudo apt-get install libatlas-dev liblapack-dev

Next, for the libsndfile requirement we do

$ sudo apt-get install libsndfile1

Finally, for the Ubuntu-specific installs, we install the cffi requirements:

$ sudo apt-get install libffi-dev

Now we get to the Python installs using pip. I will do all of this in a virtual environment using virtualenvwrapper. If you need help getting these installed, see these posts:

Assuming everything is in order, we create a virtual environment called sounds (of course, you can call it whatever you like):

$ mkvirtualenv sounds
New python executable in sounds/bin/python
Installing setuptools, pip...done.
(sounds)$

Next, we install PySoundFile, which will also install cffi, pycparser (a dependency of cffi) and numpy– expect lots of compiling here:

(sounds)$ pip install PySoundFile

... lots of compilation ...

Successfully installed PySoundFile numpy cffi pycparser
Cleaning up...
(sounds)$

Next, checkout the package information using pip

(sounds)$ pip show PySoundFile
---
Name: PySoundFile
Version: 0.6.0
Location: /home/cstrelioff/virtenvs/sounds/lib/python2.7/site-packages
Requires: numpy, cffi

2016, June 16– PySoundFile version 0.7 has changed the import name of the package from pysoundfile to soundfile, so the code as originally posted here breaks. Let’s upgrade the package to version 0.7 and change the code below. To upgrade using pip simply type

(sounds)$ pip install --upgrade PySoundFile

and check the version:

(sounds)$ pip show PySoundFile
---
Name: PySoundFile
Version: 0.7.0
Location: /home/cstrelioff/virtenvs/sounds/lib/python2.7/site-packages
Requires: numpy, cffi

–End edit.

And, to make sure that it loads in Python let’s import and try out a few things. First, print out the version, this should agree with the information from above:

from __future__ import print_function
import soundfile as sf

print("PySoundFile version: {}".format(sf.__version__))
PySoundFile version: 0.7.0

Next, let’s see what file types we can read and write:

for key, val in sf.available_formats().items():
    print("{:5s} -- desc: {}".format(key, val))
MAT4  -- desc: MAT4 (GNU Octave 2.0 / Matlab 4.2)
MAT5  -- desc: MAT5 (GNU Octave 2.1 / Matlab 5.0)
FLAC  -- desc: FLAC (FLAC Lossless Audio Codec)
SD2   -- desc: SD2 (Sound Designer II)
AIFF  -- desc: AIFF (Apple/SGI)
PAF   -- desc: PAF (Ensoniq PARIS)
CAF   -- desc: CAF (Apple Core Audio File)
W64   -- desc: W64 (SoundFoundry WAVE 64)
RAW   -- desc: RAW (header-less)
PVF   -- desc: PVF (Portable Voice Format)
NIST  -- desc: WAV (NIST Sphere)
WVE   -- desc: WVE (Psion Series 3)
RF64  -- desc: RF64 (RIFF 64)
XI    -- desc: XI (FastTracker 2)
MPC2K -- desc: MPC (Akai MPC 2k)
IRCAM -- desc: SF (Berkeley/IRCAM/CARL)
AU    -- desc: AU (Sun/NeXT)
SVX   -- desc: IFF (Amiga IFF/SVX8/SV16)
WAV   -- desc: WAV (Microsoft)
SDS   -- desc: SDS (Midi Sample Dump Standard)
VOC   -- desc: VOC (Creative Labs)
HTK   -- desc: HTK (HMM Tool Kit)
AVR   -- desc: AVR (Audio Visual Research)
OGG   -- desc: OGG (OGG Container format)
WAVEX -- desc: WAVEX (Microsoft)

That’s pretty good, with all the usual suspects (except for mp3’s, for reasons stated above). From the PySoundFile Docs, typical read/write goes something like this:

# read an existing wav file
data, samplerate = sf.read('existing_file.wav')

# write the data to a new ogg file
sf.write(data, 'new_file.ogg', samplerate=samplerate)

Note that data returned by sf.read() is already a numpy array, so compute away!

I think that’s it for this post. As always, leave comments and I’ll get to them as soon as I can.

Tags

api [1]   arduino [1]   audio [2]   audio features [1]   babel [1]   Bayesian [7]   Beta [1]   blog setup [1]   bootstrap [1]   bottleneck [1]   c++ [1]   caret [1]   cmpy [1]   conditional probability [6]   coursera [1]   coursera intro to data science [3]   css [1]   cython [1]   d3 [2]   decision trees [2]   diy [1]   dropbox [1]   dsp [1]   e1071 [1]   essentia [1]   garmin [1]   geojson [1]   ggplot2 [1]   gis [2]   git [1]   gnuplot [1]   graphs [1]   html5 [1]   igraph [1]   ipython [1]   javascript [7]   joint probability [6]   json [1]   LaTeX [2]   LDA [1]   Lea [2]   machine learning [3]   marginal probability [6]   matplotlib [1]   meteor [2]   mir [1]   MongoDB [3]   music [2]   my python setup [5]   my ubuntu setup [10]   mysql [3]   networks [1]   networkx [1]   nodejs [5]   npm [3]   numexpr [1]   numpy [1]   octave [1]   Open Oakland [2]   openpyxl [1]   pandas [3]   patsy [1]   pip [2]   pweave [1]   pygraphviz [1]   pymc [1]   PySoundFile [2]   python [15]   Python [1]   python 2.7 [5]   python 3.4 [2]   pyyaml [1]   qgis [1]   R [1]   randomForest [1]   restview [1]   resume [1]   rpart [1]   running [1]   scikit-learn [3]   scipy [1]   screen [1]   server setup [1]   shapefile [1]   social networks [1]   Socrata [1]   sound [2]   sphinx [1]   sql [4]   sqlite3 [1]   ssh [1]   ssh keys [1]   statsmodels [1]   supervised learning [2]   sympy [1]   tableau [1]   tinkerer [2]   topic models [1]   tree [1]   ubuntu 14.04 [13]   Ubuntu 14.04 [3]   ubuntu 16.04 [4]   vim [2]   virtualbox [1]   virtualenv [4]   virtualenvwrapper [3]   VPS [1]   vundle [1]   webpack [1]   yaml [1]