Download QBASIC Programming for Kids
Transcript
Chapter 17 - Sound 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 PLAY PLAY PLAY "T180 "T180 "T180 "T180 <d8d8d8 T120 g2>d2" c8<b8a8 T120 >g2d4" c8<b8a8 T120 >g2d4" 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.