Question: Your task is to read a data file with sound data and scale the amplitude and then create a . WAV sound filewith the slower

Your task is to read a data file with sound data and scale the amplitude and then create a.WAV sound filewith the slower audio. WAV files are a Microsoft sound file that stores data samples as raw data in otherwords, there is no compression of the data. I have provided you with a test datafile that has 16-bit samplesrecorded at 22050 samples/second. Unlike the ADC examples that were discussed in lecture, thesesamples have negative values i.e. they go from -32768 to 32767 instead of from 0 to 65535. This allowsthe samples to represent negative voltages without doing any offsets I have provided you with the following skeleton main functionint main(int argc, char **argv){if (argc !=4){printf("%s datafile wavfile scale
", argv[0]);exit(-1);}char* datafile = argv[1];char* wavfile = argv[2];double scale = atof(argv[3]);int16_t* sounddata;// TODO allocate space for 1000000 int16_t's// TODO open the datafile and read the data into sounddata// TODO iterate through the array and scale the valueswavdata_t wav;// TODO fill in the wav structwrite_wav(wavfile, &wav);} The code takes in three arguments an input datafile, the name of a .WAV file to write the data with thescaled signal, and a scale value. You will need to read all the data from datafile into an array. You canassume that the file is no bigger than 1000000 sound samples long that means you should allocate ansounddata array that is big enough 1000000 sound samples where each sample is a int16_t. Youshould then read the entire file into sounddata with one read of 1000000 int16_ts. However, theread may return less bytes than that. Keep track of the number of bytes that the read returns, since you can use to calculate how many samples you read into the array - each sample is 2 bytes long (the sizeof an int16_t type). In this case, it will be easier to use open and read instead of fopen and fread.Once you figure out the number of samples in sounddata, you can iterate through the samples in thesounddata array and multiply the values by scale. You just have to make sure that the resultingvalues doesnt exceed 32767 or go below -32768 which are the bounds of an int16_t type.I have provided you with a write_wav function in wav.c that helps write the data to a .WAV file.However, before you call write_wav, you need to fill in the wav data structure.The wavdata_t struct is defined as follows:typedef struct {uint16_t nchannels;uint16_t bits_per_sample;uint32_t sample_rate;uint32_t datasize;int16_t *data;} wavdata_t;For the provided testfile, nchannels=1, bits_per_sample=16, and sample_rate=22050.datasize is the number of bytes in the data set, which is equal to the number of bytes you read fromthe datafile. data is a pointer to an array that holds the data samples.Once wav data structure is filled in, you can call write_wav. I have included an expected.5.wavand an expected2.wav file that contains the expected outputfor the following runs of the code. You can compare your output with the expected output using thediff command as shown below. If the files are exactly the same, then the diff command will notshow any differences.$ gcc -o hw5 hw5.c wav.c$ ./hw5 datafile my.wav 2$ diff my.wav expected2.wav$ ./hw5 datafile my.wav 0.5$ diff my.wav expected.5.wavYou should be able to play your output .WAV file and hear the change in volume in the audio.IMPORTANT: make sure that you get no differences with your output. //hw5.c
#include
#include
#include
#include
#include "wav.h"
int main(int argc, char **argv)
{
if (argc !=4){
printf("%s datafile wavfile scale
", argv[0]);
exit(-1);
}
char* datafile = argv[1];
char* wavfile = argv[2];
double scale = atof(argv[3]);
// TODO allocate space for 1000000 int16_t's
// TODO read the data from the datafile into sounddata
// TODO iterate through the array and scale the values
wavdata_t wav;
// TODO fill in the wav struct
write_ wav(argv[2], &wav);
}//wav.c
#include
#include
#include
#include
#include
#include "wav.h"
int write_wav(char *wavfile, wavdata_t *wav)
{
int fd = open(wavfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd ==-1){
printf("Could not open file %s
", wavfile);
return -1;
}
write(fd, "RIFF", 4);
uint32_t filesize = wav->datasize +36;
uint8_t data[20];
data[0]= filesize & 0xff;
data[1]=(filesize>>8) & 0xff;
data[2]=(filesize>>16) & 0xff;
data[3]=(filesize>>24) & 0xff;
write(fd, data, 4);
write(fd, "WAVEfmt ",8);
data[0]=16;
data[1]=0;
data[2]=0;
Your task is to read a data file with sound data

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 Accounting Questions!