{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy, scipy, librosa, IPython.display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[← Back to Index](index.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pitch Transcription Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load an audio file." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x, fs = librosa.load('zigeunerweisen.wav')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Play the audio file." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IPython.display.Audio(x, rate=fs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Goal**: to identify the pitch of each note and replace each note with a pure tone of that pitch." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Detect onsets:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def get_onset_times(x, fs):\n", " onset_frames = librosa.onset.onset_detect(x, fs)\n", " return librosa.frames_to_time(onset_frames, fs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Estimate pitch using the autocorrelation method:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def estimate_pitch(segment, fs, fmin=50.0, fmax=2000.0):\n", " i_min = fs/fmax\n", " i_max = fs/fmin\n", " r = librosa.autocorrelate(segment)\n", " r[:i_min] = 0\n", " r[i_max:] = 0\n", " i = r.argmax()\n", " f0 = float(fs)/i\n", " return f0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try it out on one frame:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "196.875\n" ] } ], "source": [ "f0 = estimate_pitch(x[:2048], fs)\n", "print f0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a function to generate a pure tone at the specified frequency:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def generate_sine(f0, fs, n_duration):\n", " n = numpy.arange(n_duration)\n", " return 0.2*numpy.sin(2*numpy.pi*f0*n/float(fs))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, write a function that puts it all together:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def transcribe_pitch(signal_in, fs):\n", " \n", " # initialize output signal\n", " signal_out = numpy.zeros(len(signal_in))\n", " \n", " # get onsets\n", " onsets = get_onset_times(signal_in, fs)\n", " \n", " # get pitches\n", " for i in range(len(onsets)-1):\n", " n0 = int(onsets[i]*fs)\n", " n1 = int(onsets[i+1]*fs)\n", " pitch = estimate_pitch(signal_in[n0:n1], fs, fmin=60, fmax=4000)\n", " \n", " signal_out[n0:n1] = generate_sine(pitch, fs, n1-n0)\n", " \n", " return signal_out\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try it out on the input signal:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "signal_out = transcribe_pitch(x, fs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Play the synthesized transcription." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IPython.display.Audio(signal_out, rate=fs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[← Back to Index](index.html)" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }