sampleio.c 1005 Bytes
#include "vadpcm.h"

#define MAXSIZE 2048

static void
fmtchan(int size, char *p, int *data, int stride)
{
	int i, c;

	for(i=0; i<size; i++) {
		c = *data++;
		if(c < -0x7fff)
		  c = -0x7fff;
		if(c > 0x7fff)
		  c = 0x7fff;
		p[0] = c>>8;
		p[1] = c;

		p += stride;
	}
}

void 
writeout(FILE *outfd, int size, int *l_out, int *r_out, int chans)
{
  static char obuf[MAXSIZE<<1];
  int i;

  switch (chans){
  case 2:
    fmtchan(size, obuf, l_out, 2*chans);
    fmtchan(size, obuf+2, r_out, 2*chans);
    if (outfd!=NULL){
      if ((i=fwrite(obuf, 1, 4*size, outfd)) != 4*size) {
	fprintf(stderr, "write error %d\n",i);
	exit(1); 
      } 
    }
    break;
  case 1: /* For mono only use the left channel buffer */
    fmtchan(size, obuf, l_out, 2);
    if (outfd!=NULL){
      if ((i=fwrite(obuf, 1, 2*size, outfd)) != 2*size) {
	fprintf(stderr, "write error %d\n",i);
	exit(1); 
      } 
    }
    break;
  default:
    fprintf(stderr,"Error in number of channels\n");
    exit(1);
  }
}