home *** CD-ROM | disk | FTP | other *** search
/ Sound Sensations! / sound_sensations.iso / convert / snd2voc / snd2voc.c next >
C/C++ Source or Header  |  1991-05-15  |  4KB  |  169 lines

  1. /* snd2voc.c */
  2.  
  3. /*
  4.    Converts raw mac soundfiles (.SOU) and soundtool files (.SND)
  5.    to Soundblaster .VOC format.  You need to specify the sample
  6.    rate with raw (.SOU) files.  It gets the rate from the header
  7.    of the .SND files, so you shouldn't specify it.
  8. */
  9.  
  10.  
  11. /*
  12.     I, Bailey Brown, hereby release this code to the public domain.
  13.     If you find any bugs, please let me know on compuserve
  14.     at 73020,3442 (I'm also on Genie, but I've never used Genie mail).
  15. */
  16.  
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #define BUFSIZE     (8*1024)
  23.  
  24. #define HEADERSIZE  (0x001a)
  25.  
  26. #define MAGICSTR    "Creative Voice File"
  27. #define EOFCHAR     (0x1a)
  28. #define DATAOFFSET  (0x001a)   /* word offset to start of data */
  29. #define VERSION     (0x010a)   /* version number */
  30. #define VERSIONC    (0x1129)   /* 2's comp of version no. + 1234h */
  31. #define TYPE        (1)
  32. #define VOCTERM     (0)
  33.  
  34. #define COMP8BIT    (0)
  35.  
  36. #define SRBYTE( sample_rate )  (256 - 1000000/(sample_rate))
  37.  
  38. main(int argc, char *argv[])
  39. {
  40.     long size;
  41.     unsigned short rate = 0;
  42.  
  43.     char comptype = COMP8BIT;
  44.  
  45.     unsigned char header[HEADERSIZE+6];
  46.     unsigned char *ph;
  47.     unsigned char *s;
  48.     unsigned char ch;
  49.  
  50.     int ifd, ofd;
  51.     int bytesread;
  52.  
  53.     unsigned char buf[BUFSIZE];
  54.  
  55.     char ifname[128];
  56.     char ofname[128];
  57.  
  58.     int has_ext;
  59.  
  60.  
  61.     if (argc < 3) {
  62.         fprintf(stderr, "usage: snd2voc soundfile vocfile [sampling rate]\n");
  63.         exit(1);
  64.     }
  65.     if (argc > 3)
  66.         rate = atoi(argv[3]);
  67.  
  68.  
  69.     ph = header;
  70.     s = MAGICSTR;
  71.  
  72.     while (*s)
  73.         *ph++ = *s++;
  74.  
  75.     *ph++ = EOFCHAR;
  76.  
  77.     *((int*)ph)++ = DATAOFFSET;
  78.     *((int*)ph)++ = VERSION;
  79.     *((int*)ph)++ = VERSIONC;
  80.  
  81.     *ph++ = TYPE;
  82.  
  83.     strcpy(ifname, argv[1]);
  84.     strcpy(ofname, argv[2]);
  85.  
  86.     ifd = open(ifname, O_RDONLY | O_BINARY);
  87.     if (ifd < 0) {
  88.         strcat(ifname, ".snd");
  89.         ifd = open(ifname, O_RDONLY | O_BINARY);
  90.     }
  91.     if (ifd < 0) {
  92.         strcpy(ifname, argv[1]);
  93.         strcat(ifname, ".sou");
  94.         ifd = open(ifname, O_RDONLY | O_BINARY);
  95.     }
  96.     if (ifd < 0) {
  97.         fprintf(stderr, "can't open input file: %s\n", argv[1]);
  98.         exit(1);
  99.     }
  100.  
  101.  
  102.     s = ofname;
  103.     has_ext = 0;
  104.     while (*s && !has_ext) {
  105.         if (*s++ == '.') {
  106.             has_ext = 1;
  107.         }
  108.     }
  109.     if (!has_ext) {
  110.         strcat(ofname, ".voc");
  111.     }
  112.  
  113.     ofd = open(ofname, O_WRONLY | O_CREAT | O_BINARY);
  114.     if (ofd < 0) {
  115.         fprintf(stderr, "can't open output file: %s\n", argv[2]);
  116.         exit(1);
  117.     }
  118.  
  119.     s = ifname;
  120.     while( *s && *s != '.') s++;
  121.  
  122.     if (*(s+2) == 'n' || *(s+2) == 'N') {
  123.         read(ifd, &rate, sizeof(unsigned short));
  124.         read(ifd, &rate, sizeof(unsigned short));
  125.         lseek(ifd, 4, SEEK_CUR);
  126.         size = filelength(ifd) + 2 - 4*2;
  127.     } else {
  128.         size = filelength(ifd) + 2;
  129.         if (rate == 0) {
  130.             fprintf(stderr, "must specify sampling rate\n");
  131.             exit(1);
  132.         }
  133.     }
  134.  
  135.  
  136.     *ph++ = size & 0x0000ff;
  137.     *ph++ = (size & 0x00ff00) >> 8;
  138.     *ph++ = (size & 0xff0000) >> 16;
  139.  
  140.     *ph++ = SRBYTE(rate);
  141.     *ph++ = comptype;
  142.  
  143.  
  144.     write(ofd, &header, sizeof(header));
  145.  
  146.     while ((bytesread = read(ifd, &buf, BUFSIZE)) > 0) {
  147.         write(ofd, &buf, bytesread);
  148.     }
  149.  
  150.     ch = VOCTERM;
  151.     write(ofd, &ch, 1);
  152.  
  153.     close(ifd);
  154.     close(ofd);
  155.  
  156.     return 0;
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.