home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / src / raw2voc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2008-02-03  |  3.1 KB  |  133 lines

  1. /* ScummC
  2.  * Copyright (C) 2005-2006  Alban Bedel
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /**
  21.  * @file raw2voc.c
  22.  * @brief Tool to create voc files from raw PCM data
  23.  */
  24.  
  25. #include "config.h"
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <inttypes.h>
  31. #include <errno.h>
  32.  
  33.  
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <fcntl.h>
  37. #include "scc_fd.h"
  38. #include "scc_util.h"
  39.  
  40. #include "scc_param.h"
  41.  
  42. #include "raw2voc_help.h"
  43.  
  44. static int srate = 22050;
  45. static char* output = NULL;
  46.  
  47. static scc_param_t scc_parse_params[] = {
  48.   { "o", SCC_PARAM_STR, 0, 0, &output },
  49.   { "r", SCC_PARAM_INT, 0, 0xFFFF, &srate },
  50.   { "help", SCC_PARAM_HELP, 0, 0, &raw2voc_help },
  51.   { NULL, 0, 0, 0, NULL }
  52. };
  53.  
  54. int main(int argc, char** argv) {
  55.   scc_fd_t* ifd,*ofd;
  56.   scc_cl_arg_t* files;
  57.   char* ofl;
  58.   unsigned dlen = 26 + 1 + 3 + 2, dsize = 0;
  59.   char* data;
  60.   int r,blen;
  61.   
  62.   files = scc_param_parse_argv(scc_parse_params,argc-1,&argv[1]);
  63.   if(!files) scc_print_help(&raw2voc_help,1);
  64.  
  65.   ifd = new_scc_fd(files->val,O_RDONLY,0);
  66.   if(!ifd) {
  67.     printf("Failed to open input %s\n",files->val);
  68.     return 1;
  69.   }
  70.   
  71.   dsize = 1024;
  72.   data = malloc(1024);
  73.  
  74.   while(1) {
  75.     if(dsize <= dlen) {
  76.       dsize += 1024;
  77.       data = realloc(data,dsize);
  78.     }
  79.     r = scc_fd_read(ifd,data+dlen,dsize-dlen);
  80.     if(r < 0) {
  81.       printf("Error while reading input data.\n");
  82.       return 2;
  83.     } else if(r == 0) break;
  84.     dlen += r;
  85.   }
  86.  
  87.   scc_fd_close(ifd);
  88.  
  89.   if(dlen == 32) {
  90.     printf("No input data found.\n");
  91.     return 1;
  92.   }
  93.  
  94.   ofl = output ? output : "output.voc";
  95.   ofd = new_scc_fd(ofl,O_WRONLY|O_CREAT|O_TRUNC,0);
  96.   if(!ofd) {
  97.     printf("Failed to open output %s\n",ofl);
  98.     return 1;
  99.   }
  100.   
  101.   // write the voc header
  102.   memcpy(data,"Creative Voice File",19);
  103.   data[19] = 0x1A; // eof
  104.   data[20] = 0x1A; // header size
  105.   data[21] = 0;
  106.   data[22] = 0x0A; // version
  107.   data[23] = 0x01;
  108.   data[24] = 0x29; // magic number
  109.   data[25] = 0x11;
  110.  
  111.   // write the header of the first block
  112.   blen = dlen - 30;
  113.   data[26] = 0x01;                 // block type
  114.   data[27] = blen & 0xFF;          // block size
  115.   data[28] = (blen >> 8) & 0xFF;
  116.   data[29] = (blen >> 16) & 0xFF;
  117.   
  118.   data[30] = 256-(1000000/srate);  // sample rate
  119.   data[31] = 0;                    // packing
  120.  
  121.   if(scc_fd_write(ofd,data,dlen) != dlen) {
  122.     printf("Failed to write %s\n",ofl);
  123.     return 1;
  124.   }
  125.  
  126.   // write the terminator byte
  127.   scc_fd_w8(ofd,0);
  128.   
  129.   scc_fd_close(ofd);
  130.  
  131.   return 0;
  132. }
  133.