#include #include #include "fmt_chunk.h" int read_chars(FILE *in, char *s, int n) { int t,ch; for (t=0; t>8)&255),out); putc(((n>>16)&255),out); putc(((n>>24)&255),out); return 0; } int write_word(FILE *out, int n) { putc((n&255),out); putc(((n>>8)&255),out); return 0; } unsigned int read_long(FILE *in) { unsigned int t; t=getc(in); t=(getc(in)<<8)+t; t=(getc(in)<<16)+t; t=(getc(in)<<24)+t; return t; } unsigned short int read_word(FILE *in) { unsigned short int t; t=getc(in); t=(getc(in)<<8)+t; return t; } int parse_header(FILE *in, FILE *out) { int length; char riff_type[5]; length=read_long(in); read_chars(in,riff_type,4); printf("RIFF Header\n"); printf("----------------------------\n"); printf(" Length: %d\n",length); printf(" Type: %s\n",riff_type); printf("----------------------------\n"); /* Write RIFF Header */ if (out!=0) { fprintf(out,"RIFF"); write_long(out,4); fprintf(out,"WAVE"); } return 0; } int parse_fmt(FILE *in, struct fmt_chunk_t *fmt_chunk, FILE *out) { int length; length=read_long(in); fmt_chunk->format_type=read_word(in); fmt_chunk->channel_numbers=read_word(in); fmt_chunk->sample_rate=read_long(in); fmt_chunk->bytes_per_second=read_long(in); fmt_chunk->bytes_per_sample=read_word(in); fmt_chunk->bits_per_sample=read_word(in); printf("FMT Chunk\n"); printf("----------------------------\n"); printf(" Length: %d\n",length); printf(" Format Type: "); if (fmt_chunk->format_type==0) { printf("Mono\n"); } else if (fmt_chunk->format_type==1) { printf("Stereo\n"); } else { printf("unknown\n"); } printf(" Channel Numbers: %d\n",fmt_chunk->channel_numbers); printf(" Sample Rate: %d\n",fmt_chunk->sample_rate); printf("Bytes Per Second: %d\n",fmt_chunk->bytes_per_second); printf("Bytes Per Sample: "); if (fmt_chunk->bytes_per_sample==1) { printf("8 bit mono (%d)\n",fmt_chunk->bytes_per_sample); } else if (fmt_chunk->bytes_per_sample==2) { printf("8 bit stereo or 16 bit mono (%d)\n",fmt_chunk->bytes_per_sample); } else if (fmt_chunk->bytes_per_sample==4) { printf("16 bit stereo (%d)\n",fmt_chunk->bytes_per_sample); } printf(" Bits Per Sample: %d\n",fmt_chunk->bits_per_sample); printf("----------------------------\n"); /* Write FMT Chunk */ if (out!=0) { fprintf(out,"fmt "); write_long(out,16); write_word(out,fmt_chunk->format_type); write_word(out,fmt_chunk->channel_numbers); write_long(out,fmt_chunk->sample_rate); write_long(out,fmt_chunk->bytes_per_second); write_word(out,fmt_chunk->bytes_per_sample); write_word(out,fmt_chunk->bits_per_sample); } return 0; } int parse_data(FILE *in, struct fmt_chunk_t *fmt_chunk, FILE *out) { int length,marker,deepest; int t; char h; short int r; float ratio; deepest=0; length=read_long(in); marker=ftell(in); printf("DATA chunk\n"); printf("----------------------------\n"); printf(" Length: %d\n",length); printf("Scanning for biggest/smallest peak\n"); if (fmt_chunk->bits_per_sample==16) { for (t=0; tdeepest) deepest=r; } } else if (fmt_chunk->bits_per_sample==8) { for (t=0; tdeepest) deepest=h; } } printf("Deepest: %d\n",deepest); if (out==0) { return 0; } printf("Creating new wave\n"); fseek(in,marker,0); /* Write Data Chunk */ fprintf(out,"data"); write_long(out,length); ratio=32768/(float)deepest; printf("Ratio: %.5f\n",ratio); if (fmt_chunk->bits_per_sample==16) { for (t=0; tbits_per_sample==8) { for (t=0; t \n"); printf("-- If you exclude the output filename normalize will only analyze\n\n"); exit(1); } strcpy(inname,argv[1]); if (argc==3) { strcpy(outname,argv[2]); } else { outname[0]=0; } normalize(inname,outname); return 0; }