Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
BEEP.C
◄Up► ◄Contents► ◄Index► ◄Back►
────────────────────────────────────────────────────────────────────────────
/* BEEP.C illustrates timing and port input and output functions
* including:
* inp outp clock
*
* Also keyword:
* enum
*
* In addition to the delay use shown here, clock can be used as
* a timer as shown in SIEVE.C.
*/
#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() )
;
}