For western music the greeks were the first to figure it out. It started off by the naturalmath that occurs naturally in the harmonics seriesovertones generated by trumpets or horns and then theyother wind instruments. The Greeks applied the same mathematical ratios (golden ratio) to strings. Pythagoras invented the pythagorean tuning of (3:2) perfect fifths and Octaves (2:1) to match naturalynaturally occurring harmonic overtones. Later the greeks invented 7 modal scales based on pythagorean tuning. 7Seven Modes with 8eight notes in a scale. These scales were Ionian, Dorian, Phrygian, Lydian, Mixolydian, Aeolian and Locrian. We still use Ionian (Major) and Aeolian(Minor). Like you mentioned theThe flaw with natural harmonics is that the octaves between each mode were slightly off from each other. Aristoxenus in the 4th century BC invented the 12 tones between octaves. In in an attempt to use the early 1700'ssame ratio between each note. Later Keys were invented to use these 12 tones as a home base for each scale. The problem was that by nature these keys are slightly off from each other. To solve this J.S. Bach was a big proponent ofin the usingearly 1700's promoted the (Tempered Scale equalizinguse of the 12 tones by going sharp or flat between each tone to equalizingTempered Scale. He equalized the natural gapsoccurring gap between each of the 12twelve semitones. Brass instruments in the baroque period had a bag of different sized crooks to adjust for each Keykey that they playedperformed in. String Instrumentsinstruments also had to retune for each key change. By using the tempered scale youa performer could switch between all of the different keys without re-tuning.