home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / vrac / pclcjs.zip / PLAYCM1.C < prev    next >
C/C++ Source or Header  |  1992-12-28  |  4KB  |  201 lines

  1. /* PLAYCM1 function */
  2.  
  3.  
  4. #include <bios.h>
  5. #include <ctype.h>
  6. #include <direct.h>
  7. #include <dos.h>
  8. #include <io.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #define BUFFERSIZE 81
  14.  
  15. int play_cm1(char *song)
  16.     
  17. {
  18. /* This program reads a file of notes and durations and plays them */
  19.  
  20. /* Arguments:
  21.  
  22.           *song - pointer to name of music file; "*" for random mode
  23.    
  24.    Returns:
  25.  
  26.         0 - Proper termination
  27.         1 - Invalid music file
  28.         2 - No parameters supplied
  29.         3 - No music files found
  30.  
  31.                                                     */
  32.  
  33.     /* Structure declaration */
  34.     struct ffblk filedata;
  35.  
  36.     /* file declaration */                                            
  37.     FILE *input_file;
  38.     
  39.     /* Variable declaration */
  40.     char music_file[13], current_line[81], *pointer, temp[25], words[20];
  41.  
  42.     int freq, counter=0, length, duration, counter2=0;
  43.     int rand_flag=0, notepos=0;
  44.     
  45.     /* Code begins here */
  46.     /* Seed random number generator */
  47.     srand(10);
  48.     
  49.     /* Read song file name */
  50.     strcpy(temp,song);
  51.     if (temp[0] != '*') 
  52.         strcpy(music_file,temp);
  53.     else 
  54.         rand_flag=1;
  55.     
  56.     /* If "*" do random mode */
  57.     if (rand_flag == 1) {
  58.         if (findfirst("*.CM1", &filedata, FA_NORMAL) == 0) {
  59.             counter++;
  60.             while (findnext(&filedata) == 0)
  61.                 counter++;
  62.             counter=rand() % counter + 1;
  63.             
  64.             /* Reset file finder */
  65.             findfirst("*.CM1", &filedata, FA_NORMAL);
  66.             for (counter2=1;counter2<counter;counter2++)
  67.                 findnext(&filedata);
  68.             strcpy(music_file,filedata.ff_name);
  69.         }
  70.         else 
  71.             return 3;
  72.     }
  73.     else {    
  74.         if (pointer=stristr(music_file,".CM1") == NULL) 
  75.             strcat(music_file,".CM1");
  76.     }
  77.     
  78.     /* Open music file w/buffering */
  79.     input_file = fopen(music_file,"r");
  80.     setvbuf(input_file,NULL,_IOFBF,8192);
  81.     
  82.     if (input_file == NULL) 
  83.         return 1;
  84.     
  85.     /* Reset counter, play music */
  86.     counter = 0;
  87.     
  88.     /* Assign frequency value to each note.  notepos and words are used with
  89.        version 2.0 of MUSIC.EXE, which displays the notes on a graphical scale
  90.        and shows lyrics                                                       */
  91.     while (fgets(current_line, BUFFERSIZE, input_file) != NULL && !kbhit()) {
  92.         counter++;
  93.         if ((current_line[0] <= 'g' && current_line[0] >= 'a') || (current_line[0] <= 'G' && current_line[0] >='A') || (current_line[0] == 'r' || current_line[0] == 'R')) {
  94.             switch (current_line[0]) {
  95.                 case 'C': {
  96.                     freq=536;
  97.                     notepos=9;
  98.                 }
  99.                 break;
  100.                 case 'c': {
  101.                     freq=268;
  102.                     notepos=16;
  103.                 }
  104.                 break;
  105.                 case 'D': {
  106.                     freq=602;
  107.                     notepos=8;
  108.                 }
  109.                 break;
  110.                 case 'd': {
  111.                     freq=301;
  112.                     notepos=15;
  113.                 }
  114.                 break;
  115.                 case 'E': {
  116.                     freq=675;
  117.                     notepos=7;
  118.                 }
  119.                 break;
  120.                 case 'e': {
  121.                     freq=337;
  122.                     notepos=14;
  123.                 }
  124.                 break;
  125.                 case 'F': {
  126.                     freq=716;
  127.                     notepos=6;
  128.                 }
  129.                 break;
  130.                 case 'f': {
  131.                     freq=358;
  132.                     notepos=13;
  133.                 }
  134.                 break;
  135.                 case 'G': {
  136.                     freq=803;
  137.                     notepos=5;
  138.                 }
  139.                 break;
  140.                 case 'g': {
  141.                     freq=401;
  142.                     notepos=12;
  143.                 }
  144.                 break;
  145.                 case 'A': {
  146.                     freq=902;
  147.                     notepos=4;
  148.                 }
  149.                 break;
  150.                 case 'a': {
  151.                     freq=451;
  152.                     notepos=11;
  153.                 }
  154.                 break;
  155.                 case 'B': {
  156.                     freq=1012;
  157.                     notepos=3;
  158.                 }
  159.                 break;
  160.                 case 'b': {
  161.                     freq=506;
  162.                     notepos=10;
  163.                 }
  164.                 break;
  165.                 case 'R':
  166.                 case 'r': {
  167.                     freq=26000;
  168.                     notepos=0;
  169.                 }
  170.                 break;
  171.             }
  172.             
  173.              if (current_line[2] == 35) 
  174.                  freq = freq * 1.06;
  175.              if (current_line[2] == 98) 
  176.                  freq = freq * .94;
  177.             
  178.             if (strlen(current_line) > 3) {
  179.                 strcpy(words,¤t_line[3]);
  180.                 words[strlen(words)-1]=0;
  181.             }
  182.                 
  183.              duration = current_line[1] - 48;
  184.              length=32/duration;
  185.             if (current_line[2] == 46)
  186.                 length=length*1.5;
  187.                 
  188.              sound(freq,length);
  189.              
  190.             strcpy(words,"");
  191.         }
  192.     }
  193.     
  194.     /* Close music file */
  195.     fclose(input_file);
  196.     
  197.     return 0;
  198. }
  199.  
  200.  
  201.