/* * Music playing routine * Microsoft compilers only! */ #include #include void playsound(int note,int duration); void delay(int duration); void main() { /* * Refer to the notes in the Borland example * for an explanation of how this array works * as well as other comments on the main() * function. */ int song[][2] = { 392,250, 392,375, 330,125, 330,250, 392,250, 392,375, 294,125, 294,250, 330,250, 349,250, 392,250, 440,250, 494,250, 392,750, 392,250, 392,375, 330,125, 330,250, 392,250, 392,375, 294,125, 294,250, 587,250, 554,250, 587,250, 659,250, 440,250, 587,750, 392,250, 659,375, 659,125, 587,250, 523,250, 523,375, 494,125, 494,250, 523,250, 587,250, 494,250, 440,250, 392,250, 523,750, 523,250, 523,375, 440,125, 440,250, 523,250, 523,375, 392,125, 392,250, 392,250, 440,250, 523,250, 392,250, 587,250, 523,750, 0, 0 }; int x = 0; while(song[x++][0]) { playsound(song[x][0],song[x][1]); delay(100); } } /* * The playsound function manipulates the PC's * speaker using a bunch of routines not * explained in the book. (Nifty!) Honestly, * this is a well-used routine for the PC, * most programmers just "steal" this code * without bothering to know what it does * (I used a routine like this for years, * and it can also be found in your HELP * file in the BEEP.C program example.) */ void playsound(int note,int duration) { int status; if(!note) return; note = (unsigned)(1193280L / note); _outp(0x43,0xb6); //prep the port _outp(0x42,(char)note); //tell it which _outp(0x42,(char)(note >> 8)); // note to play status = inp( 0x61 ); //save the status _outp(0x61, status | 0x03); //play the note delay( (clock_t)duration ); //delay _outp( 0x61, status ); //turn the speaker off } /* * The delay function uses Microsoft C's clock() * function which keeps track of how long a * program has been running in milliseconds. * * clock_t is a variable type representing how * long a program has run. (It's like a long int.) * * The delay function calculates how long to wait * by adding the duration to the current time. Until * that time passes, a while loop sits and spins. */ void delay(int duration) { clock_t done; done = duration + clock(); while( done > clock() ) ; }