Installing PySoundFile on Ubuntu 14.04
Mar 3, 2015
warning
This post is more than 5 years old. While math doesn't age, code and operating systems do. Please use the code/ideas with caution and expect some issues due to the age of the content. I am keeping these posts up for archival purposes because I still find them useful for reference, even when they are out of date!
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 (links below updated):
for more details about PySoundFile.
Note:
2022, Oct 13-- There have been a lot of updates to PySoundFile since this post. Please check the links above for updated information.
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.
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:
- Install Python packages on Ubuntu 14.04 -- use this post to see how to install pip
- virtualenv and virtualenvwrapper on Ubuntu 14.04 -- use this post to install virtualenv and virtualenvwrapper (and see how to use virtual environments)
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
Note:
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
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__))
producing...
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))
producing...
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.