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

Using Audio in IPython

Audio Libraries

1. IPython.display.Audio

2. librosa

3. essentia.standard

Retrieving Audio

In [2]:
import urllib
urllib.urlretrieve(
    'http://audio.musicinformationretrieval.com/simpleLoop.wav', 
    filename='simpleLoop.wav'
)
Out[2]:
('simpleLoop.wav', <httplib.HTTPMessage instance at 0x1114350e0>)
In [3]:
%ls *.wav
noise1.wav      noise2.wav      simpleLoop.wav

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

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]:
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]:

SoX

To play or record audio from the command line, we recommend SoX (included in the stanford-mir Vagrant box).

$ rec test.wav

$ play test.wav

Visualizing Audio

In [11]:
T = 0.001    # seconds
fs = 44100   # sampling frequency
t = numpy.linspace(0, T, int(T*fs), endpoint=False) # time variable
x = numpy.sin(2*numpy.pi*3000*t)

# Plot a sine wave
plt.plot(t, x)
plt.xlabel('Time (seconds)')
Out[11]:
<matplotlib.text.Text at 0x10c823a90>
In [12]:
S, freqs, bins, im = plt.specgram(x, NFFT=1024, Fs=fs, noverlap=512)

# Plot a spectrogram
plt.xlabel('Time')
plt.ylabel('Frequency')
Out[12]:
<matplotlib.text.Text at 0x10c8313d0>

Writing Audio

librosa.output.write_wav

In [13]:
noise = 0.1*scipy.randn(44100)

# Write an array to a wav file
librosa.output.write_wav('noise2.wav', noise, 44100)
%ls *.wav
noise1.wav      noise2.wav      simpleLoop.wav