If you just need a beep in your program, you can
use BEEP:
CLS INPUT "Press Enter to hear a beep", A$ BEEP
SOUND lets you play a beep and tell it how
high or low the beep will be, and how long it will last.
This program makes a 1000Hz beep for about 1 second:
SOUND 1000, 18
SOUND is good for making sound effects. Here's
a bomb dropping:
FOR I = 4000 TO 1000 STEP -5 SOUND I, .1 NEXT I
If you want to play a song, PLAY is exactly
what you need. Try this:
PLAY "e8 d8 c8 d8 e8 e8 e4"
PLAY is like a little programming language inside of QBASIC.
"e8" means play an eighth note "e". If you are familiar
with sheet music, this will make sense. Here's a scale:
PLAY "c8 d8 e8 f8 g8 a8 b8 > c4"
The ">" greater than sign means "go up one octave". There
are many more special commands in PLAY. Check the QBASIC help
for a list of all of them.
PLAY and Triplets
Here's a familiar tune that uses a trick to
do triplets.
PLAY "T180 <d8d8d8 T120 g2>d2" PLAY "T180 c8<b8a8 T120 >g2d4" PLAY "T180 c8<b8a8 T120 >g2d4" PLAY "T180 c8<b8>c8 T120 <a2>"
Since PLAY doesn't do triplets, you have to modify the
tempo to get the right rhythm. PLAY begins with a default
tempo of "T120" which means 120 quarter notes per minute.
In the above song, we switch to T180 which is the triplet
tempo for T120. By multiplying our tempo by 1.5, we get the
triplet tempo. When the triplets are done, we switch back
to the regular tempo. You can see in the above example that
we switch back and forth between the main tempo (T120) and
the triplet tempo (T180) several times as needed.
As with everything, there's more than one way to do
triplets. 8th note triplets can also be called 12th notes,
like this:
PLAY "<d12d12d12g2>d2" PLAY "c12<b12a12>g2d4" PLAY "c12<b12a12>g2d4" PLAY "c12<b12>c12<a2>"
Using this technique, 16th note triplets are 24th notes,
etc.... You just multiply the note value by 1.5 instead of
changing the tempo.
Each of these techniques has its advantages and disadvantages.
The tempo-changing technique uses more space, but the
notes retain their values. The 12th note technique is more
compact, but not as easy to understand. Which one you use
is up to you. Just make sure the next person to read your
code understands what you are doing. Comments are a good idea.
Source: http://jpsor.ucoz.com |