home *** CD-ROM | disk | FTP | other *** search
/ PowerModul 1 / POWERMOD1.mdf / ultimod / modump.c < prev    next >
C/C++ Source or Header  |  1995-09-03  |  8KB  |  252 lines

  1. #define MAIN
  2. #include <string.h>
  3. #include <io.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <sys\types.h>
  8. #include <sys\stat.h>
  9. #include <process.h>
  10. #include <math.h>
  11. #include <process.h>
  12. #include <dos.h>
  13. #include <share.h>
  14. #include <direct.h>
  15. #include <errno.h>
  16. #include <stdarg.h>
  17. #pragma pack(1)
  18.  
  19. #define EOS                     '\0'
  20. #define MAX_INSTRUMENTS         31
  21. #define DEFAULT_NUM_INSTRUMENTS 15
  22. #define MAX_POSITIONS           128
  23. #define MAX_CHANNELS            4
  24. #define MAX_PATTERN_ROWS        64
  25.  
  26. typedef unsigned long DWORD;
  27. typedef unsigned int WORD;
  28. typedef unsigned char BYTE;
  29. #define LOBYTE(w) ((BYTE)(w))
  30. #define HIBYTE(w) ((BYTE)(((WORD)(w) >> 8) & 0xFF))
  31.  
  32. typedef struct tagMODHEADER
  33. {
  34.     char    name[20];
  35. } MODHEADER;
  36.  
  37.  
  38. typedef struct tagINSTRUMENT
  39. {
  40.     char    name[22];
  41.     WORD    sampleLength;                    
  42.     WORD    volume;
  43.     WORD    repeatOffset;
  44.     WORD    repeatLength;
  45. } INSTRUMENT;
  46.  
  47.  
  48. typedef struct tagPOSITIONTABLE
  49. {
  50.     BYTE    noOfPositions;
  51.     BYTE    reserved;
  52.     BYTE    position[ MAX_POSITIONS ];
  53. } POSITIONTABLE;
  54.  
  55. typedef struct tagNOTE
  56. {
  57.     WORD    word1;
  58.     WORD    word2;
  59. } NOTE;
  60.  
  61.  
  62. typedef struct tagCHANNEL
  63. {
  64.     NOTE    note;
  65. } CHANNEL;
  66.  
  67. typedef struct tagPATTERNROW
  68. {
  69.     CHANNEL channel[MAX_CHANNELS];
  70. } PATTERNROW;
  71.  
  72. typedef struct tagPATTERNTABLE
  73. {
  74.     PATTERNROW row[MAX_PATTERN_ROWS];
  75. } PATTERNTABLE;
  76.  
  77.  
  78. //**********************************************************************
  79. //*                                           
  80. //**********************************************************************
  81.  
  82. MODHEADER       modHeader;
  83. INSTRUMENT      instrument;
  84. POSITIONTABLE   positionTable;
  85. PATTERNTABLE    patternTable;
  86.  
  87. //**********************************************************************
  88. //*                                           
  89. //**********************************************************************
  90.  
  91. //WORD WordSwap( WORD value )
  92. //{
  93. //    return((LOBYTE(value) << 8) | HIBYTE(value));
  94. //}
  95. #define WordSwap(a)  ((WORD) (LOBYTE(a) << 8) | (WORD) HIBYTE(a))
  96.  
  97. //**********************************************************************
  98. //*                                           
  99. //**********************************************************************
  100.  
  101. main( int argc, char *argv[])
  102. {
  103.     int handle, loop;
  104.     BYTE noOfInstruments;
  105.     BYTE noOfPatternTables;
  106.     WORD noOfSampleBytes;
  107.     char buffer[256];
  108.     int  row, channel;
  109.     DWORD period, sampleRate;
  110.     WORD word1, word2;
  111.     WORD  noteInstrument;
  112.     WORD  notePeriod;
  113.     WORD  noteEffect;
  114.     WORD  noteParm;
  115.  
  116.  
  117.     if (argc <= 1 || (handle = open(argv[1], O_RDWR | O_BINARY )) == -1)
  118.         exit(1);
  119.  
  120.     //******************************************************************
  121.     //* DETERMINE THE NUMBER OF INSTRUMENTS
  122.     //******************************************************************
  123.  
  124.     lseek( handle, 1080L, SEEK_SET);
  125.  
  126.     if (read( handle, buffer, 4) == 4 &&
  127.         (strncmp(buffer, "M.K.", 4) == 0 ||
  128.          strncmp(buffer, "FLT4", 4) == 0 ||
  129.          strncmp(buffer, "FLT8", 4) == 0))
  130.         noOfInstruments = MAX_INSTRUMENTS;
  131.     else
  132.         noOfInstruments = DEFAULT_NUM_INSTRUMENTS;
  133.  
  134.     //******************************************************************
  135.     //* READ FILE HEADER
  136.     //******************************************************************
  137.  
  138.     lseek( handle, 0L, SEEK_SET);
  139.     read( handle, (char *) &modHeader, sizeof(MODHEADER) );
  140.     modHeader.name[ sizeof(modHeader.name)-1 ] = EOS;
  141.  
  142.     printf( "=======================================\n" );
  143.     printf( "MODNAME = [%s]\n", modHeader.name );
  144.  
  145.     //******************************************************************
  146.     //* READ INSTRUMENT TABLES
  147.     //******************************************************************
  148.  
  149.     for (loop = 0; loop < noOfInstruments; loop++)
  150.     {
  151.         read( handle, (char *) &instrument, sizeof(INSTRUMENT) );
  152.  
  153.         instrument.sampleLength = WordSwap( instrument.sampleLength ) * sizeof(WORD);
  154.         instrument.volume       = WordSwap( instrument.volume ) & 0x007F;
  155.         instrument.repeatLength = WordSwap( instrument.repeatLength ) * sizeof(WORD);
  156.         instrument.repeatOffset = WordSwap( instrument.repeatOffset ) * sizeof(WORD);
  157.         noOfSampleBytes += instrument.sampleLength;
  158.  
  159.         if (instrument.sampleLength)
  160.         {
  161.             printf( "--------------------------------------\n" );
  162.             printf( "INSTRUMENT = (%d) %s\n", loop+1, instrument.name );
  163.             printf( "LENGTH     = %u\n", instrument.sampleLength );
  164.             printf( "VOLUME     = %u\n", instrument.volume );
  165.             printf( "REPEAT OFF = %u\n", instrument.repeatOffset );
  166.             printf( "REPEAT LEN = %u\n", instrument.repeatLength );
  167.         }
  168.     }
  169.  
  170.     //******************************************************************
  171.     //* READ SONG HEADER
  172.     //******************************************************************
  173.  
  174.     read( handle, (char *) &positionTable, sizeof(POSITIONTABLE) );
  175.  
  176.     printf( "=======================================\n" );
  177.     printf( "NO OF SONG POSITIONS = %d\n", positionTable.noOfPositions );
  178.  
  179.     printf("SONG POSITIONS ...\n");
  180.  
  181.     for ( loop = 0; loop < positionTable.noOfPositions; loop++ )
  182.     {
  183.         printf("%3d\n", positionTable.position[loop] );
  184.     }
  185.  
  186.     if ( noOfInstruments > DEFAULT_NUM_INSTRUMENTS )
  187.         lseek( handle, 4L, SEEK_CUR);       /* skip past signature */
  188.  
  189.     //******************************************************************
  190.     //* READ PATTERN TABLES
  191.     //******************************************************************
  192.  
  193.     noOfPatternTables = 0;
  194.  
  195.     for ( loop = 0; loop < positionTable.noOfPositions; loop++ )
  196.     {
  197.         if (positionTable.position[loop] >= noOfPatternTables)
  198.             noOfPatternTables = positionTable.position[loop] + 1;
  199.     }
  200.  
  201.     for ( loop = 0; loop < noOfPatternTables; loop++)
  202.     {
  203.         printf( "---------------------------------------------------------------------------------------------------------------------------\n");
  204.  
  205.         printf( "PATTERN #%d\n", loop );
  206.         read( handle, (char *) &patternTable, sizeof(PATTERNTABLE) );
  207.  
  208.         for ( row = 0; row < MAX_PATTERN_ROWS; row++ )
  209.         {
  210.             for ( channel = 0; channel < MAX_CHANNELS; channel++ )
  211.             {
  212.                 word1 = WordSwap( patternTable.row[row].channel[channel].note.word1 );
  213.                 word2 = WordSwap( patternTable.row[row].channel[channel].note.word2 );
  214.  
  215.                 noteInstrument = ((word1 >> 8) & 0xF0) + ((word2 >> 12) & 0x0F);
  216.                 notePeriod     = (word1 & 0xFFF);
  217.                 noteEffect     = (word2 >> 8) & 0x0F;
  218.                 noteParm       = (word2 & 0xFF);
  219.  
  220.                 printf("Ins=");
  221.  
  222.                 if (noteInstrument)
  223.                     printf("[%2d]", noteInstrument);
  224.                 else
  225.                     printf("[  ]");
  226.  
  227.                 printf(" Per=");
  228.  
  229.                 if (notePeriod)
  230.                     printf("[%3d]", notePeriod);
  231.                 else
  232.                     printf("[   ]");
  233.  
  234.                 if (noteEffect)
  235.                     printf(" Eff=[%2.2X,%2.2X]", noteEffect, noteParm);
  236.                 else
  237.                     printf(" Eff=[     ]");
  238.  
  239.                 if (channel < (MAX_CHANNELS-1))
  240.                     printf("│");
  241.             }
  242.             printf("\n");
  243.         }
  244.     }
  245.  
  246.     close(handle);
  247. }
  248.  
  249.  
  250.  
  251.  
  252.