home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Best Objectech Shareware Selections
/
UNTITLED.iso
/
boss
/
musi
/
misc
/
019
/
sox.c
< prev
next >
Wrap
Text File
|
1993-02-11
|
17KB
|
754 lines
/*
* July 5, 1991
* Copyright 1991 Lance Norskog And Sundry Contributors
* This source code is freely redistributable and may be used for
* any purpose. This copyright notice must be maintained.
* Lance Norskog And Sundry Contributors are not responsible for
* the consequences of using this software.
*/
#include "st.h"
#if defined(unix) || defined(AMIGA)
#include <sys/types.h>
#include <sys/stat.h>
#endif
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <ctype.h>
#include <string.h>
#ifdef VMS
#include <errno.h>
#include <perror.h>
#define LASTCHAR ']'
#else
#include <errno.h>
#define LASTCHAR '/'
#endif
/*
* SOX main program.
*
* Rewrite for new nicer option syntax. July 13, 1991.
* Rewrite for separate effects library. Sep. 15, 1991.
*/
#ifdef AMIGA
/* This is the Amiga version string */
char amiversion[AmiVerSize]=AmiVerChars;
#endif /* AMIGA */
EXPORT float volume = 1.0; /* expansion coefficient */
int dovolume = 0;
int clipped = 0; /* Volume change clipping errors */
EXPORT float amplitude = 1.0; /* Largest sample so far */
EXPORT int writing = 0; /* are we writing to a file? */
/* export flags */
EXPORT int verbose = 0; /* be noisy on stderr */
EXPORT int summary = 0; /* just print summary of information */
long ibuf[BUFSIZ]; /* Intermediate processing buffer */
long obuf[BUFSIZ]; /* Intermediate processing buffer */
long volumechange();
#ifdef DOS
char writebuf[BUFSIZ]; /* output write buffer */
#endif
void gettype(), geteffect(), checkeffect();
EXPORT struct soundstream informat, outformat;
char *myname;
ft_t ft;
struct effect eff;
char *ifile, *ofile, *itype, *otype;
IMPORT char *optarg;
IMPORT int optind;
main(n, args)
int n;
char **args;
{
myname = args[0];
init();
ifile = ofile = NULL;
/* Get input format options */
ft = &informat;
doopts(n, args);
/* Get input file */
if (optind >= n)
#ifndef VMS
usage("No input file?");
#else
/* I think this is the VMS patch, but am not certain */
fail("Input file required");
#endif
ifile = args[optind];
if (! strcmp(ifile, "-"))
ft->fp = stdin;
else if ((ft->fp = fopen(ifile, READBINARY)) == NULL)
fail("Can't open input file '%s': %s",
ifile, strerror(errno));
ft->filename = ifile;
optind++;
/* Let -e allow no output file, just do an effect */
if (optind < n) {
if (strcmp(args[optind], "-e")) {
/* Get output format options */
ft = &outformat;
doopts(n, args);
/* Get output file */
if (optind >= n)
usage("No output file?");
ofile = args[optind];
ft->filename = ofile;
/*
* There are two choices here:
* 1) stomp the old file - normal shell "> file" behavior
* 2) fail if the old file already exists - csh mode
*/
if (! strcmp(ofile, "-"))
ft->fp = stdout;
else {
#ifdef unix
/*
* Remove old file if it's a text file, but
* preserve Unix /dev/sound files. I'm not sure
* this needs to be here, but it's not hurting
* anything.
*/
if ((ft->fp = fopen(ofile, WRITEBINARY)) &&
(filetype(fileno(ft->fp)) == S_IFREG)) {
fclose(ft->fp);
unlink(ofile);
creat(ofile, 0666);
ft->fp = fopen(ofile, WRITEBINARY);
}
#else
ft->fp = fopen(ofile, WRITEBINARY);
#endif
if (ft->fp == NULL)
fail("Can't open output file '%s': %s",
ofile, strerror(errno));
#ifdef DOS
if (setvbuf (ft->fp,writebuf,_IOFBF,sizeof(writebuf)))
fail("Can't set write buffer");
#endif
}
writing = 1;
}
optind++;
}
/* ??? */
/*
if ((optind < n) && !writing && !eff.name)
fail("Can't do an effect without an output file!");
*/
/* Get effect name */
if (optind < n) {
eff.name = args[optind];
optind++;
geteffect(&eff);
(* eff.h->getopts)(&eff, n - optind, &args[optind]);
}
/*
* If we haven't specifically set an output file
* don't write a file; we could be doing a summary
* or a format check.
*/
/*
if (! ofile)
usage("Must be given an output file name");
*/
if (! ofile)
writing = 0;
/* Check global arguments */
if (volume <= 0.0)
fail("Volume must be greater than 0.0");
#if defined(unix) || defined(AMIGA)
informat.seekable = (filetype(fileno(informat.fp)) == S_IFREG);
outformat.seekable = (filetype(fileno(outformat.fp)) == S_IFREG);
#else
#if defined(DOS)
informat.seekable = 1;
outformat.seekable = 1;
#else
informat.seekable = 0;
outformat.seekable = 0;
#endif
#endif
/* If file types have not been set with -t, set from file names. */
if (! informat.filetype) {
if (informat.filetype = strrchr(ifile, LASTCHAR))
informat.filetype++;
else
informat.filetype = ifile;
if (informat.filetype = strrchr(informat.filetype, '.'))
informat.filetype++;
else /* Default to "auto" */
informat.filetype = "auto";
}
if (writing && ! outformat.filetype) {
if (outformat.filetype = strrchr(ofile, LASTCHAR))
outformat.filetype++;
else
outformat.filetype = ofile;
if (outformat.filetype = strrchr(outformat.filetype, '.'))
outformat.filetype++;
}
/* Default the input comment to the filename.
* The output comment will be assigned when the informat
* structure is copied to the outformat.
*/
informat.comment = informat.filename;
process();
statistics();
exit(0);
}
doopts(n, args)
int n;
char **args;
{
int c;
char *str;
while ((c = getopt(n, args, "r:v:t:c:suUAbwlfdDxSV")) != -1) {
switch(c) {
case 't':
if (! ft) usage("-t");
ft->filetype = optarg;
if (ft->filetype[0] == '.')
ft->filetype++;
break;
case 'r':
if (! ft) usage("-r");
str = optarg;
if ((! sscanf(str, "%lu", &ft->info.rate)) ||
(ft->info.rate <= 0))
fail("-r must be given a positive integer");
break;
case 'v':
if (! ft) usage("-v");
str = optarg;
if ((! sscanf(str, "%e", &volume)) ||
(volume <= 0))
fail("Volume value '%s' is not a number",
optarg);
dovolume = 1;
break;
case 'c':
if (! ft) usage("-c");
str = optarg;
if (! sscanf(str, "%d", &ft->info.channels))
fail("-c must be given a number");
break;
case 'b':
if (! ft) usage("-b");
ft->info.size = BYTE;
break;
case 'w':
if (! ft) usage("-w");
ft->info.size = WORD;
break;
case 'l':
if (! ft) usage("-l");
ft->info.size = LONG;
break;
case 'f':
if (! ft) usage("-f");
ft->info.size = FLOAT;
break;
case 'd':
if (! ft) usage("-d");
ft->info.size = DOUBLE;
break;
case 'D':
if (! ft) usage("-D");
ft->info.size = IEEE;
break;
case 's':
if (! ft) usage("-s");
ft->info.style = SIGN2;
break;
case 'u':
if (! ft) usage("-u");
ft->info.style = UNSIGNED;
break;
case 'U':
if (! ft) usage("-U");
ft->info.style = ULAW;
break;
case 'A':
if (! ft) usage("-A");
ft->info.style = ALAW;
break;
case 'x':
if (! ft) usage("-x");
ft->swap = 1;
break;
/* stat effect does this ?
case 'S':
summary = 1;
break;
*/
case 'V':
verbose = 1;
break;
}
}
}
init() {
/* init files */
informat.info.rate = outformat.info.rate = 0;
informat.info.size = outformat.info.size = -1;
informat.info.style = outformat.info.style = -1;
informat.info.channels = outformat.info.channels = -1;
informat.comment = outformat.comment = NULL;
informat.swap = 0;
informat.filetype = outformat.filetype = (char *) 0;
informat.fp = stdin;
outformat.fp = stdout;
informat.filename = "input";
outformat.filename = "output";
}
/*
* Process input file -> effect -> output file
* one buffer at a time
*/
process() {
long isamp, osamp, istart;
long i, idone, odone;
gettype(&informat);
if (writing)
gettype(&outformat);
/* Read and write starters can change their formats. */
(* informat.h->startread)(&informat);
checkformat(&informat);
report("Input file: using sample rate %u\n\tsize %s, style %s, %d %s",
informat.info.rate, sizes[informat.info.size],
styles[informat.info.style], informat.info.channels,
(informat.info.channels > 1) ? "channels" : "channel");
if (informat.comment)
report("Input file: comment \"%s\"\n",
informat.comment);
if (writing) {
copyformat(&informa