Important alert: (current site time 7/15/2013 6:23:24 PM EDT)
 

article

Converting Notes to Frequencies

Email
Submitted on: 6/24/2004 11:08:49 AM
By: Lawilog 
Level: Beginner
User Rating: By 9 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 12612
author picture
(About the author)
 
     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!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

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


Other 1 submission(s) by this author

 


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
6/25/2004 10:05:52 AMjames kahl

Good info, I have been looking for something like this for some time. One flaw though, recheck your notes. B and A Sharp are not the same they are half step apart. I assume what you meant was B flat/A sharp and where you have the H should actually be B
(If this comment was disrespectful, please report it.)

 
6/25/2004 10:11:00 AMLawilog

oh. i'm sorry. in german it is diffrent. the german H is what you call B. and what we call B is your B flat. i'll correct it.
(If this comment was disrespectful, please report it.)

 
6/29/2004 2:10:11 AMConrad Brinker

I'm going to make this program, I've been looking for something to fool around with for a while. Now I just have to remember how to spit out frequencies on the PC Speaker >_@! Maybe I"ll do it the hard way and learn something new about midi stuff! =)
(If this comment was disrespectful, please report it.)

 
6/29/2004 9:23:32 AMLawilog

thank you for voting :) the easiest way to make the pc-speeker play your tone is to either use the windows function Beep(freq, duration) or the dos functions (#inlcude ) sound(freq) and nosound()
(If this comment was disrespectful, please report it.)

 
7/2/2004 3:17:35 AMRobert Redwood

Well done! I rarely find code good enough to comment on, but this is top class! PSC has needed this for a long time.
(If this comment was disrespectful, please report it.)

 
7/20/2004 4:06:27 AMLars Fischmann

Good job! Now all I need is a subrutine, to play the music in the background, and I'm all set :)
(If this comment was disrespectful, please report it.)

 

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.