home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / SBDOS10.ZIP / RAW2VOC.C < prev    next >
Text File  |  1992-06-25  |  2KB  |  83 lines

  1. /* go from raw to vocfile */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. unsigned char string1[]={'C','r','e','a','t','i','v','e',' ','V','o','i','c',
  8.                'e',' ','F','i','l','e',0x1a,0x1a,0,0x0a,1,0x29,0x11};
  9.  
  10. unsigned char buffer[64000];
  11.  
  12. main(int argc, char **argv)
  13. {
  14.    unsigned int speed, time_constant;
  15.    size_t hunk_size;
  16.    fpos_t length, count;
  17.    FILE *infile;
  18.    FILE *outfile;
  19.  
  20.    if ( argc < 4 ) { printf("Usage: raw2voc rawfile vocfile speed\n"); exit(1);}
  21.    if ((infile=fopen(argv[1],"rb"))==NULL)
  22.     {
  23.      printf("Couldnt find input file, aborting.\n");
  24.      exit(1);
  25.     }
  26.    if ((outfile=fopen(argv[2],"wb"))==NULL)
  27.     {
  28.      printf("Couldnt open output file, aborting.\n");
  29.      fclose(infile);
  30.      exit(1);
  31.     }
  32.  
  33.     speed=(unsigned int) atol(argv[3]);
  34.     printf("Using speed=%u\n",speed);
  35.  
  36.     /* write out header */
  37.     fwrite(string1,1,sizeof(string1),outfile);
  38.  
  39.     /* now insert speed, length info */
  40.     /* compute length */
  41.     fseek(infile, 0L, SEEK_END);
  42.     fgetpos(infile, &length);
  43.     printf("Input length = %ld\n",length);
  44.     fseek(infile, 0L, SEEK_SET); /* rewind file */
  45.  
  46.     fputc(1, outfile);
  47.  
  48.     /* length is 3 bytes, LSB first */
  49.     length += 2;
  50.     fputc(length&0xff,outfile);
  51.     fputc((length&0xff00)>>8,outfile);
  52.     fputc((length&0xff0000)>>16,outfile);
  53.     length -= 2;
  54.  
  55.     /* speed byte (SB) is given by
  56.      *      speed < 22222 ->   SB = 256-(1000000/speed)
  57.      *      speed > 22222 ->   SB = 65536-(256000000/speed)     */
  58.      if (speed > 22222)
  59.        time_constant = 65536L - (256000000L / (long)speed);
  60.      else
  61.        time_constant = 256 - (1000000/speed);
  62.  
  63.      fputc(time_constant & 0xff, outfile);
  64.      fputc((time_constant & 0xff00)>>8, outfile);
  65.  
  66.      /* now do the rest, copy infile to outfile */
  67.      count = 0;
  68.      do
  69.      {
  70.        hunk_size = min(length-count,64000);
  71.        fread(buffer,1,hunk_size,infile);
  72.        fwrite(buffer,1,hunk_size,outfile);
  73.        count += hunk_size;
  74.      }
  75.      while( count < length );
  76.  
  77.    /* terminate file */
  78.    fputc(0,outfile);
  79.  
  80.    fclose(infile);
  81.    fclose(outfile);
  82. }
  83.