//music2.c -- by tsaiwn@csie.nctu.edu.tw more songs in this version #define LONG 68 #define TIME 125 #define SPEED 250 #include #include #ifdef __TURBOC__ #include #include #endif void playTiger( ), playMore( ); int main( ) { int freq[ ] = {32767, 523, 587, 659, 659, 784, 880}; int i, tone, last, nnn=0; static char tmp[888]; printf("Give me a Lucky number: "); fgets(tmp, sizeof(tmp), stdin); srand( atol(tmp) ); if(atol(tmp) == 0 ) srand(time(0)); // randomize printf(" . Hit any key to stop the music.. "); playTiger( ); playMore( ); delay(1235); // 1.235 second printf("\n"); // now play Computer Music using random number for(i=1; i <= LONG; ++i) { tone = rand( ) % 7; if(tone == 0) tone = rand( ) % 7; if(tone == 4) tone = 3; last = 1 + rand( ) % 4; if(last >=3) last = 1 + rand( ) % 4; if(last >=4) last = 1 + rand( ) % 4; last = last* TIME; printf("%3d", tone); ++nnn; if(nnn%20 ==0) printf("\n"); sound( freq[tone] ); delay(last); if(kbhit( ) ) { if( getch( )== 0) getch( ); break; } // user hit a key } sound(694); delay(300); sound(523); delay(300); nosound( ); return 0; } const int tig[ ] = { 1,1,2,2,3,3,1,1, 1,1,2,2,3,3,1,1, 3,3,4,4,5,5,5,5, 3,3,4,4,5,5,5,5, 5,6,5,4,3,3,1,1, 5,6,5,4,3,3,1,1, 1,1,-3,-3,1,1,1,0, 1,1,-3,-3,8,8,8,8, 0,0, 0,0,0,0, 0,0,0,0 }; const tigLen = sizeof(tig) / sizeof(tig[0]); const int freq[ ] = { 392,440,494,32767, 523,588,660,694,784,880,988, 1047,1177, 1322 , 1401, 1574, 0 }; // /// ///// -3= 5, -2= 6, -1= 7, 0= rest, 1=1, 2=2, ...,7=7, 8=1, 9=2 void playTiger( ) { //Two tigers int i, note; int speed = SPEED; for(i=0; i < tigLen; ++i) { note = tig[i] +3; // -3 ==> 0, -2 ==> 1, -1 ==> 0, 0 ==> 1, 1==> 4, ... sound ( freq[note] ); delay(speed); if(kbhit( ) ) { if( getch( )== 0) getch( ); nosound( ); return; } // user hit a key } return; } /// void playSong(int * song, int songLen) { int i, note; int speed = SPEED; for(i=0; i < songLen; ++i) { note = song[i] +3; // -3 ==> 0, -2 ==> 1, -1 ==> 0, 0 ==> 1, 1==> 4, ... sound ( freq[note] ); delay(speed); if(kbhit( ) ) { if( getch( )== 0) getch( ); nosound( ); return; } // user hit a key } // for } const int songButterfly[ ] = { //張洪量的"美麗的花蝴蝶" thanks! 2,2,3,2,2,-3,-3,1,1,1,1,1,1,1,1,0,0, 2,2,3,2,2,-3,-3,1,-1,-1,-2,-2,0,0, 2,2,3,2,2,-2,-2,2,2,2,2,2,2,2,0,0, 2,2,3,2,2,-2,-2,-3,-3,-3,-3,-3,-3,-3,0,0, 1,1,1,-3,2,2,2,1,2,5,5,1,1,1,1,1, 4,3,4,3,4,4,1,1,-2,-2,3,3,2,2,2,2,0, 2,2,2,1,2,2,5,5,3,4,3,2,1,-2,-2,-2, 4,3,4,3,4,3,-2,3,3,3,2,2,2,2,2,2,0,0, 0,0,0,0 }; const lenButterfly = sizeof(songButterfly)/sizeof(songButterfly[0]); /// const int songRaiseHead[ ] = { //掀起你的蓋頭來 5,5,1,1,1,1,3,3, 2,3,4,4,3,3,0,0, 5,5,1,1,1,1,3,3, 2,3,4,4,3,3,0,0, 3,3,3,2,3,3,3,2, 3,5,3,2,1,1,1,1, 2,2,2,4,3,3,3,2, 1,1,5,5,5,5,5,5 , 3,3,3,2,3,3,3,2, 3,5,3,2,1,1,1,1, 2,2,2,4,3,3,3,2, 1,1,1,1,1,1,1,1, 0,0,0,0 }; const lenRaiseHead = sizeof(songRaiseHead)/sizeof(songRaiseHead[0]); /// const songHappy[ ] = { //歌名:歡樂年華 3,3,3,5, 3,2,1, 4,4,4,5, 4,3,2, 3,3,3,5, 3,2,1,0, 4,4,4,5, 4,3,2,3,4, 5,0,0,3,2, 1,0,0, 6,5,4,3, 2,0,0,3,4, 5,0,0,3,2,1, 6,5,4,3, 2,0,7,1,2, 1,0,0, 0,0,0,0 }; const lenHappy = sizeof(songHappy)/sizeof(songHappy[0]); /// const songSpring[ ] = { // 陽明春曉前面 (節拍不準:-) 6, 6, 6, 6, 8, 6, 8, 8, 6, 8, 6, 5,6,3, 5, 6, 8, 8, 6, 8, 6, 5, 3,3, 10, 12, 10, 10, 9, 10, 9, 8, 6, 8, 8, 8, 7, 6, 7, 6, 5, 3, 8, 8, 6, 5, 3, 5, 3, 5, 6, 6, 8, 8, 6, 8, 8, 6, 8, 6, 5, 6, 3, 0,0, 8, 6, 8, 9, 10, 7, 6, 0, 8, 7, 6, 8, 7, 6, 5, 3, 10, 12, 10, 10, 9, 10, 9, 10, 12, 10, 0,0,0,0 }; const lenSpring = sizeof(songSpring)/sizeof(songSpring[0]); /// void playMore( ) { playSong(songButterfly, lenButterfly); playSong((int*)songRaiseHead, lenRaiseHead); playSong((int*)songHappy, lenHappy); playSong((int*)songSpring, lenSpring); } //////////////////////////////////// /******************** A 440.00 Hz = 6 A#/Bb 466.16 Hz = 6# B 493.88 Hz = 7 C 523.25 Hz = 1 C#/Db 554.37 Hz = 1# D 587.33 Hz = 2 D#/Eb 622.25 Hz = 2# E 659.25 Hz = 3 F 698.46 Hz = 4 F#/Gb 739.99 Hz = 4# G 783.99 Hz = 5 G#/Ab 830.61 Hz = 5# A 880.00 Hz = 6 ***********************************/