Jupyter Audio Basics¶

Audio Libraries¶

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

1. librosa¶

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.

2. IPython.display.Audio¶

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

Retrieving Audio¶

To download a file from the Internet onto your local machine, you can use urllib.urlretrieve:

In [1]:
import urllib
filename, header = urllib.urlretrieve(
    'http://audio.musicinformationretrieval.com/simple_loop.wav', 
    filename='simple_loop.wav'
)
In [2]:
filename
Out[2]:
'simple_loop.wav'
In [3]:
print header
x-amz-id-2: OX2PeqV9n+GcUzzxOG2JZi6e5+xsov5k7cbeIaW/T9Cinv8LL5FoVs1ZEme6X5+aBSVeKm+Pt2I=
x-amz-request-id: 764C0BEF267F6206
Date: Thu, 13 Jul 2017 22:37:37 GMT
Last-Modified: Fri, 03 Jul 2015 18:39:43 GMT
ETag: "91747c7bd2ff230cc2f18442bf917db8"
Content-Type: audio/x-wav
Content-Length: 264644
Server: AmazonS3
Connection: close

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

In [ ]:
%ls *.wav

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

Reading Audio¶

Use librosa.load to load an audio file into an audio array. Return both the audio array as well as the sample rate:

In [5]:
import librosa
x, sr = librosa.load('simple_loop.wav')

If you receive an error with librosa.load, you may need to install ffmpeg.

Display the length of the audio array and sample rate:

In [6]:
print x.shape
print sr
(66150,)
22050

Visualizing Audio¶

In order to display plots inside the Jupyter notebook, run the following commands, preferably at the top of your notebook:

In [7]:
%matplotlib inline
import seaborn # optional
import matplotlib.pyplot as plt
import librosa.display

Plot the audio array using librosa.display.waveplot:

In [8]:
plt.figure(figsize=(12, 4))
librosa.display.waveplot(x, sr=sr)
Out[8]:
<matplotlib.collections.PolyCollection at 0x112ca8e90>

Display a spectrogram using librosa.display.specshow:

In [9]:
X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(X)
plt.figure(figsize=(12, 5))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')
Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x110d5e850>

Playing Audio¶

IPython.display.Audio¶

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

In [10]:
import IPython.display as ipd
ipd.Audio('https://ccrma.stanford.edu/workshops/mir2014/audio/CongaGroove-mono.wav') # load a remote WAV file
Out[10]:
In [11]:
ipd.Audio('simple_loop.wav') # load a local WAV file
Out[11]:

Audio can also accept a NumPy array. Let's synthesize a pure tone at 440 Hz:

In [12]:
import numpy
sr = 22050 # sample rate
T = 2.0    # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t)                # pure sine wave at 440 Hz

Listen to the audio array:

In [13]:
ipd.Audio(x, rate=sr) # load a NumPy array
Out[13]:

SoX¶

To play or record audio from the command line, we recommend SoX:

$ rec test.wav

$ play test.wav

Writing Audio¶

librosa.output.write_wav saves a NumPy array to a WAV file.

In [14]:
librosa.output.write_wav('tone_440.wav', x, sr)