import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa
import urllib
urllib.urlretrieve(
'http://audio.musicinformationretrieval.com/simpleLoop.wav',
filename='simpleLoop.wav'
)
('simpleLoop.wav', <httplib.HTTPMessage instance at 0x1114350e0>)
%ls *.wav
noise1.wav noise2.wav simpleLoop.wav
librosa.load
¶x, fs = librosa.load('simpleLoop.wav')
print x.shape
print fs
(66150,) 22050
plt.plot(x)
[<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):
from essentia.standard import MonoLoader
audio = MonoLoader(filename='simpleLoop.wav')()
audio.shape
(132300,)
N = len(audio)
t = numpy.arange(0, N)/44100.0
plt.plot(t, audio)
plt.xlabel('Time (seconds)')
<matplotlib.text.Text at 0x112355990>
For more control over the audio acquisition process, you may want to use AudioLoader
instead.
IPython.display.Audio
¶from IPython.display import Audio
# load a remote WAV file
Audio('https://ccrma.stanford.edu/workshops/mir2014/audio/CongaGroove-mono.wav')
# load a local WAV file
Audio('simpleLoop.wav')
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)
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
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)')
<matplotlib.text.Text at 0x10c823a90>
S, freqs, bins, im = plt.specgram(x, NFFT=1024, Fs=fs, noverlap=512)
# Plot a spectrogram
plt.xlabel('Time')
plt.ylabel('Frequency')
<matplotlib.text.Text at 0x10c8313d0>
librosa.output.write_wav
¶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