article

Converting Notes to Frequencies

Email
Submitted on: 1/6/2015 3:18:00 PM
By: Lawilog (from psc cd)  
Level: Beginner
User Rating: By 9 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 3312
 
     here i describe how can convert notes used in music (like c, d, e, f, g, a, b, c sharp, g flat,...) into frequencies you can use e.g. for the pc-speeker or whatever you like. if you think it's usefull, please vote!

 
				

Converting Notes to Frequencies

OK. To calculate any frequency, you need to know three things:

  • First: The fact, that a tone (note ?, i'm from germany...), that sounds one octave higher has a dobbeld frequency.
    So, if e.g. C has a frequency of 264 Hz, C one octave higher has 264 Hz * 2 = 528 Hz...still one octave higher it is 528 Hz * 2 = 1056 Hz - and so on.
  • Second, you need a start frequency of one special tone to create a function that calculates all the others.
    e.g. ("concert pitch") A = 440 Hz
  • And last but not least: in music it is said, that one octave has 12 halftones. (7 white and 5 black keys ;-)... )

Now, it is up to you, which halftone you suppose your concert pitch to be. On an normal (non-professional) piano keyboard concert pitch A would be the 58th one. which means, that there are still 4 lower octaves. (A is the 10th out of the 12, and 10 + 4*12 = 58)

...all these mathamitical and musical theorie, sorry...
Growing exponentially, the function has the following form: f(n) = k * a^n.
You know, the 58th halftone has a freq of 440. One octave higher, the 70th halftone (58+12=70) has a dobbeled freq of 880.
good. that means:
440 = k * a^58 and
880 = k * a^70
Now you transform both equqtions to "k = ...", set them equal, and calculate a with 2^(1/12). Now you use the caclulated a in one equation to get k with 440 * 2^(-58/12).
Now, you have your equation f = 440 * 2^(-58/12) * (2^(1/12))^n which is equal to:

f = 440 * 2^((n - 58)/12)

And that's it !

I will give you some examples:

note / tone n frequency in Hz
.........
B (one octace below)48246.9
C49261.6
C sharp (D flat)50277.2
D51293.7
D sharp (E flat)52311.1
E53329.6
F54349.2
F sharp (G flat)55370.0
G56392.0
G sharp (A flat)57415.3
A58440
A sharp (B flat)59466.2
B60493.9
C (next octave)61523.3
.........

So far, so good.
Now, it is up to you, to create a code, that can play meldoies. Maybe you would like to have a function first, that converts the note-string ("c", "g", "d flat", "g sharp",...) to a frequency. Than you may need a good way of storing the melodie-data (with note length and so on); a playing engine; even a midi-driver is possible... :D
But this is not what I am going to do for you. :D


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.