What you will learn in this lesson
- Play Command
- On Play Command
Today's lesson is on sound. Or more exactly, how to make funny noises with QBasic.
Sound Commands

You have already explored two dimensions of QBasic; namely text and graphics. Now let us introduce a whole new dimension to you - sound. Basically in the 1980s, when QBasic was in use, there were 2 dominant forces in sound. They were the Sound Blaster and the PC Speaker. Compared to today's SB 64 Golds and whatnot, they are very much mediocre, but do
remember, this is the 1980s that we are talking about. And sound in QBasic basically means that.
In this lesson we will teach you how to use the PC Speaker to produce robot-like sounds. SBlaster will be covered later in Module 3. The
main differences between them is that Sound Blaster has better sound quality and more effects, while the PC Speaker gives off mechanical beeps. However, SBlaster is much harder to program and takes
up more memory.
To learn how to operate the speaker, you must first use this command: Play. This command is somewhat like the Draw command in nature and in syntax. Basically the syntax is:
Play songstring$
Here, songstring$ is a mixture of numbers and letters which represent notes which the command interprets and plays. Let us delve into their details:

songstring$ A string expression that contains one or more of
the following PLAY commands:
Octave and tone commands:
Ooctave Sets the current octave (0 - 6).
< or > Moves up or down one octave.
A - G Plays the specified note
(in the current octave)
Nnote Plays a specified note (0 - 84)
(in the seven-octave)
range (0 is a rest).
Duration and tempo commands:
Llength Sets the length of each note (1 - 64).
L1 is whole note, L2 is a half note, etc.
ML Sets music legato.
MN Sets music normal.
MS Sets music staccato.
Ppause Specifies a pause (1 - 64).
P1 is a whole-note pause,
P2 is a half-note pause, etc.
Ttempo Sets the tempo in quarter notes per minute (32 - 255).
Mode commands:
MF Plays music in foreground.
MB Plays music in background.
Suffix commands:
# or + Turns preceding note into a sharp.
- Turns preceding note into a flat.
. Plays the preceding note 3/2 as long as specified.
To execute a Play command substring from a Play command string,
use the "X" command:
PLAY "X"+ VARPTR$(commandstring$)
For example:
'This will play the scale in 7 octaves
scale$ = "CDEFGAB"
PLAY "L16"
FOR i% = 0 TO 6
PLAY "O" + STR$(i%)
PLAY "X" + VARPTR$(scale$)
NEXT i%
On Play Command
The On Play command is another similar command to the Timer command, just that it uses the number of notes in the current music buffer instead of using measures of time, namely seconds. The syntax and usage of these commands are both very similar.

If event trapping is enabled, ON PLAY branches to a subroutine whenever
the music buffer contains fewer than a specified number of notes.
PLAY ON
PLAY OFF
PLAY STOP
ON PLAY(queuelimit%) GOSUB line
PLAY ON Enables play event trapping.
PLAY OFF Disables play event trapping.
PLAY STOP Suspends play event trapping. Events are processed once
event trapping is enabled by PLAY ON.
queuelimit% A number in the range 1 through 32. ON PLAY branches to
a subroutine when there are fewer than queuelimit%
notes in the music buffer.
line The label or number of the first line of the
event-trapping subroutine.
Example:
ON PLAY(3) GOSUB Background
PLAY ON
Music$ = "MBo3L8ED+ED+Eo2Bo3DCL2o2A"
PLAY Music$
LOCATE 2, 1: PRINT "Press any key to stop.";
DO WHILE INKEY$ = "": LOOP
END
Background:
i% = i% + 1
LOCATE 1, 1: PRINT "Background called "; i%; "time(s)";
PLAY Music$
RETURN
Well, I suppose that's all for the lesson. Now for the Assignment!
QB Assignment
Today's Assignment is simply to have fun, experiment, and practise using the commands taught.
Have fun!
[ Home ] [ Intro ] [ Lessons ] [ Library ] [ Reference ] [ About ]
This Webpage is ThinkQuest entry Unknown.
Email: lccorp1997@hotmail.com
This Webpage is Designed for Netscape Navigator 3.0 and MSIE 3.0
We prefer a 24-bit True Color mode with 640x480 screen mode.
This Webpage created for Thinkquest 1998.