#include <stdlib.h>
// #include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#include "waveio.h"

/**********  REVERSE a SOUNDFILE   */

int main(int argc,char *argv[])
{
    FILE *fileIn, *fileOut;
    short *x_array,data;
    long i,x_length;
    struct soundhdr hdr;
    
    if (argc!=3) {
        fprintf(stderr,"usage: reverse filein.wav fileout.wav\n");
        exit(0);
    }
    printf("Reversing %s to output file %s\n",argv[1],argv[2]);

    fileIn = opensoundin(argv[1],&hdr);
    fileOut = opensoundout(argv[2],&hdr);
    if (fileIn && fileOut)      {
        x_length = hdr.dlength/2;
        x_array = (short *) malloc(2*x_length);
        if (x_array)	{
	    fread(x_array,2,x_length,fileIn);
	    for (i=x_length-1;i>=0;i--)  {
		data = x_array[i];
		fwrite(&data,2,1,fileOut);
	    }
            while(fread(&data,1,1,fileIn))         //  Grumble, RIFF tail
                fwrite(&data,1,1,fileOut);
	}
	else {
	    printf("Not Enough Memory!!\n");
	}
    }
    else {
       printf("File Problems\n");
    }
    free(x_array);
    fclose(fileIn);
    fclose(fileOut);

    return 1;
}

