Question: Show me the steps to solve % % writefile file _ tools.c #include file _ tools.h #include size _ t read _ data (

Show me the steps to solve %%writefile file_tools.c
#include "file_tools.h"
#include
size_t read_data(char* filename, unsigned char** data){
// When we open a file, we keep that location
// as a file pointer, so declare one.
FILE* fp;
// Open the file for reading.
fp = fopen(filename,"r");
// Go to the end of the file.
fseek(fp,0L, SEEK_END);
// ftell says where we are in the file. Since we went to the
// end, this will tell us how many bytes are in the file.
size_t file_size = ftell(fp);
// Go back to the beginning.
fseek(fp,0L, SEEK_SET);
// Now, we know how many bytes we need. Ask for that much space,
// and store the address.
*data = malloc(file_size);
// Read the data and store it in our location.
fread(*data,1, file_size, fp);
// Close the file.
fclose(fp);
// Return the number of bytes we found.
return file_size;
}
size_t write_data(char* filename, size_t data_size, unsigned char* data){
// Open the file for writing.
FILE* fp = fopen(filename,"w");
if (fp == NULL){
perror("Failed to open file for writing");
return 0;
}
// Write the data to the file.
size_t written = fwrite(data,1, data_size, fp);
// Close the file.
fclose(fp);
// Return the number of bytes written.
return written;
}%%writefile reversal.c
#include
#include
#include "file_tools.h"
int main(int argc, char** argv){
// Create a variable that will hold the address of where
// our data is loaded into.
unsigned char* data;
// Load the data into that area and get back the number of
// bytes read.
size_t file_size = read_data("./shared/audio/backwards.wav", &data);
printf("Size: %lu bytes.
", file_size);
// Write the data to a new file.
size_t written_size = write_data("./shared/audio/forwards.wav", file_size, data);
printf("Written Size: %lu bytes.
", written_size);
//free the allocated memory
free(data);
return 0;
}%%writefile reversal.c
#include
#include
#include "file_tools.h"
int main(int argc, char** argv){
unsigned char* data;
size_t file_size = read_data("./shared/audio/backwards.wav", &data);
printf("Size: %lu bytes.
", file_size);
printf("First four bytes: %c %c %c %c", data[0], data[1], data[2], data[3]);
printf("
");
return 0;
}%%writefile reversal.c
#include
#include
#include "file_tools.h"
int main(int argc, char** argv){
unsigned char* data;
size_t file_size = read_data("./shared/audio/backwards.wav", &data);
printf("Size: %lu bytes.
", file_size);
printf("First four bytes: %x %x %x %x", data[0], data[1], data[2], data[3]);
printf("
");
return 0;
}%%writefile reversal.c
#include
#include
#include "file_tools.h"
int main(int argc, char** argv){
unsigned char* data;
size_t file_size = read_data("./shared/audio/backwards.wav", &data);
printf("Size: %lu bytes.
", file_size);
printf("Sample rate: %d", data[24]);
printf("
");
return 0;
}%%writefile reversal.c
#include
#include
#include "file_tools.h"
int main(int argc, char** argv){
unsigned char* data;
size_t file_size = read_data("./shared/audio/backwards.wav", &data);
printf("Size: %lu bytes.
", file_size);
int value =*(int*)&data[24];
printf("Sample rate: %d", value);
printf("
");
return 0;
} Ok. Now what?
Well, now that we understand more about working with data in C, let's do something cool!
I want you to make a program that will get information about a wav file - then modify the file so that it will play backward. We can't simply reverse all the bytes in the file though, as some are metadata, and others are samples that are more than one byte long. What we need to do is
copy the metadata
reverse the samples (not the bytes)
Let's say that you have the following samples:
|0001|0203|0405|
If we wanted this audio to play backward, we would need to rewrite it so that it looks like this:
|0405|0203|0001|
The first 44 bytes of the file are the metadata. You can copy the metadata without modifying it to the new file. Then, you need to copy each sample from the old file to the new one, in reverse. The wave file I have given you is very easy to work with; each sample is 16-bits (2-bytes), and there is only one channel. The samples start at byte number 44. Make sure you output your file to a different name. Use the code in the above cell to play your sound file after you reverse it. If you did it correctly, you will be able to hear words (and you might recognize the voice).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!