home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the DOOM Programming Gurus / Tricks_of_the_Doom_Programming_Gurus.iso / bonus / linux / dm2au / raw2au.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-25  |  2.7 KB  |  103 lines

  1.  
  2. /* This module extracts raw sound information from a wadfile */
  3.  
  4. #include    <stdio.h>
  5. #include    "doomdefs.h"
  6. #include    "directory.h"
  7.  
  8. #define ULAW_CONVERT        /* Convert the sound into sun .au format */
  9.  
  10. #define SND_MAGIC    3
  11. struct snd_header {
  12.     two_byte magic;            /* The magic number 3 */
  13.     two_byte sample_rate;        /* The sample rate (11025) */
  14.     two_byte num_samples;        /* The number of samples */
  15.     two_byte zero;            /* Null integer */
  16.     };
  17.  
  18.  
  19. int convert_rawsnd(int fd, char *name)
  20. {
  21.     struct dir_entry entry;
  22.     struct snd_header header;
  23.     char outfile[BUFSIZ];
  24.     FILE *output;
  25.     int i, samplei;
  26.     unsigned char sample, ulaw;
  27.     float sum = 0, increment;
  28.  
  29.     /* Set the fd to the start of the directory entries */
  30.     if ( set_directory(fd) < 0 ) {
  31.         fprintf(stderr, "Can't find start of directories.\n");
  32.         return(-1);
  33.     }
  34.  
  35.     /* Now cycle through the entries, looking for the sound  */
  36.     if ( get_entry(fd, name, &entry) < 0 ) {
  37.         fprintf(stderr, "Couldn't find directory entry for %s\n", name);
  38.         return(-1);
  39.     }
  40.  
  41.     /* Go to the entry and retrieve the sound */
  42.     if ( lseek(fd, entry.res_start, SEEK_SET) < 0 ) {
  43.         perror("lseek()");
  44.         return(-1);
  45.     }
  46.     if (read(fd, (char *)&header, sizeof(header)) != sizeof(header)) {
  47.         perror("header read error");
  48.         return(-1);
  49.     }
  50.     if ( header.magic != SND_MAGIC ) {
  51.         fprintf(stderr, "%s is not a sound resource (%d).\n",name,header.magic);
  52.         return(-1);
  53.     }
  54.     /* Print out the header for now */
  55.     printf("Sound: sample rate = %d, holds %d samples.\n",
  56.                 header.sample_rate, header.num_samples);
  57.  
  58.     /* increment is the number of bytes to read each time */
  59.     increment = ((float)header.sample_rate)/8000;
  60.  
  61.     /* Make sure we can write the output file */
  62. #ifdef ULAW_CONVERT
  63.     sprintf(outfile, "%s.au", name);
  64. #else
  65.     sprintf(outfile, "%s.snd", name);
  66. #endif
  67.     if ( (output=fopen(outfile, "w")) == NULL ) {
  68.         fprintf(stderr, "Can't open %s for writing.\n", outfile);
  69.         return(-1);
  70.     }
  71.     /* Write the output file */
  72.     for ( i=1, read(fd, &sample, 1); i<header.num_samples; ) {
  73. #ifdef ULAW_CONVERT
  74.         /* convert the excess 128 to two's complement */
  75.         samplei = 0x80 - sample;
  76.         /* increase the volume, convert to uLAW */
  77.         ulaw = (unsigned char)sample_cvt(samplei * 16);
  78.         /* Output the converted sample */
  79.         fputc((char)ulaw, output);
  80.  
  81.         /* skip input to compensate for sampling frequency diff */
  82.         sum += increment;
  83.         while(sum > 0) {
  84.             if ( read(fd, &sample, 1) != 1 ) {
  85.                 perror("read()");
  86.                 return(-1);
  87.             }
  88.             ++i; --sum;
  89.         }
  90. #else /* Raw snd format */
  91.         fputc(sample, output);
  92.         if ( read(fd, &sample, 1) != 1 ) {
  93.             perror("read()");
  94.             return(-1);
  95.         }
  96. #endif
  97.     }
  98.     fclose(output);
  99.  
  100.     /* Done! */
  101.     return(0);
  102. }
  103.