#include<stdio.h>
#include "io.h"

/* This program removes the RIFF headers from a WAV and extracts
the samples. It's useful for producing raw data that can then be analysed. */


int is_RIFF_header(FILE *input_file) {
	char Buffer[5];
	char data[256];
	int i;

	for (i=0; i<4; i++) {

		Buffer[i]=getc(input_file);
	}
	Buffer[4]='\0';
	//rewind(input_file);

	if(!strcmp("RIFF",Buffer)) {

		printf("Buffer is %s and we wanted RIFF!\n",Buffer);
	} else {
		printf("Buffer is %s and we wanted RIFF...\n",Buffer);
	}
	return 1;
}

int get_wave_string(FILE *input_file) {
	char Buffer[5];
	char data[256];
	int i;
	for(i=0; i<4; i++) {
		Buffer[i]=getc(input_file);
	}
	Buffer[4]='\0';
	if(!strcmp("WAVE",Buffer)) {
		printf(" Buffer is %s and we wanted WAVE!\n",Buffer);
	} else {
		printf("Buffer is %s and we wanted WAVE...\n",Buffer);
	}
	return 1;
}

int get_fmt_string(FILE *input_file) {
	char Buffer[5];
	int ans;
	char data[256];
	int i;
	for(i=0; i<4; i++) {
		Buffer[i]=getc(input_file);
	}
	Buffer[4]='\0';
	if(!strcmp("fmt ",Buffer)) {
		printf("Buffer is %s and we wanted fmt \n",Buffer);
		ans=0;
	} else {
		printf("Buffer is %s and we wanted fmt...\n",Buffer);
		ans=1;
	}
	return ans;
}

int main(int argc,char* argv[]){

FILE *inputfile;
FILE *outputfile;

inputfile = fopen(argv[1],"rb");
if (inputfile)
{
    char id[4]; //four bytes to hold 'RIFF'
    unsigned long int size; //32 bit value to hold file size
    unsigned long int format_length;
    unsigned short channel_number;
    unsigned long sample_rate;    
    unsigned long bytes_per_sec;
    unsigned short bytes_per_sample;
    unsigned short bits_per_sample;
    unsigned long length_of_data;
    unsigned short tmp,i,current_position;
    unsigned short filelength;

    fseek(inputfile,0,SEEK_END);
    filelength=ftell(inputfile);
    rewind(inputfile);
    is_RIFF_header(inputfile);
     //we had 'RIFF' let's continue

        //read in 32bit size value
	// Okay, so we're junking this value for the moment, but it hardly seems useful.
     getc(inputfile);
     getc(inputfile);
     getc(inputfile);
     getc(inputfile); 
    // a bonafide wav file would have a string saying WAVE at this point. let's check. 
    get_wave_string(inputfile);
    // Now check the next 4 byte string (should be 'fmt ', including the space.
    if(!get_fmt_string(inputfile)) {

		printf("File is a wav; proceeding to check format\n");
		format_length=get32(inputfile);
		printf("format length: %d\n",format_length);
		getc(inputfile);
                getc(inputfile); /* always 0x01 */
		channel_number=get16(inputfile);
		printf("%i channels\n",channel_number);
		sample_rate=get32(inputfile);
		printf("%d samplerate\n",sample_rate);
		bytes_per_sec=get32(inputfile);
		 bytes_per_sample=get16(inputfile);
		bits_per_sample=get16(inputfile);	
		printf("Bytes per sec %i, bytes per sample %i, bits per sample %i\n",bytes_per_sec,bytes_per_sample,bits_per_sample);

		/* That's it for the headers; now for the rest */

		/* here we have the word 'data' */
		     getc(inputfile);
		     getc(inputfile);
		     getc(inputfile);
		     getc(inputfile);
			
		length_of_data=get32(inputfile);
		 printf("Length of data %i \n",length_of_data);

		if(bits_per_sample==16){
			if(channel_number==1){
				/* dump all the data out to a text file.*/
				if(argc>2){
					printf("Using mono output, starting from point %i in file\n",ftell(inputfile));
					printf("There are %i pieces of 2byte data to get\n",(filelength-ftell(inputfile))/2);
					current_position=(filelength-ftell(inputfile))/2;
					outputfile = fopen(argv[2],"w");
					
			for(i=0; i<current_position; i++){
				tmp=get16(inputfile);
				fprintf(outputfile,"%d\n",tmp);

			} 
					fclose(outputfile);
				} else {
					printf("please invoke as wavread input_file output_file to get text output\n");
				}
				
			} else if(channel_number==2){
				 if(argc>2){
                                        outputfile = fopen(argv[2],"w");
                                        while(tmp=get16(inputfile)) {
                                                if(feof(inputfile)) break;
                                                printf(" %i ",tmp);
						fprintf(outputfile,"%d \n",tmp);
						get16(inputfile);
							

                                        }
                                        fclose(outputfile);
                                } else {
                                        printf("please invoke as wavread input_file output_file to get text output\n");
                                }

			}
		}

	} else {
		printf("Sorry, I don't think this file is a wav\n");
		return 1;
	}


}
	fclose(inputfile);
	return 0;
}

