| | |
Create Sound
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I dusted off this little code snippet from the good old DOS days. It will answer some of your questions on how to calculate the musical scales. If you need any more details, get friendly with a musician!
C++ Syntax (Toggle Plain Text)
/*********************** play.c *********************** ** ** Experimentation with sound: ** play(int octave,int note,int duration) ** Written in Turbo C V.2.0 by vegaseat 6/15/88 ** *********************************************************/ /************** note.h ***********/ #define P 0 /* pause */ #define C 1 #define CS 2 /* C sharp */ #define D 3 #define DS 4 #define E 5 #define F 6 #define FS 7 #define G 8 #define GS 9 #define A 10 #define AS 11 #define B 12 #define EN 75 /* eighth note */ #define QN 150 /* quarter note */ #define HN 300 /* half note */ #define FN 600 /* full note */ /************************************/ void play(int octave,int note,int duration); void british(void); /* Westminster Bells */ void main(void) /* test play() */ { british(); getch(); } void play(int octave,int note,int duration) /* play note (C=1 to B=12), in octave (1-8), and duration (msec) include NOTE.H for note values */ { int k; double frequency; if (note == 0) { /* pause */ delay(duration); return; } frequency = 32.625; for (k = 0; k < octave; k++) /* compute C in octave */ frequency *= 2; for (k = 0; k < note; k++) /* frequency of note */ frequency *= 1.059463094; /* twelve root of 2 */ delay(5); /* delay between keys */ sound((int) frequency); /* sound the note */ delay(duration); /* for correct duration */ nosound(); } void british(void) /* Westminster Bells, sort of */ { play(4,E,HN); play(4,C,HN); play(4,D,HN); play(3,G,HN+QN); play(3,P,QN); play(3,G,HN); play(4,D,HN); play(4,E,HN); play(4,C,HN+QN); } /* For XP/NT that don't allow port outputs there is a WIN32 API call: The Beep function generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes. BOOL Beep( DWORD dwFreq, // sound frequency, in hertz DWORD dwDuration // sound duration, in milliseconds ); */
May 'the Google' be with you!
•
•
Join Date: Oct 2004
Posts: 1
Reputation:
Solved Threads: 0
It is not that difficult and even though I am a musician you don't really need to be one just to determine the frequency of each musical note.
First, c,d,e,f,g,a,b are not all the musical notes that exist in the eastern music system, in fact there are twelve different notes (see those black keys in a piano keyboard?) and they are:
a, a#, b, c, c#, d, d#, e, f, f#, g, g#
Second, the american standard pitch states that the note A has a frequency of 440, and this is the point of reference to determine every other note.
The third part is just mathematics, and it is a simple formula: The frequency of the subsequent note equals the current note multiplied by the 12th root of 2 (the 12th root of 2 can also be expresed as 2 elevated at 1/12, or 2^(1/12)). So with this formula let's make an example: suppose you already have the note A which is 440 and you want to determine the next note which is A#, the mathematical notation is:
A# = A * (2^(1/12))
which is 440 * (2^(1/12)) and equals 466.1637...
following this sequence:
B = A# * (2^(1/12))
C = B * (2^(1/12))
C# = C * (2^(1/12))
and so on.....
You'll get a lot of decimal numbers in all of the notes (except of all A's) and the good news is that you can round to the nearest integer as this difference is imperceptible to the human ear.
Now, what happens to all the notes below A 440???? It is very easy, you just need to use the same formula, except that instead of multiplying you need to divide, for example, to get the note before A:
G# = A / (2^(1/12))
Now you can get any range of notes you want in any scale. Note that this approach requires you to obtain all the notes in a sequential mode. If you want to get a note directly from A 440 you tweak the formula a little bit. Let's say you want to obtain the next F after A 440, F would be the 8th note after A, so you multiply by the 12th root of 2 elevated at the number of steps after the current note, in this case eight. So:
F = A * (2^(1/12))^8
I hope this helps, and sorry if my english is not that good since I'm from Mexico.
First, c,d,e,f,g,a,b are not all the musical notes that exist in the eastern music system, in fact there are twelve different notes (see those black keys in a piano keyboard?) and they are:
a, a#, b, c, c#, d, d#, e, f, f#, g, g#
Second, the american standard pitch states that the note A has a frequency of 440, and this is the point of reference to determine every other note.
The third part is just mathematics, and it is a simple formula: The frequency of the subsequent note equals the current note multiplied by the 12th root of 2 (the 12th root of 2 can also be expresed as 2 elevated at 1/12, or 2^(1/12)). So with this formula let's make an example: suppose you already have the note A which is 440 and you want to determine the next note which is A#, the mathematical notation is:
A# = A * (2^(1/12))
which is 440 * (2^(1/12)) and equals 466.1637...
following this sequence:
B = A# * (2^(1/12))
C = B * (2^(1/12))
C# = C * (2^(1/12))
and so on.....
You'll get a lot of decimal numbers in all of the notes (except of all A's) and the good news is that you can round to the nearest integer as this difference is imperceptible to the human ear.
Now, what happens to all the notes below A 440???? It is very easy, you just need to use the same formula, except that instead of multiplying you need to divide, for example, to get the note before A:
G# = A / (2^(1/12))
Now you can get any range of notes you want in any scale. Note that this approach requires you to obtain all the notes in a sequential mode. If you want to get a note directly from A 440 you tweak the formula a little bit. Let's say you want to obtain the next F after A 440, F would be the 8th note after A, so you multiply by the 12th root of 2 elevated at the number of steps after the current note, in this case eight. So:
F = A * (2^(1/12))^8
I hope this helps, and sorry if my english is not that good since I'm from Mexico.
Sounds like you got lost with the calculations of the notes of the musical scale I gave you in my sample code. Well here is a table of notes and associated frequencies:
Trying to get fancy, hope it worked.
C++ Syntax (Toggle Plain Text)
Frequency Note MIDI# 8.1758 0 8.6620 1 9.1770 2 9.7227 3 10.3009 4 10.9134 5 11.5623 6 12.2499 7 12.9783 8 13.7500 9 14.5676 10 15.4339 11 16.3516 12 17.3239 13 18.3540 14 19.4454 15 20.6017 16 21.8268 17 23.1247 18 24.4997 19 25.9565 20 27.5000 A0 21 29.1352 A#0 22 30.8677 B0 23 32.7032 C1 24 34.6478 C#1 25 36.7081 D1 26 38.8909 D#1 27 41.2034 E1 28 43.6535 F1 29 46.2493 F#1 30 48.9994 G1 31 51.9131 G#1 32 55.0000 A1 33 58.2705 A#1 34 61.7354 B1 35 65.4064 C2 36 69.2957 C#2 37 73.4162 D2 38 77.7817 D#2 39 82.4069 E2 40 87.3071 F2 41 92.4986 F#2 42 97.9989 G2 43 103.8262 G#2 44 110.0000 A2 45 116.5409 A#2 46 123.4708 B2 47 130.8128 C3 48 138.5913 C#3 49 146.8324 D3 50 155.5635 D#3 51 164.8138 E3 52 174.6141 F3 53 184.9972 F#3 54 195.9977 G3 55 207.6523 G#3 56 220.0000 A3 57 233.0819 A#3 58 246.9417 B3 59 261.6256 C4 60 277.1826 C#4 61 293.6648 D4 62 311.1270 D#4 63 329.6276 E4 64 349.2282 F4 65 369.9944 F#4 66 391.9954 G4 67 415.3047 G#4 68 440.0000 A4 69 466.1638 A#4 70 493.8833 B4 71 523.2511 C5 72 554.3653 C#5 73 587.3295 D5 74 622.2540 D#5 75 659.2551 E5 76 698.4565 F5 77 739.9888 F#5 78 783.9909 G5 79 830.6094 G#5 80 880.0000 A5 81 932.3275 A#5 82 987.7666 B5 83 1046.5023 C6 84 1108.7305 C#6 85 1174.6591 D6 86 1244.5079 D#6 87 1318.5102 E6 88 1396.9129 F6 89 1479.9777 F#6 90 1567.9817 G6 91 1661.2188 G#6 92 1760.0000 A6 93 1864.6550 A#6 94 1975.5332 B6 95 2093.0045 C7 96 2217.4610 C#7 97 2349.3181 D7 98 2489.0159 D#7 99 2637.0205 E7 100 2793.8259 F7 101 2959.9554 F#7 102 3135.9635 G7 103 3322.4376 G#7 104 3520.0000 A7 105 3729.3101 A#7 106 3951.0664 B7 107 4186.0090 C8 108 4434.9221 109 4698.6363 110 4978.0317 111 5274.0409 112 5587.6517 113 5919.9108 114 6271.9270 115 6644.8752 116 7040.0000 117 7458.6202 118 7902.1328 119 8372.0181 120 8869.8442 121 9397.2726 122 9956.0635 123 10548.0818 124 11175.3034 125 11839.8215 126 12543.8540 127
Trying to get fancy, hope it worked.
May 'the Google' be with you!
![]() |
Similar Threads
- any Java Sound API gurus? (Java)
- Flash MX 2004 and SOUND issue !URGENT! (Graphics and Multimedia)
- Sound with Tkinter (Python)
- Direct Sound Help (Creating a sound buffer) (C)
- TXT Plays like a Music File? (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: Calling a fuction from a bool?
- Next Thread: prime numbers
| Thread Tools | Search this Thread |
api application array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






