//music.c  -- by tsaiwn@csie.nctu.edu.tw
#define LONG 68
#define TIME 125
#define SPEED 250
#include <stdio.h>
#include <stdlib.h>
#ifdef __TURBOC__
#include <time.h>
#include <conio.h>
#endif

void playTiger( );
int main( ) {
     int freq[ ] = {32767, 523, 587, 659, 659, 784, 880};
     int i, tone, last;  char tmp[88];
     printf("Give me a Lucky number: ");
     fgets(tmp, sizeof(tmp), stdin);
     srand( atol(tmp) );
     printf(" . Hit any key to stop the music.. ");
     playTiger( );
     delay(1235);  // 1.235 second
     for(i=1; i <= LONG; ++i) {
          tone = rand( ) % 7;
          if(tone == 0) tone = rand( ) % 7;
          if(tone == 0) tone = rand( ) % 7;
          last =  1 + rand( ) % 4; 
          if(last >=3) last =  1 + rand( ) % 4; 
          if(last >=4) last =  1 + rand( ) % 4; 
          last = last* TIME;
          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,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;
}
/****
********************/
/********************
       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
***********************************/
