Create Sound

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 2
Reputation: johnsonkek is an unknown quantity at this point 
Solved Threads: 0
johnsonkek johnsonkek is offline Offline
Newbie Poster

Create Sound

 
0
  #1
Oct 10th, 2004
HI there guys any one know how to create the correct tone for the sound using c++? i manage to know the basic only


c 262
d294
e 330
f 349
f 392
a 440
b 494
c2 524
This is from a handphone ringing tone
how do i know the frequency for like 8-8a2,c4,c5 8#a2,8-

anyone have any idea?

sound(262);
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Create Sound

 
0
  #2
Oct 10th, 2004
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!

  1. /*********************** play.c ***********************
  2. **
  3. ** Experimentation with sound:
  4. ** play(int octave,int note,int duration)
  5. ** Written in Turbo C V.2.0 by vegaseat 6/15/88
  6. **
  7. *********************************************************/
  8.  
  9. /************** note.h ***********/
  10.  
  11. #define P 0 /* pause */
  12. #define C 1
  13. #define CS 2 /* C sharp */
  14. #define D 3
  15. #define DS 4
  16. #define E 5
  17. #define F 6
  18. #define FS 7
  19. #define G 8
  20. #define GS 9
  21. #define A 10
  22. #define AS 11
  23. #define B 12
  24.  
  25. #define EN 75 /* eighth note */
  26. #define QN 150 /* quarter note */
  27. #define HN 300 /* half note */
  28. #define FN 600 /* full note */
  29.  
  30. /************************************/
  31.  
  32. void play(int octave,int note,int duration);
  33. void british(void); /* Westminster Bells */
  34.  
  35. void main(void) /* test play() */
  36. {
  37. british();
  38. getch();
  39. }
  40.  
  41. void play(int octave,int note,int duration)
  42. /* play note (C=1 to B=12), in octave (1-8), and duration (msec)
  43.   include NOTE.H for note values */
  44. {
  45. int k;
  46. double frequency;
  47.  
  48. if (note == 0) { /* pause */
  49. delay(duration);
  50. return;
  51. }
  52. frequency = 32.625;
  53. for (k = 0; k < octave; k++) /* compute C in octave */
  54. frequency *= 2;
  55. for (k = 0; k < note; k++) /* frequency of note */
  56. frequency *= 1.059463094; /* twelve root of 2 */
  57. delay(5); /* delay between keys */
  58. sound((int) frequency); /* sound the note */
  59. delay(duration); /* for correct duration */
  60. nosound();
  61. }
  62.  
  63. void british(void) /* Westminster Bells, sort of */
  64. {
  65. play(4,E,HN);
  66. play(4,C,HN);
  67. play(4,D,HN);
  68. play(3,G,HN+QN); play(3,P,QN);
  69. play(3,G,HN);
  70. play(4,D,HN);
  71. play(4,E,HN);
  72. play(4,C,HN+QN);
  73. }
  74.  
  75. /*
  76. For XP/NT that don't allow port outputs there is a WIN32 API call:
  77.  
  78. The Beep function generates simple tones on the speaker. The function
  79. is synchronous; it does not return control to its caller until the
  80. sound finishes.
  81.  
  82. BOOL Beep(
  83.  
  84.   DWORD dwFreq, // sound frequency, in hertz
  85.   DWORD dwDuration // sound duration, in milliseconds
  86.   );
  87.  
  88. */
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 2
Reputation: johnsonkek is an unknown quantity at this point 
Solved Threads: 0
johnsonkek johnsonkek is offline Offline
Newbie Poster

Re: Create Sound

 
0
  #3
Oct 11th, 2004
erm sorry but i still not very sure how u calculate the value this the the program i type just that i'm short of music that all.By the way thank u very for ur effort to reply my message.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 1
Reputation: kinbit is an unknown quantity at this point 
Solved Threads: 0
kinbit kinbit is offline Offline
Newbie Poster

Re: Create Sound

 
0
  #4
Oct 13th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Create Sound

 
0
  #5
