home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / sndbords / proaudio / pcmtlsrc / tpcm.arj / SNDFILE.C < prev    next >
C/C++ Source or Header  |  1992-07-29  |  5KB  |  250 lines

  1. ;    /*\
  2. ;    |*| $Author:   BCRANE  $
  3. ;    |*|
  4. ;    |*| $Date:   29 Jul 1992 16:58:06  $
  5. ;    |*|
  6. ;    |*| $Header:   W:/sccs/sdkapp/sndfile.c_v   1.0   29 Jul 1992 16:58:06   BCRANE  $
  7. ;    |*|
  8. ;    |*| $Log:   W:/sccs/sdkapp/sndfile.c_v  $
  9.  * 
  10.  *    Rev 1.0   29 Jul 1992 16:58:06   BCRANE
  11.  * Initial revision.
  12. ;    |*|
  13. ;    |*| $Logfile:   W:/sccs/sdkapp/sndfile.c_v  $
  14. ;    |*|
  15. ;    |*| $Modtimes$
  16. ;    |*|
  17. ;    |*| $Revision:   1.0  $
  18. ;    |*|
  19. ;    |*| $Workfile:   sndfile.c  $
  20. ;    |*|
  21. ;    \*/
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <io.h>
  28. #include <sys\types.h>
  29. #include <sys\stat.h>
  30.  
  31. typedef unsigned char uchar;
  32. typedef unsigned int uint;
  33. typedef unsigned long ulong;
  34.  
  35. #include "sndfile.h"
  36.  
  37. #define OKAY 0
  38. #define ERROR -1
  39.  
  40. #define WAVTYPE 1
  41. #define AVITYPE 2
  42. #define BUFTYPE 99
  43. #define VOCTYPE 98
  44.  
  45. static char *copyright= "getsndfile() - Copyright (c) 1992.  Media Vision, Inc.  All Rights Reserved.";
  46. static char *programmer= "Bart Crane";
  47. char *fhoutname;
  48. int fhout;
  49. getsndfile(int fhinp, char *inpname, BT *bt, long samplerate, int channels)
  50. {
  51.     ulong br;
  52.  
  53.     switch (getfiletype(fhinp))
  54.         {
  55.         case AVITYPE:
  56.             if (!(fhoutname= tempnam(".", "tpcmtmp")))
  57.                 break;
  58.             if ((fhout= open(fhoutname, O_BINARY| O_WRONLY| O_CREAT| O_TRUNC, S_IWRITE| S_IREAD)) == ERROR)
  59.                 free(fhoutname);
  60.             printf("AVI file: creating %s.\n", fhoutname);
  61.  
  62.         case WAVTYPE:
  63.             {
  64.             CHUNK ch;
  65.  
  66.             while ((br= read(fhinp, (char *) &ch, sizeof(CHUNK))) == sizeof(CHUNK))
  67.                 {
  68.                 if (idequals(ch.id, 'R', 'I', 'F', 'F'))
  69.                     {
  70.                     lseek(fhinp, 4L, SEEK_CUR);
  71.                     continue;
  72.                     }
  73.                 if (idequals(ch.id, 'W', 'A', 'V', 'E'))
  74.                     {
  75.                     lseek(fhinp, -4L, SEEK_CUR);
  76.                     continue;
  77.                     }
  78.                 if (idequals(ch.id, 'n', 'a', 'm', 'e'))
  79.                     {
  80.                     lseek(fhinp, ch.size, SEEK_CUR);
  81.                     continue;
  82.                     }
  83.                 if (idequals(ch.id, 'f', 'm', 't', ' '))
  84.                     {
  85.                     read(fhinp, (char *) &bt->wf, sizeof(WF));
  86.                     lseek(fhinp, ch.size- sizeof(WF), SEEK_CUR);
  87.                     continue;
  88.                     }
  89.                 if (idequals(ch.id, 'd', 'a', 't', 'a'))
  90.                     {
  91.                     bt->fhidx= fhinp;
  92.                     bt->foffset= lseek(fhinp, 0L, SEEK_CUR);
  93.                     bt->offset= 0;
  94.                     bt->size= ch.size;
  95.                     break;
  96.                     }
  97.                 if (idequals(ch.id, 'L', 'I', 'S', 'T'))
  98.                     {
  99.                     continue;
  100.                     }
  101.                 if (idequals(ch.id, 'm', 'o', 'v', 'i'))
  102.                     {
  103.                     lseek(fhinp, -4L, SEEK_CUR);
  104.                     continue;
  105.                     }
  106.                 if (idequals(ch.id, 'r', 'e', 'c', ' '))
  107.                     {
  108.                     lseek(fhinp, -4L, SEEK_CUR);
  109.                     continue;
  110.                     }
  111.                 if (idequals(ch.id, '0', '0', 'w', 'b'))
  112.                     {
  113.                     char *buf= calloc(ch.size, sizeof(char));
  114.                     if (buf)
  115.                         {
  116.                         read(fhinp, buf, ch.size);
  117.                         write(fhout, buf, ch.size);
  118.                         free(buf);
  119.                         }
  120.                     continue;
  121.                     }
  122.                 if (idequals(ch.id, 's', 't', 'r', 'l'))
  123.                     {
  124.                     lseek(fhinp, -4L, SEEK_CUR);
  125.                     continue;
  126.                     }
  127.                 if (idequals(ch.id, 's', 't', 'r', 'h'))
  128.                     {
  129.                     lseek(fhinp, ch.size, SEEK_CUR);
  130.                     continue;
  131.                     }
  132.                 if (idequals(ch.id, 's', 't', 'r', 'f'))
  133.                     {
  134.                     lseek(fhinp, ch.size, SEEK_CUR);
  135.                     continue;
  136.                     }
  137.                 lseek(fhinp, ch.size, SEEK_CUR);
  138.                 }
  139.             }
  140.             break;
  141.  
  142.         case VOCTYPE:
  143.         case BUFTYPE:
  144.         default:
  145.             {
  146.             bt->fhidx= fhinp;
  147.             bt->foffset= lseek(fhinp, 0L, SEEK_CUR);
  148.             bt->offset= 0;
  149.             bt->size= lseek(fhinp, 0L, SEEK_END);
  150.             bt->wf.fmttag= 1;
  151.             bt->wf.channels= channels;
  152.             bt->wf.samplerate= samplerate;
  153.             bt->wf.bytespersec= bt->wf.channels* bt->wf.samplerate;
  154.             bt->wf.blockalign= bt->wf.channels;
  155.             bt->wf.datasize= 8;
  156.  
  157.                 {
  158.                 int i;
  159.                 char *n= inpname;
  160.  
  161.                 for (i= 0; i < 8; i++)
  162.                     if (!(isxdigit(n[i])))
  163.                         break;
  164.  
  165.                 if (i == 8 && n[8] == '.' && n[11] == '\0')
  166.                     {
  167.                     bt->wf.samplerate=  (ulong) isdigit(n[4] ? n[4]- '0' : (n[4]& 0xDF)- 'A'+ 10)* 0x1000;
  168.                     bt->wf.samplerate+= (ulong) isdigit(n[5] ? n[5]- '0' : (n[5]& 0xDF)- 'A'+ 10)* 0x0100;
  169.                     bt->wf.samplerate+= (ulong) isdigit(n[6] ? n[6]- '0' : (n[6]& 0xDF)- 'A'+ 10)* 0x0010;
  170.                     bt->wf.samplerate+= (ulong) isdigit(n[7] ? n[7]- '0' : (n[7]& 0xDF)- 'A'+ 10)* 0x0001;
  171.                     }
  172.  
  173.                 }
  174.             }
  175.             break;
  176.         }
  177.  
  178.     if (fhoutname)
  179.         free(fhoutname);
  180.  
  181.     if (fhout)
  182.         close(fhout);
  183.  
  184.     return(OKAY);
  185. }
  186.  
  187. static char cvfstr[]= "Creative Voice File";
  188. getfiletype(int fhinp)
  189. {
  190.     union header
  191.         {
  192.         struct riffhdr
  193.             {
  194.             CHUNK ch;
  195.             char class[4];
  196.             } rh;
  197.         char voc[19];
  198.         } typbuf;
  199.  
  200.     checkriff:
  201.     lseek(fhinp, 0L, SEEK_SET);
  202.     clrbuf((char *) &typbuf, sizeof(typbuf.rh));
  203.  
  204.     if ((read(fhinp, (char *) &typbuf, sizeof(typbuf.rh))) != sizeof(typbuf.rh))
  205.         goto checkvoc;
  206.  
  207.     if (idequals(typbuf.rh.ch.id, 'R', 'I', 'F', 'F'))
  208.         {
  209.         if (idequals(typbuf.rh.class, 'W', 'A', 'V', 'E'))
  210.             return(WAVTYPE);
  211.         else
  212.         if (idequals(typbuf.rh.class, 'A', 'V', 'I', ' '))
  213.             return(AVITYPE);
  214.         else
  215.             return(BUFTYPE);
  216.         }
  217.  
  218.     checkvoc:
  219.     lseek(fhinp, 0L, SEEK_SET);
  220.     clrbuf((char *) &typbuf, sizeof(typbuf.voc));
  221.  
  222.     if ((read(fhinp, (char *) &typbuf, sizeof(typbuf.voc))) != sizeof(typbuf.voc))
  223.         goto checkbuf;
  224.  
  225.         {
  226.         int i;
  227.  
  228.         for (i= 0; i < sizeof(typbuf.voc); i++)
  229.             if (cvfstr[i] != typbuf.voc[i])
  230.                 break;
  231.  
  232.         if (i == sizeof(typbuf.voc))
  233.             return(VOCTYPE);
  234.         }
  235.  
  236.     checkbuf:
  237.     lseek(fhinp, 0L, SEEK_SET);
  238.     clrbuf((char *) &typbuf, sizeof(typbuf));
  239.  
  240.     return(BUFTYPE);
  241. }
  242.  
  243. clrbuf(char *buf, uint size)
  244. {
  245.     while (size--)
  246.         *buf++= '\0';
  247. }
  248.  
  249.  
  250.