The origins of our musical system began with Pythagoras around 500 BC. He created an instrument with a single string called a mono chord. He experimented by dividing it into segments and listening to the results. The first thing he noticed was that if you divide it into two segments you get a note which is higher but sounds equal in color to the first. This is what we call an octave. Then he noticed if you divide it into three segments you get another higher pitch which sounds slightly different in color to the first note. This is what we call a perfect fifth. In music notation this would be C G.
Once he determined that the perfect fifth was important he repeated the process 12 times using the perfect fifth interval. This produced pitches more or less equivalent to our modern circle of fifths. C G D A E B F# C# G# D# A# F C. However when he got back to the note C , it was not a perfect match but rather a little sharper than the original. This is known as the Pythagorean comma. Therefore in this system sharps and flats are slightly different in pitch.
Musicians and theorist tolerated this discrepancy for hundreds of years. In 1450 another system appeared called just tuning. This tried to use other fractions besides the perfect fifth to create purer sounding intervals.
Octave 2/1
Fifth 3/2
Fourth 4/3
Major 3rd 5/4
Minor third 6/5
Major 2nd 9/8, or 10/9
Etc…
The system also suffered from a similar disadvantage. Namely not all the intervals could be pure and coexist. For example, three Major thirds (an augmented triad) should take you back to the octave.
C E G# C
However, mathematically…
5/4 x 5/4 x 5/4 = 125/64 = 1.953
1.953 does not equal 2/1
In 1722 JS Bach published a book called the Well Tempered Clavier generally regarded as the most important book of music ever published. He proposed that rather than using pure mathematical ratios we should “temper” the ratios slightly to agree with one another. Pythagorean tuning yields an error of about 25 cents. He reasoned that if he took each perfect fifth and tuned it slightly flat by about two cents (remember he was doing this by ear, not a computer) after he completed the circle of twelve fifths he would get back to a more or less pure octave.
12 x .02 = .24 which is close to .25
To prove his point he composed 48 pieces of music in each of the 12 major and minor keys. This would be impossible or at least sound very poor in Pythagorean or Just Tuning.
This is the musical system we use today known as equal temperament. That is why we have 12 notes in our system and also how we derive the tuning.