Oct 13th, 2004
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:
  1. Frequency Note MIDI#
  2.  
  3. 8.1758 0
  4. 8.6620 1
  5. 9.1770 2
  6. 9.7227 3
  7. 10.3009 4
  8. 10.9134 5
  9. 11.5623 6
  10. 12.2499 7
  11. 12.9783 8
  12. 13.7500 9
  13. 14.5676 10
  14. 15.4339 11
  15. 16.3516 12
  16. 17.3239 13
  17. 18.3540 14
  18. 19.4454 15
  19. 20.6017 16
  20. 21.8268 17
  21. 23.1247 18
  22. 24.4997 19
  23. 25.9565 20
  24. 27.5000 A0 21
  25. 29.1352 A#0 22
  26. 30.8677 B0 23
  27. 32.7032 C1 24
  28. 34.6478 C#1 25
  29. 36.7081 D1 26
  30. 38.8909 D#1 27
  31. 41.2034 E1 28
  32. 43.6535 F1 29
  33. 46.2493 F#1 30
  34. 48.9994 G1 31
  35. 51.9131 G#1 32
  36. 55.0000 A1 33
  37. 58.2705 A#1 34
  38. 61.7354 B1 35
  39. 65.4064 C2 36
  40. 69.2957 C#2 37
  41. 73.4162 D2 38
  42. 77.7817 D#2 39
  43. 82.4069 E2 40
  44. 87.3071 F2 41
  45. 92.4986 F#2 42
  46. 97.9989 G2 43
  47. 103.8262 G#2 44
  48. 110.0000 A2 45
  49. 116.5409 A#2 46
  50. 123.4708 B2 47
  51. 130.8128 C3 48
  52. 138.5913 C#3 49
  53. 146.8324 D3 50
  54. 155.5635 D#3 51
  55. 164.8138 E3 52
  56. 174.6141 F3 53
  57. 184.9972 F#3 54
  58. 195.9977 G3 55
  59. 207.6523 G#3 56
  60. 220.0000 A3 57
  61. 233.0819 A#3 58
  62. 246.9417 B3 59
  63. 261.6256 C4 60
  64. 277.1826 C#4 61
  65. 293.6648 D4 62
  66. 311.1270 D#4 63
  67. 329.6276 E4 64
  68. 349.2282 F4 65
  69. 369.9944 F#4 66
  70. 391.9954 G4 67
  71. 415.3047 G#4 68
  72. 440.0000 A4 69
  73. 466.1638 A#4 70
  74. 493.8833 B4 71
  75. 523.2511 C5 72
  76. 554.3653 C#5 73
  77. 587.3295 D5 74
  78. 622.2540 D#5 75
  79. 659.2551 E5 76
  80. 698.4565 F5 77
  81. 739.9888 F#5 78
  82. 783.9909 G5 79
  83. 830.6094 G#5 80
  84. 880.0000 A5 81
  85. 932.3275 A#5 82
  86. 987.7666 B5 83
  87. 1046.5023 C6 84
  88. 1108.7305 C#6 85
  89. 1174.6591 D6 86
  90. 1244.5079 D#6 87
  91. 1318.5102 E6 88
  92. 1396.9129 F6 89
  93. 1479.9777 F#6 90
  94. 1567.9817 G6 91
  95. 1661.2188 G#6 92
  96. 1760.0000 A6 93
  97. 1864.6550 A#6 94
  98. 1975.5332 B6 95
  99. 2093.0045 C7 96
  100. 2217.4610 C#7 97
  101. 2349.3181 D7 98
  102. 2489.0159 D#7 99
  103. 2637.0205 E7 100
  104. 2793.8259 F7 101
  105. 2959.9554 F#7 102
  106. 3135.9635 G7 103
  107. 3322.4376 G#7 104
  108. 3520.0000 A7 105
  109. 3729.3101 A#7 106
  110. 3951.0664 B7 107
  111. 4186.0090 C8 108
  112. 4434.9221 109
  113. 4698.6363 110
  114. 4978.0317 111
  115. 5274.0409 112
  116. 5587.6517 113
  117. 5919.9108 114
  118. 6271.9270 115
  119. 6644.8752 116
  120. 7040.0000 117
  121. 7458.6202 118
  122. 7902.1328 119
  123. 8372.0181 120
  124. 8869.8442 121
  125. 9397.2726 122
  126. 9956.0635 123
  127. 10548.0818 124
  128. 11175.3034 125
  129. 11839.8215 126
  130. 12543.8540 127

Trying to get fancy, hope it worked.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC