/*
 *	flip.c
 *
 *	Reads an 8 bit raw audio file and flips the sign bit.
 *
 *	6/18/96 hmk
 */
 
#include <stdio.h>


#define OUTNAME "flip.raw"

#define BUFSIZE 4096

char buf[BUFSIZE];


	void
main(argc, argv)
	int argc;
	char **argv;
{
	long	i, j, r, size;
	FILE 	*ifp, *ofp;
	

	if (argc != 2) {
		printf("Usage: %s filename\n", argv[0]);
		exit(0);
	}
	if ((ifp = fopen(argv[1], "rb")) == NULL) {
		perror(argv[1]);
		exit(-1);
	}
	if ((ofp = fopen(OUTNAME, "wb")) == NULL) {
		perror(OUTNAME);
		exit(-2);
	}
	fseek(ifp, 0, 2);
	size = ftell(ifp);
	fseek(ifp, 0, 0);
	r = size % BUFSIZE;

	for (i = 0 ; i < (size / BUFSIZE); i++) {
		if (fread(buf, BUFSIZE, 1, ifp) != 1) {
			perror(argv[1]);
			exit(-3);
		}
		for (j = 0; j < BUFSIZE; j++) {
			buf[j] = (buf[j] & 0x80) ? buf[j] & 0x7f : buf[j] | 0x80;
		}
		
		if (fwrite(buf, BUFSIZE, 1, ofp) != 1) {
			perror(OUTNAME);
			exit(-3);
		}
	}
	if (fread(buf, r, 1, ifp) != 1) {
		perror(argv[1]);
		exit(-3);
	}
	for (j = 0; j < r; j++) {
		buf[j] = (buf[j] & 0x80) ? buf[j] & 0x7f : buf[j] | 0x80;
	}
		
	if (fwrite(buf, r, 1, ofp) != 1) {
		perror(OUTNAME);
		exit(-3);
	}
	fclose(ifp);
	fclose(ofp);
	exit(0);
}	
