home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / muldisk1.zip / PAS / PCM / FIXWAVE.C < prev    next >
C/C++ Source or Header  |  1992-12-08  |  10KB  |  437 lines

  1. /*$Author:   DCODY  $*/
  2. /*$Date:   08 Dec 1992 16:38:42  $*/
  3. /*$Header:   X:/sccs/pcmapps/fixwave.c_v   1.3   08 Dec 1992 16:38:42   DCODY  $*/
  4. /*$Log:   X:/sccs/pcmapps/fixwave.c_v  $
  5.  * 
  6.  *    Rev 1.3   08 Dec 1992 16:38:42   DCODY
  7.  * removed some warning messages by doing explicit type casting.
  8.  * 
  9.  *    Rev 1.2   06 Oct 1992 15:19:20   DCODY
  10.  * fixwave now just reports the contents of the file, it doesn't fix the file
  11.  * unless the user explicitly requests it.
  12.  * 
  13.  *    Rev 1.1   23 Jun 1992 16:09:36   DCODY
  14.  * pas2 update...
  15.  * 
  16.  *    Rev 1.0   15 Jun 1992 09:26:32   BCRANE
  17.  * Initial revision.
  18. */
  19. /*$Logfile:   X:/sccs/pcmapps/fixwave.c_v  $*/
  20. /*$Modtimes$*/
  21. /*$Revision:   1.3  $*/
  22. /*$Workfile:   fixwave.c  $*/
  23.  
  24.  
  25. ;    /*\
  26. ;---|*|----====< FIXWAVE.C >====----
  27. ;---|*|
  28. ;---|*| This program Corrects the average # of samples in a stereo file.
  29. ;---|*|
  30. ;    \*/
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <dos.h>
  35.  
  36. #include "play.h"
  37.  
  38.  
  39. ;    /*\
  40. ;---|*|----====< Global Variables >====----
  41. ;    \*/
  42.  
  43. #define TRUE    -1
  44. #define FALSE     0
  45.  
  46.         struct find_t  w_file;            /* wave file stuff                */
  47.         static FILE *inf;                /* user input file                */
  48.         fpos_t pos1;
  49.         fpos_t pos2;
  50.         fpos_t pos3;
  51.         fpos_t pos4;
  52.  
  53.  
  54.         RiffWave fhd = {
  55.             { "RIFF", 0L },
  56.             { "WAVE",
  57.                 { "fmt ", sizeof(WaveInfo) , { 1, 1, 11025L, 11025L, 1, 8}, },
  58.                 { "data", 0L },
  59.             }
  60.         };
  61.  
  62.         int align;
  63.         int fixed;
  64.         int rdhead;
  65.         int bitspersample;
  66.  
  67.         unsigned long rate;
  68.         unsigned long ave;
  69.  
  70.         int DoSave = FALSE;             // save-the-changes flag
  71.  
  72.  
  73. ;    /*\
  74. ;---|*|
  75. ;---|*|----====< Main >====----
  76. ;---|*|
  77. ;---|*| Play the voice file out to the PCM hardware
  78. ;---|*|
  79. ;    \*/
  80.  
  81. main(argc,argv)
  82.     int  argc;
  83.     char *argv[];
  84. {
  85. int n,ch,work;
  86. unsigned long lwork;
  87. unsigned long l;
  88. char c;
  89.  
  90.     /* we have not fixed any fields                                     */
  91.  
  92.         fixed = FALSE;
  93.  
  94.     /* Check parameters                                                 */
  95.  
  96.         CommandLine (argc,argv);
  97.  
  98.     /* get the 1st 2 characters in the file                             */
  99.  
  100.         n = fgetc(inf) & 0xff;
  101.         n = n + ((fgetc(inf) & 0xff) << 8);
  102.  
  103.         fseek (inf,0L,SEEK_SET); /* rewind to the start                 */
  104.  
  105.     /* special case a .WAV file                                         */
  106.  
  107.         if (n != 0x4952) {
  108.             printf ("this is not a .wav file!\n");
  109.             exit(1);
  110.         }
  111.  
  112.     /* print the RIFF portion of the header                             */
  113.  
  114.         printf ("RIFF header information:\n\n");
  115.  
  116.         for (n=4;n;n--)
  117.             putch (fgetc(inf));
  118.         printf ("\n");
  119.  
  120.         fgetpos (inf,&pos3);
  121.         l  =  (unsigned long) fgetc(inf) & 0xff;
  122.         l += ((unsigned long) fgetc(inf) & 0xff) << 8;
  123.         l += ((unsigned long) fgetc(inf) & 0xff) <<16;
  124.         l += ((unsigned long) fgetc(inf) & 0xff) <<24;
  125.  
  126.         fhd.riff.length = l;
  127.  
  128.         printf ("  length=%lu\n",l);
  129.  
  130.     /* pas up the wave block header name                                */
  131.  
  132.         if ((unsigned long) fgetc(inf) != 'W') {
  133.             printf ("\aUnknown header in the .WAV file! (should be 'WAVE')\n");
  134.             exit(1);
  135.         }
  136.  
  137.         putch ('W');
  138.         for (n=3;n;n--)
  139.             putch ((unsigned long) fgetc(inf));
  140.         printf ("\n");
  141.  
  142.     /* pass up the format section name                                    */
  143.  
  144.         c = (unsigned long) fgetc(inf);
  145.         if (c != 'f') {
  146.             printf ("\aUnknown header in the .WAV file! (should be 'fmt ')\n");
  147.             exit(1);
  148.         }
  149.         putch (c);
  150.         for (n=3;n;n--)
  151.             putch((unsigned long) fgetc(inf));    /* move past the data    */
  152.         printf ("\n");
  153.  
  154.         // fmt <length>
  155.  
  156.         l  =  (unsigned long) fgetc(inf) & 0xff;
  157.         l += ((unsigned long) fgetc(inf) & 0xff) << 8;
  158.         l += ((unsigned long) fgetc(inf) & 0xff) <<16;
  159.         l += ((unsigned long) fgetc(inf) & 0xff) <<24;
  160.  
  161.         printf ("  length =%lu\n",l);
  162.  
  163.         // format tag
  164.  
  165.         l  = (unsigned long) fgetc(inf)  & 0xff;
  166.         l += ((unsigned long) fgetc(inf) & 0xff) << 8;
  167.  
  168.         printf ("  format =%lu\n",l);
  169.  
  170.         // # of channel
  171.  
  172.         ch =  (unsigned long) fgetc(inf) & 0xff;
  173.         ch += ((unsigned long) fgetc(inf) & 0xff) << 8;
  174.  
  175.         printf ("  channel=%d\n",ch);
  176.  
  177.         // Samples per second
  178.  
  179.         l  =  (unsigned long) fgetc(inf) & 0xff;
  180.         l += ((unsigned long) fgetc(inf) & 0xff) << 8;
  181.         l += ((unsigned long) fgetc(inf) & 0xff) <<16;
  182.         l += ((unsigned long) fgetc(inf) & 0xff) <<24;
  183.  
  184.         rate = l;
  185.         printf ("  s-rate =%lu\n",rate);
  186.  
  187.         fgetpos (inf,&pos1);
  188.         l  =  (unsigned long) fgetc(inf) & 0xff;
  189.         l += ((unsigned long) fgetc(inf) & 0xff) << 8;
  190.         l += ((unsigned long) fgetc(inf) & 0xff) <<16;
  191.         l += ((unsigned long) fgetc(inf) & 0xff) <<24;
  192.  
  193.         // Average Samples per second
  194.  
  195.         ave = l;
  196.         printf ("  average=%lu\n",ave);
  197.  
  198.         // Block Alignment
  199.  
  200.         fgetpos (inf,&pos2);
  201.         align  =  (unsigned long) fgetc(inf) & 0xff;
  202.         align += ((unsigned long) fgetc(inf) & 0xff) << 8;
  203.  
  204.         printf ("  align  =%d\n",align);
  205.  
  206.         // Bits per sample
  207.  
  208.         l  = (unsigned long) fgetc(inf)  & 0xff;
  209.         l += ((unsigned long) fgetc(inf) & 0xff) << 8;
  210.         bitspersample = l & 0xff;
  211.  
  212.         printf ("  bits   =%lu\n",l);
  213.  
  214.         c = (unsigned long) fgetc(inf);
  215.         if (c != 'd') {
  216.             printf ("\aUnknown header in the .WAV file! (should be 'fmt ')\n");
  217.             exit(1);
  218.         }
  219.         putch (c);
  220.         for (n=3;n;n--)
  221.             putch((unsigned long) fgetc(inf));    /* move past the data    */
  222.         printf ("\n");
  223.  
  224.         fgetpos (inf,&pos4);
  225.         l  =  (unsigned long) fgetc(inf) & 0xff;
  226.         l += ((unsigned long) fgetc(inf) & 0xff) << 8;
  227.         l += ((unsigned long) fgetc(inf) & 0xff) <<16;
  228.         l += ((unsigned long) fgetc(inf) & 0xff) <<24;
  229.  
  230.         fhd.wave.data.length = l;
  231.  
  232.         printf ("  length =%lu\n\n",l);
  233.  
  234.         ///// This is the average sample count for stereo file errors /////
  235.  
  236.         if (ch == 2) {
  237.             lwork = rate << (((bitspersample==16)?1:0) + ch-1);
  238.             if (lwork != ave) {
  239.                 printf ("This stereo file average sample count is incorrect,\n");
  240.                 if (DoSave) {
  241.                     printf ("now saving the average samples as %lu\n",lwork );
  242.                     fsetpos (inf,&pos1);
  243.                     fputc ((int)( lwork      & 0xff),inf);
  244.                     fputc ((int)((lwork>> 8) & 0xff),inf);
  245.                     fputc ((int)((lwork>>16) & 0xff),inf);
  246.                     fputc ((int)((lwork>>24) & 0xff),inf);
  247.                     fixed = TRUE;
  248.                 }
  249.             }
  250.             else
  251.                 printf ("This stereo file average sample count is correct...\n");
  252.         }
  253.         else
  254.             printf ("This is a mono file. The average sample count is correct...\n");
  255.  
  256.         ///// This is the alignment size error created by FIXWAVE.EXE /////
  257.  
  258.         if (bitspersample == 16) {
  259.             work = 1 << (((ch == 1) ? 0 : 1) + 1);
  260.             if (align != work) {
  261.                 align = work & 0xff;
  262.                 printf ("This file's sample alignment is incorrect,\n");
  263.  
  264.                 if (DoSave) {
  265.                     printf ("now saving the alignment as %d\n",work);
  266.                     fsetpos (inf,&pos2);
  267.                     fputc (( work        & 0xff),inf);
  268.                     fputc (((work >> 8) & 0xff),inf);
  269.                     fixed = TRUE;
  270.                 }
  271.  
  272.             }
  273.             else
  274.                 printf ("This file's alignment is correct...\n");
  275.         }
  276.  
  277.         ///// This is the alignment size error /////
  278.  
  279.         if (ch == 2) {
  280.             work = 1 << (((bitspersample == 8) ? 0 : 1) + ch-1);
  281.             if (align != work) {
  282.                 printf ("This stereo file alignment is incorrect,\n");
  283.  
  284.                 if (DoSave) {
  285.                     printf ("now saving the alignment as %d\n",work);
  286.                     fsetpos (inf,&pos2);
  287.                     fputc (( work        & 0xff),inf);
  288.                     fputc (((work >> 8) & 0xff),inf);
  289.                     fixed = TRUE;
  290.                 }
  291.             }
  292.             else
  293.                 printf ("This stereo file alignment count is correct...\n");
  294.         }
  295.         else
  296.             printf ("This is a mono file. The alignment count sample count is correct...\n");
  297.  
  298.         ///// This is the 2-off error in the RIFF and DATA lengths    /////
  299.  
  300.         rdhead = 0;
  301.         if ((w_file.size - fhd.riff.length) == 6) {
  302.  
  303.             printf ("The RIFF and DATA lengths are incorrect,\n");
  304.  
  305.             if (DoSave) {
  306.  
  307.                 printf ("now correcting...\n");
  308.  
  309.                 // correct the RIFF size
  310.  
  311.                 l = fhd.riff.length - 2;
  312.                 fsetpos (inf,&pos3);
  313.                 fputc ((int)( l       & 0xff),inf);
  314.                 fputc ((int)((l >> 8) & 0xff),inf);
  315.                 fputc ((int)((l >>16) & 0xff),inf);
  316.                 fputc ((int)((l >>24) & 0xff),inf);
  317.  
  318.                 // correct the DATA size
  319.  
  320.                 l = fhd.wave.data.length - 2;
  321.                 fsetpos (inf,&pos4);
  322.                 fputc ((int)( l       & 0xff),inf);
  323.                 fputc ((int)((l >> 8) & 0xff),inf);
  324.                 fputc ((int)((l >>16) & 0xff),inf);
  325.                 fputc ((int)((l >>24) & 0xff),inf);
  326.                 rdhead = fixed = TRUE;
  327.  
  328.             }
  329.         }
  330.  
  331.         if (w_file.size < fhd.riff.length) {
  332.  
  333.             printf ("The RIFF and DATA lengths are greater than the file length,\n");
  334.  
  335.             if (DoSave) {
  336.  
  337.                 printf ("now correcting...\n");
  338.  
  339.                 // correct the RIFF size
  340.  
  341.                 l = w_file.size - sizeof (RiffHeader);
  342.                 fsetpos (inf,&pos3);
  343.                 fputc ((int)( l       & 0xff),inf);
  344.                 fputc ((int)((l >> 8) & 0xff),inf);
  345.                 fputc ((int)((l >>16) & 0xff),inf);
  346.                 fputc ((int)((l >>24) & 0xff),inf);
  347.  
  348.                 rdhead = fixed = TRUE;
  349.  
  350.                 if (w_file.size < fhd.wave.data.length) {
  351.  
  352.                     // correct the DATA size
  353.  
  354.                     l = w_file.size -
  355.                         (sizeof (RiffHeader) + sizeof(WaveHeader) - sizeof (long));
  356.                     fsetpos (inf,&pos4);
  357.                     fputc ((int)( l       & 0xff),inf);
  358.                     fputc ((int)((l >> 8) & 0xff),inf);
  359.                     fputc ((int)((l >>16) & 0xff),inf);
  360.                     fputc ((int)((l >>24) & 0xff),inf);
  361.                 }
  362.             }
  363.         }
  364.  
  365.         if (!rdhead)
  366.             printf ("This RIFF and DATA size are correct...\n");
  367.  
  368.     /* save the changes, if any were made...                                */
  369.  
  370.         if (fixed)
  371.             fclose (inf);
  372.  
  373. }
  374.  
  375. ;    /*\
  376. ;---|*|----====< CommandLine >====----
  377. ;---|*|
  378. ;---|*| Check the parameters
  379. ;---|*|
  380. ;    \*/
  381.  
  382. CommandLine (argc,argv)
  383.     int argc;
  384.     char *argv[];
  385. {
  386. int n;
  387. char *s;
  388.  
  389.     // say hello...
  390.  
  391.         printf ("\nFIXWAVE, Version 1.04 - Checks/Fixes .WAV header errors from RECFILE.EXE.\n");
  392.         printf ("Courtesy of Media Vision, Inc.\n\n");
  393.  
  394.     // stop if there are no more parameters
  395.  
  396.         if (argc < 2) {
  397.             printf ("To Use: DOS>FIXWAVE FILE.WAV [/C] \n\n");
  398.             printf ("Where:  [/C] corrects detected errors.\n\n");
  399.             exit(1);
  400.         }
  401.  
  402.     // open the file and get the file size.
  403.  
  404.         if ((inf = fopen(argv[1],"r+b")) == 0) {
  405.             printf ("\aBad File Name, \"%s\" Try Again!\n",argv[1]);
  406.             exit(1);
  407.         }
  408.         _dos_findfirst (argv[1],_A_ARCH|_A_NORMAL,&w_file);
  409.  
  410.     // process any switches
  411.  
  412.         n = 2;
  413.         while (n < argc) {
  414.  
  415.             s = argv[n++];
  416.  
  417.             if (*s == '/') s++;
  418.             if (*s == '-') s++;
  419.  
  420.             switch (*s & 0x5f) {
  421.  
  422.                 case 'C':
  423.                     DoSave = TRUE;
  424.                     break;
  425.  
  426.                 default:
  427.                     break;
  428.  
  429.             }
  430.         }
  431. }
  432.  
  433. ;    /*\
  434. ;---|*| end of FIXWAVE.C
  435. ;    \*/
  436.  
  437.