{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Audio analysis with TimeSide and [Aubio](http://aubio.org)\n",
    "\n",
    "In the following example, we illustrate how to perform common audio signal analysis tasks like *pitch estimation* and *onsets detection* with **TimeSide** by using some analyzers based on [Aubio](http://aubio.org)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%pylab inline\n",
    "\n",
    "from __future__ import division\n",
    "import matplotlib.pylab as pylab\n",
    "pylab.rcParams['figure.figsize'] = 16, 8  # that's default image size for this interactive session\n",
    "\n",
    "import timeside\n",
    "from timeside.core import get_processor\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "# Audio Source = a 15s excerpt of Peter and the Wolf by Prokofiev\n",
    "# from the Internet Archive : https://archive.org/details/PeterAndTheWolf_753\n",
    "#audiofile = 'https://ia801408.us.archive.org/3/items/PeterAndTheWolf_753/Peter_and_the_Wolf.mp3'\n",
    "audiofile = 'https://ia801408.us.archive.org/3/items/PeterAndTheWolf_753/PeterAndTheWolf_01.mp3'\n",
    "## Setup the processing pipe\n",
    "file_decoder = get_processor('file_decoder')(uri=audiofile, start=5, duration=15)\n",
    "aubio_pitch = get_processor('aubio_pitch')()\n",
    "aubio_temporal = get_processor('aubio_temporal')()\n",
    "specgram_ = get_processor('spectrogram_analyzer')()\n",
    "waveform = get_processor('waveform_analyzer')()\n",
    "\n",
    "\n",
    "pipe = (file_decoder | aubio_pitch | aubio_temporal | specgram_ | waveform)\n",
    "pipe.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display Spectrogram + Aubio Pitch + Aubio Beat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.figure(1)\n",
    "\n",
    "spec_res = specgram_.results['spectrogram_analyzer']\n",
    "N = spec_res.parameters['fft_size']\n",
    "plt.imshow(20 * np.log10(spec_res.data.T),\n",
    "           origin='lower',\n",
    "           extent=[spec_res.time[0], spec_res.time[-1], 0,\n",
    "                   (N // 2 + 1) / N * spec_res.data_object.frame_metadata.samplerate],\n",
    "           aspect='auto')\n",
    "\n",
    "res_pitch = aubio_pitch.results['aubio_pitch.pitch']\n",
    "plt.plot(res_pitch.time, res_pitch.data)\n",
    "\n",
    "res_beats = aubio_temporal.results['aubio_temporal.beat']\n",
    "\n",
    "for time in res_beats.time:\n",
    "    plt.axvline(time, color='r')\n",
    "\n",
    "plt.title('Spectrogram + Aubio pitch + Aubio beat')\n",
    "plt.grid()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display waveform + Onsets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.figure(2)\n",
    "res_wave = waveform.results['waveform_analyzer']\n",
    "plt.plot(res_wave.time, res_wave.data)\n",
    "res_onsets = aubio_temporal.results['aubio_temporal.onset']\n",
    "for time in res_onsets.time:\n",
    "    plt.axvline(time, color='r')\n",
    "plt.grid()\n",
    "plt.title('Waveform + Aubio onset')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "res_pitch.render();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "res_beats.render();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "res_onsets.render();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
