#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "wav.h"
int check_format(WAVHEADER header);
int get_block_size(WAVHEADER header);
int main(int argc, char *argv[])
{
// Ensure proper usage
// TODO #1 (done)
if (argc != 3) // 3 arguments 1 for the run, 1 for input , 1 for output
{
printf("./reverse input.wav output.wav\n");
return 1;
}
// Open input file for reading
// TODO #2 (done)
FILE *file; // creating a pointer to file
file = fopen(argv[1], "r");//opening the file with "r" for read only
if (file == NULL) // making sure the file is opened
{
return 1;
}
// Read header
// TODO #3(done)
WAVHEADER header; //creating a place to store the header
WAVHEADER* header1 = &header; // creating a pointer for the sake of the function w/ size of the header
fread(header1, sizeof(WAVHEADER), 1, file); //reading from the file into the header
//(the file to read into, size of what we will read, n. of times, source)
header = *header1; //putting the data we took in it's place
// Use check_format to ensure WAV format
// TODO #4(done)
if(check_format(header) != 1)
{
return 1;
}
// Open output file for writing
// TODO #5 (done)
FILE* outfile;
outfile = fopen(argv[2], "w"); //"w" for write
if (outfile == NULL)
{
return 1;
printf("output file failed to open \n");
}
// Write header to file
// TODO #6 (done)
fwrite(header1, sizeof(WAVHEADER), 1, outfile);
//(the sorce, size, n. of times, the file to write in)
// Use get_block_size to calculate size of block
// TODO #7(done)
int size = get_block_size(header);
// Write reversed audio to file
// TODO #8
int pointerStart = ftell(file);
fseek(file, 0, SEEK_END);
fseek(outfile, pointerStart, SEEK_SET);
int pointerEnd = ftell(file);
char buffer[size];
for (int i =0 ; i < pointerEnd ; i++)
{
fseek(file, -i, SEEK_END);
fread(buffer, size,1, file);
fseek(outfile, pointerStart +i, SEEK_SET);
fwrite(buffer, size, 1, outfile);
}
//closing files
fclose(file);
fclose(outfile);
}
int check_format(WAVHEADER header)
{
// TODO #4 (done)
BYTE test[4] = "WAVE";
for (int i = 0; i < 4; i++)
{
if (header.format[i] != test[i])
{
return 2;
}
}
return 1;
}
int get_block_size(WAVHEADER header)
{
// TODO #7 (done)
int bsize;
bsize = header.numChannels * (header.bitsPerSample/8);
return bsize;
}
this is the whole code and i can't find what's wrong i tried changing the
for (int i =0 ; i < pointerEnd ; i++)
{
fseek(file, -i, SEEK_END);
fread(buffer, size,1, file);
fseek(outfile, pointerStart +i, SEEK_SET);
fwrite(buffer, size, 1, outfile);
}
to
for (int i =0 ; i < pointerEnd - pointerStart; i++)
{
fseek(file, -i, SEEK_END);
fread(buffer, size,1, file);
fseek(outfile, pointerStart +i, SEEK_SET);
fwrite(buffer, size, 1, outfile);
because it made sense for a moment but it didn't work so i removed it