#!/usr/bin/env python # coding: utf-8 # In[ ]: import numpy, scipy, matplotlib.pyplot as plt, sklearn, librosa, mir_eval, urllib, IPython.display, stanford_mir plt.rcParams['figure.figsize'] = (14,5) # [← Back to Index](index.html) # # Exercise: Instrument Classification using K-NN # This exercise is loosely based upon "Lab 1" from previous MIR workshops ([2010](https://ccrma.stanford.edu/workshops/mir2010/Lab1_2010.pdf)). # For more on K-NN, see the [notebook on K-NN](knn.ipynb). # For help from a similar exercise, [follow the steps in the feature sonification exercise](feature_sonification.ipynb#Step-1:-Retrieve-Audio) first. # ## Goals # 1. Extract spectral features from an audio signal. # 2. Train a K-Nearest Neighbor classifier. # 3. Use the classifier to classify beats in a drum loop. # ## Step 1: Retrieve Audio, Detect Onsets, and Segment # Download the file `simple_loop.wav` onto your local machine. # In[ ]: filename = 'simple_loop.wav' get_ipython().run_line_magic('pinfo', 'urllib.urlretrieve') # Load the audio file: # In[ ]: get_ipython().run_line_magic('pinfo', 'librosa.load') # Play the audio file: # In[ ]: get_ipython().run_line_magic('pinfo', 'IPython.display.Audio') # Detect onsets: # In[ ]: get_ipython().run_line_magic('pinfo', 'librosa.onset.onset_detect') # Convert onsets from frames to seconds (and samples): # In[ ]: get_ipython().run_line_magic('pinfo', 'librosa.frames_to_time') # In[ ]: get_ipython().run_line_magic('pinfo', 'librosa.frames_to_samples') # Listen to a click track, with clicks located at each onset, plus the original audio: # In[ ]: get_ipython().run_line_magic('pinfo', 'mir_eval.sonify.clicks') # In[ ]: get_ipython().run_line_magic('pinfo', 'IPython.display.Audio') # ## Step 2: Extract Features # For each segment, compute the zero crossing rate and spectral centroid. # In[ ]: get_ipython().run_line_magic('pinfo', 'librosa.zero_crossings') # In[ ]: get_ipython().run_line_magic('pinfo', 'librosa.feature.spectral_centroid') # Scale the features to be in the range [-1, 1]: # In[ ]: get_ipython().run_line_magic('pinfo', 'sklearn.preprocessing.MinMaxScaler') # In[ ]: get_ipython().run_line_magic('pinfo', 'sklearn.preprocessing.MinMaxScaler.fit_transform') # ## Step 3: Train K-NN Classifier # Use `stanford_mir.download_drum_samples` to download ten kick drum samples and ten snare drum samples. Each audio file contains a single drum hit at the beginning of the file. # In[ ]: get_ipython().run_line_magic('pinfo', 'stanford_mir.download_drum_samples') # For each audio file, extract one feature vector. Concatenate all of these feature vectors into one feature table. # In[ ]: get_ipython().run_line_magic('pinfo', 'numpy.concatenate') # ## Step 4: Run the Classifier # Create a K-NN classifer model object: # In[ ]: get_ipython().run_line_magic('pinfo', 'sklearn.neighbors.KNeighborsClassifier') # Train the classifier: # In[ ]: get_ipython().run_line_magic('pinfo', 'sklearn.neighbors.KNeighborsClassifier.fit') # Finally, run the classifier on the test input audio file: # In[ ]: get_ipython().run_line_magic('pinfo', 'sklearn.neighbors.KNeighborsClassifier.predict') # ## Step 5: Sonify the Classifier Output # Play a "beep" for each detected kick drum. Repeat for the snare drum. # In[ ]: get_ipython().run_line_magic('pinfo', 'mir_eval.sonify.clicks') # ## For Further Exploration # In addition to the features used above, extract the following features: # # - spectral centroid # - spectral spread # - spectral skewness # - spectral kurtosis # - spectral rolloff # - MFCCs # Re-train the classifier, and re-run the classifier over the test audio signal. Do the results change? # Repeat the steps above for more audio files. # [← Back to Index](index.html)