This is sndtostereo.c in view mode; [Download] [Up]
/*
* sndtostereo.c
* Convert mono to stereo
*/
#import <libc.h>
#import <sound/sound.h>
static void fails(char *s, char *s2)
{
printf("sndtostereo: "); /* error message */
printf(s,s2);
printf("\n\n");
exit(1); /* error exit */
}
void check_sndin_errors(int err, SNDSoundStruct *sndin, char *name)
{
int nchans;
if (err) {
fprintf(stderr, "Cannot read soundfile: %s\n", name);
exit(1);
}
nchans = sndin->channelCount;
if (sndin->dataFormat != SND_FORMAT_LINEAR_16) {
fprintf(stderr, "Soundfile %s must be 16 bit linear\n",name);
exit(1);
}
if (sndin->channelCount != 1) {
fprintf(stderr, "Soundfile %s must be mono!\n", name);
exit(1);
}
}
void main (int argc, char *argv[])
{
int err, sampleCount, width, sampleCount2, width2;
SNDSoundStruct *sndin;
SNDSoundStruct *sndin2;
SNDSoundStruct *sndout;
short *indata, *indata2, *outdata;
int startsamp=0;
int inchans = 1;
char *outname;
if (argc < 3) {
fprintf(stderr,"%s - convert mono to stereo\n",argv[0]);
fprintf(stderr, "Usage: sndtostereo monosoundfile [rightchannelsoundfile] stereosoundfile\n");
exit(1);
}
/*
* Input sound file(s)
*/
err = SNDReadSoundfile(argv[1], &sndin);
check_sndin_errors(err,sndin,argv[1]);
if (argc > 3) {
inchans = 2;
err = SNDReadSoundfile(argv[2], &sndin2);
check_sndin_errors(err,sndin2,argv[2]);
outname = argv[3];
} else
outname = argv[2];
SNDGetDataPointer(sndin, (char **)(&indata), &sampleCount, &width);
if (inchans == 2) {
SNDGetDataPointer(sndin2, (char **)(&indata2), &sampleCount2, &width2);
if (width != width2) {
fprintf(stderr, "Input soundfiles must be the same format!\n");
exit(1);
}
if (sampleCount != sampleCount2) {
fprintf(stderr,
"Warning: Input soundfiles are different lengths\n");
sampleCount = MIN(sampleCount,sampleCount2);
}
}
SNDAlloc(&sndout,
sampleCount * sizeof(short) * 2 /* for stereo */,
sndin->dataFormat,
sndin->samplingRate,
2,
strlen(sndin->info) + 1);
strcpy(sndout->info,sndin->info);
SNDGetDataPointer(sndout, (char **)(&outdata), &sampleCount2, &width2);
if (inchans == 2) {
for (startsamp = 0; startsamp < sampleCount; startsamp++) {
*outdata++ = *indata++;
*outdata++ = *indata2++;
}
} else {
for (startsamp = 0; startsamp < sampleCount; startsamp++) {
*outdata++ = *indata;
*outdata++ = *indata++;
}
}
if (SNDWriteSoundfile(outname, sndout))
fails("could not write output file '%s'",argv[2]);
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.