If you have an audio sample that was recorded at one pitch and needs to be played back at a different pitch, it's useful to know how to shift pitch up or down.
If you know the original sample rate and how many semitones you want to shift the pitch of your sample, here's how you make the calculation:
[Original sample rate] * 2.0 to the [Semitone Shift]/12th Power.
In C# code, this equation would be:
new_samplerate = (System.Math.Pow( 2.0, (semitones / 12.0 )) * original_samplerate);
This same calculation also works for BPM (beats per minute) calculations of the same nature:
new_bpm = (System.Math.Pow( 2.0, (semitone_shift / 12.0 )) * original_bpm);
The reverse calculation can be made if you have a target sample rate you want to reach from your original sample rate and need to know how many semitones to shift your sample to get there.
semitone_shift = (System.Math.Log((new_samplerate/old_samplerate)) / System.Math.Log(2.0)) * 12;
To do the same thing in C/C++ code, replace the System.Math functions with log() and pow() from the math library.
The Sound Programming site is copyright © 2006-2024 Jason Champion.