{
 "metadata": {
  "name": "",
  "signature": "sha256:9f061a2cc6cbebeaca7a1ea19b5b37a68914601c93400dee837284902e04876f"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Unsupervised Instrument Classification Using K-Means "
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This lab is loosely based on [Lab 3](https://ccrma.stanford.edu/workshops/mir2010/Lab3_2010.pdf) (2010)."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Read Audio"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Retrieve an audio file, load it into an array, and listen to it."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import urllib\n",
      "urllib.urlretrieve?\n",
      "\n",
      "from essentia.standard import MonoLoader\n",
      "MonoLoader?\n",
      "\n",
      "from IPython.display import Audio\n",
      "Audio?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Extract Features"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Extract a set of features from the audio. Use any of the features we have learned so far: zero crossing rate, spectral moments, MFCCs, chroma, etc. For more, see the [Essentia algorithm overview](http://essentia.upf.edu/documentation/algorithms_overview.html)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "num_frames = 100 # placeholder\n",
      "num_features = 5 # placeholder\n",
      "features = zeros([num_frames, num_features]) # placeholder"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Scale Features"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use `sklearn.preprocessing.MinMaxScaler` to scale your features to be within `[-1, 1]`."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sklearn.preprocessing import MinMaxScaler\n",
      "MinMaxScaler?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Plot Features"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use `scatter` to plot features on a 2-D plane. (Choose two features at a time.)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "scatter?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Cluster Using K-Means"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use `KMeans` to cluster your features and compute labels."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sklearn.cluster import KMeans\n",
      "KMeans?"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Plot Features by Class Label"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use `scatter`, but this time choose a different marker color (or type) for each class."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Listen to Clustered Frames"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use the `concatenated_signal` function from the previous exercise to concatenate frames from the same cluster into one signal. Then listen to the signal. Compare across separate classes. What do you hear?\n",
      "\n",
      "You may want to do this for every frame, or only for onset-detected frames (using `essentia.standard.OnsetRate`)."
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Bonus"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use a different number of clusters."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use a different initialization method in `KMeans`."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use different features. Compare tonal features against timbral features."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Use different audio files."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}