This is sndsum.c in view mode; [Download] [Up]
/*
* sndsum.c
* add one sound to another
*/
#import <stdio.h>
#import <sound/sound.h>
#import <architecture/byte_order.h>
#import <c.h>
static void fails(char *s, char *s2)
{
printf("sndsum: "); /* error message */
printf(s,s2);
printf("\n\n");
exit(1); /* error exit */
}
void main (int argc, char *argv[])
{
int err, nchans, sampleCount;
SNDSoundStruct *sndin1;
SNDSoundStruct *sndin2;
FILE *outstream;
short *data1;
short *data2;
if (argc != 4) {
fprintf(stderr,"%s - add one sound to another\n",argv[0]);
fprintf(stderr, "Usage: sndsum in1.snd in2.snd out.snd\n");
fprintf(stderr, " (out = in1 + in2)\n");
exit(1);
}
/*
* Input sound file
*/
err = SNDReadSoundfile(argv[1], &sndin1);
if (err) {
fprintf(stderr, "Cannot read soundfile: %s\n", argv[1]);
exit(1);
}
err = SNDReadSoundfile(argv[2], &sndin2);
if (err) {
fprintf(stderr, "Cannot read soundfile: %s\n", argv[2]);
exit(1);
}
nchans = sndin1->channelCount;
if (sndin1->dataFormat != SND_FORMAT_LINEAR_16
|| sndin1->dataFormat != sndin2->dataFormat) {
fprintf(stderr, "Both soundfiles must be 16 bit linear\n");
exit(1);
}
sampleCount = sndin1->dataSize / sizeof(short); /* channel independent */
sampleCount = MIN(sampleCount,sndin2->dataSize / sizeof(short));
data1 = (short *) ((char *)sndin1 + sndin1->dataLocation);
data2 = (short *) ((char *)sndin2 + sndin2->dataLocation);
{
register short *sp1 = data1;
register short *sp2 = data2;
register int i;
for (i = 0; i < sampleCount; i++) {
*sp1 = (signed short) NXSwapHostShortToBig((signed short)
NXSwapBigShortToHost(*sp1) + (signed short) NXSwapBigShortToHost(*sp2));
sp1++;
sp2++;
}
}
/*
* Output sound file
*/
if (NULL == (outstream = fopen(argv[3],"w")))
fails("could not open output file '%s'", argv[3]);
if (SNDWriteHeader(fileno(outstream), sndin1))
fails("could not write output file '%s'", argv[3]);
fwrite(data1, sizeof(short), sampleCount, outstream);
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.