{
 "metadata": {
  "name": "",
  "signature": "sha256:3a3278f7c6fb206a34581be477af529e901600f877d2eeeb0456448031912235"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Pitch Transcription Exercise"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Load an audio file."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import essentia.standard as ess\n",
      "loader = ess.MonoLoader(filename='../test/test.wav', sampleRate=44100)\n",
      "signal_in = loader()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from IPython.display import Audio\n",
      "#Audio(data=signal_in, rate=44100)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Find each note and its pitch."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def get_onsets(signal_in):\n",
      "    onset_times, onset_rate = ess.OnsetRate()(signal_in)\n",
      "    return onset_times"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "yin = ess.PitchYinFFT()\n",
      "spectrum = ess.Spectrum()\n",
      "\n",
      "def get_pitch(segment):\n",
      "    if len(segment) < 4096:\n",
      "        N = len(segment) if len(segment) % 2 == 0 else len(segment) - 1\n",
      "    else:\n",
      "        N = 4096\n",
      "    pitch, pitch_conf = yin(spectrum(segment[:N]))\n",
      "    return pitch"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def generate_sine(pitch, n_duration):\n",
      "    #print midi, f0\n",
      "    n = arange(n_duration)\n",
      "    return 0.2*sin(2*pi*pitch*n/44100.0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def transcribe_pitch(signal_in):\n",
      "    \n",
      "    signal_out = zeros(len(signal_in))\n",
      "    \n",
      "    # get onsets\n",
      "    onsets = get_onsets(signal_in)\n",
      "    \n",
      "    # get pitches\n",
      "    for i in range(len(onsets)-1):\n",
      "        n0 = int(onsets[i]*44100)\n",
      "        n1 = int(onsets[i+1]*44100)\n",
      "        pitch = get_pitch(signal_in[n0:n1])\n",
      "        \n",
      "        signal_out[n0:n1] = generate_sine(pitch, n1-n0)\n",
      "    \n",
      "    return signal_out\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "signal_out = transcribe_pitch(signal_in)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Play the synthesized transcription."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import essentia\n",
      "#Audio(data=signal_out, rate=44100)\n",
      "signal_out = sum([signal_out, signal_in], axis=0)\n",
      "ess.MonoWriter(filename='../test/out.wav')(essentia.array(signal_out))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    }
   ],
   "metadata": {}
  }
 ]
}