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

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

/* Copyright 1988-1992, NeXT Inc.  All rights reserved. */
/*
 * sndnoise.c - create uniform noise at specified amplitude and duration.
 */

#import <stdio.h>
#import <libc.h>
#import <math.h>		/* pow() */
#import <sound/sound.h>
#import <architecture/byte_order.h>

#define SRATE 44100

static void fails(char *s, char *s2)
{
    fprintf(stderr, "sndnoise: ");	/* error message  */
    fprintf(stderr, s,s2);
    fprintf(stderr, "\n\n");
    exit(1);			/* error exit */
}

void main (int argc, char *argv[])
{
    int i,err, nchans, sampleCount;
    SNDSoundStruct *sound;
    FILE *outstream;
    short *data;
    double amp,duration;
    short samp;

    if (argc != 4) {
	fprintf(stderr,
		"%s - create uniform noise at specified amplitude and duration.\n",
		argv[0]);
	fprintf(stderr, "Usage: %s amp(<1.0) duration(sec) out.snd\n",argv[0]);
	exit(1);
    }

/*
 * Noise factor
 */
    sscanf(argv[1],"%lf",&amp);
    sscanf(argv[2],"%lf",&duration);

/*
 * Input sound file
 */
    sampleCount = duration * SRATE;

    err = SNDAlloc(&sound,sampleCount * sizeof(short),
		   SND_FORMAT_LINEAR_16,SRATE,1,4);

    data = (short *) ((char *)sound + sound->dataLocation);
    srandom(12345);
    for (i = 0; i < sampleCount; i++) {
	samp = (short) random();
	if (amp <= 1.0)
	  samp *= amp;
	*data++ = NXSwapHostShortToBig(samp);
    }	  

/*
 * Output sound file
 */
#if 1
    SNDWriteSoundfile(argv[3],sound);
#else
    /* Lower level method, useful if sound created from a single buffer */
    if (NULL == (outstream = fopen(argv[3],"w")))
      fails("could not open output file '%s'",argv[3]);
    printf("Writing output file %s\n",argv[3]);
    if (SNDWriteHeader(fileno(outstream),sound))
      fails("could not write output file '%s'",argv[3]);
    fwrite(data,2,sampleCount,outstream);
#endif

}

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