Important alert: (current site time 7/15/2013 6:23:24 PM EDT)
|
|
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 |
|
(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...
- 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.
- 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.
- You may link to this article from another website, but ONLY if it is not wrapped in a frame.
- 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) | 48 | 246.9 |
| C | 49 | 261.6 |
| C sharp (D flat) | 50 | 277.2 |
| D | 51 | 293.7 |
| D sharp (E flat) | 52 | 311.1 |
| E | 53 | 329.6 |
| F | 54 | 349.2 |
| F sharp (G flat) | 55 | 370.0 |
| G | 56 | 392.0 |
| G sharp (A flat) | 57 | 415.3 |
| A | 58 | 440 |
| A sharp (B flat) | 59 | 466.2 |
| B | 60 | 493.9 |
| C (next octave) | 61 | 523.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
|
Your Vote
|
| |
Other User Comments
|
6/25/2004 10:05:52 AM: james 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 AM: Lawilog
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 AM: Conrad 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 AM: Lawilog
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 AM: Robert 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 AM: Lars 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.
|
|