This is resample.c in view mode; [Download] [Up]
/*
* FILE: resample.c
* Sampling Rate Conversion Program
*/
static char resampleVersion[]
= "\n\tresample version 1.0 (Oct 31, 1994 - jos@ccrma.stanford.edu)\n\n";
#define USAGE "\
\n\
USAGE: \n\
\n\
resample [-by factor] [-to srate] [-aQuality] [-noFilterInterp] [-linearSignalInterp] [-f filterFile] [-terse] [-version] in.snd out.snd\n\
\n\
One of the options '-by' or '-to' must be present.\n\n\
"
#import "filterkit.h"
#import "resample.h"
#import <sound/sound.h>
#import <libc.h>
//#import <stdio.h>
//#import <math.h>
//#import <string.h>
//#import <stdlib.h>
static int trace = 1; /* for verbose output */
static void fail(char *s)
{
fprintf(stderr,"\n*** resample: %s \n",s); /* Display error message */
fprintf(stderr,USAGE);
exit(1); /* Exit, indicating error */
}
static void fails(char *s, char *s2)
{
printf("resample: "); /* Display error message */
printf(s,s2);
printf("\n\n");
exit(1); /* Exit, indicating error */
}
/* Global file pointers: */
SNDSoundStruct *sndin, *sndout;
HWORD *inPtr, *outPtr;
FILE *outstream;
int inCount, outCount, outCountReal;
int main(int argc, char *argv[])
{
double factor = -2.0; /* factor = Sndout/Sndin */
double newsrate=0;
BOOL interpFilt = TRUE; /* TRUE means interpolate filter coeffs */
BOOL largeFilter = FALSE; /* TRUE means use 65-tap FIR filter */
BOOL linearInterp = FALSE; /* TRUE => no filter, linearly interpolate */
BOOL knowFactor = FALSE; /* Used to detect insufficient command-line spec */
int nChans,outCount;
int width;
char filterFile[512];
while (--argc && **(++argv)=='-') {
++(argv[0]); /* skip over '-' */
switch (*argv[0]) {
case 'v': /* -version */
printf(resampleVersion);
if (argc == 1)
exit(0);
break;
case 'a': /* -aaa */
largeFilter = TRUE;
if (trace)
printf("Choosing higher quality filter.\n");
break;
case 'b': /* -by factor */
if (--argc)
sscanf(*(++argv),"%lf",&factor);
if (trace)
printf("Sampling-rate conversion factor set to %f.\n",factor);
knowFactor = TRUE;
break;
case 't':
if (*(argv[0]+1) == 'e') { /* -terse */
trace = 0;
break;
}
if (--argc) /* -to srate */
sscanf(*(++argv),"%lf",&newsrate);
if (trace)
printf("Target sampling-rate set to %f.\n",newsrate);
knowFactor = TRUE;
break;
case 'n': /* -noInterpolation */
interpFilt = FALSE;
if (trace)
printf("Filter-table interpolation disabled.\n");
break;
case 'f': { /* -filter filterFile */
char *c = *++argv;
strcpy(filterFile,c);
if (trace)
printf("Filter file set to %s.\n",filterFile);
break; }
case 'l': /* -linearInpterpolation */
linearInterp = TRUE;
if (trace)
printf("Using linear instead of bandlimited interpolation\n");
break;
default:
fprintf(stderr,"Unknown switch -%s\n",argv[0]);
fprintf(stderr,USAGE);
exit(1);
}
}
/* if (argc < 2)
fail("Need to specify input and output soundfiles"); */
if (!knowFactor)
fail("Must specify sampling-rate conversion factor via '-to' or '-by' option");
if (SNDReadSoundfile(*argv,&sndin))
fails("could not open input file '%s'",*argv);
else
SNDGetDataPointer(sndin, (char **)(&inPtr), &inCount, &width);
nChans = sndin->channelCount;
inCount /= nChans; /* to get sample frames */
if (newsrate>0) {
if (factor>0)
fprintf(stderr,"Command-line sampling-rate conversion-factor ignored ..."
" the '-to' option overrides the '-by' option.\n");
factor = newsrate/((double)sndin->samplingRate);
if (trace)
printf("Sampling rate conversion factor set to %f\n",factor);
}
if (factor < 0) {
factor = -factor;
factor = GetDouble("Sampling-rate conversion factor",factor,"");
}
SNDAlloc(&sndout,
(int)(factor * (double)sndin->dataSize) + 1,
SND_FORMAT_LINEAR_16,
(int)(sndin->samplingRate * factor + 0.5),
nChans,strlen(sndin->info)+1);
strcpy(sndout->info,sndin->info);
SNDGetDataPointer(sndout, (char **)(&outPtr), &outCount, &width);
outCount /= nChans; /* to get sample frames */
if (NULL == (outstream = fopen(*++argv,"w")))
fails("could not open output file '%s'",*argv);
if (SNDWriteHeader(fileno(outstream),sndout))
fails("could not write output file '%s'",*argv);
printf("\nStarting Conversion...\n");
outCountReal = resample(factor, inPtr, outPtr, inCount, outCount, nChans,
interpFilt, linearInterp, largeFilter, filterFile);
if (outCountReal <= 0)
fail("Conversion factor out of range");
if (trace && (outCount != outCountReal))
fprintf(stderr,
"outCount = %d, outCountReal = %d\n",outCount,outCountReal);
SNDSwapHostToSound(outPtr, outPtr, outCountReal, nChans, SND_FORMAT_LINEAR_16);
fwrite(outPtr,2*nChans,outCountReal,outstream);
SNDFree(sndin);
fclose(outstream);
SNDFree(sndout);
printf("...Conversion Finished: %d output samples.\n\n",outCount);
exit(0);
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.