Basics: From Frequencies to Equal Temperament - tschw/perfect-harmony GitHub Wiki

Sounds we perceive are vibrations, usually of air, picked up by our ears. When we half the length of a string (like tapping the 12th fret on a guitar, for example) and pluck it, we get the same musical note just higher. In this case, the string swings twice as fast or, in other, more technical terms; has twice the frequency. Together with the note from a string swinging at full length we get the most consonant, that is musically pleasing in the sense of least tension and highest amount of neutrality, interval called an octave (*).

When we take a third of the string instead, we have the string swing three times as fast and get a so called perfect fifth (*) in the next octave. This too sounds consonant, but introduces some harmonic content rather than just repeating the note - it's the next consonant interval (a.k.a. the 2nd harmonic or overtone when talking about components of complex sounds as from a single note played on an actual instrument rather than a measure of distance in musical composition).

As implied before, pitch is like many other natural quantities that refer to perception exponential, so distances are factors (that is, get multiplied - in contrast to linear quantities in which distances sum up). Keeping that in mind, the perfect fifth in the same octave is found at 2/3 = 0.666.. times the length of the string. By the same reasoning it is easy to see that taking fourth the length of a string, it swings two octaves higher than the full string. Also, only prime numbers (as by definition of their very nature) introduce distances that can not be expressed as factors of lower ones and those start sounding spicily dissonant real soon.

So let's instead examine what we get from stacking up perfect fifths and look at where it gets us in the octave between 440 and 880 Hertz. This unit refers to frequency and means periods (that is repetitions of a waveform / vibrations) per second.

One octave higher means twice as many swings in the same amount of time, so we divide by two until we're in the frequency range of a single octave:

for (i = 0; i < 14; ++i) {
	note = 440 * 3**i; // ** is exponentiation: 3*3*3..., i times, 1 for i=0
	while (note >= 880) note /= 2;
	console.log(note);
}

The little program produces the following output:

440
660
495
742.5
556.875
835.3125
626.484375
469.86328125
704.794921875
528.59619140625
792.894287109375
594.6707153320312
446.00303649902344 
669.0045547485352

At 446... we're almost back to where we started! Now let's sort these frequencies and look at the factor between consecutive ones:

notes = [];
for (i = 0; i < 12; ++i) { 
	note = 440* 3**i;
	while (note >= 880) note /= 2;
	notes.push(note);
}
notes.sort();
prev = 0;
for (note of notes) {
	if (prev != 0) {
		console.log(prev);
		console.log(`* ${note / prev} =`);
	}
	prev = note;
}
console.log(notes.at(-1));

The little program produces the following output:

440 
* 1.06787109375 = 
469.86328125 
* 1.0534979423868314 = 
495 
* 1.06787109375 = 
528.59619140625 
* 1.0534979423868314 = 
556.875 
* 1.06787109375 = 
594.6707153320312 
* 1.0534979423868314 = 
626.484375 
* 1.0534979423868314 = 
660 
* 1.06787109375 = 
704.794921875 
* 1.0534979423868314 = 
742.5 
* 1.06787109375 = 
792.894287109375 
* 1.0534979423868314 =
835.3125

Turns out the factors are very similar!

Applying the factor once more we want to arrive at an octave, so let's make that happen choosing the factor to be the 12th root of two (which happens to be 1.05946...):

for (i = 0; i < 14; ++i) {
	note = 440 * 2**(i/12);
	while (note >= 880) note /= 2;
	console.log(note);
}

The little program produces the following output:

440 
466.1637615180899 
493.8833012561241 
523.2511306011972 
554.3652619537442 
587.3295358348151 
622.2539674441618 
659.2551138257398 
698.4564628660078 
739.9888454232688 
783.9908719634985 
830.6093951598903 
440
466.1637615180899

Bingo! We arrive back at 440 Hz!

These frequencies are called equal temperament and what non-historical, musical instruments are typically being tuned to. 440 Hz is an A, so the corresponding note names are A, A♯ or B♭, B, C, C♯ or D♭, D, D♯ or E♭, E, F, F♯ or G♭, G, G♯ or A♭, finally repeated A and A♯ or B♭ again.

Allowing to leave the single octave like this...

for (i = 12; i < 14; ++i) {
	note = 440 * 2**(i/12);
	console.log(note);
}

..the last two values we get are A and A♯ or B♭ in the next octave:

880
932.3275230361799

(*) The names of the intervals may seem off without prior knowledge in music here. That is so because they refer to positions within musical scales, which are selections of the twelve notes and beyond the scope of this discussion, and instead detailed in this article.

Sorting the twelve notes, as we do here, we derive neighboring notes with the distance of a semitone in respect to each other. The fifth is at a distance of 7 semitones ahead, but can also be reached going back by 5 semitones, (called a fourth), because musical notes repeat in octaves of twelve semitones and 7 + 5 = 12.