home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / sndbords / proaudio / pas_sdk1 / pas / pcm / fixwave.c < prev    next >
C/C++ Source or Header  |  1992-10-02  |  10KB  |  430 lines

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