In this Notebook, we show a couple of ways of working with audio data. This includes both playing the audio as well as working with the raw audio data as NumPy arrays.
First, we show how the
Audio class can be used to play raw audio data in a NumPy
from audiodisplay import Audio
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
Here we create a simple signal at A (440 Hz):
max_time = 3 f = 440.0 rate = 44100.0 times = np.arange(max_time*rate)/rate signal = 2.0**16*np.sin(2*pi*f*times)/2.0 signal = signal.astype(np.int16)
The signal looks as you might expect:
[<matplotlib.lines.Line2D at 0x111da2a10>]
We can simply pass the NumPy array and the sampling rate to the
Audio object to get
an HTML5 audio widget. This widget and audio data will be embedded in the notebook so
the audio can be played back later.
Second, we show how to load audio data from a
.wav file. Here we have a 5 second
clip from J.S. Bach's Cello Suite #3 in C.
filename = 'data/Bach Cello Suite #3.wav'