home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / MUSIC / POLYTC.ZIP / POLY_XLA.TC < prev    next >
Text File  |  1991-05-25  |  3KB  |  139 lines

  1. /*
  2.  *    Program:        poly_xlate.c
  3.  *
  4.  *    Created by:        Dan Oliver
  5.  *
  6.  *    Creation date:    12-30-87
  7.  *                                      CHANGE LOG
  8.  *      Date        Who        Comments
  9.  *    --------    ----    -------------------------------------------------------
  10.  */
  11.  
  12. #include <stdio.h>
  13.  
  14. #define TRUE        (1)
  15. #define FALSE        (0)
  16.  
  17. #define TBASE        16384                /* Tempo base */
  18. #define DBASE        8192                /* Duration base */
  19.  
  20. int notes[48] =
  21. {
  22.     256, 271, 287, 304, 322, 341, 362, 383, 406, 430, 456, 483,    /* Bass */
  23.     512, 542, 575, 609, 645, 683, 724, 767, 813, 861, 912, 967,    /* Low */
  24.     1024, 1085, 1149, 1218, 1290, 1367, 1448, 1534, 1625, 1722, 1825, 1933,    /* Medium */
  25.     2048, 2170, 2299, 2435, 2580, 2734, 2896, 3069, 3251, 3444, 3649, 3866    /* High */
  26. };
  27.  
  28. char *nname[12] =
  29. {
  30.     "C ", "C#", "D ", "D#", "E ", "F ", "F#", "G ", "G#", "A ", "A#", "B "
  31. };
  32.  
  33. char title[81];                            /* Song title */
  34. int tempo;                                /* Tempo */
  35. int measure;                            /* Measure duration */
  36.  
  37. unsigned voice[3];                        /* Voice data */
  38. unsigned duration;                        /* Voices duration */
  39.  
  40. /*****************************************************************************/
  41. main( argc, argv )
  42. int argc;
  43. char *argv[];
  44. /*
  45.  *    Description:    This program takes a polyphonic music data file as input
  46.  *                    (stdin) and outputs a translation (into english, more or
  47.  *                    less) of the song (to stdout).
  48.  *
  49.  *    Return:            -none-
  50.  */
  51. {
  52.  
  53. /*
  54.  *  Read title, measure duration, and tempo and output them...
  55.  */
  56.         fgets( title, 80, stdin );
  57.         fscanf( stdin, "%2d %5d\n", &measure, &tempo );
  58.     tempo -= TBASE;
  59.         fprintf( stdout, "Title: %s\nMeasure duration: %d\nTempo: %d\n", title, measure, tempo );
  60.         fprintf( stdout, "Voice1  Voice2  Voice3  Duration\n------  ------  ------  --------\n" );
  61. /*
  62.  *  Now start into data read/display loop...
  63.  */
  64.         while( (fscanf(stdin, "%d", &voice[0]) != -1) && voice[0] )
  65.     {
  66.                 fscanf( stdin, "%d %d %d\n", &voice[1], &voice[2], &duration );
  67.         display( voice[0] );
  68.         display( voice[1] );
  69.         display( voice[2] );
  70.                 fprintf( stdout, "%8d\n", duration-DBASE );
  71.     }
  72.         fprintf( stdout, "\n" );
  73. }
  74.  
  75. /*****************************************************************************/
  76. display( dvoice )
  77. unsigned dvoice;
  78. {
  79.     int value;
  80.     int octave;
  81.     int sindex;
  82.     int eindex;
  83.     int nindex;
  84.     int i;
  85.     int j;
  86.     char doctave;
  87.     
  88.     value = dvoice & 0x1fff;
  89.     octave = value/256;
  90.     if( octave >= 2 && octave <= 3 )
  91.         octave = 2;
  92.     if( octave >= 4 && octave <= 7 )
  93.         octave = 3;
  94.     if(octave >= 8 && octave <= 15 )
  95.         octave = 4;
  96.  
  97.     switch( octave )
  98.     {
  99.         case 1:
  100.             sindex = 0;
  101.             eindex = 12;
  102.             doctave = 'b';
  103.             break;
  104.         case 2:
  105.             sindex = 12;
  106.             eindex = 24;
  107.             doctave = 'l';
  108.             break;
  109.         case 3:
  110.             sindex = 24;
  111.             eindex = 36;
  112.             doctave = 'm';
  113.             break;
  114.         case 4:
  115.             sindex = 36;
  116.             eindex = 48;
  117.             doctave = 'h';
  118.             break;
  119.         default:
  120.             sindex = 0;
  121.             eindex = 0;
  122.             break;
  123.     }
  124.  
  125.     for( nindex=0, i=sindex, j=eindex; i < j; i++ )
  126.     {
  127.         if( value == notes[i] )
  128.         {
  129.             nindex = i % 12;
  130.             break;
  131.         }
  132.     }
  133.  
  134.     if( nindex )
  135.                 fprintf( stdout, "(%c)%s   ", doctave, nname[nindex] );
  136.     else
  137.                 fprintf( stdout, "        " );
  138. }
  139.