In [1]:
%matplotlib inline
import seaborn
import numpy, scipy, matplotlib.pyplot as plt, IPython.display as ipd
import librosa, librosa.display
plt.rcParams['figure.figsize'] = (13, 5)
In this exercise notebook, we will segment, feature extract, and analyze audio files. Goals:
Load the audio file simple_loop.wav
into an array.
In [2]:
x, sr = librosa.load('audio/simple_loop.wav')
Show the sample rate:
In [3]:
print sr
Listen to the audio signal.
In [4]:
ipd.Audio(x, rate=sr)
Out[4]:
Display the audio signal.
In [5]:
librosa.display.waveplot(x, sr=sr)
Out[5]:
Compute the short-time Fourier transform:
In [6]:
X = librosa.stft(x)
For display purposes, compute the log amplitude of the STFT:
In [7]:
Xmag = librosa.logamplitude(X)
Display the spectrogram.
In [8]:
# Play with the parameters, including x_axis and y_axis
librosa.display.specshow(Xmag, sr=sr, x_axis='time', y_axis='log')
Out[8]:
Find the times, in seconds, when onsets occur in the audio signal.
In [9]:
onset_frames = librosa.onset.onset_detect(x, sr=sr)
print onset_frames
In [10]:
onset_times = librosa.frames_to_time(onset_frames, sr=sr)
print onset_times
Convert the onset frames into sample indices.
In [11]:
onset_samples = librosa.frames_to_samples(onset_frames)
print onset_samples
Play a "beep" at each onset.
In [12]:
# Use the `length` parameter so the click track is the same length as the original signal
clicks = librosa.clicks(times=onset_times, length=len(x))
In [13]:
# Play the click track "added to" the original signal
ipd.Audio(x+clicks, rate=sr)
Out[13]:
Save into an array, segments
, 100-ms segments beginning at each onset.
In [14]:
frame_sz = int(0.100*sr)
segments = numpy.array([x[i:i+frame_sz] for i in onset_samples])
Here is a function that adds 300 ms of silence onto the end of each segment and concatenates them into one signal.
Later, we will use this function to listen to each segment, perhaps sorted in a different order.
In [15]:
def concatenate_segments(segments, sr=22050, pad_time=0.300):
padded_segments = [numpy.concatenate([segment, numpy.zeros(int(pad_time*sr))]) for segment in segments]
return numpy.concatenate(padded_segments)
In [16]:
concatenated_signal = concatenate_segments(segments, sr)
Listen to the newly concatenated signal.
In [17]:
ipd.Audio(concatenated_signal, rate=sr)
Out[17]:
For each segment, compute the zero crossing rate.
In [18]:
zcrs = [sum(librosa.core.zero_crossings(segment)) for segment in segments]
print zcrs
Use argsort
to find an index array, ind
, such that segments[ind]
is sorted by zero crossing rate.
In [19]:
ind = numpy.argsort(zcrs)
print ind
Sort the segments by zero crossing rate, and concatenate the sorted segments.
In [20]:
concatenated_signal = concatenate_segments(segments[ind], sr)
Listen to the sorted segments. What do you hear?
In [21]:
ipd.Audio(concatenated_signal, rate=sr)
Out[21]:
Repeat the steps above using other features from librosa.feature
, e.g. rmse
, spectral_centroid
, spectral_bandwidth
.
Repeat the steps above for other audio files:
In [22]:
ls audio