I am programming a small synthesizer application and I input musical notes by clicking along the length of a bar. Now, the musical scale is logarithmic and my question is, how do I convert the position of the mouse to a relevant pitch. At the moment I calculate a ratio. It works, sort of, but I get a wide range of closely packed low notes, and at the far end I takes of with just a few pixels translating into multiple octaves. Basically I want, if I click at the center of the bar (1/2), the frequency is doubled, and 1/4 is another double in freq. etc… I'm being stupid here!
Frequencies of musical notes are indeed logarithmic. The frequency doubles when you go one octave higher, and halves when you go one octave lower. A standard A is exactly 440Hz.
So, you need a power law to translate location into a frequency. Something like f*2.0^(x/w) where w is the width of an octave, f is a scale factor and ^ is the power operator.