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