home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d114 / wblander.lha / WBLander / Source / 8svx_to_c.c < prev    next >
C/C++ Source or Header  |  1987-11-22  |  4KB  |  182 lines

  1. /* quasi-minimal IFF 8SVX sample file reader
  2.  * by Karl Lehenbauer
  3.  */
  4.  
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <assert.h>
  10.  
  11. #include "iff/iff.h"
  12. #include "iff/8svx.h"
  13.  
  14. #define N_PER_LINE 15
  15.  
  16. SplashForm(bufp,nbytes)
  17. char *bufp;
  18. int nbytes;
  19. {
  20.     int n_thisline = 0;
  21.  
  22.     fputs("{",stdout);
  23.  
  24.     while (--nbytes)
  25.     {
  26.         printf("%4d,",*bufp++);
  27.         if (++n_thisline >= N_PER_LINE)
  28.         {
  29.             fputs("\n ",stdout);
  30.             n_thisline = 0;
  31.         }
  32.     }
  33.     printf("%4d};\n",*bufp);
  34. }
  35.  
  36. PutID(id)
  37. ID id;
  38. {
  39.     fprintf(stderr,"%c%c%c%c",
  40.        (char)((id>>24L) & 0x7f),
  41.        (char)((id>>16L) & 0x7f),
  42.        (char)((id>>8)   & 0x7f),
  43.        (char)(id        & 0x7f) );
  44. }
  45.  
  46. LONG chunkuntil(fd,chunktype)
  47. int fd;
  48. ULONG chunktype;
  49. {
  50.     ChunkHeader untilheader;
  51.     int sawsize;
  52.  
  53.     while (1)
  54.     {
  55.         if ((sawsize = read(fd,&untilheader,sizeof(untilheader))) != sizeof(untilheader))
  56.         {
  57.             printf("Something's wrong with chunkuntil! (sawsize %d)\n",sawsize);
  58.             return(-1);
  59.         }
  60.         /* fputs("chunkuntil: checking ",stderr);
  61.         PutID(untilheader.ckID);
  62.         fprintf(stderr,", size is %d...",untilheader.ckSize); */
  63.  
  64.         if (untilheader.ckID == chunktype)
  65.         {
  66.             /* fputs("found it\n",stderr); */
  67.             return(untilheader.ckSize);
  68.         }
  69.  
  70.         /* fputs("skipped\n",stderr); */
  71.  
  72.         /* skip over chunk data and skip an extra byte if length is odd */
  73.         lseek(fd,untilheader.ckSize,1);
  74.         if (untilheader.ckSize & 1)
  75.             lseek(fd,1L,1);
  76.     }
  77. }
  78.  
  79. svx_to_c(fname,ofname)
  80. char *fname, *ofname;
  81. {
  82.     int iffile;
  83.     ChunkHeader chunkhead;
  84.     Voice8Header voicehead;
  85.     LONG samplebytes;
  86.     LONG formtype;
  87.     char *sampbufp;
  88.  
  89.     /* fputs("svx_to_c",stderr); */
  90.     assert(sizeof(chunkhead) == 8);
  91.  
  92.     if (fname[strlen(fname)-2] != '.')
  93.     {
  94.         fputs("svx_to_c: IFF sample file name must end with a period and a single char\n",stderr);
  95.         exit(-1);
  96.     }
  97.  
  98.     if ((iffile = open(fname, O_RDONLY)) < 0)
  99.     {
  100.         fprintf(stderr,"svx_to_c: can't open IFF sample file %s\n",fname);
  101.         return(-1);
  102.     }
  103.  
  104.     if (freopen(ofname,"w", stdout) == NULL)
  105.     {
  106.         fprintf(stderr,"svx_to_c: can't open output file %s\n",ofname);
  107.         return(-1);
  108.     }
  109.  
  110.     if (read(iffile, &chunkhead, sizeof(chunkhead)) < 0)
  111.     {
  112.         fprintf(stderr,"svx_to_c: initial read from IFF sample file %s failed!\n",fname);
  113.         return(-1);
  114.     }
  115.  
  116.     if (chunkhead.ckID != FORM)
  117.     {
  118.         fprintf(stderr,"svx_to_c: File %s isn't IFF, is too complex, or doesn't start with FORM\n",fname);
  119.         return(-1);
  120.     }
  121.     /* fprintf(stderr,"svx_to_c: FORM found, size is %d\n",chunkhead.ckSize); */
  122.  
  123.     read(iffile, &formtype, sizeof(formtype));
  124.  
  125.     if (formtype != ID_8SVX)
  126.     {
  127.         fprintf(stderr,"svx_to_c: File %s is IFF but is not 8SVX\n",fname);
  128.         return(-1);
  129.     }
  130.     /* fputs("svx_to_c: IFF FORM is 8SVX\n",stderr); */
  131.  
  132.     if (chunkuntil(iffile,ID_VHDR) != 20)
  133.     {
  134.         fputs("svx_to_c: IFF 8SVX VHDR chunk search failed or length not 20\n",stderr);
  135.         return(-1);
  136.     }
  137.     assert(sizeof(voicehead) == 20);
  138.  
  139.     if (read(iffile,&voicehead,sizeof(voicehead)) != sizeof(voicehead))
  140.     {
  141.         fputs("svx_to_c: read of IFF VHDR chunk failed\n",stderr);
  142.         return(-1);
  143.     }
  144.     /* fprintf(stderr,"Samples per second: %d\n",voicehead.samplesPerSec); */
  145.  
  146.     if ((samplebytes = chunkuntil(iffile,ID_BODY)) < 0)
  147.     {
  148.         fputs("svx_to_c: IFF 8SVX BODY chunk search failed\n",stderr);
  149.         return(-1);
  150.     }
  151.  
  152.     /* fprintf(stderr,"Number of bytes in waveform: %d\n",samplebytes); */
  153.  
  154.     if ((sampbufp = (char *)AllocMem(samplebytes,MEMF_CHIP)) == (char *)NULL)
  155.     {
  156.         fputs("svx_to_c: unable to allocate memory for waveform\n",stderr);
  157.         return(-1);
  158.     }
  159.     if (read(iffile,sampbufp,samplebytes) != samplebytes)
  160.     {
  161.         fputs("Read of sample data failed!\n",stderr);
  162.         return(-1);
  163.     }
  164.  
  165.     /* write out C program containing IFF sample */
  166.  
  167.     puts("#include <exec/types.h>");
  168.     puts("#include \"audio:sample.h\"\n");
  169.  
  170.     fname[strlen(fname)-2] = '\0';
  171.     printf("GLOBAL BYTE %swave[] = \n",fname);
  172.     SplashForm(sampbufp,samplebytes);
  173.  
  174.     printf("\nGLOBAL Sample %sSample = {%d, %d , &%swave[0]};\n",fname,voicehead.samplesPerSec,samplebytes,fname);
  175.  
  176.     FreeMem(sampbufp,samplebytes);
  177.     /* fputs("svx_to_c: TTFN\n",stderr); */
  178.     return(1);
  179. }
  180.  
  181. /* end of file */
  182.