Music, Sounds, ON PLAY and Using the Soundcard

This tutorial is about statements used to play music, create sounds
and the ON PLAY statement.

bulletTable of Contents:
bulletBEEP
bulletON PLAY... GOSUB
bulletPLAY
bulletSOUND
bulletUsing the Microphone
bulletUsing the Soundcard to Produce Sound
bulletBEEP

Use the BEEP statement to make
the computer beep once.

bulletON PLAY... GOSUB

 

Use the ON PLAY statement to make a program
call a subroutine each time there are less
than the specified amount of notes left
to play. For example: if a program
has to play the notes A, B, A and B it
has to play 4 notes and if the following
lines are used in the program:
ON PLAY(2) GOSUB 1, the program will
go to line number 1 as soon as it
has played "ABA" because only the
last note (B) is left to play because
there were less than 2 notes left.

Use the RETURN statement to return
to the point in the program when
the subroutine was called.

Use PLAY OFF to turn ON PLAY off.
Use PLAY ON to turn ON PLAY on.
Use PLAY STOP to pause ON PLAY.

bulletExample:

ON PLAY(3) GOSUB 1
PLAY ON4
CLS
 DO
  PLAY "CDEFGAB"
 LOOP
END

1 PRINT "*";
RETURN

 

bulletPLAY

Use the PLAY statement to play music.
Here are the commands for the PLAY statement:

C, D, E, F, G, A and B are the notes.

Ooctave = Set the octave.
< = Down one octave.
> = Up one octave
Nnote = Play a note (range: 0-84, 0 = pause.)

Llength = Set the length of a note (range: 1-64, 1 = A whole note.)
Tnumber of quarter notes per minute (range: 32-255.)

ML = Play the full length set by L (legato.)
MN = Play 7/8 of the length set by L (normal.)
MS = Play 3/4 of the length set by L (staccato.)

Pnumber of quarter notes, range: 1-64.

MB = Play music in background, this will let the computer play
     music while the program continues with other things.
MF = Play music in foreground, the program will be paused
     until the music is done playing.

bulletExample 1:

PLAY "CDEFGAB"

 

bulletExample 2:

PLAY "L16 ML CDEFGAB"

 

bulletSOUND

Use the SOUND statement to make sounds.
The frequency can range from 37 to 32767
and the duration from 0 to 65535.
This how SOUND is used:
SOUND frequency, duration

bulletExample:

SOUND 37, 1

 

 

bulletUsing the Microphone:

Use OUT to tell the soundcard to return one
sample of input from the microphone like this:
OUT base address + 12, 32

To read the sample returned use INP like this:
INP base address + 10

The volume for the microphone is set like this:
OUT base address + 4, 10
OUT base address + 5, volume
The range for the volume is 0 to 7.

To get the volume for the microphone, use INP like this:
OUT base address + 4, 10
INP base address + 5

Base address + number refers to the address
of an I/O port which is the base address plus number.

bulletExample:

DEFINT A-Z

'Set the volume for the microphone.
OUT 548, 10
OUT 549, 7

x = 0
SCREEN 12: CLS
PSET (0, 128), 2
DO
  IF x = 639 THEN
    x = 0
    CLS
    PSET (0, 128), 2
  ELSE
    'Tell soundcard to return one sample from the microphone.
    OUT 556, 32
    LINE -(x, INP(554)), 2
    x = x + 1
    END IF
LOOP

 

bulletUsing the Soundcard to Produce Sound:

Use OUT to tell the soundcard that a sample
has to be sent to the speakers like this:
OUT base address + 12, 16

Use OUT base address + 12, sample to
send one sample to the speakers.

On computers which run above a certain speed it is possible
that new instructions are sent while producing sound while the
soundcard is still processing earlier instructions.
Use WAIT base address + 12, 128, 128 to wait until
the soundcard is ready for new instructions.

Before producing sound, the soundcard has to be reset like this:
OUT base address + 6, 1
OUT base address + 6, 0

The volume for both speakers is set separatly but has
to be sent as one value and is returned as one value.
The range for the volume is 0 to 15.
The value that has to be sent to set the volume is calculated like this:
volume = (left speaker volume * 16) + right speaker volume

This is how the volume for both speakers is set:
OUT base address + 4, 34
OUT base address + 5, volume

To get the volume for both speakers, use INP like this:
OUT base address + 4, 36
INP base address + 5

The value for the volume that is returned is converted to two
separate volumes for both speakers like this:
left speaker volume = volume \ 16
right speaker volume = volume - (left speaker volume * 16)

bulletExample:

'Reset the soundcard.
OUT 550, 1
OUT 550, 0

'Set the volume for both speakers to 7.
OUT 548, 34
OUT 549, 119

'Produce a sound that becomes increasingly lower.

 FOR Length = 0 TO 100
   FOR Sample = 0 TO Length
     WAIT 556, 128, 128
     OUT 556, 16
     WAIT 556, 128, 128
     OUT 556, 0
   NEXT Sample

   FOR Sample = 0 TO Length
     WAIT 556, 128, 128
     OUT 556, 16
     WAIT 556, 128, 128
     OUT 556, 255
   NEXT Sample
 NEXT Length

 

If you click on the link below you will find some QBasic books we have selected from amazon.com
Recommended QBasic Books

 

QBasic For All!
Copyright 1999 - 2002. All rights reserved
Anastasios Yalanopoulos