In [1]:
import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa

Using Audio in IPython

Audio Libraries

We will mainly use three libraries for audio acquisition and playback:

1. IPython.display.Audio

2. librosa

3. essentia.standard

Introduced in IPython 2.0, IPython.display.Audio lets you play audio directly in an IPython notebook.

librosa is a Python package for music and audio processing by Brian McFee. A large portion was ported from Dan Ellis's Matlab audio processing examples.

Essentia is an open-source library for audio analysis and music information retrieval from the Music Technology Group at Universitat Pompeu Fabra. Although Essentia is written in C++, we will use the Python bindings for Essentia.

Retrieving Audio

To download a file onto your local machine (or Vagrant box) in Python, you can use urllib.urlretrieve:


In [2]:
import urllib
urllib.urlretrieve(
    'http://audio.musicinformationretrieval.com/simpleLoop.wav', 
    filename='simpleLoop.wav'
)


Out[2]:
('simpleLoop.wav', <httplib.HTTPMessage instance at 0x1114350e0>)

To check that the file downloaded successfully, list the files in the working directory:


In [3]:
%ls *.wav


noise1.wav      noise2.wav      simpleLoop.wav

If you only want to listen to, and not manipulate, a remote audio file, use IPython.display.Audio instead. (See Playing Audio.)

Reading Audio

librosa.load


In [4]:
x, fs = librosa.load('simpleLoop.wav')
print x.shape
print fs


(66150,)
22050

In [5]:
plt.plot(x)


Out[5]:
[<matplotlib.lines.Line2D at 0x1119c1c90>]

essentia.standard.Monoloader

MonoLoader reads (and downmixes, if necessary) an audio file into a single channel (as will often be the case during this workshop). MonoLoader also resamples the audio to a sampling frequency of your choice (default = 44100 Hz):


In [6]:
from essentia.standard import MonoLoader
audio = MonoLoader(filename='simpleLoop.wav')()
audio.shape


Out[6]:
(132300,)

In [7]:
N = len(audio)
t = numpy.arange(0, N)/44100.0
plt.plot(t, audio)
plt.xlabel('Time (seconds)')


Out[7]:
<matplotlib.text.Text at 0x112355990>

For more control over the audio acquisition process, you may want to use AudioLoader instead.

Playing Audio

IPython.display.Audio

Using IPython.display.Audio, you can play a local audio file or a remote audio file:


In [8]:
from IPython.display import Audio
# load a remote WAV file
Audio('https://ccrma.stanford.edu/workshops/mir2014/audio/CongaGroove-mono.wav')


Out[8]:

In [9]:
# load a local WAV file
Audio('simpleLoop.wav')


Out[9]:

Audio can also accept a NumPy array:


In [10]:
fs = 44100 # sampling frequency
T = 1.5    # seconds
t = numpy.linspace(0, T, int(T*fs), endpoint=False) # time variable
x = numpy.sin(2*numpy.pi*440*t)                # pure sine wave at 440 Hz

# load a NumPy array
Audio(x, rate=fs)


Out[10]: