C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
BEEP.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* BEEP.C illustrates timing and port input and output functions
 * including:
 *      _inp             _outp            clock
 *
 * Also keyword:
 *      enum
 *
 */
 
#include <time.h>
#include <conio.h>
 
/* Use intrinsic versions of _outp and _inp */
#pragma intrinsic( _outp, _inp )
 
/* Prototypes */
void Beep( int frequency, int duration );
void Sleep( clock_t wait );
 
enum NOTES      /* Enumeration of notes and frequencies     */
{
    C0 = 262, D0 = 296, E0 = 330, F0 = 349, G0 = 392, A0 = 440, B0 = 494,
    C1 = 523, D1 = 587, E1 = 659, F1 = 698, G1 = 784, A1 = 880, B1 = 988,
    EIGHTH = 125, QUARTER = 250, HALF = 500, WHOLE = 1000, END = 0
} song[] =      /* Array initialized to notes of song       */
{
    C1, HALF, G0, HALF, A0, HALF, E0, HALF, F0, HALF, E0, QUARTER,
    D0, QUARTER, C0, WHOLE, END
};
 
void main ()
{
    int note;
 
    for( note = 0; song[note]; note += 2 )
        Beep( song[note], song[note + 1] );
}
 
/* Sounds the speaker for a time specified in microseconds by duration
 * at a pitch specified in hertz by frequency.
 */
void Beep( int frequency, int duration )
{
    int control;
 
    /* If frequency is 0, Beep doesn't try to make a sound. It
     * just sleeps for the duration.
     */
    if( frequency )
    {
        /* 75 is about the shortest reliable duration of a sound. */
        if( duration < 75 )
            duration = 75;
 
        /* Prepare timer by sending 10111100 to port 43. */
        _outp( 0x43, 0xb6 );
 
        /* Divide input frequency by timer ticks per second and
         * write (byte by byte) to timer.
         */
        frequency = (unsigned)(1193180L / frequency);
        _outp( 0x42, (char)frequency );
        _outp( 0x42, (char)(frequency >> 8) );
 
        /* Save speaker control byte. */
        control = inp( 0x61 );
 
        /* Turn on the speaker (with bits 0 and 1). */
        _outp( 0x61, control | 0x3 );
    }
 
    Sleep( (clock_t)duration );
 
    /* Turn speaker back on if necessary. */
    if( frequency )
        _outp( 0x61, control );
 
}
 
/* Pauses for a specified number of microseconds. */
void Sleep( clock_t wait )
{
    clock_t goal;
 
    goal = wait + clock();
    while( goal > clock() )
        ;
}
                                    -♦-