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

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

/*
 * sndrms.c
 *	Measure rms level of a sound
 */
#import <stdio.h>
#import <math.h>		/* pow() */
#import <sound/sound.h>
#import <architecture/byte_order.h>

void main (int argc, char *argv[])
{
    int err, nchans, sampleCount;
    SNDSoundStruct *sndin;
    short *data;
    double mean,meansq,var,rms;
    double sum=0.0, sumsq=0.0;
    double max,min;
    
    if (argc != 2) {
	fprintf(stderr,"%s - measure rms level of a sound\n",argv[0]);
	fprintf(stderr, "Usage: sndrms in.snd\n");
	exit(1);
    }
    
    /*
     * Input sound file
     */
    err = SNDReadSoundfile(argv[1], &sndin);
    if (err) {
	fprintf(stderr, "Cannot read soundfile: %s\n", argv[1]);
	exit(1);
    }
    nchans = sndin->channelCount;
    if (sndin->dataFormat != SND_FORMAT_LINEAR_16) {
	fprintf(stderr, "Soundfile must be 16 bit linear\n");
	exit(1);
    }
    sampleCount = sndin->dataSize / sizeof(short); /* channel independent */
    data = (short *) ((char *)sndin + sndin->dataLocation);
    
    {
      register short *sp = data;
      register int temp;
      register int i;
      double scl = pow(2,-15);
      max = min = (signed short) NXSwapBigShortToHost(*sp);
      for (i = 0; i < sampleCount; i++) {
	  temp = (signed short) NXSwapBigShortToHost(*sp++);
	  sum += temp;
	  sumsq += temp*temp;
	  if (max < temp)
	    max = temp;
	  else if (min > temp)
	    min = temp;
      }	  
      mean = sum / ((double) sampleCount);
      meansq = sumsq / ((double) sampleCount);
      var = meansq - mean*mean;
      rms = sqrt(var);
      printf("RMS  = %.20g (%.20g)\n",rms,rms*scl);
      printf("Mean = %.20g (%.20g)\n",mean,mean*scl);
      printf("Max  = %d (limit =  32767) (%.20g) (limit =  1.0)\n",
	     (int)max,max*scl);
      printf("Min  = %d (limit = -32768) (%.20g) (limit = -1.0)\n",
	     (int)min,min*scl);
    }	  
}

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