home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / musi / misc / 019 / sox.c < prev    next >
Text File  |  1993-02-11  |  17KB  |  754 lines

  1. /*
  2.  * July 5, 1991
  3.  * Copyright 1991 Lance Norskog And Sundry Contributors
  4.  * This source code is freely redistributable and may be used for
  5.  * any purpose.  This copyright notice must be maintained. 
  6.  * Lance Norskog And Sundry Contributors are not responsible for 
  7.  * the consequences of using this software.
  8.  */
  9.  
  10. #include "st.h"
  11. #if    defined(unix) || defined(AMIGA)
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #endif
  15. #ifdef    __STDC__
  16. #include <stdarg.h>
  17. #else
  18. #include <varargs.h>
  19. #endif
  20. #include <ctype.h>
  21. #include <string.h>
  22. #ifdef VMS
  23. #include <errno.h>
  24. #include <perror.h>
  25. #define LASTCHAR        ']'
  26. #else
  27. #include <errno.h>
  28. #define LASTCHAR        '/'
  29. #endif
  30.  
  31. /*
  32.  * SOX main program.
  33.  *
  34.  * Rewrite for new nicer option syntax.  July 13, 1991.
  35.  * Rewrite for separate effects library.  Sep. 15, 1991.
  36.  */
  37.  
  38. #ifdef AMIGA
  39. /* This is the Amiga version string */
  40. char amiversion[AmiVerSize]=AmiVerChars;      
  41. #endif /* AMIGA */
  42.  
  43.  
  44. EXPORT float volume = 1.0;    /* expansion coefficient */
  45. int dovolume = 0;
  46. int clipped = 0;    /* Volume change clipping errors */
  47.  
  48. EXPORT float amplitude = 1.0;    /* Largest sample so far */
  49.  
  50. EXPORT int writing = 0;    /* are we writing to a file? */
  51.  
  52. /* export flags */
  53. EXPORT int verbose = 0;    /* be noisy on stderr */
  54. EXPORT int summary = 0;    /* just print summary of information */
  55.  
  56. long ibuf[BUFSIZ];    /* Intermediate processing buffer */
  57. long obuf[BUFSIZ];    /* Intermediate processing buffer */
  58.  
  59.  
  60. long volumechange();
  61.  
  62. #ifdef    DOS
  63. char writebuf[BUFSIZ];    /* output write buffer */
  64. #endif
  65.  
  66. void    gettype(), geteffect(), checkeffect();
  67.  
  68. EXPORT struct soundstream informat, outformat;
  69.  
  70. char *myname;
  71.  
  72. ft_t ft;
  73. struct effect eff;
  74. char *ifile, *ofile, *itype, *otype;
  75. IMPORT char *optarg;
  76. IMPORT int optind;
  77.  
  78. main(n, args)
  79. int n;
  80. char **args;
  81. {
  82.     myname = args[0];
  83.     init();
  84.     
  85.     ifile = ofile = NULL;
  86.  
  87.     /* Get input format options */
  88.     ft = &informat;
  89.     doopts(n, args);
  90.     /* Get input file */
  91.     if (optind >= n)
  92. #ifndef    VMS
  93.         usage("No input file?");
  94. #else
  95.         /* I think this is the VMS patch, but am not certain */
  96.         fail("Input file required");    
  97. #endif
  98.     ifile = args[optind];
  99.     if (! strcmp(ifile, "-"))
  100.         ft->fp = stdin;
  101.     else if ((ft->fp = fopen(ifile, READBINARY)) == NULL)
  102.         fail("Can't open input file '%s': %s", 
  103.             ifile, strerror(errno));
  104.     ft->filename = ifile;
  105.     optind++;
  106.  
  107.     /* Let -e allow no output file, just do an effect */
  108.     if (optind < n) {
  109.         if (strcmp(args[optind], "-e")) {
  110.         /* Get output format options */
  111.         ft = &outformat;
  112.         doopts(n, args);
  113.         /* Get output file */
  114.         if (optind >= n)
  115.             usage("No output file?");
  116.         ofile = args[optind];
  117.         ft->filename = ofile;
  118.         /*
  119.          * There are two choices here:
  120.          *    1) stomp the old file - normal shell "> file" behavior
  121.          *    2) fail if the old file already exists - csh mode
  122.          */
  123.         if (! strcmp(ofile, "-"))
  124.             ft->fp = stdout;
  125.         else {
  126. #ifdef    unix
  127.              /*    
  128.              * Remove old file if it's a text file, but 
  129.               * preserve Unix /dev/sound files.  I'm not sure
  130.              * this needs to be here, but it's not hurting
  131.              * anything.
  132.              */
  133.             if ((ft->fp = fopen(ofile, WRITEBINARY)) && 
  134.                    (filetype(fileno(ft->fp)) == S_IFREG)) {
  135.                 fclose(ft->fp);
  136.                 unlink(ofile);
  137.                 creat(ofile, 0666);
  138.                 ft->fp = fopen(ofile, WRITEBINARY);
  139.             }
  140. #else
  141.             ft->fp = fopen(ofile, WRITEBINARY);
  142. #endif
  143.             if (ft->fp == NULL)
  144.                 fail("Can't open output file '%s': %s", 
  145.                     ofile, strerror(errno));
  146. #ifdef    DOS
  147.             if (setvbuf (ft->fp,writebuf,_IOFBF,sizeof(writebuf)))
  148.                 fail("Can't set write buffer");
  149. #endif
  150.         }
  151.         writing = 1;
  152.         }
  153.         optind++;
  154.     }
  155.  
  156.     /* ??? */
  157. /*
  158.     if ((optind < n) && !writing && !eff.name)
  159.         fail("Can't do an effect without an output file!");
  160. */
  161.  
  162.     /* Get effect name */
  163.     if (optind < n) {
  164.         eff.name = args[optind];
  165.         optind++;
  166.         geteffect(&eff);
  167.         (* eff.h->getopts)(&eff, n - optind, &args[optind]);
  168.     }
  169.  
  170.     /* 
  171.      * If we haven't specifically set an output file 
  172.      * don't write a file; we could be doing a summary
  173.      * or a format check.
  174.      */
  175. /*
  176.     if (! ofile)
  177.         usage("Must be given an output file name");
  178. */
  179.     if (! ofile)
  180.         writing = 0;
  181.     /* Check global arguments */
  182.     if (volume <= 0.0)
  183.         fail("Volume must be greater than 0.0");
  184. #if    defined(unix) || defined(AMIGA)
  185.     informat.seekable  = (filetype(fileno(informat.fp)) == S_IFREG);
  186.     outformat.seekable = (filetype(fileno(outformat.fp)) == S_IFREG); 
  187. #else
  188. #if    defined(DOS)
  189.     informat.seekable  = 1;
  190.     outformat.seekable = 1;
  191. #else
  192.     informat.seekable  = 0;
  193.     outformat.seekable = 0;
  194. #endif
  195. #endif
  196.  
  197.     /* If file types have not been set with -t, set from file names. */
  198.     if (! informat.filetype) {
  199.         if (informat.filetype = strrchr(ifile, LASTCHAR))
  200.             informat.filetype++;
  201.         else
  202.             informat.filetype = ifile;
  203.         if (informat.filetype = strrchr(informat.filetype, '.'))
  204.             informat.filetype++;
  205.         else /* Default to "auto" */
  206.             informat.filetype = "auto";
  207.     }
  208.     if (writing && ! outformat.filetype) {
  209.         if (outformat.filetype = strrchr(ofile, LASTCHAR))
  210.             outformat.filetype++;
  211.         else
  212.             outformat.filetype = ofile;
  213.         if (outformat.filetype = strrchr(outformat.filetype, '.'))
  214.             outformat.filetype++;
  215.     }
  216.     /* Default the input comment to the filename. 
  217.      * The output comment will be assigned when the informat 
  218.      * structure is copied to the outformat. 
  219.      */
  220.     informat.comment = informat.filename;
  221.  
  222.     process();
  223.     statistics();
  224.     exit(0);
  225. }
  226.  
  227. doopts(n, args)
  228. int n;
  229. char **args;
  230. {
  231.     int c;
  232.     char *str;
  233.  
  234.     while ((c = getopt(n, args, "r:v:t:c:suUAbwlfdDxSV")) != -1) {
  235.         switch(c) {
  236.         case 't':
  237.             if (! ft) usage("-t");
  238.             ft->filetype = optarg;
  239.             if (ft->filetype[0] == '.')
  240.                 ft->filetype++;
  241.             break;
  242.  
  243.         case 'r':
  244.             if (! ft) usage("-r");
  245.             str = optarg;
  246.             if ((! sscanf(str, "%lu", &ft->info.rate)) ||
  247.                     (ft->info.rate <= 0))
  248.                 fail("-r must be given a positive integer");
  249.             break;
  250.         case 'v':
  251.             if (! ft) usage("-v");
  252.             str = optarg;
  253.             if ((! sscanf(str, "%e", &volume)) ||
  254.                     (volume <= 0))
  255.                 fail("Volume value '%s' is not a number",
  256.                     optarg);
  257.             dovolume = 1;
  258.             break;
  259.  
  260.         case 'c':
  261.             if (! ft) usage("-c");
  262.             str = optarg;
  263.             if (! sscanf(str, "%d", &ft->info.channels))
  264.                 fail("-c must be given a number");
  265.             break;
  266.         case 'b':
  267.             if (! ft) usage("-b");
  268.             ft->info.size = BYTE;
  269.             break;
  270.         case 'w':
  271.             if (! ft) usage("-w");
  272.             ft->info.size = WORD;
  273.             break;
  274.         case 'l':
  275.             if (! ft) usage("-l");
  276.             ft->info.size = LONG;
  277.             break;
  278.         case 'f':
  279.             if (! ft) usage("-f");
  280.             ft->info.size = FLOAT;
  281.             break;
  282.         case 'd':
  283.             if (! ft) usage("-d");
  284.             ft->info.size = DOUBLE;
  285.             break;
  286.         case 'D':
  287.             if (! ft) usage("-D");
  288.             ft->info.size = IEEE;
  289.             break;
  290.  
  291.         case 's':
  292.             if (! ft) usage("-s");
  293.             ft->info.style = SIGN2;
  294.             break;
  295.         case 'u':
  296.             if (! ft) usage("-u");
  297.             ft->info.style = UNSIGNED;
  298.             break;
  299.         case 'U':
  300.             if (! ft) usage("-U");
  301.             ft->info.style = ULAW;
  302.             break;
  303.         case 'A':
  304.             if (! ft) usage("-A");
  305.             ft->info.style = ALAW;
  306.             break;
  307.         
  308.         case 'x':
  309.             if (! ft) usage("-x");
  310.             ft->swap = 1;
  311.             break;
  312.         
  313. /*  stat effect does this ?
  314.         case 'S':
  315.             summary = 1;
  316.             break;
  317. */
  318.         case 'V':
  319.             verbose = 1;
  320.             break;
  321.         }
  322.     }
  323. }
  324.  
  325. init() {
  326.  
  327.     /* init files */
  328.     informat.info.rate      = outformat.info.rate  = 0;
  329.     informat.info.size      = outformat.info.size  = -1;
  330.     informat.info.style     = outformat.info.style = -1;
  331.     informat.info.channels  = outformat.info.channels = -1;
  332.     informat.comment   = outformat.comment = NULL;
  333.     informat.swap      = 0;
  334.     informat.filetype  = outformat.filetype  = (char *) 0;
  335.     informat.fp        = stdin;
  336.     outformat.fp       = stdout;
  337.     informat.filename  = "input";
  338.     outformat.filename = "output";
  339. }
  340.  
  341. /* 
  342.  * Process input file -> effect -> output file
  343.  *    one buffer at a time
  344.  */
  345.  
  346. process() {
  347.     long isamp, osamp, istart;
  348.     long i, idone, odone;
  349.  
  350.     gettype(&informat);
  351.     if (writing)
  352.         gettype(&outformat);
  353.     /* Read and write starters can change their formats. */
  354.     (* informat.h->startread)(&informat);
  355.     checkformat(&informat);
  356.     report("Input file: using sample rate %u\n\tsize %s, style %s, %d %s",
  357.         informat.info.rate, sizes[informat.info.size], 
  358.         styles[informat.info.style], informat.info.channels, 
  359.         (informat.info.channels > 1) ? "channels" : "channel");
  360.     if (informat.comment)
  361.         report("Input file: comment \"%s\"\n",
  362.             informat.comment);
  363.     if (writing) {
  364.         copyformat(&informa