ftp.nice.ch/pub/next/unix/audio/sndutil.1.3.s.tar.gz#/sndutil-1.3/sndtools/snddiff.c

This is snddiff.c in view mode; [Download] [Up]

/*
 *	This program reads in a soundfile, applies a first-order difference
 *	operator 1 - z^(-1) and writes a new soundfile.
 *	Currently supports only SND_FORMAT_LINEAR_16, mono or stereo.
 *
 *	Usage:  snddiff <inputSoundFile> <outputSoundFile>
 *
 *	The algorithm:
 *          y(n) = x(n) - x(n-1)
 */

#import <stdio.h>
#import <sound/sound.h>

static void fail(char *s, char *s2)
{
    printf("*** %s%s\n\n",s,s2);
    exit(1);                        /* Exit, indicating error */
}

int processSound(int inCount, short *inPtr, short *outPtr, int nChans, 
		 double adaption_time)
{
    int i,c;
    double xm1,x,y;

    if (nChans == 1) {
        x = (double) inPtr[0];
	for (i=1; i<inCount; i++) {
	    xm1 = x;
	    x = (double) inPtr[i];
	    y =  x  -  xm1;			/* Zero at DC */
	    outPtr[i] = (short) y;
        }
    } else {
	for (c=0; c<nChans; c++)  {
	    x = (double) inPtr[c];
	    for (i=1; i<inCount; i++) {
		xm1 = x;
		x = (double) inPtr[2*i+c];
		y =  x  -  xm1;			/* Zero at DC */
		outPtr[nChans*i+c] = (short) y;
	    }
	}
    }
    return inCount;
}

void main(int argc,char **argv) {
    SNDSoundStruct *sndin, *sndout;
    short *inPtr, *outPtr;
    FILE *outstream;
    int inCount, outCount, width, nChans;
    double adaption_time = 250;
    
    if (argc != 3) {
	fprintf(stderr,
		"%s - output signal y(n) = x(n) - x(n-1), where x = input signal.\n",
		argv[0]);
	printf("Usage: %s in.snd out.snd\n", argv[0]);
	printf("SEE ALSO: sndsub for subtracting one signal from another.\n");
	exit(1);
    }
    
    while (*argv[1] == '-') {
	if (*(argv[1]+1) == 't') {
	    argv++;
	    sscanf(argv[1],"%lf",&adaption_time);
	    printf("DC filter adaption time set to %f\n",adaption_time);
	}
	argv++;
	argc -= 2;
    }
    
    if (SNDReadSoundfile(*++argv,&sndin))
      fail("could not open input file ",*argv);
    else
      SNDGetDataPointer(sndin, (char **)(&inPtr), &inCount, &width);
    if (width != 2)
      fail("Can only handle 16-bit soundfiles","");
    nChans = sndin->channelCount;
    inCount /= nChans;		/* to get sample frames */
    SNDSwapSoundToHost(inPtr, inPtr, inCount, nChans, sndin->dataFormat);
    
    SNDAlloc(&sndout,
	     sndin->dataSize,
	     SND_FORMAT_LINEAR_16,
	     sndin->samplingRate,
	     nChans,4);

    SNDGetDataPointer(sndout, (char **)(&outPtr), &outCount, &width);
    outCount /= nChans;		/* to get sample frames */
    
    if (NULL == (outstream = fopen(*++argv,"w")))
      fail("could not open output file ",*argv);
    if (SNDWriteHeader(fileno(outstream),sndout))
      fail("could not write output file ",*argv);
    
    outCount = processSound(inCount, inPtr, outPtr, nChans, adaption_time);
    
    SNDSwapHostToSound(outPtr, outPtr, outCount, nChans, sndout->dataFormat);
    fwrite(outPtr, 2 * nChans, outCount, outstream);
    
    SNDFree(sndin);
    fclose(outstream);
    SNDFree(sndout);
    
    exit(0);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.