# Tune a piece of music

Suppose I have an instrument which is out of tune. For example lets say, as an example, the notes it plays are as follows: C+20 cents, C#+20 cents, D+20 cents D#+20cents etc. In other words, it plays all its notes 20 cents too high.

I then record this instrument, having played polyphonic music on it.

What algorithm can I use to tune the track so that the notes become: C+0 cents, C#+0 cents, D+0 cents, D#+0 cents. I wish to solve this in the frequency domain and I have implemented an FFT and IFFT in this regard.

UPDATE

Please note that the algorithm should work out the 20 cent offset by itself without me having to supply it. I might later record another instrument with a -35 cent offset. All I know is that the notes are relatively in tune, but the instrument itself relative to other instruments might be out of tune by x cents.

I am interested in human voice, but also other instruments like violin, flute, guitar, piano and analog synth.

• What instrument are we talking about? The properties of the sound generation can affect the answer to some degree, specifically the pitch stability of the instrument. Also, are you certain that all notes are detuned equally? – Jazzmaniac Dec 4 '13 at 10:18

Essentially what you'll need to do it adjust the pitch of the audio data such that your slightly sharp notes are 20 cents flatter.

By far the easiest way to do this would be to play the audio at a lower sampling rate, which will lower the pitch accordingly. However, this approach will also slow down the original tempo.

A solution in which the signal retains its speed would be to use a time-scale modification algorithm to speed up the audio while retaining the pitch, followed by playing at a lower sampling rate, which returns the audio to the original tempo, with the desired lower pitch.

Clearly, the factor by which you adjust the time scale and pitch adjustment factor must be carefully chosen to yield the desired results.

In terms of implementation, playing back a reduced sampling frequency is (relatively) trivial. A frequency domain algorithm which can perform the time-scaling for you is the phase vocoder. You might check out the following links to help you implement it.

Good luck!

If you want to keep in the frequency domain, do you need to do a phase vocoder pitch shift !

Do you will need a very strong Pitch Track extraction method to find the frequency notes for then pass to your pitch shift what is the scale factor to be changed(tuned) in your signal, for example the midle C (c4), in a lot of documentions you will find the frequency 261.6, but its is a bit detune C4 note in -0.16 cents, follow this example the exactly C4 frequency must be 261.6256, to know the real C4 frequency just do:

 440*(2^(1/12))^-9 = 261.6256 = 0 cents


C4 is the nine key to the left of A4 then -9!

you said +20cents, for this example C4 + 20cents is 264.666hz, we a talkin about 3.0404hz difference !

Is important say that when you apply a pitch scale factor in your signal this can produce a bit artificial results, how high you need pitch scale more artificial may seem !

The phase vocoder can be implemented to change the pitch directly, without need change the tempo and then resample, for it see the code from Stephan M. Bernsee

Summarizing you have a big challenge ahead :-)

In case you don't need to write an algorithm: There are simple freeware tools that can do this for you. For example "Audacity" http://audacity.sourceforge.net/ has a very good pitch shifter.

There's an open source tool called zita-at1 that performs autotuning. If you're interested in how these things work, you may want to check it out.

http://kokkinizita.linuxaudio.org/linuxaudio/zita-at1-doc/quickguide.html