Getting Started
Introduction
Sample Programs
QBasic IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Web Sites
More Tutorials
Vendors
Books
Magazines
NewsLetters
NewsGroups
Forums
User Groups
Talk Shows
Blogs

GBIC >> QBasic >> Tutorial >> Events

QBasic Information Center Tutorials - Events
These tutorials were written to help you get a quick, but thorough, understanding of QBasic - the scope of the language as well as it's specific capabilities.

Sound
QBasic provides a few features for adding sound to applications. While generally limited, these features have been adequate to support an entire generation of DOS-based games.

    • Creating Sounds
     play, beep, sound 
    • Event Trapping
     on play, play 

Simple Sounds
Two of the functions, beep and sound, are very easy methods of producing a simple sound - a single tone for a limitation duration.

  • beep
    Used alone, with no arguments, beep simply creates a single note of short duration - 800 Hz for 1/4 second.
        beep
        

    Beep is typically used to acknowledge an event, such as a keypress or completion of a task.

  • sound
    With sound, you still get a single note but you can control the frequency and duration.
        sound freq%, duration%      
        sound 4000, 2             # 2 seconds at 4Khz
        

    These sounds are typically used to differentiate between specific events - such as a piano programs that gives a different tone for each key pressed.

Complex Sounds
The play function allows creation of more complex sounds/melodies than the single note features of beep/sound.

A string of notes and instructions are used by the play function to create the more complex melodies. Examples of this are given in the functions reference below.

Sound Functions Reference
Here's a quick reference of the available sound functions, in alphabetical order.

  • on play - enable/disable/suspend play event trapping
        on play gosub line    # capture play events
     
        play on                # enable play trapping
        play off               # disable play trapping
        play stop              # suspend play trapping.
                                events are saved and processed
                                when event trapping is enabled
        

  • play - plays multiple musical notes
        play commandstring%         # commandstring% contains play directions
    
        play "CDEFGAB"              # 7 notes, default settings
        Music$ = "MBo3L8ED+ED+Eo2Bo3DCL2o2A"   # melody
        play Music$
        Music$ = "MBT180O2P2P8L8GGGL2E-P24P8L8FFFL2D"   # melody
        play Music$
        

    The commandstring$ can consist of 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.
        

  • beep - sends control string to a device driver
        beep         # no arguments, cannot control duration
        

  • sound - plays a sound through the speaker
        sound frequency%, duration%    # single frequency 37-32,767
                                       # duration is clock ticks
                                         (10.2 ticks per second)    
        

If you have any suggestions or corrections, please let me know.