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

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

/*
 * atcenv.c
 *	Write out amplitude envelope of an ATC-compressed soundfile.
 */
#import <libc.h>
#import <stdio.h>
#import <math.h>
#import <sound/sound.h>

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

void main (int argc, char *argv[])
{
  int j;
  int err, nchans, inDataBytes;
  SNDSoundStruct *sndin;
  SNDCompressionSubheader *subheader = NULL;
  SNDSoundStruct *envsoundout;
  unsigned char *sp,*data;
  short *ep,*edata;
  int nsamps,nsampsPH;
  int frame_size;
  short amp,frame_exp;
  int frameNumber;

  if (argc != 3 && argc != 2) {
    fprintf(stderr,
	    "%s - write out amplitude envelope of an ATC-compressed soundfile\n",
	    argv[0]);
    fprintf(stderr, "Usage: %s atc_format_in.snd [envelope_out.snd]\n\n",argv[0]);
    exit(1);
  }

  /*
   * Input sound file
   */
  err = SNDReadSoundfile(argv[1], &sndin);
  if (err)
    fails("Cannot read soundfile: %s", argv[1]);

  nchans = sndin->channelCount;
  if (sndin->dataFormat != SND_FORMAT_COMPRESSED)
    fails("Input soundfile must be ATC format",NULL);
  if (sndin->dataSize <= sizeof(SNDCompressionSubheader))
    fails("Compressed soundfile is bad",NULL);
  subheader = (SNDCompressionSubheader *) 
    ((char *)sndin + sndin->dataLocation);
  if(subheader->method != SND_CFORMAT_ATC)
    fails("Input soundfile must be ATC format",NULL);

  nsamps = subheader->originalSize / sizeof(short);
  nsampsPH = nsamps + sndin->dataLocation;
  data = (unsigned char *)subheader + sizeof(SNDCompressionSubheader);
  inDataBytes = sndin->dataSize - sizeof(SNDCompressionSubheader);

  /* This is probably a no-op in all current cases but provides some
   * hope of working on future architectures.
   */
  SNDSwapSoundToHost(data, data, inDataBytes / sizeof(short) * nchans,
		     nchans, sndin->dataFormat);

  envsoundout = (SNDSoundStruct *)malloc(subheader->originalSize
					 + sndin->dataLocation);

  sndin->dataSize = subheader->originalSize;
  sndin->dataFormat = SND_FORMAT_LINEAR_16;
  sp = data;
  ep = edata = (short *)envsoundout; /* zeroed header was compressed */
  frameNumber = 0;
    
  while (++frameNumber) {
    if ( (sp - data) >= inDataBytes)
      break;
    frame_size = *sp;
    frame_exp = *(sp+1);
    sp += frame_size;
    amp = (32767 >> frame_exp);
    fprintf(stderr,"Frame %d amp = %d,\tlength = %d\n",
	    1 + (frameNumber-1)/nchans, amp, frame_size);
    if ((ep + ATC_FRAME_SIZE -edata) > nsampsPH) {
      fprintf(stderr,"Soundout overflow at sp-data = %d "
	      "and inDataBytes = %d\n",
	      (int)(sp-data),inDataBytes);
      fprintf(stderr,"Have ep-envsoundout = %d and nsampsPH = %d\n",
	      (int)(ep - edata),nsampsPH);
      break;
    }
    for (j=0; j<ATC_FRAME_SIZE; j++)
      *ep++ = amp;
    if ((ep - edata) >= nsampsPH) 
      break;
  }	  

  /* copy header and info, overwriting compressed zeroed header */
  bcopy((char *) sndin, (char *) envsoundout, sndin->dataLocation);

  /*
   * Output sound file
   */
  if (argc == 3) {
    printf("Writing output file %s\n", argv[2]);
    ep = (short *) ((char *) envsoundout + envsoundout->dataLocation);
    nsamps = envsoundout->dataSize /
      (sizeof(short) * envsoundout->channelCount);
    SNDSwapHostToSound(ep, ep, nsamps, nchans, envsoundout->dataFormat);
    if (SNDWriteSoundfile(argv[2], envsoundout))
      fails("could not write output file '%s'",argv[2]);
  }
  exit(0);
}

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