%matplotlib inline
import numpy, scipy, matplotlib.pyplot as plt, IPython.display as ipd
import sklearn, pandas
import librosa, librosa.display
import stanford_mir; stanford_mir.init()
Load 30 seconds of an audio file:
filename_brahms = 'audio/brahms_hungarian_dance_5.mp3'
x_brahms, sr_brahms = librosa.load(filename_brahms, duration=30)
Load 30 seconds of another audio file:
filename_busta = 'audio/busta_rhymes_hits_for_days.mp3'
x_busta, sr_busta = librosa.load(filename_busta, duration=30)
Play the audio files:
ipd.Audio(x_brahms, rate=sr_brahms)
ipd.Audio(x_busta, rate=sr_busta)
Plot the time-domain waveform of the audio signals:
plt.figure(figsize=(14, 5))
librosa.display.waveplot(x_brahms, sr_brahms)
plt.figure(figsize=(14, 5))
librosa.display.waveplot(x_busta, sr_busta)
Plot the spectrograms:
S_brahms = librosa.feature.melspectrogram(x_brahms, sr=sr_brahms)
Sdb_brahms = librosa.amplitude_to_db(S_brahms)
plt.figure(figsize=(15, 5))
librosa.display.specshow(Sdb_brahms, sr=sr_brahms, x_axis='time', y_axis='mel')
plt.colorbar()
S_busta = librosa.feature.melspectrogram(x_busta, sr=sr_busta)
Sdb_busta = librosa.amplitude_to_db(S_busta)
plt.figure(figsize=(15, 5))
librosa.display.specshow(Sdb_busta, sr=sr_busta, x_axis='time', y_axis='mel')
plt.colorbar()
In what ways do the time-domain waveform and spectrogram differ between the two files? What differences in musical attributes might this reflect? What additional insights are gained from plotting the spectrogram?
For each segment, compute the MFCCs. Experiment with n_mfcc
to select a different number of coefficients, e.g. 12.
n_mfcc = 12
mfcc_brahms = librosa.feature.mfcc(x_brahms, sr=sr_brahms, n_mfcc=n_mfcc).T
We transpose the result to accommodate scikit-learn which assumes that each row is one observation, and each column is one feature dimension:
mfcc_brahms.shape
Scale the features to have zero mean and unit variance:
scaler = sklearn.preprocessing.StandardScaler()
mfcc_brahms_scaled = scaler.fit_transform(mfcc_brahms)
Verify that the scaling worked:
mfcc_brahms_scaled.mean(axis=0)
mfcc_brahms_scaled.std(axis=0)
Extract MFCCs from the second audio file.
mfcc_busta = librosa.feature.mfcc(x_busta, sr=sr_busta, n_mfcc=n_mfcc).T
We transpose the resulting matrix so that each row is one observation, i.e. one set of MFCCs. Note that the shape and size of the resulting MFCC matrix is equivalent to that for the first audio file:
print(mfcc_brahms.shape)
print(mfcc_busta.shape)
Scale the resulting MFCC features to have approximately zero mean and unit variance. Re-use the scaler from above.
mfcc_busta_scaled = scaler.transform(mfcc_busta)
Verify that the mean of the MFCCs for the second audio file is approximately equal to zero and the variance is approximately equal to one.
mfcc_busta_scaled.mean(axis=0)
mfcc_busta_scaled.std(axis=0)
Concatenate all of the scaled feature vectors into one feature table.
features = numpy.vstack((mfcc_brahms_scaled, mfcc_busta_scaled))
features.shape
Construct a vector of ground-truth labels, where 0 refers to the first audio file, and 1 refers to the second audio file.
labels = numpy.concatenate((numpy.zeros(len(mfcc_brahms_scaled)), numpy.ones(len(mfcc_busta_scaled))))
Create a classifer model object:
# Support Vector Machine
model = sklearn.svm.SVC()
Train the classifier:
model.fit(features, labels)
To test the classifier, we will extract an unused 10-second segment from the earlier audio fields as test excerpts:
x_brahms_test, sr_brahms = librosa.load(filename_brahms, duration=10, offset=120)
x_busta_test, sr_busta = librosa.load(filename_busta, duration=10, offset=120)
Listen to both of the test audio excerpts:
ipd.Audio(x_brahms_test, rate=sr_brahms)
ipd.Audio(x_busta_test, rate=sr_busta)
Compute MFCCs from both of the test audio excerpts:
mfcc_brahms_test = librosa.feature.mfcc(x_brahms_test, sr=sr_brahms, n_mfcc=n_mfcc).T
mfcc_busta_test = librosa.feature.mfcc(x_busta_test, sr=sr_busta, n_mfcc=n_mfcc).T
print(mfcc_brahms_test.shape)
print(mfcc_busta_test.shape)
Scale the MFCCs using the previous scaler:
mfcc_brahms_test_scaled = scaler.transform(mfcc_brahms_test)
mfcc_busta_test_scaled = scaler.transform(mfcc_busta_test)
Concatenate all test features together:
features_test = numpy.vstack((mfcc_brahms_test_scaled, mfcc_busta_test_scaled))
Concatenate all test labels together:
labels_test = numpy.concatenate((numpy.zeros(len(mfcc_brahms_test)), numpy.ones(len(mfcc_busta_test))))
Compute the predicted labels:
predicted_labels = model.predict(features_test)
Finally, compute the accuracy score of the classifier on the test data:
score = model.score(features_test, labels_test)
score
Currently, the classifier returns one prediction for every MFCC vector in the test audio signal. Let's modify the procedure above such that the classifier returns a single prediction for a 10-second excerpt.
predicted_labels = model.predict(mfcc_brahms_test_scaled)
numpy.argmax([(predicted_labels == c).sum() for c in (0, 1)])
predicted_labels = model.predict(mfcc_busta_test_scaled)
numpy.argmax([(predicted_labels == c).sum() for c in (0, 1)])
Read the MFCC features from the first test audio excerpt into a data frame:
df_brahms = pandas.DataFrame(mfcc_brahms_test_scaled)
df_brahms.shape
df_brahms.head()
df_busta = pandas.DataFrame(mfcc_busta_test_scaled)
Compute the pairwise correlation of every pair of 12 MFCCs against one another for both test audio excerpts. For each audio excerpt, which pair of MFCCs are the most correlated? least correlated?
df_brahms.corr()
df_busta.corr()
Display a scatter plot of any two of the MFCC dimensions (i.e. columns of the data frame) against one another. Try for multiple pairs of MFCC dimensions.
df_brahms.plot.scatter(1, 2, figsize=(7, 7))
Display a scatter plot of any two of the MFCC dimensions (i.e. columns of the data frame) against one another. Try for multiple pairs of MFCC dimensions.
df_busta.plot.scatter(1, 2, figsize=(7, 7))
Plot a histogram of all values across a single MFCC, i.e. MFCC coefficient number. Repeat for a few different MFCC numbers:
df_brahms[0].plot.hist(bins=20, figsize=(14, 5))
df_busta[0].plot.hist(bins=20, figsize=(14, 5))
df_brahms[11].plot.hist(bins=20, figsize=(14, 5))
df_busta[11].plot.hist(bins=20, figsize=(14, 5))