Plotting a Sound File in IPython
Sometimes it’s interesting to see how a sound looks. Hearing the sound tells you one thing about it, but seeing it tells you other things. In addition, you can use the sound data as part of an analysis.
For example, you might want to perform a Fast Fourier Transform (FFT). However, before you can do anything with the sound, you need to import it into your application. Here’s an example of how you can import a sound file and then plot it so you can see it.
import matplotlib.pyplot as plt from scipy.io import wavfile as wav rate, data = wav.read('bells.wav') %matplotlib inline plt.plot(data) plt.show()
The example code works only with .wav files. However, other multimedia import routines are available. The output of the read() method provides you with the data rate used to play the sound and the actual sound data. It’s the data that you need for the plot.
In order to see the code and the plot together in IPython Notebook, you need to call the %matplotlib inline magic function. The actual plot is quite simple. Here are typical results from a sound file plot.