This is How-to-use-sound-functions in view mode; [Up]
Date: Sun 12-Dec-1991 11:55:38 From: arun@ac.dal.ca Subject: How to use sound functions Here is a simple little routine to less you process sound files. It is really quite fun to experiment with. It is currently set up to make a weird kind of echo. For a high pass filter try: (in the function Process at the end) for (i=1; i<numSamples; i++) { outSamples[i]=inSamples[i]-inSamples[i-1]; } For a low pass filter try: outSamples[i]=(inSamples[i]+inSamples[i+1])>>1; Have fun... -George (using friends account) respond to: george@[129.173.30.20] #include <stdio.h> #include <sound/sound.h> #include <math.h> /* Sound error handling macro */ #define soundErrorExit() {\ fprintf(stderr, "%s: sound error: %s\n", programName, \ SNDSoundError(soundError)); \ exit(1); \ } static void transform(SNDSoundStruct **outputSound, SNDSoundStruct *inputSound); static void Process(short *outSamples, short *inSamples, int numSamples); static int soundError; /* used by soundErrorExit() macro */ static char *programName; main(int argc, char *argv[]) { SNDSoundStruct *inputSound, *outputSound; char *inputSoundFile, *outputSoundFile; /* Check arguments */ programName = argv[0]; if (argc != 3) { fprintf(stderr, "Usage: %s <inputSoundFile> <outputSoundFile>\n", programName); exit(1); } inputSoundFile = argv[1]; outputSoundFile = argv[2]; /* Read sound file */ if (soundError = SNDReadSoundfile(inputSoundFile, &inputSound)) soundErrorExit(); /* Transform sound */ transform(&outputSound, inputSound); /* Write sound file */ if (soundError = SNDWriteSoundfile(outputSoundFile, outputSound)) soundErrorExit(); } /* Transform inputSound and place result in outputSound */ static void transform(SNDSoundStruct **outputSound, SNDSoundStruct *inputSound) { int size = inputSound->dataSize; int format = inputSound->dataFormat; int rate = inputSound->samplingRate; int channels = inputSound->channelCount; short *inPtr; short *outPtr; printf("size = %d format = %d rate = %d channels = %d \n", size, format, rate, channels); /* Abort if unsupported format */ if (format != SND_FORMAT_LINEAR_16) { fprintf(stderr, "%s: unsupported sound format: %d\n", programName, format); exit(1); } /* Allocate a new sound */ if (soundError = SNDAlloc(outputSound, size, format, rate, channels, 4)) soundErrorExit(); /* Process input sound an write to output sound */ inPtr = (short *) ((char *) inputSound + inputSound->dataLocation); outPtr = (short *) ((char *) *outputSound + (*outputSound)->dataLocation); Process(outPtr, inPtr, size / 2); } /* Process the sound any way you want to */ static void Process(short *outSamples, short *inSamples, int numSamples) { int i, echo, r1, r2; for (echo=1; echo<4; echo++) { r1=random()&0x7f00; r2=random()&0x7f00; for (i=0x7f00; i<numSamples; i+=2) { inSamples[i] = (inSamples[i]+inSamples[i-r1]>>1)*.75; inSamples[i+1] = (inSamples[i+1]-inSamples[i+1-r2]>>1)*.75; } } for (i=0; i<numSamples; i++) { outSamples[i]=inSamples[i]; } }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.