Ok, so if I have an audio clip which is middle C, how much do I have to add to the pitch to make it go up to a D? What is the generic pitch rule (if there is one)?
The general rule is to multiply the frequency by 1.05946^n, where n is the number of semitones you want to raise it.
Thus, to raise the pitch from C to D (two semitones) you set the pitch property to
1.05946^2, which equals 1.12245549. (You will see that 1.05946^12 == 2, corresponds to raising the pitch by an octave, which is consistent with what RoflHarris is saying).
You can test with values from this table:
http://www.phy.mtu.edu/~suits/notefreqs.html
The way the pitch works is that 1 is standard pitch and going to 2 is double the frequency.
So if middle C is 261.626Hz and D4 is 293.665Hz then you simply need to calculate the increase in pitch required.
293.665-261.626=32.039
1 octave ( or to 2 ) 261.626
So 32.039/261.626 = 0.122461
So increase the pitch to 1.122461 to change something that is pitch middle C to D4
Use Mathf.Pow and the twelfth root of two (approx 1.0594). for incrementing / decrementing pitch by semitones
Twelfth root of two - Wikipedia
Add this script to an AudioSource to increment the pitch of the sound by a semitone every half second:
using UnityEngine;
using System.Collections;
public class Notescale : MonoBehaviour {
float scale = Mathf.Pow (2f,1.0f/12f);
AudioSource sound;
void Start () {
sound = GetComponent<AudioSource> ();
StartCoroutine(PlayScale ());
}
IEnumerator PlayScale () {
for (int i=0; i<72; i++) {
sound.pitch = Mathf.Pow (scale,i);
sound.Play ();
yield return new WaitForSeconds(0.5f);
}
}
}
One more answer to help musical noobies: the note sequence is A-A#-B-C-C#-D-D#-E-F-F#-G-G#.
Notice that there are no semitones between B-C and E-F, but every other pair has semitones in between (the #notes). These sandwiched semitones correspond to the piano’s small black keys, and the full notes are the white ones.
So, if you have an E and wants a G, you must change the pitch to 1.05946^3, since there are three semitones in between.
Finally, the scale is endless: after G# comes another A, but with exactly 2 times the previous A pitch, and so on.
FYI: I calculated it and 1.059463 is in fact closer to getting 2.
If you want a double: 1.05946309435928
This is almost always going to be irrelevant but in some cases accuracy will matter. Note that these values are rounded off as floats:
1.05946 >1.999930
1.059462 > 1.999975
1.059463 > 1.999998
1.059464 > 2.000021
and that double
1.05946309435928 > 1.99999999999965
if you use the first value which is the furthest and you skip 2 octaves that will give you
1.05946^24 == 3.999720
1.059463^24 == 3.999991
and again that double
1.05946309435928^24 == 3.99999999999862
but then again I think 4 == 4 so that’s a thing too.